blob: e9e38606e7046875b6a13f2bb62ff78942b64f8e [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"
32#include "py/misc.h"
33#include "py/nlr.h"
34#include "py/obj.h"
Damien Georgee1e359f2015-02-07 17:24:10 +000035#include "py/objlist.h"
Damien Georgeb4b10fd2015-01-01 23:30:53 +000036#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.
43typedef 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 George94fe6e52015-11-27 13:07:48 +000055 byte *gc_pool_start;
56 byte *gc_pool_end;
Damien Georgeb4b10fd2015-01-01 23:30:53 +000057
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 Georgee1e359f2015-02-07 17:24:10 +000069
70 #if MICROPY_PY_GC_COLLECT_RETVAL
71 mp_uint_t gc_collected;
72 #endif
Damien Georgeb4b10fd2015-01-01 23:30:53 +000073} 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.
77typedef 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
Paul Sokolovsky1a1d11f2015-12-05 00:09:10 +0200103 // dictionary with loaded modules (may be exposed as sys.modules)
104 mp_obj_dict_t mp_loaded_modules_dict;
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000105
106 // pending exception object (MP_OBJ_NULL if not pending)
Damien George994ff732015-11-17 14:27:21 +0000107 volatile mp_obj_t mp_pending_exception;
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000108
Paul Sokolovsky8b85d142015-04-25 03:17:41 +0300109 // current exception being handled, for sys.exc_info()
110 #if MICROPY_PY_SYS_EXC_INFO
Damien George999cedb2015-11-27 17:01:44 +0000111 mp_obj_base_t *cur_exception;
Paul Sokolovsky8b85d142015-04-25 03:17:41 +0300112 #endif
113
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000114 // dictionary for the __main__ module
115 mp_obj_dict_t dict_main;
116
Damien Georgee1e359f2015-02-07 17:24:10 +0000117 // these two lists must be initialised per port, after the call to mp_init
118 mp_obj_list_t mp_sys_path_obj;
119 mp_obj_list_t mp_sys_argv_obj;
120
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000121 // dictionary for overridden builtins
122 #if MICROPY_CAN_OVERRIDE_BUILTINS
123 mp_obj_dict_t *mp_module_builtins_override_dict;
124 #endif
125
126 // include any root pointers defined by a port
127 MICROPY_PORT_ROOT_POINTERS
128
Paul Sokolovskye0d77402015-10-27 00:04:33 +0300129 // root pointers for extmod
130 #if MICROPY_PY_LWIP_SLIP
131 mp_obj_t lwip_slip_stream;
132 #endif
133
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000134 //
135 // END ROOT POINTER SECTION
136 ////////////////////////////////////////////////////////////
137
Damien Georgeade9a052015-06-13 21:53:22 +0100138 // pointer and sizes to store interned string data
139 // (qstr_last_chunk can be root pointer but is also stored in qstr pool)
140 byte *qstr_last_chunk;
141 mp_uint_t qstr_last_alloc;
142 mp_uint_t qstr_last_used;
143
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000144 // Stack top at the start of program
145 // Note: this entry is used to locate the end of the root pointer section.
146 char *stack_top;
147
148 #if MICROPY_STACK_CHECK
149 mp_uint_t stack_limit;
150 #endif
151
152 mp_uint_t mp_optimise_value;
153
154 // size of the emergency exception buf, if it's dynamically allocated
155 #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF && MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE == 0
156 mp_int_t mp_emergency_exception_buf_size;
157 #endif
158} mp_state_vm_t;
159
160// This structure combines the above 2 structures, and adds the local
161// and global dicts.
162// Note: if this structure changes then revisit all nlr asm code since they
163// have the offset of nlr_top hard-coded.
164typedef struct _mp_state_ctx_t {
165 // these must come first for root pointer scanning in GC to work
166 mp_obj_dict_t *dict_locals;
167 mp_obj_dict_t *dict_globals;
168 // this must come next for root pointer scanning in GC to work
169 mp_state_vm_t vm;
170 mp_state_mem_t mem;
171} mp_state_ctx_t;
172
173extern mp_state_ctx_t mp_state_ctx;
174
175#define MP_STATE_CTX(x) (mp_state_ctx.x)
176#define MP_STATE_VM(x) (mp_state_ctx.vm.x)
177#define MP_STATE_MEM(x) (mp_state_ctx.mem.x)
178
179#endif // __MICROPY_INCLUDED_PY_MPSTATE_H__