Numworks Epsilon  1.4.1
Graphing Calculator Operating System
mpstate.h
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) 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 #ifndef MICROPY_INCLUDED_PY_MPSTATE_H
27 #define MICROPY_INCLUDED_PY_MPSTATE_H
28 
29 #include <stdint.h>
30 
31 #include "py/mpconfig.h"
32 #include "py/mpthread.h"
33 #include "py/misc.h"
34 #include "py/nlr.h"
35 #include "py/obj.h"
36 #include "py/objlist.h"
37 #include "py/objexcept.h"
38 
39 // This file contains structures defining the state of the MicroPython
40 // memory system, runtime and virtual machine. The state is a global
41 // variable, but in the future it is hoped that the state can become local.
42 
43 // This structure contains dynamic configuration for the compiler.
44 #if MICROPY_DYNAMIC_COMPILER
45 typedef struct mp_dynamic_compiler_t {
46  uint8_t small_int_bits; // must be <= host small_int_bits
47  bool opt_cache_map_lookup_in_bytecode;
48  bool py_builtins_str_unicode;
49 } mp_dynamic_compiler_t;
50 extern mp_dynamic_compiler_t mp_dynamic_compiler;
51 #endif
52 
53 // These are the values for sched_state
54 #define MP_SCHED_IDLE (1)
55 #define MP_SCHED_LOCKED (-1)
56 #define MP_SCHED_PENDING (0) // 0 so it's a quick check in the VM
57 
58 typedef struct _mp_sched_item_t {
62 
63 // This structure hold information about the memory allocation system.
64 typedef struct _mp_state_mem_t {
65  #if MICROPY_MEM_STATS
66  size_t total_bytes_allocated;
67  size_t current_bytes_allocated;
68  size_t peak_bytes_allocated;
69  #endif
70 
73  #if MICROPY_ENABLE_FINALISER
74  byte *gc_finaliser_table_start;
75  #endif
78 
81  size_t *gc_sp;
83 
84  // This variable controls auto garbage collection. If set to 0 then the
85  // GC won't automatically run when gc_alloc can't find enough blocks. But
86  // you can still allocate/free memory and also explicitly call gc_collect.
88 
89  #if MICROPY_GC_ALLOC_THRESHOLD
90  size_t gc_alloc_amount;
91  size_t gc_alloc_threshold;
92  #endif
93 
95 
96  #if MICROPY_PY_GC_COLLECT_RETVAL
97  size_t gc_collected;
98  #endif
99 
100  #if MICROPY_PY_THREAD
101  // This is a global mutex used to make the GC thread-safe.
102  mp_thread_mutex_t gc_mutex;
103  #endif
105 
106 // This structure hold runtime and VM information. It includes a section
107 // which contains root pointers that must be scanned by the GC.
108 typedef struct _mp_state_vm_t {
110  // START ROOT POINTER SECTION
111  // everything that needs GC scanning must go here
112  // this must start at the start of this structure
113  //
114 
116 
117  // non-heap memory for creating an exception if we can't allocate RAM
119 
120  // memory for exception arguments if we can't allocate RAM
121  #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF
122  #if MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE > 0
123  // statically allocated buf (needs to be aligned to mp_obj_t)
124  mp_obj_t mp_emergency_exception_buf[MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE / sizeof(mp_obj_t)];
125  #else
126  // dynamically allocated buf
127  byte *mp_emergency_exception_buf;
128  #endif
129  #endif
130 
131  #if MICROPY_KBD_EXCEPTION
132  // exception object of type KeyboardInterrupt
133  mp_obj_exception_t mp_kbd_exception;
134  #endif
135 
136  // dictionary with loaded modules (may be exposed as sys.modules)
138 
139  // pending exception object (MP_OBJ_NULL if not pending)
141 
142  #if MICROPY_ENABLE_SCHEDULER
143  volatile int16_t sched_state;
144  uint16_t sched_sp;
146  #endif
147 
148  // current exception being handled, for sys.exc_info()
149  #if MICROPY_PY_SYS_EXC_INFO
150  mp_obj_base_t *cur_exception;
151  #endif
152 
153  // dictionary for the __main__ module
155 
156  // these two lists must be initialised per port, after the call to mp_init
159 
160  // dictionary for overridden builtins
161  #if MICROPY_CAN_OVERRIDE_BUILTINS
162  mp_obj_dict_t *mp_module_builtins_override_dict;
163  #endif
164 
165  // include any root pointers defined by a port
167 
168  // root pointers for extmod
169 
170  #if MICROPY_PY_OS_DUPTERM
171  mp_obj_t dupterm_objs[MICROPY_PY_OS_DUPTERM];
172  mp_obj_t dupterm_arr_obj;
173  #endif
174 
175  #if MICROPY_PY_LWIP_SLIP
176  mp_obj_t lwip_slip_stream;
177  #endif
178 
179  #if MICROPY_VFS
180  struct _mp_vfs_mount_t *vfs_cur;
181  struct _mp_vfs_mount_t *vfs_mount_table;
182  #endif
183 
184  //
185  // END ROOT POINTER SECTION
187 
188  // pointer and sizes to store interned string data
189  // (qstr_last_chunk can be root pointer but is also stored in qstr pool)
193 
194  #if MICROPY_PY_THREAD
195  // This is a global mutex used to make qstr interning thread-safe.
196  mp_thread_mutex_t qstr_mutex;
197  #endif
198 
200 
201  // size of the emergency exception buf, if it's dynamically allocated
202  #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF && MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE == 0
203  mp_int_t mp_emergency_exception_buf_size;
204  #endif
205 
206  #if MICROPY_PY_THREAD_GIL
207  // This is a global mutex used to make the VM/runtime thread-safe.
208  mp_thread_mutex_t gil_mutex;
209  #endif
210 } mp_state_vm_t;
211 
212 // This structure holds state that is specific to a given thread.
213 // Everything in this structure is scanned for root pointers.
214 typedef struct _mp_state_thread_t {
217 
218  // Note: nlr asm code has the offset of this hard-coded
219  nlr_buf_t *nlr_top; // ROOT POINTER
220 
221  // Stack top at the start of program
222  char *stack_top;
223 
224  #if MICROPY_STACK_CHECK
225  size_t stack_limit;
226  #endif
228 
229 // This structure combines the above 3 structures.
230 // The order of the entries are important for root pointer scanning in the GC to work.
231 // Note: if this structure changes then revisit all nlr asm code since they
232 // have the offset of nlr_top hard-coded.
233 typedef struct _mp_state_ctx_t {
238 
240 
241 #define MP_STATE_VM(x) (mp_state_ctx.vm.x)
242 #define MP_STATE_MEM(x) (mp_state_ctx.mem.x)
243 
244 #if MICROPY_PY_THREAD
245 extern mp_state_thread_t *mp_thread_get_state(void);
246 #define MP_STATE_THREAD(x) (mp_thread_get_state()->x)
247 #else
248 #define MP_STATE_THREAD(x) (mp_state_ctx.thread.x)
249 #endif
250 
251 #endif // MICROPY_INCLUDED_PY_MPSTATE_H
size_t qstr_last_used
Definition: mpstate.h:192
mp_obj_dict_t dict_main
Definition: mpstate.h:154
struct _mp_state_vm_t mp_state_vm_t
MICROPY_PORT_ROOT_POINTERS byte * qstr_last_chunk
Definition: mpstate.h:190
intptr_t mp_int_t
Definition: mpconfigport.h:73
uintptr_t mp_uint_t
Definition: mpconfigport.h:74
mp_obj_list_t mp_sys_path_obj
Definition: mpstate.h:157
size_t * gc_sp
Definition: mpstate.h:81
byte * gc_pool_start
Definition: mpstate.h:76
mp_state_ctx_t mp_state_ctx
Definition: mpstate.c:33
unsigned short uint16_t
Definition: stdint.h:5
unsigned char uint8_t
Definition: stdint.h:4
byte * gc_pool_end
Definition: mpstate.h:77
nlr_buf_t * nlr_top
Definition: mpstate.h:219
#define MICROPY_SCHEDULER_DEPTH
Definition: mpconfig.h:658
mp_state_thread_t thread
Definition: mpstate.h:234
#define MICROPY_ALLOC_GC_STACK_SIZE
Definition: mpconfig.h:106
mp_uint_t mp_optimise_value
Definition: mpstate.h:199
int gc_stack_overflow
Definition: mpstate.h:79
size_t gc_last_free_atb_index
Definition: mpstate.h:94
size_t gc_stack[MICROPY_ALLOC_GC_STACK_SIZE]
Definition: mpstate.h:80
uint16_t gc_auto_collect_enabled
Definition: mpstate.h:87
struct _mp_state_thread_t mp_state_thread_t
signed short int16_t
Definition: stdint.h:10
char * stack_top
Definition: mpstate.h:222
byte * gc_alloc_table_start
Definition: mpstate.h:71
#define MICROPY_PORT_ROOT_POINTERS
Definition: mpconfigport.h:104
unsigned char byte
Definition: misc.h:37
struct _mp_sched_item_t mp_sched_item_t
mp_obj_t func
Definition: mpstate.h:59
qstr_pool_t * last_pool
Definition: mpstate.h:115
uint16_t gc_lock_depth
Definition: mpstate.h:82
mp_obj_exception_t mp_emergency_exception_obj
Definition: mpstate.h:118
mp_state_mem_t mem
Definition: mpstate.h:236
Definition: nlr.h:39
mp_obj_t arg
Definition: mpstate.h:60
mp_obj_dict_t * dict_locals
Definition: mpstate.h:215
size_t qstr_last_alloc
Definition: mpstate.h:191
struct _mp_state_mem_t mp_state_mem_t
uint64_t mp_obj_t
Definition: obj.h:39
mp_obj_dict_t mp_loaded_modules_dict
Definition: mpstate.h:137
mp_state_vm_t vm
Definition: mpstate.h:235
volatile mp_obj_t mp_pending_exception
Definition: mpstate.h:140
size_t gc_alloc_table_byte_len
Definition: mpstate.h:72
mp_obj_list_t mp_sys_argv_obj
Definition: mpstate.h:158
struct _mp_state_ctx_t mp_state_ctx_t
mp_obj_dict_t * dict_globals
Definition: mpstate.h:216