mpstate.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. // CONTINUE ROOT POINTER SECTION
  95. // This must start at the start of this structure and follows
  96. // the state in the mp_state_thread_t structure, continuing
  97. // the root pointer section from there.
  98. //
  99. qstr_pool_t *last_pool;
  100. // non-heap memory for creating an exception if we can't allocate RAM
  101. mp_obj_exception_t mp_emergency_exception_obj;
  102. // memory for exception arguments if we can't allocate RAM
  103. #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF
  104. #if MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE > 0
  105. // statically allocated buf (needs to be aligned to mp_obj_t)
  106. mp_obj_t mp_emergency_exception_buf[MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE / sizeof(mp_obj_t)];
  107. #else
  108. // dynamically allocated buf
  109. byte *mp_emergency_exception_buf;
  110. #endif
  111. #endif
  112. #if MICROPY_KBD_EXCEPTION
  113. // exception object of type KeyboardInterrupt
  114. mp_obj_exception_t mp_kbd_exception;
  115. #endif
  116. // dictionary with loaded modules (may be exposed as sys.modules)
  117. mp_obj_dict_t mp_loaded_modules_dict;
  118. // pending exception object (MP_OBJ_NULL if not pending)
  119. volatile mp_obj_t mp_pending_exception;
  120. #if MICROPY_ENABLE_SCHEDULER
  121. mp_sched_item_t sched_stack[MICROPY_SCHEDULER_DEPTH];
  122. #endif
  123. // current exception being handled, for sys.exc_info()
  124. #if MICROPY_PY_SYS_EXC_INFO
  125. mp_obj_base_t *cur_exception;
  126. #endif
  127. // dictionary for the __main__ module
  128. mp_obj_dict_t dict_main;
  129. // these two lists must be initialised per port, after the call to mp_init
  130. mp_obj_list_t mp_sys_path_obj;
  131. mp_obj_list_t mp_sys_argv_obj;
  132. // dictionary for overridden builtins
  133. #if MICROPY_CAN_OVERRIDE_BUILTINS
  134. mp_obj_dict_t *mp_module_builtins_override_dict;
  135. #endif
  136. // include any root pointers defined by a port
  137. MICROPY_PORT_ROOT_POINTERS
  138. // root pointers for extmod
  139. #if MICROPY_REPL_EVENT_DRIVEN
  140. vstr_t *repl_line;
  141. #endif
  142. #if MICROPY_PY_OS_DUPTERM
  143. mp_obj_t dupterm_objs[MICROPY_PY_OS_DUPTERM];
  144. #endif
  145. #if MICROPY_PY_LWIP_SLIP
  146. mp_obj_t lwip_slip_stream;
  147. #endif
  148. #if MICROPY_VFS
  149. struct _mp_vfs_mount_t *vfs_cur;
  150. struct _mp_vfs_mount_t *vfs_mount_table;
  151. #endif
  152. //
  153. // END ROOT POINTER SECTION
  154. ////////////////////////////////////////////////////////////
  155. // pointer and sizes to store interned string data
  156. // (qstr_last_chunk can be root pointer but is also stored in qstr pool)
  157. byte *qstr_last_chunk;
  158. size_t qstr_last_alloc;
  159. size_t qstr_last_used;
  160. #if MICROPY_PY_THREAD
  161. // This is a global mutex used to make qstr interning thread-safe.
  162. mp_thread_mutex_t qstr_mutex;
  163. #endif
  164. #if MICROPY_ENABLE_COMPILER
  165. mp_uint_t mp_optimise_value;
  166. #endif
  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_ENABLE_SCHEDULER
  172. volatile int16_t sched_state;
  173. uint16_t sched_sp;
  174. #endif
  175. #if MICROPY_PY_THREAD_GIL
  176. // This is a global mutex used to make the VM/runtime thread-safe.
  177. mp_thread_mutex_t gil_mutex;
  178. #endif
  179. } mp_state_vm_t;
  180. // This structure holds state that is specific to a given thread.
  181. // Everything in this structure is scanned for root pointers.
  182. typedef struct _mp_state_thread_t {
  183. // Stack top at the start of program
  184. char *stack_top;
  185. #if MICROPY_STACK_CHECK
  186. size_t stack_limit;
  187. #endif
  188. #if MICROPY_ENABLE_PYSTACK
  189. uint8_t *pystack_start;
  190. uint8_t *pystack_end;
  191. uint8_t *pystack_cur;
  192. #endif
  193. ////////////////////////////////////////////////////////////
  194. // START ROOT POINTER SECTION
  195. // Everything that needs GC scanning must start here, and
  196. // is followed by state in the mp_state_vm_t structure.
  197. //
  198. mp_obj_dict_t *dict_locals;
  199. mp_obj_dict_t *dict_globals;
  200. nlr_buf_t *nlr_top;
  201. } mp_state_thread_t;
  202. // This structure combines the above 3 structures.
  203. // The order of the entries are important for root pointer scanning in the GC to work.
  204. typedef struct _mp_state_ctx_t {
  205. mp_state_thread_t thread;
  206. mp_state_vm_t vm;
  207. mp_state_mem_t mem;
  208. } mp_state_ctx_t;
  209. extern mp_state_ctx_t mp_state_ctx;
  210. #define MP_STATE_VM(x) (mp_state_ctx.vm.x)
  211. #define MP_STATE_MEM(x) (mp_state_ctx.mem.x)
  212. #if MICROPY_PY_THREAD
  213. extern mp_state_thread_t *mp_thread_get_state(void);
  214. #define MP_STATE_THREAD(x) (mp_thread_get_state()->x)
  215. #else
  216. #define MP_STATE_THREAD(x) (mp_state_ctx.thread.x)
  217. #endif
  218. #endif // MICROPY_INCLUDED_PY_MPSTATE_H