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" |
| 32 | #include "py/misc.h" |
| 33 | #include "py/nlr.h" |
| 34 | #include "py/obj.h" |
Damien George | e1e359f | 2015-02-07 17:24:10 +0000 | [diff] [blame] | 35 | #include "py/objlist.h" |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 36 | #include "py/objexcept.h" |
| 37 | |
| 38 | // This file contains structures defining the state of the Micro Python |
| 39 | // memory system, runtime and virtual machine. The state is a global |
| 40 | // variable, but in the future it is hoped that the state can become local. |
| 41 | |
| 42 | // This structure hold information about the memory allocation system. |
| 43 | typedef struct _mp_state_mem_t { |
| 44 | #if MICROPY_MEM_STATS |
| 45 | size_t total_bytes_allocated; |
| 46 | size_t current_bytes_allocated; |
| 47 | size_t peak_bytes_allocated; |
| 48 | #endif |
| 49 | |
| 50 | byte *gc_alloc_table_start; |
| 51 | mp_uint_t gc_alloc_table_byte_len; |
| 52 | #if MICROPY_ENABLE_FINALISER |
| 53 | byte *gc_finaliser_table_start; |
| 54 | #endif |
Damien George | 94fe6e5 | 2015-11-27 13:07:48 +0000 | [diff] [blame] | 55 | byte *gc_pool_start; |
| 56 | byte *gc_pool_end; |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 57 | |
| 58 | int gc_stack_overflow; |
| 59 | mp_uint_t gc_stack[MICROPY_ALLOC_GC_STACK_SIZE]; |
| 60 | mp_uint_t *gc_sp; |
| 61 | uint16_t gc_lock_depth; |
| 62 | |
| 63 | // This variable controls auto garbage collection. If set to 0 then the |
| 64 | // GC won't automatically run when gc_alloc can't find enough blocks. But |
| 65 | // you can still allocate/free memory and also explicitly call gc_collect. |
| 66 | uint16_t gc_auto_collect_enabled; |
| 67 | |
| 68 | mp_uint_t gc_last_free_atb_index; |
Damien George | e1e359f | 2015-02-07 17:24:10 +0000 | [diff] [blame] | 69 | |
| 70 | #if MICROPY_PY_GC_COLLECT_RETVAL |
| 71 | mp_uint_t gc_collected; |
| 72 | #endif |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 73 | } mp_state_mem_t; |
| 74 | |
| 75 | // This structure hold runtime and VM information. It includes a section |
| 76 | // which contains root pointers that must be scanned by the GC. |
| 77 | typedef struct _mp_state_vm_t { |
| 78 | //////////////////////////////////////////////////////////// |
| 79 | // START ROOT POINTER SECTION |
| 80 | // everything that needs GC scanning must go here |
| 81 | // this must start at the start of this structure |
| 82 | // |
| 83 | |
| 84 | // Note: nlr asm code has the offset of this hard-coded |
| 85 | nlr_buf_t *nlr_top; |
| 86 | |
| 87 | qstr_pool_t *last_pool; |
| 88 | |
| 89 | // non-heap memory for creating an exception if we can't allocate RAM |
| 90 | mp_obj_exception_t mp_emergency_exception_obj; |
| 91 | |
| 92 | // memory for exception arguments if we can't allocate RAM |
| 93 | #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF |
| 94 | #if MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE > 0 |
| 95 | // statically allocated buf |
| 96 | byte mp_emergency_exception_buf[MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE]; |
| 97 | #else |
| 98 | // dynamically allocated buf |
| 99 | byte *mp_emergency_exception_buf; |
| 100 | #endif |
| 101 | #endif |
| 102 | |
| 103 | // map with loaded modules |
| 104 | // TODO: expose as sys.modules |
| 105 | mp_map_t mp_loaded_modules_map; |
| 106 | |
| 107 | // pending exception object (MP_OBJ_NULL if not pending) |
Damien George | 994ff73 | 2015-11-17 14:27:21 +0000 | [diff] [blame] | 108 | volatile mp_obj_t mp_pending_exception; |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 109 | |
Paul Sokolovsky | 8b85d14 | 2015-04-25 03:17:41 +0300 | [diff] [blame] | 110 | // current exception being handled, for sys.exc_info() |
| 111 | #if MICROPY_PY_SYS_EXC_INFO |
Damien George | 999cedb | 2015-11-27 17:01:44 +0000 | [diff] [blame^] | 112 | mp_obj_base_t *cur_exception; |
Paul Sokolovsky | 8b85d14 | 2015-04-25 03:17:41 +0300 | [diff] [blame] | 113 | #endif |
| 114 | |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 115 | // dictionary for the __main__ module |
| 116 | mp_obj_dict_t dict_main; |
| 117 | |
Damien George | e1e359f | 2015-02-07 17:24:10 +0000 | [diff] [blame] | 118 | // these two lists must be initialised per port, after the call to mp_init |
| 119 | mp_obj_list_t mp_sys_path_obj; |
| 120 | mp_obj_list_t mp_sys_argv_obj; |
| 121 | |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 122 | // dictionary for overridden builtins |
| 123 | #if MICROPY_CAN_OVERRIDE_BUILTINS |
| 124 | mp_obj_dict_t *mp_module_builtins_override_dict; |
| 125 | #endif |
| 126 | |
| 127 | // include any root pointers defined by a port |
| 128 | MICROPY_PORT_ROOT_POINTERS |
| 129 | |
Paul Sokolovsky | e0d7740 | 2015-10-27 00:04:33 +0300 | [diff] [blame] | 130 | // root pointers for extmod |
| 131 | #if MICROPY_PY_LWIP_SLIP |
| 132 | mp_obj_t lwip_slip_stream; |
| 133 | #endif |
| 134 | |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 135 | // |
| 136 | // END ROOT POINTER SECTION |
| 137 | //////////////////////////////////////////////////////////// |
| 138 | |
Damien George | ade9a05 | 2015-06-13 21:53:22 +0100 | [diff] [blame] | 139 | // pointer and sizes to store interned string data |
| 140 | // (qstr_last_chunk can be root pointer but is also stored in qstr pool) |
| 141 | byte *qstr_last_chunk; |
| 142 | mp_uint_t qstr_last_alloc; |
| 143 | mp_uint_t qstr_last_used; |
| 144 | |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 145 | // Stack top at the start of program |
| 146 | // Note: this entry is used to locate the end of the root pointer section. |
| 147 | char *stack_top; |
| 148 | |
| 149 | #if MICROPY_STACK_CHECK |
| 150 | mp_uint_t stack_limit; |
| 151 | #endif |
| 152 | |
| 153 | mp_uint_t mp_optimise_value; |
| 154 | |
| 155 | // size of the emergency exception buf, if it's dynamically allocated |
| 156 | #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF && MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE == 0 |
| 157 | mp_int_t mp_emergency_exception_buf_size; |
| 158 | #endif |
| 159 | } mp_state_vm_t; |
| 160 | |
| 161 | // This structure combines the above 2 structures, and adds the local |
| 162 | // and global dicts. |
| 163 | // Note: if this structure changes then revisit all nlr asm code since they |
| 164 | // have the offset of nlr_top hard-coded. |
| 165 | typedef struct _mp_state_ctx_t { |
| 166 | // these must come first for root pointer scanning in GC to work |
| 167 | mp_obj_dict_t *dict_locals; |
| 168 | mp_obj_dict_t *dict_globals; |
| 169 | // this must come next for root pointer scanning in GC to work |
| 170 | mp_state_vm_t vm; |
| 171 | mp_state_mem_t mem; |
| 172 | } mp_state_ctx_t; |
| 173 | |
| 174 | extern mp_state_ctx_t mp_state_ctx; |
| 175 | |
| 176 | #define MP_STATE_CTX(x) (mp_state_ctx.x) |
| 177 | #define MP_STATE_VM(x) (mp_state_ctx.vm.x) |
| 178 | #define MP_STATE_MEM(x) (mp_state_ctx.mem.x) |
| 179 | |
| 180 | #endif // __MICROPY_INCLUDED_PY_MPSTATE_H__ |