mpstate.h 8.0 KB

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