Skip to content

Commit f9e54e0

Browse files
committed
modgc: Add new module for GC-related functionality.
1 parent 912ca77 commit f9e54e0

7 files changed

Lines changed: 83 additions & 12 deletions

File tree

py/builtin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,4 @@ extern const mp_obj_module_t mp_module_cmath;
7777
extern const mp_obj_module_t mp_module_micropython;
7878
extern const mp_obj_module_t mp_module_struct;
7979
extern const mp_obj_module_t mp_module_sys;
80+
extern const mp_obj_module_t mp_module_gc;

py/builtintables.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ STATIC const mp_map_elem_t mp_builtin_module_table[] = {
174174
#if MICROPY_ENABLE_MOD_SYS
175175
{ MP_OBJ_NEW_QSTR(MP_QSTR_sys), (mp_obj_t)&mp_module_sys },
176176
#endif
177+
#if MICROPY_ENABLE_MOD_GC && MICROPY_ENABLE_GC
178+
{ MP_OBJ_NEW_QSTR(MP_QSTR_gc), (mp_obj_t)&mp_module_gc },
179+
#endif
177180

178181
// extra builtin modules as defined by a port
179182
MICROPY_EXTRA_BUILTIN_MODULES

py/modgc.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
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+
27+
#include "misc.h"
28+
#include "mpconfig.h"
29+
#include "qstr.h"
30+
#include "obj.h"
31+
#include "builtin.h"
32+
#include "runtime.h"
33+
#include "objlist.h"
34+
#include "objtuple.h"
35+
#include "objstr.h"
36+
#include "gc.h"
37+
38+
#if MICROPY_ENABLE_MOD_GC && MICROPY_ENABLE_GC
39+
40+
STATIC mp_obj_t py_gc_collect(void) {
41+
gc_collect();
42+
return mp_const_none;
43+
}
44+
MP_DEFINE_CONST_FUN_OBJ_0(gc_collect_obj, py_gc_collect);
45+
46+
STATIC const mp_map_elem_t mp_module_gc_globals_table[] = {
47+
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_gc) },
48+
{ MP_OBJ_NEW_QSTR(MP_QSTR_collect), (mp_obj_t)&gc_collect_obj },
49+
};
50+
51+
STATIC const mp_obj_dict_t mp_module_gc_globals = {
52+
.base = {&mp_type_dict},
53+
.map = {
54+
.all_keys_are_qstrs = 1,
55+
.table_is_fixed_array = 1,
56+
.used = ARRAY_SIZE(mp_module_gc_globals_table),
57+
.alloc = ARRAY_SIZE(mp_module_gc_globals_table),
58+
.table = (mp_map_elem_t*)mp_module_gc_globals_table,
59+
},
60+
};
61+
62+
const mp_obj_module_t mp_module_gc = {
63+
.base = { &mp_type_module },
64+
.name = MP_QSTR_gc,
65+
.globals = (mp_obj_dict_t*)&mp_module_gc_globals,
66+
};
67+
68+
#endif

py/mpconfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,11 @@ typedef double mp_float_t;
200200
#define MICROPY_ENABLE_MOD_CMATH (0)
201201
#endif
202202

203+
// Whether to provide "gc" module
204+
#ifndef MICROPY_ENABLE_MOD_GC
205+
#define MICROPY_ENABLE_MOD_GC (1)
206+
#endif
207+
203208
// Whether to provide "io" module
204209
#ifndef MICROPY_ENABLE_MOD_IO
205210
#define MICROPY_ENABLE_MOD_IO (1)

py/py.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ PY_O_BASENAME = \
8888
builtintables.o \
8989
modarray.o \
9090
modcollections.o \
91+
modgc.o \
9192
modio.o \
9293
modmath.o \
9394
modcmath.o \

py/qstrdefs.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,11 @@ Q(BytesIO)
346346
Q(getvalue)
347347
#endif
348348

349+
#if MICROPY_ENABLE_MOD_GC
350+
Q(gc)
351+
Q(collect)
352+
#endif
353+
349354
#if MICROPY_ENABLE_PROPERTY
350355
Q(property)
351356
Q(getter)

unix/main.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -247,15 +247,6 @@ mp_obj_t qstr_info(void) {
247247
return mp_const_none;
248248
}
249249

250-
#if MICROPY_ENABLE_GC
251-
// TODO: this doesn't belong here
252-
STATIC mp_obj_t pyb_gc(void) {
253-
gc_collect();
254-
return mp_const_none;
255-
}
256-
MP_DEFINE_CONST_FUN_OBJ_0(pyb_gc_obj, pyb_gc);
257-
#endif
258-
259250
// Process options which set interpreter init options
260251
void pre_process_options(int argc, char **argv) {
261252
for (int a = 1; a < argc; a++) {
@@ -338,9 +329,6 @@ int main(int argc, char **argv) {
338329

339330
mp_store_name(qstr_from_str("mem_info"), mp_make_function_n(0, mem_info));
340331
mp_store_name(qstr_from_str("qstr_info"), mp_make_function_n(0, qstr_info));
341-
#if MICROPY_ENABLE_GC
342-
mp_store_name(qstr_from_str("gc"), (mp_obj_t)&pyb_gc_obj);
343-
#endif
344332

345333
// Here is some example code to create a class and instance of that class.
346334
// First is the Python, then the C code.

0 commit comments

Comments
 (0)