2015-01-01 18:30:53 -05:00
|
|
|
/*
|
2017-06-30 03:22:17 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2015-01-01 18:30:53 -05:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2020-06-03 18:40:05 -04:00
|
|
|
* SPDX-FileCopyrightText: Copyright (c) 2014 Damien P. George
|
2015-01-01 18:30:53 -05:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
all: Unify header guard usage.
The code conventions suggest using header guards, but do not define how
those should look like and instead point to existing files. However, not
all existing files follow the same scheme, sometimes omitting header guards
altogether, sometimes using non-standard names, making it easy to
accidentally pick a "wrong" example.
This commit ensures that all header files of the MicroPython project (that
were not simply copied from somewhere else) follow the same pattern, that
was already present in the majority of files, especially in the py folder.
The rules are as follows.
Naming convention:
* start with the words MICROPY_INCLUDED
* contain the full path to the file
* replace special characters with _
In addition, there are no empty lines before #ifndef, between #ifndef and
one empty line before #endif. #endif is followed by a comment containing
the name of the guard macro.
py/grammar.h cannot use header guards by design, since it has to be
included multiple times in a single C file. Several other files also do not
need header guards as they are only used internally and guaranteed to be
included only once:
* MICROPY_MPHALPORT_H
* mpconfigboard.h
* mpconfigport.h
* mpthreadport.h
* pin_defs_*.h
* qstrdefs*.h
2017-06-29 17:14:58 -04:00
|
|
|
#ifndef MICROPY_INCLUDED_PY_MPSTATE_H
|
|
|
|
#define MICROPY_INCLUDED_PY_MPSTATE_H
|
2015-01-01 18:30:53 -05:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "py/mpconfig.h"
|
2016-04-25 11:28:57 -04:00
|
|
|
#include "py/mpthread.h"
|
2015-01-01 18:30:53 -05:00
|
|
|
#include "py/misc.h"
|
|
|
|
#include "py/nlr.h"
|
|
|
|
#include "py/obj.h"
|
2015-02-07 12:24:10 -05:00
|
|
|
#include "py/objlist.h"
|
2015-01-01 18:30:53 -05:00
|
|
|
#include "py/objexcept.h"
|
|
|
|
|
2023-10-25 18:36:23 -04:00
|
|
|
#if CIRCUITPY_WARNINGS
|
|
|
|
#include "shared-bindings/warnings/__init__.h"
|
|
|
|
#endif
|
|
|
|
|
2023-08-10 20:06:32 -04:00
|
|
|
// #if CIRCUITPY
|
|
|
|
// #error CIRCUITPY is TRUE in mpstate.h.
|
|
|
|
// #else
|
|
|
|
// #error CIRCUITPY is **FALSE** in mpstate.h
|
|
|
|
// #endif
|
|
|
|
|
2017-06-30 03:22:17 -04:00
|
|
|
// This file contains structures defining the state of the MicroPython
|
2015-01-01 18:30:53 -05:00
|
|
|
// memory system, runtime and virtual machine. The state is a global
|
|
|
|
// variable, but in the future it is hoped that the state can become local.
|
|
|
|
|
2023-06-05 08:38:36 -04:00
|
|
|
#if MICROPY_PY_SYS_ATTR_DELEGATION
|
|
|
|
// Must be kept in sync with sys_mutable_keys in modsys.c.
|
2021-07-26 10:39:04 -04:00
|
|
|
enum {
|
2023-06-05 02:52:29 -04:00
|
|
|
#if MICROPY_PY_SYS_PATH
|
|
|
|
MP_SYS_MUTABLE_PATH,
|
|
|
|
#endif
|
2021-07-26 10:43:35 -04:00
|
|
|
#if MICROPY_PY_SYS_PS1_PS2
|
|
|
|
MP_SYS_MUTABLE_PS1,
|
|
|
|
MP_SYS_MUTABLE_PS2,
|
|
|
|
#endif
|
2021-07-26 10:41:27 -04:00
|
|
|
#if MICROPY_PY_SYS_TRACEBACKLIMIT
|
|
|
|
MP_SYS_MUTABLE_TRACEBACKLIMIT,
|
|
|
|
#endif
|
2021-07-26 10:39:04 -04:00
|
|
|
MP_SYS_MUTABLE_NUM,
|
|
|
|
};
|
2023-06-05 08:38:36 -04:00
|
|
|
#endif // MICROPY_PY_SYS_ATTR_DELEGATION
|
2021-07-26 10:39:04 -04:00
|
|
|
|
2016-02-11 17:30:53 -05:00
|
|
|
// This structure contains dynamic configuration for the compiler.
|
|
|
|
#if MICROPY_DYNAMIC_COMPILER
|
|
|
|
typedef struct mp_dynamic_compiler_t {
|
|
|
|
uint8_t small_int_bits; // must be <= host small_int_bits
|
2021-04-22 20:55:39 -04:00
|
|
|
uint8_t native_arch;
|
2021-04-23 15:26:42 -04:00
|
|
|
uint8_t nlr_buf_num_regs;
|
2016-02-11 17:30:53 -05:00
|
|
|
} mp_dynamic_compiler_t;
|
|
|
|
extern mp_dynamic_compiler_t mp_dynamic_compiler;
|
|
|
|
#endif
|
|
|
|
|
2017-02-16 02:05:06 -05:00
|
|
|
// These are the values for sched_state
|
|
|
|
#define MP_SCHED_IDLE (1)
|
|
|
|
#define MP_SCHED_LOCKED (-1)
|
|
|
|
#define MP_SCHED_PENDING (0) // 0 so it's a quick check in the VM
|
|
|
|
|
|
|
|
typedef struct _mp_sched_item_t {
|
|
|
|
mp_obj_t func;
|
|
|
|
mp_obj_t arg;
|
|
|
|
} mp_sched_item_t;
|
|
|
|
|
2018-01-23 20:09:58 -05:00
|
|
|
// This structure holds information about a single contiguous area of
|
|
|
|
// memory reserved for the memory manager.
|
|
|
|
typedef struct _mp_state_mem_area_t {
|
|
|
|
#if MICROPY_GC_SPLIT_HEAP
|
|
|
|
struct _mp_state_mem_area_t *next;
|
2015-01-01 18:30:53 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
byte *gc_alloc_table_start;
|
2015-12-16 20:09:11 -05:00
|
|
|
size_t gc_alloc_table_byte_len;
|
2015-01-01 18:30:53 -05:00
|
|
|
#if MICROPY_ENABLE_FINALISER
|
|
|
|
byte *gc_finaliser_table_start;
|
|
|
|
#endif
|
2015-11-27 08:07:48 -05:00
|
|
|
byte *gc_pool_start;
|
|
|
|
byte *gc_pool_end;
|
2015-01-01 18:30:53 -05:00
|
|
|
|
2018-01-23 20:09:58 -05:00
|
|
|
size_t gc_last_free_atb_index;
|
py/gc: Speed up incremental GC cycles by tracking the last used block.
In applications that use little memory and run GC regularly, the cost of
the sweep phase quickly becomes prohibitives as the amount of RAM
increases.
On an ESP32-S3 with 2 MB of external SPIRAM, for example, a trivial GC
cycle takes a minimum of 40ms, virtually all of it in the sweep phase.
Similarly, on the UNIX port with 1 GB of heap, a trivial GC takes 47 ms,
again virtually all of it in the sweep phase.
This commit speeds up the sweep phase in the case most of the heap is empty
by keeping track of the ID of the highest block we allocated in an area
since the last GC.
The performance benchmark run on PYBV10 shows between +0 and +2%
improvement across the existing performance tests. These tests don't
really stress the GC, so they were also run with gc.threshold(30000) and
gc.threshold(10000). For the 30000 case, performance improved by up to
+10% with this commit. For the 10000 case, performance improved by at
least +10% on 6 tests, and up to +25%.
Signed-off-by: Damien George <damien@micropython.org>
2022-12-15 17:09:19 -05:00
|
|
|
size_t gc_last_used_block; // The block ID of the highest block allocated in the area
|
2018-01-23 20:09:58 -05:00
|
|
|
} mp_state_mem_area_t;
|
|
|
|
|
|
|
|
// This structure hold information about the memory allocation system.
|
|
|
|
typedef struct _mp_state_mem_t {
|
|
|
|
#if MICROPY_MEM_STATS
|
|
|
|
size_t total_bytes_allocated;
|
|
|
|
size_t current_bytes_allocated;
|
|
|
|
size_t peak_bytes_allocated;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
mp_state_mem_area_t area;
|
|
|
|
|
2015-01-01 18:30:53 -05:00
|
|
|
int gc_stack_overflow;
|
2022-04-13 00:26:16 -04:00
|
|
|
MICROPY_GC_STACK_ENTRY_TYPE gc_block_stack[MICROPY_ALLOC_GC_STACK_SIZE];
|
|
|
|
#if MICROPY_GC_SPLIT_HEAP
|
|
|
|
// Array that tracks the area for each block on gc_block_stack.
|
|
|
|
mp_state_mem_area_t *gc_area_stack[MICROPY_ALLOC_GC_STACK_SIZE];
|
|
|
|
#endif
|
2015-01-01 18:30:53 -05:00
|
|
|
|
2023-08-08 20:41:17 -04:00
|
|
|
// This variable controls auto garbage collection. If set to 0 then the
|
2015-01-01 18:30:53 -05:00
|
|
|
// GC won't automatically run when gc_alloc can't find enough blocks. But
|
|
|
|
// you can still allocate/free memory and also explicitly call gc_collect.
|
|
|
|
uint16_t gc_auto_collect_enabled;
|
|
|
|
|
py/gc: Implement GC running by allocation threshold.
Currently, MicroPython runs GC when it could not allocate a block of memory,
which happens when heap is exhausted. However, that policy can't work well
with "inifinity" heaps, e.g. backed by a virtual memory - there will be a
lot of swap thrashing long before VM will be exhausted. Instead, in such
cases "allocation threshold" policy is used: a GC is run after some number of
allocations have been made. Details vary, for example, number or total amount
of allocations can be used, threshold may be self-adjusting based on GC
outcome, etc.
This change implements a simple variant of such policy for MicroPython. Amount
of allocated memory so far is used for threshold, to make it useful to typical
finite-size, and small, heaps as used with MicroPython ports. And such GC policy
is indeed useful for such types of heaps too, as it allows to better control
fragmentation. For example, if a threshold is set to half size of heap, then
for an application which usually makes big number of small allocations, that
will (try to) keep half of heap memory in a nice defragmented state for an
occasional large allocation.
For an application which doesn't exhibit such behavior, there won't be any
visible effects, except for GC running more frequently, which however may
affect performance. To address this, the GC threshold is configurable, and
by default is off so far. It's configured with gc.threshold(amount_in_bytes)
call (can be queries without an argument).
2016-07-20 17:37:30 -04:00
|
|
|
#if MICROPY_GC_ALLOC_THRESHOLD
|
|
|
|
size_t gc_alloc_amount;
|
|
|
|
size_t gc_alloc_threshold;
|
|
|
|
#endif
|
|
|
|
|
2018-01-23 20:09:58 -05:00
|
|
|
#if MICROPY_GC_SPLIT_HEAP
|
|
|
|
mp_state_mem_area_t *gc_last_free_area;
|
|
|
|
#endif
|
2015-02-07 12:24:10 -05:00
|
|
|
|
|
|
|
#if MICROPY_PY_GC_COLLECT_RETVAL
|
2015-12-16 20:09:11 -05:00
|
|
|
size_t gc_collected;
|
2015-02-07 12:24:10 -05:00
|
|
|
#endif
|
2016-04-25 11:28:57 -04:00
|
|
|
|
2020-01-22 12:19:37 -05:00
|
|
|
#if MICROPY_PY_THREAD && !MICROPY_PY_THREAD_GIL
|
2016-04-25 11:28:57 -04:00
|
|
|
// This is a global mutex used to make the GC thread-safe.
|
|
|
|
mp_thread_mutex_t gc_mutex;
|
|
|
|
#endif
|
2019-04-08 19:58:50 -04:00
|
|
|
|
2023-10-19 16:42:36 -04:00
|
|
|
// CIRCUITPY-CHANGE
|
2021-03-15 09:57:36 -04:00
|
|
|
void **permanent_pointers;
|
2015-01-01 18:30:53 -05:00
|
|
|
} mp_state_mem_t;
|
|
|
|
|
|
|
|
// This structure hold runtime and VM information. It includes a section
|
|
|
|
// which contains root pointers that must be scanned by the GC.
|
|
|
|
typedef struct _mp_state_vm_t {
|
2018-05-12 08:09:34 -04:00
|
|
|
//
|
|
|
|
// CONTINUE ROOT POINTER SECTION
|
|
|
|
// This must start at the start of this structure and follows
|
|
|
|
// the state in the mp_state_thread_t structure, continuing
|
|
|
|
// the root pointer section from there.
|
2015-01-01 18:30:53 -05:00
|
|
|
//
|
|
|
|
|
|
|
|
qstr_pool_t *last_pool;
|
|
|
|
|
2022-09-29 10:08:28 -04:00
|
|
|
#if MICROPY_TRACKED_ALLOC
|
|
|
|
struct _m_tracked_node_t *m_tracked_head;
|
|
|
|
#endif
|
|
|
|
|
2021-08-02 02:30:48 -04:00
|
|
|
// non-heap memory for creating a traceback if we can't allocate RAM
|
|
|
|
mp_obj_traceback_t mp_emergency_traceback_obj;
|
|
|
|
|
2015-01-01 18:30:53 -05:00
|
|
|
// non-heap memory for creating an exception if we can't allocate RAM
|
|
|
|
mp_obj_exception_t mp_emergency_exception_obj;
|
|
|
|
|
|
|
|
// memory for exception arguments if we can't allocate RAM
|
|
|
|
#if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF
|
|
|
|
#if MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE > 0
|
2017-04-10 02:02:56 -04:00
|
|
|
// statically allocated buf (needs to be aligned to mp_obj_t)
|
|
|
|
mp_obj_t mp_emergency_exception_buf[MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE / sizeof(mp_obj_t)];
|
2015-01-01 18:30:53 -05:00
|
|
|
#else
|
|
|
|
// dynamically allocated buf
|
|
|
|
byte *mp_emergency_exception_buf;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2016-12-14 21:00:19 -05:00
|
|
|
#if MICROPY_KBD_EXCEPTION
|
|
|
|
// exception object of type KeyboardInterrupt
|
|
|
|
mp_obj_exception_t mp_kbd_exception;
|
|
|
|
#endif
|
|
|
|
|
2018-05-14 16:57:50 -04:00
|
|
|
// exception object of type ReloadException
|
|
|
|
mp_obj_exception_t mp_reload_exception;
|
|
|
|
|
2015-12-04 17:09:10 -05:00
|
|
|
// dictionary with loaded modules (may be exposed as sys.modules)
|
|
|
|
mp_obj_dict_t mp_loaded_modules_dict;
|
2015-01-01 18:30:53 -05:00
|
|
|
|
|
|
|
// dictionary for the __main__ module
|
|
|
|
mp_obj_dict_t dict_main;
|
|
|
|
|
|
|
|
// dictionary for overridden builtins
|
|
|
|
#if MICROPY_CAN_OVERRIDE_BUILTINS
|
|
|
|
mp_obj_dict_t *mp_module_builtins_override_dict;
|
|
|
|
#endif
|
|
|
|
|
2022-07-01 13:29:08 -04:00
|
|
|
// Include any root pointers registered with MP_REGISTER_ROOT_POINTER().
|
|
|
|
#ifndef NO_QSTR
|
|
|
|
// Only include root pointer definitions when not doing qstr extraction, because
|
|
|
|
// the qstr extraction stage also generates the root pointers header file.
|
|
|
|
#include "genhdr/root_pointers.h"
|
2019-07-23 10:49:06 -04:00
|
|
|
#endif
|
|
|
|
|
2015-01-01 18:30:53 -05:00
|
|
|
//
|
|
|
|
// END ROOT POINTER SECTION
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
|
2015-06-13 16:53:22 -04:00
|
|
|
// pointer and sizes to store interned string data
|
|
|
|
// (qstr_last_chunk can be root pointer but is also stored in qstr pool)
|
2021-04-06 12:33:03 -04:00
|
|
|
char *qstr_last_chunk;
|
2015-12-17 07:41:40 -05:00
|
|
|
size_t qstr_last_alloc;
|
|
|
|
size_t qstr_last_used;
|
2015-06-13 16:53:22 -04:00
|
|
|
|
2020-01-22 12:19:37 -05:00
|
|
|
#if MICROPY_PY_THREAD && !MICROPY_PY_THREAD_GIL
|
2016-05-26 05:06:46 -04:00
|
|
|
// This is a global mutex used to make qstr interning thread-safe.
|
|
|
|
mp_thread_mutex_t qstr_mutex;
|
|
|
|
#endif
|
|
|
|
|
2018-04-04 00:24:03 -04:00
|
|
|
#if MICROPY_ENABLE_COMPILER
|
2015-01-01 18:30:53 -05:00
|
|
|
mp_uint_t mp_optimise_value;
|
2021-04-23 15:26:42 -04:00
|
|
|
#if MICROPY_EMIT_NATIVE
|
|
|
|
uint8_t default_emit_opt; // one of MP_EMIT_OPT_xxx
|
|
|
|
#endif
|
2018-04-04 00:24:03 -04:00
|
|
|
#endif
|
2015-01-01 18:30:53 -05:00
|
|
|
|
|
|
|
// size of the emergency exception buf, if it's dynamically allocated
|
|
|
|
#if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF && MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE == 0
|
|
|
|
mp_int_t mp_emergency_exception_buf_size;
|
|
|
|
#endif
|
2016-05-26 06:42:53 -04:00
|
|
|
|
2018-05-12 08:09:34 -04:00
|
|
|
#if MICROPY_ENABLE_SCHEDULER
|
|
|
|
volatile int16_t sched_state;
|
2022-03-23 02:13:03 -04:00
|
|
|
|
|
|
|
#if MICROPY_SCHEDULER_STATIC_NODES
|
|
|
|
// These will usually point to statically allocated memory. They are not
|
|
|
|
// traced by the GC. They are assumed to be zero'd out before mp_init() is
|
|
|
|
// called (usually because this struct lives in the BSS).
|
|
|
|
struct _mp_sched_node_t *sched_head;
|
|
|
|
struct _mp_sched_node_t *sched_tail;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// These index sched_queue.
|
2021-04-22 20:55:39 -04:00
|
|
|
uint8_t sched_len;
|
|
|
|
uint8_t sched_idx;
|
2018-05-12 08:09:34 -04:00
|
|
|
#endif
|
|
|
|
|
2022-12-16 01:31:21 -05:00
|
|
|
#if MICROPY_ENABLE_VM_ABORT
|
|
|
|
bool vm_abort;
|
|
|
|
nlr_buf_t *nlr_abort;
|
|
|
|
#endif
|
|
|
|
|
2016-05-26 06:42:53 -04:00
|
|
|
#if MICROPY_PY_THREAD_GIL
|
|
|
|
// This is a global mutex used to make the VM/runtime thread-safe.
|
|
|
|
mp_thread_mutex_t gil_mutex;
|
|
|
|
#endif
|
2021-08-18 00:52:48 -04:00
|
|
|
|
|
|
|
#if MICROPY_OPT_MAP_LOOKUP_CACHE
|
|
|
|
// See mp_map_lookup.
|
|
|
|
uint8_t map_lookup_cache[MICROPY_OPT_MAP_LOOKUP_CACHE_SIZE];
|
|
|
|
#endif
|
2015-01-01 18:30:53 -05:00
|
|
|
} mp_state_vm_t;
|
|
|
|
|
2016-04-22 18:44:56 -04:00
|
|
|
// This structure holds state that is specific to a given thread.
|
|
|
|
// Everything in this structure is scanned for root pointers.
|
|
|
|
typedef struct _mp_state_thread_t {
|
|
|
|
// Stack top at the start of program
|
|
|
|
char *stack_top;
|
|
|
|
|
2023-10-19 16:42:36 -04:00
|
|
|
// CIRCUITPY-CHANGE
|
2017-07-23 15:32:05 -04:00
|
|
|
#if MICROPY_MAX_STACK_USAGE
|
2021-03-15 09:57:36 -04:00
|
|
|
char *stack_bottom;
|
2017-07-23 15:32:05 -04:00
|
|
|
#endif
|
|
|
|
|
2016-04-22 18:44:56 -04:00
|
|
|
#if MICROPY_STACK_CHECK
|
|
|
|
size_t stack_limit;
|
|
|
|
#endif
|
py: Introduce a Python stack for scoped allocation.
This patch introduces the MICROPY_ENABLE_PYSTACK option (disabled by
default) which enables a "Python stack" that allows to allocate and free
memory in a scoped, or Last-In-First-Out (LIFO) way, similar to alloca().
A new memory allocation API is introduced along with this Py-stack. It
includes both "local" and "nonlocal" LIFO allocation. Local allocation is
intended to be equivalent to using alloca(), whereby the same function must
free the memory. Nonlocal allocation is where another function may free
the memory, so long as it's still LIFO.
Follow-up patches will convert all uses of alloca() and VLA to the new
scoped allocation API. The old behaviour (using alloca()) will still be
available, but when MICROPY_ENABLE_PYSTACK is enabled then alloca() is no
longer required or used.
The benefits of enabling this option are (or will be once subsequent
patches are made to convert alloca()/VLA):
- Toolchains without alloca() can use this feature to obtain correct and
efficient scoped memory allocation (compared to using the heap instead
of alloca(), which is slower).
- Even if alloca() is available, enabling the Py-stack gives slightly more
efficient use of stack space when calling nested Python functions, due to
the way that compilers implement alloca().
- Enabling the Py-stack with the stackless mode allows for even more
efficient stack usage, as well as retaining high performance (because the
heap is no longer used to build and destroy stackless code states).
- With Py-stack and stackless enabled, Python-calling-Python is no longer
recursive in the C mp_execute_bytecode function.
The micropython.pystack_use() function is included to measure usage of the
Python stack.
2017-11-26 07:28:40 -05:00
|
|
|
|
|
|
|
#if MICROPY_ENABLE_PYSTACK
|
|
|
|
uint8_t *pystack_start;
|
|
|
|
uint8_t *pystack_end;
|
|
|
|
uint8_t *pystack_cur;
|
|
|
|
#endif
|
2018-05-12 08:09:34 -04:00
|
|
|
|
2021-05-04 09:56:43 -04:00
|
|
|
// Locking of the GC is done per thread.
|
|
|
|
uint16_t gc_lock_depth;
|
|
|
|
|
2018-05-12 08:09:34 -04:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// START ROOT POINTER SECTION
|
|
|
|
// Everything that needs GC scanning must start here, and
|
|
|
|
// is followed by state in the mp_state_vm_t structure.
|
|
|
|
//
|
|
|
|
|
|
|
|
mp_obj_dict_t *dict_locals;
|
|
|
|
mp_obj_dict_t *dict_globals;
|
|
|
|
|
|
|
|
nlr_buf_t *nlr_top;
|
2023-05-08 21:03:04 -04:00
|
|
|
nlr_jump_callback_node_t *nlr_jump_callback_top;
|
2021-04-23 15:26:42 -04:00
|
|
|
|
2021-05-10 22:53:22 -04:00
|
|
|
// pending exception object (MP_OBJ_NULL if not pending)
|
|
|
|
volatile mp_obj_t mp_pending_exception;
|
|
|
|
|
2021-06-29 03:34:34 -04:00
|
|
|
// If MP_OBJ_STOP_ITERATION is propagated then this holds its argument.
|
|
|
|
mp_obj_t stop_iteration_arg;
|
|
|
|
|
2021-04-23 15:26:42 -04:00
|
|
|
#if MICROPY_PY_SYS_SETTRACE
|
|
|
|
mp_obj_t prof_trace_callback;
|
|
|
|
bool prof_callback_is_executing;
|
|
|
|
struct _mp_code_state_t *current_code_state;
|
|
|
|
#endif
|
2023-10-25 18:36:23 -04:00
|
|
|
|
|
|
|
#if CIRCUITPY_WARNINGS
|
|
|
|
warnings_action_t warnings_action;
|
|
|
|
#endif
|
2016-04-22 18:44:56 -04:00
|
|
|
} mp_state_thread_t;
|
|
|
|
|
2017-02-27 07:56:46 -05:00
|
|
|
// This structure combines the above 3 structures.
|
|
|
|
// The order of the entries are important for root pointer scanning in the GC to work.
|
2015-01-01 18:30:53 -05:00
|
|
|
typedef struct _mp_state_ctx_t {
|
2016-04-22 18:44:56 -04:00
|
|
|
mp_state_thread_t thread;
|
2015-01-01 18:30:53 -05:00
|
|
|
mp_state_vm_t vm;
|
|
|
|
mp_state_mem_t mem;
|
|
|
|
} mp_state_ctx_t;
|
|
|
|
|
|
|
|
extern mp_state_ctx_t mp_state_ctx;
|
|
|
|
|
|
|
|
#define MP_STATE_VM(x) (mp_state_ctx.vm.x)
|
|
|
|
#define MP_STATE_MEM(x) (mp_state_ctx.mem.x)
|
2021-05-10 23:18:17 -04:00
|
|
|
#define MP_STATE_MAIN_THREAD(x) (mp_state_ctx.thread.x)
|
2015-01-01 18:30:53 -05:00
|
|
|
|
2016-04-22 18:52:33 -04:00
|
|
|
#if MICROPY_PY_THREAD
|
|
|
|
extern mp_state_thread_t *mp_thread_get_state(void);
|
|
|
|
#define MP_STATE_THREAD(x) (mp_thread_get_state()->x)
|
2022-12-16 01:30:26 -05:00
|
|
|
#define mp_thread_is_main_thread() (mp_thread_get_state() == &mp_state_ctx.thread)
|
2016-04-22 18:52:33 -04:00
|
|
|
#else
|
2021-05-10 23:18:17 -04:00
|
|
|
#define MP_STATE_THREAD(x) MP_STATE_MAIN_THREAD(x)
|
2022-12-16 01:30:26 -05:00
|
|
|
#define mp_thread_is_main_thread() (true)
|
2016-04-22 18:52:33 -04:00
|
|
|
#endif
|
2016-04-22 18:44:56 -04:00
|
|
|
|
all: Unify header guard usage.
The code conventions suggest using header guards, but do not define how
those should look like and instead point to existing files. However, not
all existing files follow the same scheme, sometimes omitting header guards
altogether, sometimes using non-standard names, making it easy to
accidentally pick a "wrong" example.
This commit ensures that all header files of the MicroPython project (that
were not simply copied from somewhere else) follow the same pattern, that
was already present in the majority of files, especially in the py folder.
The rules are as follows.
Naming convention:
* start with the words MICROPY_INCLUDED
* contain the full path to the file
* replace special characters with _
In addition, there are no empty lines before #ifndef, between #ifndef and
one empty line before #endif. #endif is followed by a comment containing
the name of the guard macro.
py/grammar.h cannot use header guards by design, since it has to be
included multiple times in a single C file. Several other files also do not
need header guards as they are only used internally and guaranteed to be
included only once:
* MICROPY_MPHALPORT_H
* mpconfigboard.h
* mpconfigport.h
* mpthreadport.h
* pin_defs_*.h
* qstrdefs*.h
2017-06-29 17:14:58 -04:00
|
|
|
#endif // MICROPY_INCLUDED_PY_MPSTATE_H
|