File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -26,3 +26,7 @@ MP_DECLARE_CONST_FUN_OBJ(mp_builtin_repr_obj);
2626MP_DECLARE_CONST_FUN_OBJ (mp_builtin_sorted_obj );
2727MP_DECLARE_CONST_FUN_OBJ (mp_builtin_sum_obj );
2828MP_DECLARE_CONST_FUN_OBJ (mp_builtin_str_obj );
29+
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 );
Original file line number Diff line number Diff line change 1+ #include <stdint.h>
2+ #include <stdlib.h>
3+ #include <stdio.h>
4+ #include <stdarg.h>
5+ #include <string.h>
6+ #include <assert.h>
7+
8+ #include "misc.h"
9+ #include "mpconfig.h"
10+ #include "obj.h"
11+ #include "builtin.h"
12+
13+ // Various builtins specific to MicroPython runtime,
14+ // living in micropython module
15+
16+ #if MICROPY_MEM_STATS
17+ static mp_obj_t mem_total () {
18+ return MP_OBJ_NEW_SMALL_INT (m_get_total_bytes_allocated ());
19+ }
20+
21+ static mp_obj_t mem_current () {
22+ return MP_OBJ_NEW_SMALL_INT (m_get_current_bytes_allocated ());
23+ }
24+
25+ static mp_obj_t mem_peak () {
26+ return MP_OBJ_NEW_SMALL_INT (m_get_peak_bytes_allocated ());
27+ }
28+
29+ MP_DEFINE_CONST_FUN_OBJ_0 (mp_builtin_mem_total_obj , mem_total );
30+ MP_DEFINE_CONST_FUN_OBJ_0 (mp_builtin_mem_current_obj , mem_current );
31+ MP_DEFINE_CONST_FUN_OBJ_0 (mp_builtin_mem_peak_obj , mem_peak );
32+ #endif
Original file line number Diff line number Diff line change @@ -101,6 +101,7 @@ PY_O_BASENAME = \
101101 builtin.o \
102102 builtinimport.o \
103103 builtineval.o \
104+ builtinmp.o \
104105 vm.o \
105106 showbc.o \
106107 repl.o \
Original file line number Diff line number Diff line change @@ -153,15 +153,18 @@ void rt_init(void) {
153153 mp_map_add_qstr (& map_builtins , MP_QSTR_bytearray , (mp_obj_t )& mp_builtin_bytearray_obj );
154154
155155#if MICROPY_CPYTHON_COMPAT
156- // Add (empty) micropython module, so it was possible to "import micropython",
157- // which can be a placeholder module on CPython.
158- mp_obj_t m_mp = mp_obj_new_module (qstr_from_str_static ("micropython" ));
159- rt_store_name (qstr_from_str_static ("micropython" ), m_mp );
160-
161156 // Precreate sys module, so "import sys" didn't throw exceptions.
162157 mp_obj_new_module (qstr_from_str_static ("sys" ));
163158#endif
164159
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
167+
165168 next_unique_code_id = 1 ; // 0 indicates "no code"
166169 unique_codes_alloc = 0 ;
167170 unique_codes = NULL ;
You can’t perform that action at this time.
0 commit comments