8000 Apply suggestions from code review · rschanafelt/circuitpython@35ffb64 · GitHub
Skip to content

Commit 35ffb64

Browse files
tannewtdhalbert
andcommitted
Apply suggestions from code review
Co-authored-by: Dan Halbert <halbert@halwitz.org>
1 parent c352e73 commit 35ffb64

File tree

22 files changed

+67
-0
lines changed

22 files changed

+67
-0
lines changed

ports/unix/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ typedef long mp_off_t;
112112

113113
// Always enable GC.
114114
#define MICROPY_ENABLE_GC (1)
115+
// CIRCUITPY-CHANGE
115116
#define MICROPY_ENABLE_SELECTIVE_COLLECT (1)
116117

117118
#if !(defined(MICROPY_GCREGS_SETJMP) || defined(__x86_64__) || defined(__i386__) || defined(__thumb2__) || defined(__thumb__) || defined(__arm__))

py/bc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,13 +302,15 @@ static inline void mp_module_context_alloc_tables(mp_module_context_t *context,
302302
#if MICROPY_EMIT_BYTECODE_USES_QSTR_TABLE
303303
size_t nq = (n_qstr * sizeof(qstr_short_t) + sizeof(mp_uint_t) - 1) / sizeof(mp_uint_t);
304304
size_t no = n_obj;
305+
// CIRCUITPY-CHANGE
305306
mp_uint_t *mem = m_malloc_items(nq + no);
306307
context->constants.qstr_table = (qstr_short_t *)mem;
307308
context->constants.obj_table = (mp_obj_t *)(mem + nq);
308309
#else
309310
if (n_obj == 0) {
310311
context->constants.obj_table = NULL;
311312
} else {
313+
// CIRCUITPY-CHANGE
312314
context->constants.obj_table = m_malloc_items(n_obj);
313315
}
314316
#endif

py/compile.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3689,6 +3689,7 @@ void mp_compile_to_raw_code(mp_parse_tree_t *parse_tree, qstr source_file, bool
36893689

36903690
mp_obj_t mp_compile(mp_parse_tree_t *parse_tree, qstr source_file, bool is_repl) {
36913691
mp_compiled_module_t cm;
3692+
// CIRCUITPY-CHANGE
36923693
cm.context = m_malloc_with_collect(sizeof(mp_module_context_t));
36933694
cm.context->module.globals = mp_globals_get();
36943695
mp_compile_to_raw_code(parse_tree, source_file, is_repl, &cm);

py/emitbc.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ struct _emit_t {
7676
};
7777

7878
emit_t *emit_bc_new(mp_emit_common_t *emit_common) {
79+
// CIRCUITPY-CHANGE
7980
emit_t *emit = m_new_struct_with_collect(emit_t, 1);
8081
emit->emit_common = emit_common;
8182
return emit;

py/gc.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,9 @@ static size_t compute_heap_size(size_t total_blocks) {
331331
static bool gc_try_add_heap(size_t failed_alloc) {
332332
// 'needed' is the size of a heap large enough to hold failed_alloc, with
333333
// the additional metadata overheads as calculated in gc_setup_area().
334+
// CIRCUITPY-CHANGE: calculation of how much to grow the heap
334335
size_t total_new_blocks = (failed_alloc + BYTES_PER_BLOCK - 1) / BYTES_PER_BLOCK;
336+
// CIRCUITPY-CHANGE
335337
size_t needed = compute_heap_size(total_new_blocks);
336338

337339
size_t avail = gc_get_max_new_split();
@@ -376,6 +378,7 @@ static bool gc_try_add_heap(size_t failed_alloc) {
376378
total_blocks += area->gc_alloc_table_byte_len * BLOCKS_PER_ATB;
377379
}
378380

381+
// CIRCUITPY-CHANGE
379382
size_t total_heap = compute_heap_size(total_blocks);
380383

381384
DEBUG_printf("total_heap " UINT_FMT " bytes\n", total_heap);
@@ -498,6 +501,7 @@ static void MP_NO_INSTRUMENT PLACE_IN_ITCM(gc_mark_subtree)(size_t block)
498501
// check that the consecutive blocks didn't overflow past the end of the area
499502
assert(area->gc_pool_start + (block + n_blocks) * BYTES_PER_BLOCK <= area->gc_pool_end);
500503

504+
// CIRCUITPY-CHANGE
501505
// check if this block should be collected
502506
#if MICROPY_ENABLE_SELECTIVE_COLLECT
503507
bool should_scan = CTB_GET(area, block);
@@ -1005,6 +1009,7 @@ void *gc_alloc(size_t n_bytes, unsigned int alloc_flags) {
10051009
(void)has_finaliser;
10061010
#endif
10071011

1012+
// CIRCUITPY-CHANGE
10081013
#if MICROPY_ENABLE_SELECTIVE_COLLECT
10091014
bool do_not_collect = (alloc_flags & GC_ALLOC_FLAG_DO_NOT_COLLECT) != 0;
10101015
GC_ENTER();
@@ -1184,6 +1189,7 @@ void *gc_realloc(void *ptr, mp_uint_t n_bytes) {
11841189
void *gc_realloc(void *ptr_in, size_t n_bytes, bool allow_move) {
11851190
// check for pure allocation
11861191
if (ptr_in == NULL) {
1192+
// CIRCUITPY-CHANGE
11871193
return gc_alloc(n_bytes, 0);
11881194
}
11891195

@@ -1329,6 +1335,7 @@ void *gc_realloc(void *ptr_in, size_t n_bytes, bool allow_move) {
13291335
}
13301336
#endif
13311337

1338+
// CIRCUITPY-CHANGE
13321339
#if MICROPY_ENABLE_SELECTIVE_COLLECT
13331340
if (!CTB_GET(area, block)) {
13341341
alloc_flags |= GC_ALLOC_FLAG_DO_NOT_COLLECT;
@@ -1343,6 +1350,7 @@ void *gc_realloc(void *ptr_in, size_t n_bytes, bool allow_move) {
13431350
}
13441351

13451352
// can't resize inplace; try to find a new contiguous chain
1353+
// CIRCUITPY-CHANGE
13461354
void *ptr_out = gc_alloc(n_bytes, alloc_flags);
13471355

13481356
// check that the alloc succeeded

py/gc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ void gc_sweep_all(void);
7373

7474
enum {
7575
GC_ALLOC_FLAG_HAS_FINALISER = 1,
76+
// CIRCUITPY-CHANGE
7677
#if MICROPY_ENABLE_SELECTIVE_COLLECT
7778
GC_ALLOC_FLAG_DO_NOT_COLLECT = 2,
7879
#endif

py/lexer.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,7 @@ void mp_lexer_to_next(mp_lexer_t *lex) {
840840
}
841841

842842
mp_lexer_t *mp_lexer_new(qstr src_name, mp_reader_t reader) {
843+
// CIRCUITPY-CHANGE
843844
mp_lexer_t *lex = m_new_struct_with_collect(mp_lexer_t, 1);
844845

845846
lex->source_name = src_name;

py/malloc.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ void *m_malloc_helper(size_t num_bytes, uint8_t flags) {
119119
MP_STATE_MEM(current_bytes_allocated) += num_bytes;
120120
UPDATE_PEAK();
121121
#endif
122+
// CIRCUITPY-CHANGE
122123
// If this config is set then the GC clears all memory, so we don't need to.
123124
#if !MICROPY_GC_CONSERVATIVE_CLEAR
124125
if (flags & M_MALLOC_ENSURE_ZEROED) {
@@ -130,10 +131,12 @@ void *m_malloc_helper(size_t num_bytes, uint8_t flags) {
130131
}
131132

132133
void *m_malloc(size_t num_bytes) {
134+
// CIRCUITPY-CHANGE
133135
return m_malloc_helper(num_bytes, M_MALLOC_RAISE_ERROR);
134136
}
135137

136138
void *m_malloc_maybe(size_t num_bytes) {
139+
// CIRCUITPY-CHANGE
137140
return m_malloc_helper(num_bytes, 0);
138141
}
139142

@@ -142,10 +145,12 @@ void *m_malloc0(size_t num_bytes) {
142145
}
143146

144147
void *m_malloc_with_collect(size_t num_bytes) {
148+
// CIRCUITPY-CHANGE
145149
return m_malloc_helper(num_bytes, M_MALLOC_RAISE_ERROR | M_MALLOC_COLLECT);
146150
}
147151

148152
void *m_malloc_maybe_with_collect(size_t num_bytes) {
153+
// CIRCUITPY-CHANGE
149154
return m_malloc_helper(num_bytes, M_MALLOC_COLLECT);
150155
}
151156

py/map.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ void mp_map_init(mp_map_t *map, size_t n) {
9595
map->table = NULL;
9696
} else {
9797
map->alloc = n;
98+
// CIRCUITPY-CHANGE
9899
map->table = malloc_table(map->alloc);
99100
}
100101
map->used = 0;
@@ -136,6 +137,7 @@ static void mp_map_rehash(mp_map_t *map) {
136137
size_t new_alloc = get_hash_alloc_greater_or_equal_to(map->alloc + 1);
137138
DEBUG_printf("mp_map_rehash(%p): " UINT_FMT " -> " UINT_FMT "\n", map, old_alloc, new_alloc);
138139
mp_map_elem_t *old_table = map->table;
140+
// CIRCUITPY-CHANGE
139141
mp_map_elem_t *new_table = malloc_table(new_alloc);
140142
// If we reach this point, table resizing succeeded, now we can edit the old map.
141143
map->alloc = new_alloc;
@@ -332,6 +334,7 @@ mp_map_elem_t *MICROPY_WRAP_MP_MAP_LOOKUP(mp_map_lookup)(mp_map_t * map, mp_obj_
332334
void mp_set_init(mp_set_t *set, size_t n) {
333335
set->alloc = n;
334336
set->used = 0;
337+
// CIRCUITPY-CHANGE
335338
set->table = m_malloc_items0(set->alloc);
336339
}
337340

@@ -340,6 +343,7 @@ static void mp_set_rehash(mp_set_t *set) {
340343
mp_obj_t *old_table = set->table;
341344
set->alloc = get_hash_alloc_greater_or_equal_to(set->alloc + 1);
342345
set->used = 0;
346+
// CIRCUITPY-CHANGE
343347
set->table = m_malloc_items0(set->alloc);
344348
for (size_t i = 0; i < old_alloc; i++) {
345349
if (old_table[i] != MP_OBJ_NULL && old_table[i] != MP_OBJ_SENTINEL) {

py/misc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ typedef unsigned int uint;
7474

7575
// TODO make a lazy m_renew that can increase by a smaller amount than requested (but by at least 1 more element)
7676

77+
// CIRCUITPY-CHANGE: new wrappers for selective collect, and use of m_malloc_helper()
7778
// The following are convenience wrappers for m_malloc_helper and can save space at the call sites.
7879
// m_malloc and m_new allocate space that is not collected and does not have a finaliser.
7980
// Use m_malloc_items() to allocate space for mp_obj_t that will be collected.

0 commit comments

Comments
 (0)
0