Skip to content

Commit e1e359f

Browse files
committed
py: Put mp_sys_path, mp_sys_argv and gc_collected in mp_state_ctx_t.
Without mp_sys_path and mp_sys_argv in the root pointer section of the state, their memory was being incorrectly collected by GC.
1 parent 8785645 commit e1e359f

File tree

5 files changed

+17
-21
lines changed

5 files changed

+17
-21
lines changed

py/gc.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,9 @@ STATIC void gc_deal_with_stack_overflow(void) {
222222
}
223223
}
224224

225-
#if MICROPY_PY_GC_COLLECT_RETVAL
226-
uint gc_collected;
227-
#endif
228-
229225
STATIC void gc_sweep(void) {
230226
#if MICROPY_PY_GC_COLLECT_RETVAL
231-
gc_collected = 0;
227+
MP_STATE_MEM(gc_collected) = 0;
232228
#endif
233229
// free unmarked heads and their tails
234230
int free_tail = 0;
@@ -253,7 +249,7 @@ STATIC void gc_sweep(void) {
253249
#endif
254250
free_tail = 1;
255251
#if MICROPY_PY_GC_COLLECT_RETVAL
256-
gc_collected++;
252+
MP_STATE_MEM(gc_collected)++;
257253
#endif
258254
// fall through to free the head
259255

py/modgc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ extern uint gc_collected;
3939
STATIC mp_obj_t py_gc_collect(void) {
4040
gc_collect();
4141
#if MICROPY_PY_GC_COLLECT_RETVAL
42-
return MP_OBJ_NEW_SMALL_INT(gc_collected);
42+
return MP_OBJ_NEW_SMALL_INT(MP_STATE_MEM(gc_collected));
4343
#else
4444
return mp_const_none;
4545
#endif

py/modsys.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#include "py/mpstate.h"
2728
#include "py/nlr.h"
2829
#include "py/builtin.h"
2930
#include "py/objlist.h"
@@ -42,13 +43,6 @@ extern mp_uint_t mp_sys_stdin_obj;
4243
extern mp_uint_t mp_sys_stdout_obj;
4344
extern mp_uint_t mp_sys_stderr_obj;
4445

45-
// These two lists must be initialised per port (after the call to mp_init).
46-
// TODO document these properly, they aren't constants or functions...
47-
/// \constant path - a mutable list of directories to search for imported modules
48-
mp_obj_list_t mp_sys_path_obj;
49-
/// \constant argv - a mutable list of arguments this program started with
50-
mp_obj_list_t mp_sys_argv_obj;
51-
5246
/// \constant version - Python language version that this implementation conforms to, as a string
5347
STATIC const MP_DEFINE_STR_OBJ(version_obj, "3.4.0");
5448

@@ -99,8 +93,8 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_print_exception_obj, 1, 2, mp_sys_pri
9993
STATIC const mp_map_elem_t mp_module_sys_globals_table[] = {
10094
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_sys) },
10195

102-
{ MP_OBJ_NEW_QSTR(MP_QSTR_path), (mp_obj_t)&mp_sys_path_obj },
103-
{ MP_OBJ_NEW_QSTR(MP_QSTR_argv), (mp_obj_t)&mp_sys_argv_obj },
96+
{ MP_OBJ_NEW_QSTR(MP_QSTR_path), (mp_obj_t)&MP_STATE_VM(mp_sys_path_obj) },
97+
{ MP_OBJ_NEW_QSTR(MP_QSTR_argv), (mp_obj_t)&MP_STATE_VM(mp_sys_argv_obj) },
10498
{ MP_OBJ_NEW_QSTR(MP_QSTR_version), (mp_obj_t)&version_obj },
10599
{ MP_OBJ_NEW_QSTR(MP_QSTR_version_info), (mp_obj_t)&mp_sys_version_info_obj },
106100
#ifdef MICROPY_PY_SYS_PLATFORM

py/mpstate.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#include "py/misc.h"
3333
#include "py/nlr.h"
3434
#include "py/obj.h"
35-
#include "py/objlist.h" // in case port needs mp_obj_list_t in root pointers
35+
#include "py/objlist.h"
3636
#include "py/objexcept.h"
3737

3838
// This file contains structures defining the state of the Micro Python
@@ -66,6 +66,10 @@ typedef struct _mp_state_mem_t {
6666
uint16_t gc_auto_collect_enabled;
6767

6868
mp_uint_t gc_last_free_atb_index;
69+
70+
#if MICROPY_PY_GC_COLLECT_RETVAL
71+
mp_uint_t gc_collected;
72+
#endif
6973
} mp_state_mem_t;
7074

7175
// This structure hold runtime and VM information. It includes a section
@@ -106,6 +110,10 @@ typedef struct _mp_state_vm_t {
106110
// dictionary for the __main__ module
107111
mp_obj_dict_t dict_main;
108112

113+
// these two lists must be initialised per port, after the call to mp_init
114+
mp_obj_list_t mp_sys_path_obj;
115+
mp_obj_list_t mp_sys_argv_obj;
116+
109117
// dictionary for overridden builtins
110118
#if MICROPY_CAN_OVERRIDE_BUILTINS
111119
mp_obj_dict_t *mp_module_builtins_override_dict;

py/runtime.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,8 @@ mp_obj_t mp_convert_native_to_obj(mp_uint_t val, mp_uint_t type);
125125
mp_obj_t mp_native_call_function_n_kw(mp_obj_t fun_in, mp_uint_t n_args_kw, const mp_obj_t *args);
126126
void mp_native_raise(mp_obj_t o);
127127

128-
extern struct _mp_obj_list_t mp_sys_path_obj;
129-
extern struct _mp_obj_list_t mp_sys_argv_obj;
130-
#define mp_sys_path ((mp_obj_t)&mp_sys_path_obj)
131-
#define mp_sys_argv ((mp_obj_t)&mp_sys_argv_obj)
128+
#define mp_sys_path ((mp_obj_t)&MP_STATE_VM(mp_sys_path_obj))
129+
#define mp_sys_argv ((mp_obj_t)&MP_STATE_VM(mp_sys_argv_obj))
132130

133131
#if MICROPY_WARNINGS
134132
void mp_warning(const char *msg, ...);

0 commit comments

Comments
 (0)