2015-01-01 18:30:53 -05:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
#ifndef __MICROPY_INCLUDED_PY_MPSTATE_H__
|
|
|
|
#define __MICROPY_INCLUDED_PY_MPSTATE_H__
|
|
|
|
|
|
|
|
#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"
|
|
|
|
|
|
|
|
// This file contains structures defining the state of the Micro Python
|
|
|
|
// 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.
|
|
|
|
|
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
|
|
|
|
bool opt_cache_map_lookup_in_bytecode;
|
|
|
|
bool py_builtins_str_unicode;
|
|
|
|
} 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;
|
|
|
|
|
2015-01-01 18:30:53 -05:00
|
|
|
// 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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
int gc_stack_overflow;
|
2015-12-16 20:09:11 -05:00
|
|
|
size_t gc_stack[MICROPY_ALLOC_GC_STACK_SIZE];
|
|
|
|
size_t *gc_sp;
|
2015-01-01 18:30:53 -05:00
|
|
|
uint16_t gc_lock_depth;
|
|
|
|
|
|
|
|
// This variable controls auto garbage collection. If set to 0 then the
|
|
|
|
// 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
|
|
|
|
|
2015-12-16 20:09:11 -05:00
|
|
|
size_t gc_last_free_atb_index;
|
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
|
|
|
|
|
|
|
#if MICROPY_PY_THREAD
|
|
|
|
// This is a global mutex used to make the GC thread-safe.
|
|
|
|
mp_thread_mutex_t gc_mutex;
|
|
|
|
#endif
|
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 {
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// START ROOT POINTER SECTION
|
|
|
|
// everything that needs GC scanning must go here
|
|
|
|
// this must start at the start of this structure
|
|
|
|
//
|
|
|
|
|
|
|
|
qstr_pool_t *last_pool;
|
|
|
|
|
|
|
|
// 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
|
|
|
|
// statically allocated buf
|
|
|
|
byte mp_emergency_exception_buf[MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE];
|
|
|
|
#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
|
|
|
|
|
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
|
|
|
|
|
|
|
// pending exception object (MP_OBJ_NULL if not pending)
|
2015-11-17 09:27:21 -05:00
|
|
|
volatile mp_obj_t mp_pending_exception;
|
2015-01-01 18:30:53 -05:00
|
|
|
|
2017-02-16 02:05:06 -05:00
|
|
|
#if MICROPY_ENABLE_SCHEDULER
|
|
|
|
volatile int16_t sched_state;
|
|
|
|
uint16_t sched_sp;
|
|
|
|
mp_sched_item_t sched_stack[MICROPY_SCHEDULER_DEPTH];
|
|
|
|
#endif
|
|
|
|
|
2015-04-24 20:17:41 -04:00
|
|
|
// current exception being handled, for sys.exc_info()
|
|
|
|
#if MICROPY_PY_SYS_EXC_INFO
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_base_t *cur_exception;
|
2015-04-24 20:17:41 -04:00
|
|
|
#endif
|
|
|
|
|
2015-01-01 18:30:53 -05:00
|
|
|
// dictionary for the __main__ module
|
|
|
|
mp_obj_dict_t dict_main;
|
|
|
|
|
2015-02-07 12:24:10 -05:00
|
|
|
// these two lists must be initialised per port, after the call to mp_init
|
|
|
|
mp_obj_list_t mp_sys_path_obj;
|
|
|
|
mp_obj_list_t mp_sys_argv_obj;
|
|
|
|
|
2015-01-01 18:30:53 -05:00
|
|
|
// dictionary for overridden builtins
|
|
|
|
#if MICROPY_CAN_OVERRIDE_BUILTINS
|
|
|
|
mp_obj_dict_t *mp_module_builtins_override_dict;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// include any root pointers defined by a port
|
|
|
|
MICROPY_PORT_ROOT_POINTERS
|
|
|
|
|
2015-10-26 17:04:33 -04:00
|
|
|
// root pointers for extmod
|
2016-01-01 07:19:26 -05:00
|
|
|
|
|
|
|
#if MICROPY_PY_OS_DUPTERM
|
|
|
|
mp_obj_t term_obj;
|
2016-07-04 06:32:30 -04:00
|
|
|
mp_obj_t dupterm_arr_obj;
|
2016-01-01 07:19:26 -05:00
|
|
|
#endif
|
|
|
|
|
2015-10-26 17:04:33 -04:00
|
|
|
#if MICROPY_PY_LWIP_SLIP
|
|
|
|
mp_obj_t lwip_slip_stream;
|
|
|
|
#endif
|
|
|
|
|
2017-01-26 23:10:09 -05:00
|
|
|
#if MICROPY_VFS
|
2017-01-27 06:40:15 -05:00
|
|
|
struct _mp_vfs_mount_t *vfs_cur;
|
|
|
|
struct _mp_vfs_mount_t *vfs_mount_table;
|
2017-01-26 23:10:09 -05: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)
|
|
|
|
byte *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
|
|
|
|
2016-05-26 05:06:46 -04:00
|
|
|
#if MICROPY_PY_THREAD
|
|
|
|
// This is a global mutex used to make qstr interning thread-safe.
|
|
|
|
mp_thread_mutex_t qstr_mutex;
|
|
|
|
#endif
|
|
|
|
|
2015-01-01 18:30:53 -05:00
|
|
|
mp_uint_t mp_optimise_value;
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
#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
|
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 {
|
2017-02-27 07:56:46 -05:00
|
|
|
mp_obj_dict_t *dict_locals;
|
|
|
|
mp_obj_dict_t *dict_globals;
|
|
|
|
|
2016-04-22 18:44:56 -04:00
|
|
|
// Note: nlr asm code has the offset of this hard-coded
|
|
|
|
nlr_buf_t *nlr_top; // ROOT POINTER
|
|
|
|
|
|
|
|
// Stack top at the start of program
|
|
|
|
char *stack_top;
|
|
|
|
|
|
|
|
#if MICROPY_STACK_CHECK
|
|
|
|
size_t stack_limit;
|
|
|
|
#endif
|
|
|
|
} 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
|
|
|
// Note: if this structure changes then revisit all nlr asm code since they
|
|
|
|
// have the offset of nlr_top hard-coded.
|
|
|
|
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;
|
|
|
|
|
2017-02-27 07:56:46 -05:00
|
|
|
#define MP_STATE_CTX(x) MP_STATE_THREAD(x)
|
2015-01-01 18:30:53 -05:00
|
|
|
#define MP_STATE_VM(x) (mp_state_ctx.vm.x)
|
|
|
|
#define MP_STATE_MEM(x) (mp_state_ctx.mem.x)
|
|
|
|
|
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)
|
|
|
|
#else
|
2016-04-22 18:44:56 -04:00
|
|
|
#define MP_STATE_THREAD(x) (mp_state_ctx.thread.x)
|
2016-04-22 18:52:33 -04:00
|
|
|
#endif
|
2016-04-22 18:44:56 -04:00
|
|
|
|
2015-01-01 18:30:53 -05:00
|
|
|
#endif // __MICROPY_INCLUDED_PY_MPSTATE_H__
|