Numworks Epsilon  1.4.1
Graphing Calculator Operating System
modmicropython.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 <stdio.h>
28 
29 #include "py/builtin.h"
30 #include "py/stackctrl.h"
31 #include "py/runtime.h"
32 #include "py/gc.h"
33 #include "py/mphal.h"
34 
35 // Various builtins specific to MicroPython runtime,
36 // living in micropython module
37 
39  if (n_args == 0) {
40  return MP_OBJ_NEW_SMALL_INT(MP_STATE_VM(mp_optimise_value));
41  } else {
42  MP_STATE_VM(mp_optimise_value) = mp_obj_get_int(args[0]);
43  return mp_const_none;
44  }
45 }
46 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_micropython_opt_level_obj, 0, 1, mp_micropython_opt_level);
47 
48 #if MICROPY_PY_MICROPYTHON_MEM_INFO
49 
50 #if MICROPY_MEM_STATS
51 STATIC mp_obj_t mp_micropython_mem_total(void) {
52  return MP_OBJ_NEW_SMALL_INT(m_get_total_bytes_allocated());
53 }
54 STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_mem_total_obj, mp_micropython_mem_total);
55 
56 STATIC mp_obj_t mp_micropython_mem_current(void) {
57  return MP_OBJ_NEW_SMALL_INT(m_get_current_bytes_allocated());
58 }
59 STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_mem_current_obj, mp_micropython_mem_current);
60 
61 STATIC mp_obj_t mp_micropython_mem_peak(void) {
62  return MP_OBJ_NEW_SMALL_INT(m_get_peak_bytes_allocated());
63 }
64 STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_mem_peak_obj, mp_micropython_mem_peak);
65 #endif
66 
67 mp_obj_t mp_micropython_mem_info(size_t n_args, const mp_obj_t *args) {
68  (void)args;
69 #if MICROPY_MEM_STATS
70  mp_printf(&mp_plat_print, "mem: total=" UINT_FMT ", current=" UINT_FMT ", peak=" UINT_FMT "\n",
71  (mp_uint_t)m_get_total_bytes_allocated(), (mp_uint_t)m_get_current_bytes_allocated(), (mp_uint_t)m_get_peak_bytes_allocated());
72 #endif
73 #if MICROPY_STACK_CHECK
74  mp_printf(&mp_plat_print, "stack: " UINT_FMT " out of " UINT_FMT "\n",
75  mp_stack_usage(), (mp_uint_t)MP_STATE_THREAD(stack_limit));
76 #else
77  mp_printf(&mp_plat_print, "stack: " UINT_FMT "\n", mp_stack_usage());
78 #endif
79 #if MICROPY_ENABLE_GC
80  gc_dump_info();
81  if (n_args == 1) {
82  // arg given means dump gc allocation table
84  }
85 #else
86  (void)n_args;
87 #endif
88  return mp_const_none;
89 }
90 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_micropython_mem_info_obj, 0, 1, mp_micropython_mem_info);
91 
92 STATIC mp_obj_t mp_micropython_qstr_info(size_t n_args, const mp_obj_t *args) {
93  (void)args;
94  size_t n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
95  qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes);
96  mp_printf(&mp_plat_print, "qstr pool: n_pool=%u, n_qstr=%u, n_str_data_bytes=%u, n_total_bytes=%u\n",
97  n_pool, n_qstr, n_str_data_bytes, n_total_bytes);
98  if (n_args == 1) {
99  // arg given means dump qstr data
100  qstr_dump_data();
101  }
102  return mp_const_none;
103 }
104 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_micropython_qstr_info_obj, 0, 1, mp_micropython_qstr_info);
105 
106 #if MICROPY_STACK_CHECK
107 STATIC mp_obj_t mp_micropython_stack_use(void) {
109 }
110 STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_stack_use_obj, mp_micropython_stack_use);
111 #endif
112 
113 #endif // MICROPY_PY_MICROPYTHON_MEM_INFO
114 
115 #if MICROPY_ENABLE_GC
116 STATIC mp_obj_t mp_micropython_heap_lock(void) {
117  gc_lock();
118  return mp_const_none;
119 }
120 STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_heap_lock_obj, mp_micropython_heap_lock);
121 
122 STATIC mp_obj_t mp_micropython_heap_unlock(void) {
123  gc_unlock();
124  return mp_const_none;
125 }
126 STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_heap_unlock_obj, mp_micropython_heap_unlock);
127 #endif
128 
129 #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF && (MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE == 0)
130 STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_alloc_emergency_exception_buf_obj, mp_alloc_emergency_exception_buf);
131 #endif
132 
133 #if MICROPY_KBD_EXCEPTION
134 STATIC mp_obj_t mp_micropython_kbd_intr(mp_obj_t int_chr_in) {
136  return mp_const_none;
137 }
138 STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_micropython_kbd_intr_obj, mp_micropython_kbd_intr);
139 #endif
140 
141 #if MICROPY_ENABLE_SCHEDULER
142 STATIC mp_obj_t mp_micropython_schedule(mp_obj_t function, mp_obj_t arg) {
143  if (!mp_sched_schedule(function, arg)) {
144  mp_raise_msg(&mp_type_RuntimeError, "schedule stack full");
145  }
146  return mp_const_none;
147 }
148 STATIC MP_DEFINE_CONST_FUN_OBJ_2(mp_micropython_schedule_obj, mp_micropython_schedule);
149 #endif
150 
152  { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_micropython) },
153  { MP_ROM_QSTR(MP_QSTR_const), MP_ROM_PTR(&mp_identity_obj) },
154  { MP_ROM_QSTR(MP_QSTR_opt_level), MP_ROM_PTR(&mp_micropython_opt_level_obj) },
155 #if MICROPY_PY_MICROPYTHON_MEM_INFO
156 #if MICROPY_MEM_STATS
157  { MP_ROM_QSTR(MP_QSTR_mem_total), MP_ROM_PTR(&mp_micropython_mem_total_obj) },
158  { MP_ROM_QSTR(MP_QSTR_mem_current), MP_ROM_PTR(&mp_micropython_mem_current_obj) },
159  { MP_ROM_QSTR(MP_QSTR_mem_peak), MP_ROM_PTR(&mp_micropython_mem_peak_obj) },
160 #endif
161  { MP_ROM_QSTR(MP_QSTR_mem_info), MP_ROM_PTR(&mp_micropython_mem_info_obj) },
162  { MP_ROM_QSTR(MP_QSTR_qstr_info), MP_ROM_PTR(&mp_micropython_qstr_info_obj) },
163  #if MICROPY_STACK_CHECK
164  { MP_ROM_QSTR(MP_QSTR_stack_use), MP_ROM_PTR(&mp_micropython_stack_use_obj) },
165  #endif
166 #endif
167 #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF && (MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE == 0)
168  { MP_ROM_QSTR(MP_QSTR_alloc_emergency_exception_buf), MP_ROM_PTR(&mp_alloc_emergency_exception_buf_obj) },
169 #endif
170  #if MICROPY_ENABLE_GC
171  { MP_ROM_QSTR(MP_QSTR_heap_lock), MP_ROM_PTR(&mp_micropython_heap_lock_obj) },
172  { MP_ROM_QSTR(MP_QSTR_heap_unlock), MP_ROM_PTR(&mp_micropython_heap_unlock_obj) },
173  #endif
174  #if MICROPY_KBD_EXCEPTION
175  { MP_ROM_QSTR(MP_QSTR_kbd_intr), MP_ROM_PTR(&mp_micropython_kbd_intr_obj) },
176  #endif
177  #if MICROPY_ENABLE_SCHEDULER
178  { MP_ROM_QSTR(MP_QSTR_schedule), MP_ROM_PTR(&mp_micropython_schedule_obj) },
179  #endif
180 };
181 
182 STATIC MP_DEFINE_CONST_DICT(mp_module_micropython_globals, mp_module_micropython_globals_table);
183 
185  .base = { &mp_type_module },
186  .globals = (mp_obj_dict_t*)&mp_module_micropython_globals,
187 };
const mp_print_t mp_plat_print
Definition: mpprint.c:51
void gc_dump_alloc_table(void)
void gc_dump_info(void)
uintptr_t mp_uint_t
Definition: mpconfigport.h:74
NORETURN void mp_raise_msg(const mp_obj_type_t *exc_type, const char *msg)
Definition: runtime.c:1448
STATIC const mp_rom_map_elem_t mp_module_micropython_globals_table[]
#define mp_const_none
Definition: obj.h:614
mp_obj_t mp_alloc_emergency_exception_buf(mp_obj_t size_in)
mp_uint_t mp_stack_usage(void)
Definition: stackctrl.c:42
#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
#define MP_STATE_THREAD(x)
Definition: mpstate.h:248
#define MP_STATE_VM(x)
Definition: mpstate.h:241
mp_int_t mp_obj_get_int(mp_const_obj_t arg)
Definition: obj.c:225
#define STATIC
Definition: mpconfig.h:1178
void gc_lock(void)
#define MP_DEFINE_CONST_FUN_OBJ_1(obj_name, fun_name)
Definition: obj.h:285
#define MP_OBJ_NEW_SMALL_INT(small_int)
Definition: obj.h:87
STATIC MP_DEFINE_CONST_DICT(mp_module_micropython_globals, mp_module_micropython_globals_table)
#define UINT_FMT
Definition: mpconfigport.h:71
args
Definition: i18n.py:175
#define MP_DEFINE_CONST_FUN_OBJ_0(obj_name, fun_name)
Definition: obj.h:282
#define MP_DEFINE_CONST_FUN_OBJ_2(obj_name, fun_name)
Definition: obj.h:288
STATIC mp_obj_t mp_micropython_opt_level(size_t n_args, const mp_obj_t *args)
void mp_hal_set_interrupt_char(int c)
void qstr_dump_data(void)
const mp_obj_type_t mp_type_module
Definition: objmodule.c:94
mp_obj_t mp_micropython_mem_info(size_t n_args, const mp_obj_t *args)
uint64_t mp_obj_t
Definition: obj.h:39
const mp_obj_module_t mp_module_micropython
int mp_printf(const mp_print_t *print, const char *fmt,...)
Definition: mpprint.c:380
void gc_unlock(void)
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_micropython_opt_level_obj, 0, 1, mp_micropython_opt_level)
void qstr_pool_info(size_t *n_pool, size_t *n_qstr, size_t *n_str_data_bytes, size_t *n_total_bytes)
Definition: qstr.c:289
const mp_obj_type_t mp_type_RuntimeError