mpstate.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. size_t *gc_sp;
  73. uint16_t gc_lock_depth;
  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. #if MICROPY_GC_ALLOC_THRESHOLD
  79. size_t gc_alloc_amount;
  80. size_t gc_alloc_threshold;
  81. #endif
  82. size_t gc_last_free_atb_index;
  83. #if MICROPY_PY_GC_COLLECT_RETVAL
  84. size_t gc_collected;
  85. #endif
  86. #if MICROPY_PY_THREAD
  87. // This is a global mutex used to make the GC thread-safe.
  88. mp_thread_mutex_t gc_mutex;
  89. #endif
  90. } mp_state_mem_t;
  91. // This structure hold runtime and VM information. It includes a section
  92. // which contains root pointers that must be scanned by the GC.
  93. typedef struct _mp_state_vm_t {
  94. ////////////////////////////////////////////////////////////
  95. // START ROOT POINTER SECTION
  96. // everything that needs GC scanning must go here
  97. // this must start at the start of this structure
  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. volatile int16_t sched_state;
  122. uint16_t sched_sp;
  123. mp_sched_item_t sched_stack[MICROPY_SCHEDULER_DEPTH];
  124. #endif
  125. // current exception being handled, for sys.exc_info()
  126. #if MICROPY_PY_SYS_EXC_INFO
  127. mp_obj_base_t *cur_exception;
  128. #endif
  129. // dictionary for the __main__ module
  130. mp_obj_dict_t dict_main;
  131. // these two lists must be initialised per port, after the call to mp_init
  132. mp_obj_list_t mp_sys_path_obj;
  133. mp_obj_list_t mp_sys_argv_obj;
  134. // dictionary for overridden builtins
  135. #if MICROPY_CAN_OVERRIDE_BUILTINS
  136. mp_obj_dict_t *mp_module_builtins_override_dict;
  137. #endif
  138. // include any root pointers defined by a port
  139. MICROPY_PORT_ROOT_POINTERS
  140. // root pointers for extmod
  141. #if MICROPY_PY_OS_DUPTERM
  142. mp_obj_t dupterm_objs[MICROPY_PY_OS_DUPTERM];
  143. mp_obj_t dupterm_arr_obj;
  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. mp_uint_t mp_optimise_value;
  165. // size of the emergency exception buf, if it's dynamically allocated
  166. #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF && MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE == 0
  167. mp_int_t mp_emergency_exception_buf_size;
  168. #endif
  169. #if MICROPY_PY_THREAD_GIL
  170. // This is a global mutex used to make the VM/runtime thread-safe.
  171. mp_thread_mutex_t gil_mutex;
  172. #endif
  173. } mp_state_vm_t;
  174. // This structure holds state that is specific to a given thread.
  175. // Everything in this structure is scanned for root pointers.
  176. typedef struct _mp_state_thread_t {
  177. mp_obj_dict_t *dict_locals;
  178. mp_obj_dict_t *dict_globals;
  179. // Note: nlr asm code has the offset of this hard-coded
  180. nlr_buf_t *nlr_top; // ROOT POINTER
  181. // Stack top at the start of program
  182. char *stack_top;
  183. #if MICROPY_STACK_CHECK
  184. size_t stack_limit;
  185. #endif
  186. } mp_state_thread_t;
  187. // This structure combines the above 3 structures.
  188. // The order of the entries are important for root pointer scanning in the GC to work.
  189. // Note: if this structure changes then revisit all nlr asm code since they
  190. // have the offset of nlr_top hard-coded.
  191. typedef struct _mp_state_ctx_t {
  192. mp_state_thread_t thread;
  193. mp_state_vm_t vm;
  194. mp_state_mem_t mem;
  195. } mp_state_ctx_t;
  196. extern mp_state_ctx_t mp_state_ctx;
  197. #define MP_STATE_VM(x) (mp_state_ctx.vm.x)
  198. #define MP_STATE_MEM(x) (mp_state_ctx.mem.x)
  199. #if MICROPY_PY_THREAD
  200. extern mp_state_thread_t *mp_thread_get_state(void);
  201. #define MP_STATE_THREAD(x) (mp_thread_get_state()->x)
  202. #else
  203. #define MP_STATE_THREAD(x) (mp_state_ctx.thread.x)
  204. #endif
  205. #endif // MICROPY_INCLUDED_PY_MPSTATE_H