Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the Micro Python 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" |
Damien George | c93d9ca | 2016-04-25 15:28:57 +0000 | [diff] [blame] | 32 | #include "py/mpthread.h" |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 33 | #include "py/misc.h" |
| 34 | #include "py/nlr.h" |
| 35 | #include "py/obj.h" |
Damien George | e1e359f | 2015-02-07 17:24:10 +0000 | [diff] [blame] | 36 | #include "py/objlist.h" |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 37 | #include "py/objexcept.h" |
| 38 | |
| 39 | // This file contains structures defining the state of the Micro Python |
| 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 | |
Damien George | ea23520 | 2016-02-11 22:30:53 +0000 | [diff] [blame] | 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 | |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 53 | // This structure hold information about the memory allocation system. |
| 54 | typedef struct _mp_state_mem_t { |
| 55 | #if MICROPY_MEM_STATS |
| 56 | size_t total_bytes_allocated; |
| 57 | size_t current_bytes_allocated; |
| 58 | size_t peak_bytes_allocated; |
| 59 | #endif |
| 60 | |
| 61 | byte *gc_alloc_table_start; |
Damien George | d977d26 | 2015-12-16 20:09:11 -0500 | [diff] [blame] | 62 | size_t gc_alloc_table_byte_len; |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 63 | #if MICROPY_ENABLE_FINALISER |
| 64 | byte *gc_finaliser_table_start; |
| 65 | #endif |
Damien George | 94fe6e5 | 2015-11-27 13:07:48 +0000 | [diff] [blame] | 66 | byte *gc_pool_start; |
| 67 | byte *gc_pool_end; |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 68 | |
| 69 | int gc_stack_overflow; |
Damien George | d977d26 | 2015-12-16 20:09:11 -0500 | [diff] [blame] | 70 | size_t gc_stack[MICROPY_ALLOC_GC_STACK_SIZE]; |
| 71 | size_t *gc_sp; |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 72 | uint16_t gc_lock_depth; |
| 73 | |
| 74 | // This variable controls auto garbage collection. If set to 0 then the |
| 75 | // GC won't automatically run when gc_alloc can't find enough blocks. But |
| 76 | // you can still allocate/free memory and also explicitly call gc_collect. |
| 77 | uint16_t gc_auto_collect_enabled; |
| 78 | |
Paul Sokolovsky | 93e353e | 2016-07-21 00:37:30 +0300 | [diff] [blame] | 79 | #if MICROPY_GC_ALLOC_THRESHOLD |
| 80 | size_t gc_alloc_amount; |
| 81 | size_t gc_alloc_threshold; |
| 82 | #endif |
| 83 | |
Damien George | d977d26 | 2015-12-16 20:09:11 -0500 | [diff] [blame] | 84 | size_t gc_last_free_atb_index; |
Damien George | e1e359f | 2015-02-07 17:24:10 +0000 | [diff] [blame] | 85 | |
| 86 | #if MICROPY_PY_GC_COLLECT_RETVAL |
Damien George | d977d26 | 2015-12-16 20:09:11 -0500 | [diff] [blame] | 87 | size_t gc_collected; |
Damien George | e1e359f | 2015-02-07 17:24:10 +0000 | [diff] [blame] | 88 | #endif |
Damien George | c93d9ca | 2016-04-25 15:28:57 +0000 | [diff] [blame] | 89 | |
| 90 | #if MICROPY_PY_THREAD |
| 91 | // This is a global mutex used to make the GC thread-safe. |
| 92 | mp_thread_mutex_t gc_mutex; |
| 93 | #endif |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 94 | } mp_state_mem_t; |
| 95 | |
| 96 | // This structure hold runtime and VM information. It includes a section |
| 97 | // which contains root pointers that must be scanned by the GC. |
| 98 | typedef struct _mp_state_vm_t { |
| 99 | //////////////////////////////////////////////////////////// |
| 100 | // START ROOT POINTER SECTION |
| 101 | // everything that needs GC scanning must go here |
| 102 | // this must start at the start of this structure |
| 103 | // |
| 104 | |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 105 | qstr_pool_t *last_pool; |
| 106 | |
| 107 | // non-heap memory for creating an exception if we can't allocate RAM |
| 108 | mp_obj_exception_t mp_emergency_exception_obj; |
| 109 | |
| 110 | // memory for exception arguments if we can't allocate RAM |
| 111 | #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF |
| 112 | #if MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE > 0 |
| 113 | // statically allocated buf |
| 114 | byte mp_emergency_exception_buf[MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE]; |
| 115 | #else |
| 116 | // dynamically allocated buf |
| 117 | byte *mp_emergency_exception_buf; |
| 118 | #endif |
| 119 | #endif |
| 120 | |
Damien George | 7f1da0a | 2016-12-15 13:00:19 +1100 | [diff] [blame] | 121 | #if MICROPY_KBD_EXCEPTION |
| 122 | // exception object of type KeyboardInterrupt |
| 123 | mp_obj_exception_t mp_kbd_exception; |
| 124 | #endif |
| 125 | |
Paul Sokolovsky | 1a1d11f | 2015-12-05 00:09:10 +0200 | [diff] [blame] | 126 | // dictionary with loaded modules (may be exposed as sys.modules) |
| 127 | mp_obj_dict_t mp_loaded_modules_dict; |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 128 | |
| 129 | // pending exception object (MP_OBJ_NULL if not pending) |
Damien George | 994ff73 | 2015-11-17 14:27:21 +0000 | [diff] [blame] | 130 | volatile mp_obj_t mp_pending_exception; |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 131 | |
Paul Sokolovsky | 8b85d14 | 2015-04-25 03:17:41 +0300 | [diff] [blame] | 132 | // current exception being handled, for sys.exc_info() |
| 133 | #if MICROPY_PY_SYS_EXC_INFO |
Damien George | 999cedb | 2015-11-27 17:01:44 +0000 | [diff] [blame] | 134 | mp_obj_base_t *cur_exception; |
Paul Sokolovsky | 8b85d14 | 2015-04-25 03:17:41 +0300 | [diff] [blame] | 135 | #endif |
| 136 | |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 137 | // dictionary for the __main__ module |
| 138 | mp_obj_dict_t dict_main; |
| 139 | |
Damien George | e1e359f | 2015-02-07 17:24:10 +0000 | [diff] [blame] | 140 | // these two lists must be initialised per port, after the call to mp_init |
| 141 | mp_obj_list_t mp_sys_path_obj; |
| 142 | mp_obj_list_t mp_sys_argv_obj; |
| 143 | |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 144 | // dictionary for overridden builtins |
| 145 | #if MICROPY_CAN_OVERRIDE_BUILTINS |
| 146 | mp_obj_dict_t *mp_module_builtins_override_dict; |
| 147 | #endif |
| 148 | |
| 149 | // include any root pointers defined by a port |
| 150 | MICROPY_PORT_ROOT_POINTERS |
| 151 | |
Paul Sokolovsky | e0d7740 | 2015-10-27 00:04:33 +0300 | [diff] [blame] | 152 | // root pointers for extmod |
Paul Sokolovsky | 00ee84e | 2016-01-01 14:19:26 +0200 | [diff] [blame] | 153 | |
| 154 | #if MICROPY_PY_OS_DUPTERM |
| 155 | mp_obj_t term_obj; |
Paul Sokolovsky | 426112c | 2016-07-04 13:32:30 +0300 | [diff] [blame] | 156 | mp_obj_t dupterm_arr_obj; |
Paul Sokolovsky | 00ee84e | 2016-01-01 14:19:26 +0200 | [diff] [blame] | 157 | #endif |
| 158 | |
Paul Sokolovsky | e0d7740 | 2015-10-27 00:04:33 +0300 | [diff] [blame] | 159 | #if MICROPY_PY_LWIP_SLIP |
| 160 | mp_obj_t lwip_slip_stream; |
| 161 | #endif |
| 162 | |
Damien George | dcb9ea7 | 2017-01-27 15:10:09 +1100 | [diff] [blame] | 163 | #if MICROPY_VFS |
Damien George | 3f6b4e0 | 2017-01-27 22:40:15 +1100 | [diff] [blame] | 164 | struct _mp_vfs_mount_t *vfs_cur; |
| 165 | struct _mp_vfs_mount_t *vfs_mount_table; |
Damien George | dcb9ea7 | 2017-01-27 15:10:09 +1100 | [diff] [blame] | 166 | #endif |
| 167 | |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 168 | // |
| 169 | // END ROOT POINTER SECTION |
| 170 | //////////////////////////////////////////////////////////// |
| 171 | |
Damien George | ade9a05 | 2015-06-13 21:53:22 +0100 | [diff] [blame] | 172 | // pointer and sizes to store interned string data |
| 173 | // (qstr_last_chunk can be root pointer but is also stored in qstr pool) |
| 174 | byte *qstr_last_chunk; |
Damien George | 2578485 | 2015-12-17 12:41:40 +0000 | [diff] [blame] | 175 | size_t qstr_last_alloc; |
| 176 | size_t qstr_last_used; |
Damien George | ade9a05 | 2015-06-13 21:53:22 +0100 | [diff] [blame] | 177 | |
Damien George | 1f54ad2 | 2016-05-26 09:06:46 +0000 | [diff] [blame] | 178 | #if MICROPY_PY_THREAD |
| 179 | // This is a global mutex used to make qstr interning thread-safe. |
| 180 | mp_thread_mutex_t qstr_mutex; |
| 181 | #endif |
| 182 | |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 183 | mp_uint_t mp_optimise_value; |
| 184 | |
| 185 | // size of the emergency exception buf, if it's dynamically allocated |
| 186 | #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF && MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE == 0 |
| 187 | mp_int_t mp_emergency_exception_buf_size; |
| 188 | #endif |
Damien George | 4cec63a | 2016-05-26 10:42:53 +0000 | [diff] [blame] | 189 | |
| 190 | #if MICROPY_PY_THREAD_GIL |
| 191 | // This is a global mutex used to make the VM/runtime thread-safe. |
| 192 | mp_thread_mutex_t gil_mutex; |
| 193 | #endif |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 194 | } mp_state_vm_t; |
| 195 | |
Damien George | 330165a | 2016-04-22 22:44:56 +0000 | [diff] [blame] | 196 | // This structure holds state that is specific to a given thread. |
| 197 | // Everything in this structure is scanned for root pointers. |
| 198 | typedef struct _mp_state_thread_t { |
Damien George | 05fe66f | 2017-02-27 23:56:46 +1100 | [diff] [blame] | 199 | mp_obj_dict_t *dict_locals; |
| 200 | mp_obj_dict_t *dict_globals; |
| 201 | |
Damien George | 330165a | 2016-04-22 22:44:56 +0000 | [diff] [blame] | 202 | // Note: nlr asm code has the offset of this hard-coded |
| 203 | nlr_buf_t *nlr_top; // ROOT POINTER |
| 204 | |
| 205 | // Stack top at the start of program |
Damien George | 330165a | 2016-04-22 22:44:56 +0000 | [diff] [blame] | 206 | char *stack_top; |
| 207 | |
| 208 | #if MICROPY_STACK_CHECK |
| 209 | size_t stack_limit; |
| 210 | #endif |
| 211 | } mp_state_thread_t; |
| 212 | |
Damien George | 05fe66f | 2017-02-27 23:56:46 +1100 | [diff] [blame] | 213 | // This structure combines the above 3 structures. |
| 214 | // The order of the entries are important for root pointer scanning in the GC to work. |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 215 | // Note: if this structure changes then revisit all nlr asm code since they |
| 216 | // have the offset of nlr_top hard-coded. |
| 217 | typedef struct _mp_state_ctx_t { |
Damien George | 330165a | 2016-04-22 22:44:56 +0000 | [diff] [blame] | 218 | mp_state_thread_t thread; |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 219 | mp_state_vm_t vm; |
| 220 | mp_state_mem_t mem; |
| 221 | } mp_state_ctx_t; |
| 222 | |
| 223 | extern mp_state_ctx_t mp_state_ctx; |
| 224 | |
Damien George | 05fe66f | 2017-02-27 23:56:46 +1100 | [diff] [blame] | 225 | #define MP_STATE_CTX(x) MP_STATE_THREAD(x) |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 226 | #define MP_STATE_VM(x) (mp_state_ctx.vm.x) |
| 227 | #define MP_STATE_MEM(x) (mp_state_ctx.mem.x) |
| 228 | |
Damien George | 27cc077 | 2016-04-22 22:52:33 +0000 | [diff] [blame] | 229 | #if MICROPY_PY_THREAD |
| 230 | extern mp_state_thread_t *mp_thread_get_state(void); |
| 231 | #define MP_STATE_THREAD(x) (mp_thread_get_state()->x) |
| 232 | #else |
Damien George | 330165a | 2016-04-22 22:44:56 +0000 | [diff] [blame] | 233 | #define MP_STATE_THREAD(x) (mp_state_ctx.thread.x) |
Damien George | 27cc077 | 2016-04-22 22:52:33 +0000 | [diff] [blame] | 234 | #endif |
Damien George | 330165a | 2016-04-22 22:44:56 +0000 | [diff] [blame] | 235 | |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 236 | #endif // __MICROPY_INCLUDED_PY_MPSTATE_H__ |