1/*
2 * This file is part of the MicroPython 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
27#include "py/mpstate.h"
28#include "py/obj.h"
29#include "py/gc.h"
30
31#if MICROPY_PY_GC && MICROPY_ENABLE_GC
32
33// collect(): run a garbage collection
34STATIC mp_obj_t py_gc_collect(void) {
35 gc_collect();
36 #if MICROPY_PY_GC_COLLECT_RETVAL
37 return MP_OBJ_NEW_SMALL_INT(MP_STATE_MEM(gc_collected));
38 #else
39 return mp_const_none;
40 #endif
41}
42MP_DEFINE_CONST_FUN_OBJ_0(gc_collect_obj, py_gc_collect);
43
44// disable(): disable the garbage collector
45STATIC mp_obj_t gc_disable(void) {
46 MP_STATE_MEM(gc_auto_collect_enabled) = 0;
47 return mp_const_none;
48}
49MP_DEFINE_CONST_FUN_OBJ_0(gc_disable_obj, gc_disable);
50
51// enable(): enable the garbage collector
52STATIC mp_obj_t gc_enable(void) {
53 MP_STATE_MEM(gc_auto_collect_enabled) = 1;
54 return mp_const_none;
55}
56MP_DEFINE_CONST_FUN_OBJ_0(gc_enable_obj, gc_enable);
57
58STATIC mp_obj_t gc_isenabled(void) {
59 return mp_obj_new_bool(MP_STATE_MEM(gc_auto_collect_enabled));
60}
61MP_DEFINE_CONST_FUN_OBJ_0(gc_isenabled_obj, gc_isenabled);
62
63// mem_free(): return the number of bytes of available heap RAM
64STATIC mp_obj_t gc_mem_free(void) {
65 gc_info_t info;
66 gc_info(&info);
67 return MP_OBJ_NEW_SMALL_INT(info.free);
68}
69MP_DEFINE_CONST_FUN_OBJ_0(gc_mem_free_obj, gc_mem_free);
70
71// mem_alloc(): return the number of bytes of heap RAM that are allocated
72STATIC mp_obj_t gc_mem_alloc(void) {
73 gc_info_t info;
74 gc_info(&info);
75 return MP_OBJ_NEW_SMALL_INT(info.used);
76}
77MP_DEFINE_CONST_FUN_OBJ_0(gc_mem_alloc_obj, gc_mem_alloc);
78
79#if MICROPY_GC_ALLOC_THRESHOLD
80STATIC mp_obj_t gc_threshold(size_t n_args, const mp_obj_t *args) {
81 if (n_args == 0) {
82 if (MP_STATE_MEM(gc_alloc_threshold) == (size_t)-1) {
83 return MP_OBJ_NEW_SMALL_INT(-1);
84 }
85 return mp_obj_new_int(MP_STATE_MEM(gc_alloc_threshold) * MICROPY_BYTES_PER_GC_BLOCK);
86 }
87 mp_int_t val = mp_obj_get_int(args[0]);
88 if (val < 0) {
89 MP_STATE_MEM(gc_alloc_threshold) = (size_t)-1;
90 } else {
91 MP_STATE_MEM(gc_alloc_threshold) = val / MICROPY_BYTES_PER_GC_BLOCK;
92 }
93 return mp_const_none;
94}
95MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(gc_threshold_obj, 0, 1, gc_threshold);
96#endif
97
98STATIC const mp_rom_map_elem_t mp_module_gc_globals_table[] = {
99 { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_gc) },
100 { MP_ROM_QSTR(MP_QSTR_collect), MP_ROM_PTR(&gc_collect_obj) },
101 { MP_ROM_QSTR(MP_QSTR_disable), MP_ROM_PTR(&gc_disable_obj) },
102 { MP_ROM_QSTR(MP_QSTR_enable), MP_ROM_PTR(&gc_enable_obj) },
103 { MP_ROM_QSTR(MP_QSTR_isenabled), MP_ROM_PTR(&gc_isenabled_obj) },
104 { MP_ROM_QSTR(MP_QSTR_mem_free), MP_ROM_PTR(&gc_mem_free_obj) },
105 { MP_ROM_QSTR(MP_QSTR_mem_alloc), MP_ROM_PTR(&gc_mem_alloc_obj) },
106 #if MICROPY_GC_ALLOC_THRESHOLD
107 { MP_ROM_QSTR(MP_QSTR_threshold), MP_ROM_PTR(&gc_threshold_obj) },
108 #endif
109};
110
111STATIC MP_DEFINE_CONST_DICT(mp_module_gc_globals, mp_module_gc_globals_table);
112
113const mp_obj_module_t mp_module_gc = {
114 .base = { &mp_type_module },
115 .globals = (mp_obj_dict_t *)&mp_module_gc_globals,
116};
117
118#endif
119