Numworks Epsilon  1.4.1
Graphing Calculator Operating System
modgc.c
Go to the documentation of this file.
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
34 STATIC 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 }
42 MP_DEFINE_CONST_FUN_OBJ_0(gc_collect_obj, py_gc_collect);
43 
44 // disable(): disable the garbage collector
45 STATIC mp_obj_t gc_disable(void) {
46  MP_STATE_MEM(gc_auto_collect_enabled) = 0;
47  return mp_const_none;
48 }
49 MP_DEFINE_CONST_FUN_OBJ_0(gc_disable_obj, gc_disable);
50 
51 // enable(): enable the garbage collector
52 STATIC mp_obj_t gc_enable(void) {
53  MP_STATE_MEM(gc_auto_collect_enabled) = 1;
54  return mp_const_none;
55 }
56 MP_DEFINE_CONST_FUN_OBJ_0(gc_enable_obj, gc_enable);
57 
58 STATIC mp_obj_t gc_isenabled(void) {
59  return mp_obj_new_bool(MP_STATE_MEM(gc_auto_collect_enabled));
60 }
61 MP_DEFINE_CONST_FUN_OBJ_0(gc_isenabled_obj, gc_isenabled);
62 
63 // mem_free(): return the number of bytes of available heap RAM
64 STATIC 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 }
69 MP_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
72 STATIC 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 }
77 MP_DEFINE_CONST_FUN_OBJ_0(gc_mem_alloc_obj, gc_mem_alloc);
78 
79 #if MICROPY_GC_ALLOC_THRESHOLD
80 STATIC 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 }
95 MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(gc_threshold_obj, 0, 1, gc_threshold);
96 #endif
97 
98 STATIC 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 
111 STATIC MP_DEFINE_CONST_DICT(mp_module_gc_globals, mp_module_gc_globals_table);
112 
114  .base = { &mp_type_module },
115  .globals = (mp_obj_dict_t*)&mp_module_gc_globals,
116 };
117 
118 #endif
void gc_collect(void)
Definition: port.cpp:113
intptr_t mp_int_t
Definition: mpconfigport.h:73
#define mp_const_none
Definition: obj.h:614
#define MP_DEFINE_CONST_DICT(dict_name, table_name)
Definition: obj.h:317
size_t free
Definition: gc.h:56
unsigned int size_t
Definition: stddef.h:7
#define MP_ROM_QSTR(q)
Definition: obj.h:241
#define MP_ROM_PTR(p)
Definition: obj.h:242
mp_obj_base_t base
Definition: obj.h:814
mp_int_t mp_obj_get_int(mp_const_obj_t arg)
Definition: obj.c:225
#define STATIC
Definition: mpconfig.h:1178
size_t used
Definition: gc.h:55
#define MP_STATE_MEM(x)
Definition: mpstate.h:242
#define MP_OBJ_NEW_SMALL_INT(small_int)
Definition: obj.h:87
#define MICROPY_BYTES_PER_GC_BLOCK
Definition: mpconfig.h:101
void gc_info(gc_info_t *info)
args
Definition: i18n.py:175
mp_obj_t mp_obj_new_int(mp_int_t value)
Definition: objint.c:353
#define MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name, n_args_min, n_args_max, fun_name)
Definition: obj.h:297
Definition: gc.h:53
#define MP_DEFINE_CONST_FUN_OBJ_0(obj_name, fun_name)
Definition: obj.h:282
const mp_obj_module_t mp_module_gc
const mp_obj_type_t mp_type_module
Definition: objmodule.c:94
uint64_t mp_obj_t
Definition: obj.h:39