Skip to content

Commit 91d457a

Browse files
committed
py: Put micropython module init code in builtinmp.c.
1 parent 5892336 commit 91d457a

3 files changed

Lines changed: 18 additions & 13 deletions

File tree

py/builtin.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,4 @@ MP_DECLARE_CONST_FUN_OBJ(mp_builtin_sorted_obj);
2727
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_sum_obj);
2828
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_str_obj);
2929

30-
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_mem_total_obj);
31-
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_mem_current_obj);
32-
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_mem_peak_obj);
30+
void mp_module_micropython_init(void);

py/builtinmp.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,39 @@
77

88
#include "misc.h"
99
#include "mpconfig.h"
10+
#include "mpqstr.h"
1011
#include "obj.h"
12+
#include "runtime.h"
1113
#include "builtin.h"
1214

1315
// Various builtins specific to MicroPython runtime,
1416
// living in micropython module
1517

1618
#if MICROPY_MEM_STATS
1719
static mp_obj_t mem_total() {
18-
return MP_OBJ_NEW_SMALL_INT(m_get_total_bytes_allocated());
20+
return MP_OBJ_NEW_SMALL_INT((machine_int_t)m_get_total_bytes_allocated());
1921
}
2022

2123
static mp_obj_t mem_current() {
22-
return MP_OBJ_NEW_SMALL_INT(m_get_current_bytes_allocated());
24+
return MP_OBJ_NEW_SMALL_INT((machine_int_t)m_get_current_bytes_allocated());
2325
}
2426

2527
static mp_obj_t mem_peak() {
26-
return MP_OBJ_NEW_SMALL_INT(m_get_peak_bytes_allocated());
28+
return MP_OBJ_NEW_SMALL_INT((machine_int_t)m_get_peak_bytes_allocated());
2729
}
2830

2931
MP_DEFINE_CONST_FUN_OBJ_0(mp_builtin_mem_total_obj, mem_total);
3032
MP_DEFINE_CONST_FUN_OBJ_0(mp_builtin_mem_current_obj, mem_current);
3133
MP_DEFINE_CONST_FUN_OBJ_0(mp_builtin_mem_peak_obj, mem_peak);
3234
#endif
35+
36+
void mp_module_micropython_init(void) {
37+
mp_obj_t m_mp = mp_obj_new_module(MP_QSTR_micropython);
38+
rt_store_name(MP_QSTR_micropython, m_mp);
39+
40+
#if MICROPY_MEM_STATS
41+
rt_store_attr(m_mp, qstr_from_str_static("mem_total"), (mp_obj_t)&mp_builtin_mem_total_obj);
42+
rt_store_attr(m_mp, qstr_from_str_static("mem_current"), (mp_obj_t)&mp_builtin_mem_current_obj);
43+
rt_store_attr(m_mp, qstr_from_str_static("mem_peak"), (mp_obj_t)&mp_builtin_mem_peak_obj);
44+
#endif
45+
}

py/runtime.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,7 @@ void rt_init(void) {
157157
mp_obj_new_module(qstr_from_str_static("sys"));
158158
#endif
159159

160-
mp_obj_t m_mp = mp_obj_new_module(qstr_from_str_static("micropython"));
161-
rt_store_name(qstr_from_str_static("micropython"), m_mp);
162-
#if MICROPY_MEM_STATS
163-
rt_store_attr(m_mp, qstr_from_str_static("mem_total"), (mp_obj_t)&mp_builtin_mem_total_obj);
164-
rt_store_attr(m_mp, qstr_from_str_static("mem_current"), (mp_obj_t)&mp_builtin_mem_current_obj);
165-
rt_store_attr(m_mp, qstr_from_str_static("mem_peak"), (mp_obj_t)&mp_builtin_mem_peak_obj);
166-
#endif
160+
mp_module_micropython_init();
167161

168162
next_unique_code_id = 1; // 0 indicates "no code"
169163
unique_codes_alloc = 0;

0 commit comments

Comments
 (0)