blob: d68ff7e6c0abc8879704c28a17746da95b851f1c [file] [log] [blame]
Paul Sokolovskyf9e54e02014-05-06 02:16:43 +03001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Damien Georgeb4b10fd2015-01-01 23:30:53 +000027#include "py/mpstate.h"
Damien George51dfcb42015-01-01 20:27:54 +000028#include "py/obj.h"
29#include "py/gc.h"
Paul Sokolovskyf9e54e02014-05-06 02:16:43 +030030
Damien Georgeee3fd462014-05-24 23:03:12 +010031#if MICROPY_PY_GC && MICROPY_ENABLE_GC
Paul Sokolovskyf9e54e02014-05-06 02:16:43 +030032
Damien George30dd23a2014-08-10 17:50:28 +010033/// \module gc - control the garbage collector
34
Damien George30dd23a2014-08-10 17:50:28 +010035/// \function collect()
36/// Run a garbage collection.
Paul Sokolovskyf9e54e02014-05-06 02:16:43 +030037STATIC mp_obj_t py_gc_collect(void) {
38 gc_collect();
Paul Sokolovsky755a55f2014-06-05 22:48:02 +030039#if MICROPY_PY_GC_COLLECT_RETVAL
Damien Georgee1e359f2015-02-07 17:24:10 +000040 return MP_OBJ_NEW_SMALL_INT(MP_STATE_MEM(gc_collected));
Paul Sokolovsky755a55f2014-06-05 22:48:02 +030041#else
Paul Sokolovskyf9e54e02014-05-06 02:16:43 +030042 return mp_const_none;
Paul Sokolovsky755a55f2014-06-05 22:48:02 +030043#endif
Paul Sokolovskyf9e54e02014-05-06 02:16:43 +030044}
45MP_DEFINE_CONST_FUN_OBJ_0(gc_collect_obj, py_gc_collect);
46
Damien George30dd23a2014-08-10 17:50:28 +010047/// \function disable()
48/// Disable the garbage collector.
Damien George8c1c7482014-05-08 23:04:49 +010049STATIC mp_obj_t gc_disable(void) {
Damien Georgeb4b10fd2015-01-01 23:30:53 +000050 MP_STATE_MEM(gc_auto_collect_enabled) = 0;
Damien George8c1c7482014-05-08 23:04:49 +010051 return mp_const_none;
52}
53MP_DEFINE_CONST_FUN_OBJ_0(gc_disable_obj, gc_disable);
54
Damien George30dd23a2014-08-10 17:50:28 +010055/// \function enable()
56/// Enable the garbage collector.
Damien George8c1c7482014-05-08 23:04:49 +010057STATIC mp_obj_t gc_enable(void) {
Damien Georgeb4b10fd2015-01-01 23:30:53 +000058 MP_STATE_MEM(gc_auto_collect_enabled) = 1;
Damien George8c1c7482014-05-08 23:04:49 +010059 return mp_const_none;
60}
61MP_DEFINE_CONST_FUN_OBJ_0(gc_enable_obj, gc_enable);
62
Damien George109c1de2014-10-31 21:30:46 +000063STATIC mp_obj_t gc_isenabled(void) {
Paul Sokolovsky1b586f32015-10-11 12:09:43 +030064 return mp_obj_new_bool(MP_STATE_MEM(gc_auto_collect_enabled));
Damien George109c1de2014-10-31 21:30:46 +000065}
66MP_DEFINE_CONST_FUN_OBJ_0(gc_isenabled_obj, gc_isenabled);
67
Damien George30dd23a2014-08-10 17:50:28 +010068/// \function mem_free()
69/// Return the number of bytes of available heap RAM.
Paul Sokolovsky5aa740c2014-06-24 21:23:40 +030070STATIC mp_obj_t gc_mem_free(void) {
71 gc_info_t info;
72 gc_info(&info);
Damien Georgebb4c6f32014-07-31 10:49:14 +010073 return MP_OBJ_NEW_SMALL_INT(info.free);
Paul Sokolovsky5aa740c2014-06-24 21:23:40 +030074}
75MP_DEFINE_CONST_FUN_OBJ_0(gc_mem_free_obj, gc_mem_free);
76
Damien George30dd23a2014-08-10 17:50:28 +010077/// \function mem_alloc()
78/// Return the number of bytes of heap RAM that are allocated.
Paul Sokolovsky5aa740c2014-06-24 21:23:40 +030079STATIC mp_obj_t gc_mem_alloc(void) {
80 gc_info_t info;
81 gc_info(&info);
Damien Georgebb4c6f32014-07-31 10:49:14 +010082 return MP_OBJ_NEW_SMALL_INT(info.used);
Paul Sokolovsky5aa740c2014-06-24 21:23:40 +030083}
84MP_DEFINE_CONST_FUN_OBJ_0(gc_mem_alloc_obj, gc_mem_alloc);
85
Damien Georgecbf76742015-11-27 13:38:15 +000086STATIC const mp_rom_map_elem_t mp_module_gc_globals_table[] = {
87 { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_gc) },
88 { MP_ROM_QSTR(MP_QSTR_collect), MP_ROM_PTR(&gc_collect_obj) },
89 { MP_ROM_QSTR(MP_QSTR_disable), MP_ROM_PTR(&gc_disable_obj) },
90 { MP_ROM_QSTR(MP_QSTR_enable), MP_ROM_PTR(&gc_enable_obj) },
91 { MP_ROM_QSTR(MP_QSTR_isenabled), MP_ROM_PTR(&gc_isenabled_obj) },
92 { MP_ROM_QSTR(MP_QSTR_mem_free), MP_ROM_PTR(&gc_mem_free_obj) },
93 { MP_ROM_QSTR(MP_QSTR_mem_alloc), MP_ROM_PTR(&gc_mem_alloc_obj) },
Paul Sokolovskyf9e54e02014-05-06 02:16:43 +030094};
95
Damien George3b603f22014-11-29 14:39:27 +000096STATIC MP_DEFINE_CONST_DICT(mp_module_gc_globals, mp_module_gc_globals_table);
Paul Sokolovskyf9e54e02014-05-06 02:16:43 +030097
98const mp_obj_module_t mp_module_gc = {
99 .base = { &mp_type_module },
100 .name = MP_QSTR_gc,
101 .globals = (mp_obj_dict_t*)&mp_module_gc_globals,
102};
103
104#endif