Skip to content

Commit b4b10fd

Browse files
committed
py: Put all global state together in state structures.
This patch consolidates all global variables in py/ core into one place, in a global structure. Root pointers are all located together to make GC tracing easier and more efficient.
1 parent ad2307c commit b4b10fd

34 files changed

+465
-296
lines changed

bare-arm/main.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
#include "py/nlr.h"
66
#include "py/parsehelper.h"
77
#include "py/compile.h"
8-
#include "py/runtime0.h"
98
#include "py/runtime.h"
9+
#include "py/stackctrl.h"
1010
#include "py/repl.h"
1111
#include "py/pfenv.h"
1212

@@ -48,6 +48,7 @@ void do_str(const char *src) {
4848
}
4949

5050
int main(int argc, char **argv) {
51+
mp_stack_set_limit(10240);
5152
mp_init();
5253
do_str("print('hello world!', list(x+1 for x in range(10)), end='eol\n')");
5354
mp_deinit();

esp8266/main.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,15 @@
3232
#include "py/compile.h"
3333
#include "py/runtime0.h"
3434
#include "py/runtime.h"
35+
#include "py/stackctrl.h"
3536
#include "py/gc.h"
3637
#include "pyexec.h"
3738
#include "gccollect.h"
3839
#include MICROPY_HAL_H
3940

4041
void user_init(void) {
4142
soft_reset:
42-
//mp_stack_set_limit((char*)&_ram_end - (char*)&_heap_end - 1024);
43+
mp_stack_set_limit(10240);
4344
mp_hal_init();
4445
gc_init(&_heap_start, &_heap_end);
4546
gc_collect_init();

py/builtin.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ extern const mp_obj_module_t mp_module_sys;
9090
extern const mp_obj_module_t mp_module_gc;
9191

9292
extern const mp_obj_dict_t mp_module_builtins_globals;
93-
extern mp_obj_dict_t *mp_module_builtins_override_dict;
9493

9594
struct _dummy_t;
9695
extern struct _dummy_t mp_sys_stdin_obj;

py/emitbc.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <string.h>
3131
#include <assert.h>
3232

33+
#include "py/mpstate.h"
3334
#include "py/emit.h"
3435
#include "py/bc0.h"
3536

@@ -383,7 +384,7 @@ STATIC void emit_bc_adjust_stack_size(emit_t *emit, mp_int_t delta) {
383384
STATIC void emit_bc_set_source_line(emit_t *emit, mp_uint_t source_line) {
384385
//printf("source: line %d -> %d offset %d -> %d\n", emit->last_source_line, source_line, emit->last_source_line_offset, emit->bytecode_offset);
385386
#if MICROPY_ENABLE_SOURCE_LINE
386-
if (mp_optimise_value >= 3) {
387+
if (MP_STATE_VM(mp_optimise_value) >= 3) {
387388
// If we compile with -O3, don't store line numbers.
388389
return;
389390
}

py/gc.c

Lines changed: 74 additions & 87 deletions
Large diffs are not rendered by default.

py/gc.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@ void gc_lock(void);
3939
void gc_unlock(void);
4040
bool gc_is_locked(void);
4141

42-
// This variable controls auto garbage collection. If set to 0 then the
43-
// GC won't automatically run when gc_alloc can't find enough blocks. But
44-
// you can still allocate/free memory and also explicitly call gc_collect.
45-
extern uint16_t gc_auto_collect_enabled;
46-
4742
// A given port must implement gc_collect by using the other collect functions.
4843
void gc_collect(void);
4944
void gc_collect_start(void);

py/lexer.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,14 @@
2727
#include <stdio.h>
2828
#include <assert.h>
2929

30+
#include "py/mpstate.h"
3031
#include "py/lexer.h"
3132

3233
#define TAB_SIZE (8)
3334

3435
// TODO seems that CPython allows NULL byte in the input stream
3536
// don't know if that's intentional or not, but we don't allow it
3637

37-
mp_uint_t mp_optimise_value;
38-
3938
// TODO replace with a call to a standard function
4039
STATIC bool str_strn_equal(const char *str, const char *strn, mp_uint_t len) {
4140
mp_uint_t i = 0;
@@ -662,7 +661,7 @@ STATIC void mp_lexer_next_token_into(mp_lexer_t *lex, bool first_token) {
662661
if (str_strn_equal(tok_kw[i], lex->vstr.buf, lex->vstr.len)) {
663662
if (i == MP_ARRAY_SIZE(tok_kw) - 1) {
664663
// tok_kw[MP_ARRAY_SIZE(tok_kw) - 1] == "__debug__"
665-
lex->tok_kind = (mp_optimise_value == 0 ? MP_TOKEN_KW_TRUE : MP_TOKEN_KW_FALSE);
664+
lex->tok_kind = (MP_STATE_VM(mp_optimise_value) == 0 ? MP_TOKEN_KW_TRUE : MP_TOKEN_KW_FALSE);
666665
} else {
667666
lex->tok_kind = MP_TOKEN_KW_FALSE + i;
668667
}

py/lexer.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,4 @@ typedef enum {
192192
mp_import_stat_t mp_import_stat(const char *path);
193193
mp_lexer_t *mp_lexer_new_from_file(const char *filename);
194194

195-
extern mp_uint_t mp_optimise_value;
196-
197195
#endif // __MICROPY_INCLUDED_PY_LEXER_H__

py/malloc.c

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
#include "py/mpconfig.h"
3232
#include "py/misc.h"
33+
#include "py/mpstate.h"
3334

3435
#if 0 // print debugging info
3536
#define DEBUG_printf DEBUG_printf
@@ -38,11 +39,7 @@
3839
#endif
3940

4041
#if MICROPY_MEM_STATS
41-
STATIC size_t total_bytes_allocated = 0;
42-
STATIC size_t current_bytes_allocated = 0;
43-
STATIC size_t peak_bytes_allocated = 0;
44-
45-
#define UPDATE_PEAK() { if (current_bytes_allocated > peak_bytes_allocated) peak_bytes_allocated = current_bytes_allocated; }
42+
#define UPDATE_PEAK() { if (MP_STATE_MEM(current_bytes_allocated) > MP_STATE_MEM(peak_bytes_allocated)) MP_STATE_MEM(peak_bytes_allocated) = MP_STATE_MEM(current_bytes_allocated); }
4643
#endif
4744

4845
#if MICROPY_ENABLE_GC
@@ -68,8 +65,8 @@ void *m_malloc(size_t num_bytes) {
6865
return m_malloc_fail(num_bytes);
6966
}
7067
#if MICROPY_MEM_STATS
71-
total_bytes_allocated += num_bytes;
72-
current_bytes_allocated += num_bytes;
68+
MP_STATE_MEM(total_bytes_allocated) += num_bytes;
69+
MP_STATE_MEM(current_bytes_allocated) += num_bytes;
7370
UPDATE_PEAK();
7471
#endif
7572
DEBUG_printf("malloc %d : %p\n", num_bytes, ptr);
@@ -79,8 +76,8 @@ void *m_malloc(size_t num_bytes) {
7976
void *m_malloc_maybe(size_t num_bytes) {
8077
void *ptr = malloc(num_bytes);
8178
#if MICROPY_MEM_STATS
82-
total_bytes_allocated += num_bytes;
83-
current_bytes_allocated += num_bytes;
79+
MP_STATE_MEM(total_bytes_allocated) += num_bytes;
80+
MP_STATE_MEM(current_bytes_allocated) += num_bytes;
8481
UPDATE_PEAK();
8582
#endif
8683
DEBUG_printf("malloc %d : %p\n", num_bytes, ptr);
@@ -94,8 +91,8 @@ void *m_malloc_with_finaliser(size_t num_bytes) {
9491
return m_malloc_fail(num_bytes);
9592
}
9693
#if MICROPY_MEM_STATS
97-
total_bytes_allocated += num_bytes;
98-
current_bytes_allocated += num_bytes;
94+
MP_STATE_MEM(total_bytes_allocated) += num_bytes;
95+
MP_STATE_MEM(current_bytes_allocated) += num_bytes;
9996
UPDATE_PEAK();
10097
#endif
10198
DEBUG_printf("malloc %d : %p\n", num_bytes, ptr);
@@ -124,8 +121,8 @@ void *m_realloc(void *ptr, size_t old_num_bytes, size_t new_num_bytes) {
124121
// allocated total. If we process only positive increments,
125122
// we'll count 3K.
126123
size_t diff = new_num_bytes - old_num_bytes;
127-
total_bytes_allocated += diff;
128-
current_bytes_allocated += diff;
124+
MP_STATE_MEM(total_bytes_allocated) += diff;
125+
MP_STATE_MEM(current_bytes_allocated) += diff;
129126
UPDATE_PEAK();
130127
#endif
131128
DEBUG_printf("realloc %p, %d, %d : %p\n", ptr, old_num_bytes, new_num_bytes, new_ptr);
@@ -143,8 +140,8 @@ void *m_realloc_maybe(void *ptr, size_t old_num_bytes, size_t new_num_bytes) {
143140
// Also, don't count failed reallocs.
144141
if (!(new_ptr == NULL && new_num_bytes != 0)) {
145142
size_t diff = new_num_bytes - old_num_bytes;
146-
total_bytes_allocated += diff;
147-
current_bytes_allocated += diff;
143+
MP_STATE_MEM(total_bytes_allocated) += diff;
144+
MP_STATE_MEM(current_bytes_allocated) += diff;
148145
UPDATE_PEAK();
149146
}
150147
#endif
@@ -155,21 +152,21 @@ void *m_realloc_maybe(void *ptr, size_t old_num_bytes, size_t new_num_bytes) {
155152
void m_free(void *ptr, size_t num_bytes) {
156153
free(ptr);
157154
#if MICROPY_MEM_STATS
158-
current_bytes_allocated -= num_bytes;
155+
MP_STATE_MEM(current_bytes_allocated) -= num_bytes;
159156
#endif
160157
DEBUG_printf("free %p, %d\n", ptr, num_bytes);
161158
}
162159

163160
#if MICROPY_MEM_STATS
164161
size_t m_get_total_bytes_allocated(void) {
165-
return total_bytes_allocated;
162+
return MP_STATE_MEM(total_bytes_allocated);
166163
}
167164

168165
size_t m_get_current_bytes_allocated(void) {
169-
return current_bytes_allocated;
166+
return MP_STATE_MEM(current_bytes_allocated);
170167
}
171168

172169
size_t m_get_peak_bytes_allocated(void) {
173-
return peak_bytes_allocated;
170+
return MP_STATE_MEM(peak_bytes_allocated);
174171
}
175172
#endif

py/modgc.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#include "py/mpstate.h"
2728
#include "py/obj.h"
2829
#include "py/gc.h"
2930

@@ -48,21 +49,21 @@ MP_DEFINE_CONST_FUN_OBJ_0(gc_collect_obj, py_gc_collect);
4849
/// \function disable()
4950
/// Disable the garbage collector.
5051
STATIC mp_obj_t gc_disable(void) {
51-
gc_auto_collect_enabled = 0;
52+
MP_STATE_MEM(gc_auto_collect_enabled) = 0;
5253
return mp_const_none;
5354
}
5455
MP_DEFINE_CONST_FUN_OBJ_0(gc_disable_obj, gc_disable);
5556

5657
/// \function enable()
5758
/// Enable the garbage collector.
5859
STATIC mp_obj_t gc_enable(void) {
59-
gc_auto_collect_enabled = 1;
60+
MP_STATE_MEM(gc_auto_collect_enabled) = 1;
6061
return mp_const_none;
6162
}
6263
MP_DEFINE_CONST_FUN_OBJ_0(gc_enable_obj, gc_enable);
6364

6465
STATIC mp_obj_t gc_isenabled(void) {
65-
return MP_BOOL(gc_auto_collect_enabled);
66+
return MP_BOOL(MP_STATE_MEM(gc_auto_collect_enabled));
6667
}
6768
MP_DEFINE_CONST_FUN_OBJ_0(gc_isenabled_obj, gc_isenabled);
6869

0 commit comments

Comments
 (0)