blob: b79fd7c5c73b2054e17b788264c4400d9ebad26b [file] [log] [blame]
Damien Georgeb4b10fd2015-01-01 23:30:53 +00001/*
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 Georgec93d9ca2016-04-25 15:28:57 +000032#include "py/mpthread.h"
Damien Georgeb4b10fd2015-01-01 23:30:53 +000033#include "py/misc.h"
34#include "py/nlr.h"
35#include "py/obj.h"
Damien Georgee1e359f2015-02-07 17:24:10 +000036#include "py/objlist.h"
Damien Georgeb4b10fd2015-01-01 23:30:53 +000037#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 Georgeea235202016-02-11 22:30:53 +000043// This structure contains dynamic configuration for the compiler.
44#if MICROPY_DYNAMIC_COMPILER
45typedef 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;
50extern mp_dynamic_compiler_t mp_dynamic_compiler;
51#endif
52
Damien Georgeb4b10fd2015-01-01 23:30:53 +000053// This structure hold information about the memory allocation system.
54typedef 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 Georged977d262015-12-16 20:09:11 -050062 size_t gc_alloc_table_byte_len;
Damien Georgeb4b10fd2015-01-01 23:30:53 +000063 #if MICROPY_ENABLE_FINALISER
64 byte *gc_finaliser_table_start;
65 #endif
Damien George94fe6e52015-11-27 13:07:48 +000066 byte *gc_pool_start;
67 byte *gc_pool_end;
Damien Georgeb4b10fd2015-01-01 23:30:53 +000068
69 int gc_stack_overflow;
Damien Georged977d262015-12-16 20:09:11 -050070 size_t gc_stack[MICROPY_ALLOC_GC_STACK_SIZE];
71 size_t *gc_sp;
Damien Georgeb4b10fd2015-01-01 23:30:53 +000072 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
Damien Georged977d262015-12-16 20:09:11 -050079 size_t gc_last_free_atb_index;
Damien Georgee1e359f2015-02-07 17:24:10 +000080
81 #if MICROPY_PY_GC_COLLECT_RETVAL
Damien Georged977d262015-12-16 20:09:11 -050082 size_t gc_collected;
Damien Georgee1e359f2015-02-07 17:24:10 +000083 #endif
Damien Georgec93d9ca2016-04-25 15:28:57 +000084
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
Damien Georgeb4b10fd2015-01-01 23:30:53 +000089} mp_state_mem_t;
90
91// This structure hold runtime and VM information. It includes a section
92// which contains root pointers that must be scanned by the GC.
93typedef 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
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000100 qstr_pool_t *last_pool;
101
102 // non-heap memory for creating an exception if we can't allocate RAM
103 mp_obj_exception_t mp_emergency_exception_obj;
104
105 // memory for exception arguments if we can't allocate RAM
106 #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF
107 #if MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE > 0
108 // statically allocated buf
109 byte mp_emergency_exception_buf[MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE];
110 #else
111 // dynamically allocated buf
112 byte *mp_emergency_exception_buf;
113 #endif
114 #endif
115
Paul Sokolovsky1a1d11f2015-12-05 00:09:10 +0200116 // dictionary with loaded modules (may be exposed as sys.modules)
117 mp_obj_dict_t mp_loaded_modules_dict;
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000118
119 // pending exception object (MP_OBJ_NULL if not pending)
Damien George994ff732015-11-17 14:27:21 +0000120 volatile mp_obj_t mp_pending_exception;
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000121
Paul Sokolovsky8b85d142015-04-25 03:17:41 +0300122 // current exception being handled, for sys.exc_info()
123 #if MICROPY_PY_SYS_EXC_INFO
Damien George999cedb2015-11-27 17:01:44 +0000124 mp_obj_base_t *cur_exception;
Paul Sokolovsky8b85d142015-04-25 03:17:41 +0300125 #endif
126
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000127 // dictionary for the __main__ module
128 mp_obj_dict_t dict_main;
129
Damien Georgee1e359f2015-02-07 17:24:10 +0000130 // 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
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000134 // dictionary for overridden builtins
135 #if MICROPY_CAN_OVERRIDE_BUILTINS
136 mp_obj_dict_t *mp_module_builtins_override_dict;
137 #endif
138
139 // include any root pointers defined by a port
140 MICROPY_PORT_ROOT_POINTERS
141
Paul Sokolovskye0d77402015-10-27 00:04:33 +0300142 // root pointers for extmod
Paul Sokolovsky00ee84e2016-01-01 14:19:26 +0200143
144 #if MICROPY_PY_OS_DUPTERM
145 mp_obj_t term_obj;
146 #endif
147
Paul Sokolovskye0d77402015-10-27 00:04:33 +0300148 #if MICROPY_PY_LWIP_SLIP
149 mp_obj_t lwip_slip_stream;
150 #endif
151
Paul Sokolovsky72085a62016-02-15 00:02:03 +0200152 #if MICROPY_FSUSERMOUNT
153 // for user-mountable block device (max fixed at compile time)
154 struct _fs_user_mount_t *fs_user_mount[MICROPY_FATFS_VOLUMES];
155 #endif
156
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000157 //
158 // END ROOT POINTER SECTION
159 ////////////////////////////////////////////////////////////
160
Damien Georgeade9a052015-06-13 21:53:22 +0100161 // pointer and sizes to store interned string data
162 // (qstr_last_chunk can be root pointer but is also stored in qstr pool)
163 byte *qstr_last_chunk;
Damien George25784852015-12-17 12:41:40 +0000164 size_t qstr_last_alloc;
165 size_t qstr_last_used;
Damien Georgeade9a052015-06-13 21:53:22 +0100166
Damien George1f54ad22016-05-26 09:06:46 +0000167 #if MICROPY_PY_THREAD
168 // This is a global mutex used to make qstr interning thread-safe.
169 mp_thread_mutex_t qstr_mutex;
170 #endif
171
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000172 mp_uint_t mp_optimise_value;
173
174 // size of the emergency exception buf, if it's dynamically allocated
175 #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF && MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE == 0
176 mp_int_t mp_emergency_exception_buf_size;
177 #endif
Damien George4cec63a2016-05-26 10:42:53 +0000178
179 #if MICROPY_PY_THREAD_GIL
180 // This is a global mutex used to make the VM/runtime thread-safe.
181 mp_thread_mutex_t gil_mutex;
182 #endif
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000183} mp_state_vm_t;
184
Damien George330165a2016-04-22 22:44:56 +0000185// This structure holds state that is specific to a given thread.
186// Everything in this structure is scanned for root pointers.
187typedef struct _mp_state_thread_t {
188 // Note: nlr asm code has the offset of this hard-coded
189 nlr_buf_t *nlr_top; // ROOT POINTER
190
191 // Stack top at the start of program
192 // Note: this entry is used to locate the end of the root pointer section.
193 char *stack_top;
194
195 #if MICROPY_STACK_CHECK
196 size_t stack_limit;
197 #endif
198} mp_state_thread_t;
199
200// This structure combines the above 3 structures, and adds the local
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000201// and global dicts.
202// Note: if this structure changes then revisit all nlr asm code since they
203// have the offset of nlr_top hard-coded.
204typedef struct _mp_state_ctx_t {
205 // these must come first for root pointer scanning in GC to work
206 mp_obj_dict_t *dict_locals;
207 mp_obj_dict_t *dict_globals;
Damien George330165a2016-04-22 22:44:56 +0000208 // these must come next in this order for root pointer scanning in GC to work
209 mp_state_thread_t thread;
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000210 mp_state_vm_t vm;
211 mp_state_mem_t mem;
212} mp_state_ctx_t;
213
214extern mp_state_ctx_t mp_state_ctx;
215
216#define MP_STATE_CTX(x) (mp_state_ctx.x)
217#define MP_STATE_VM(x) (mp_state_ctx.vm.x)
218#define MP_STATE_MEM(x) (mp_state_ctx.mem.x)
219
Damien George27cc0772016-04-22 22:52:33 +0000220#if MICROPY_PY_THREAD
221extern mp_state_thread_t *mp_thread_get_state(void);
222#define MP_STATE_THREAD(x) (mp_thread_get_state()->x)
223#else
Damien George330165a2016-04-22 22:44:56 +0000224#define MP_STATE_THREAD(x) (mp_state_ctx.thread.x)
Damien George27cc0772016-04-22 22:52:33 +0000225#endif
Damien George330165a2016-04-22 22:44:56 +0000226
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000227#endif // __MICROPY_INCLUDED_PY_MPSTATE_H__