Skip to content

Commit a6dd85c

Browse files
tannewtdhalbert
andauthored
Apply suggestions from code review
Co-authored-by: Dan Halbert <[email protected]>
1 parent 7f97695 commit a6dd85c

File tree

17 files changed

+40
-0
lines changed

17 files changed

+40
-0
lines changed

py/gc.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ static bool gc_try_add_heap(size_t failed_alloc) {
378378
total_blocks += area->gc_alloc_table_byte_len * BLOCKS_PER_ATB;
379379
}
380380

381+
// CIRCUITPY-CHANGE
381382
size_t total_heap = compute_heap_size(total_blocks);
382383

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

504+
// CIRCUITPY-CHANGE
503505
// check if this block should be collected
504506
#if MICROPY_ENABLE_SELECTIVE_COLLECT
505507
bool should_scan = CTB_GET(area, block);
@@ -1007,6 +1009,7 @@ void *gc_alloc(size_t n_bytes, unsigned int alloc_flags) {
10071009
(void)has_finaliser;
10081010
#endif
10091011

1012+
// CIRCUITPY-CHANGE
10101013
#if MICROPY_ENABLE_SELECTIVE_COLLECT
10111014
bool do_not_collect = (alloc_flags & GC_ALLOC_FLAG_DO_NOT_COLLECT) != 0;
10121015
GC_ENTER();
@@ -1186,6 +1189,7 @@ void *gc_realloc(void *ptr, mp_uint_t n_bytes) {
11861189
void *gc_realloc(void *ptr_in, size_t n_bytes, bool allow_move) {
11871190
// check for pure allocation
11881191
if (ptr_in == NULL) {
1192+
// CIRCUITPY-CHANGE
11891193
return gc_alloc(n_bytes, 0);
11901194
}
11911195

@@ -1331,6 +1335,7 @@ void *gc_realloc(void *ptr_in, size_t n_bytes, bool allow_move) {
13311335
}
13321336
#endif
13331337

1338+
// CIRCUITPY-CHANGE
13341339
#if MICROPY_ENABLE_SELECTIVE_COLLECT
13351340
if (!CTB_GET(area, block)) {
13361341
alloc_flags |= GC_ALLOC_FLAG_DO_NOT_COLLECT;
@@ -1345,6 +1350,7 @@ void *gc_realloc(void *ptr_in, size_t n_bytes, bool allow_move) {
13451350
}
13461351

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

13501356
// 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.

py/mpstate.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ typedef struct _mp_state_mem_area_t {
100100
#if MICROPY_ENABLE_FINALISER
101101
byte *gc_finaliser_table_start;
102102
#endif
103+
// CIRCUITPY-CHANGE
103104
#if MICROPY_ENABLE_SELECTIVE_COLLECT
104105
byte *gc_collect_table_start;
105106
#endif

py/obj.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747

4848
// Allocates an object and also sets type, for mp_obj_malloc{,_var} macros.
4949
MP_NOINLINE void *mp_obj_malloc_helper(size_t num_bytes, const mp_obj_type_t *type) {
50+
// CIRCUITPY-CHANGE
5051
mp_obj_base_t *base = (mp_obj_base_t *)m_malloc_helper(num_bytes, M_MALLOC_RAISE_ERROR | M_MALLOC_COLLECT);
5152
base->type = type;
5253
return base;
@@ -55,6 +56,7 @@ MP_NOINLINE void *mp_obj_malloc_helper(size_t num_bytes, const mp_obj_type_t *ty
5556
#if MICROPY_ENABLE_FINALISER
5657
// Allocates an object and also sets type, for mp_obj_malloc{,_var}_with_finaliser macros.
5758
MP_NOINLINE void *mp_obj_malloc_with_finaliser_helper(size_t num_bytes, const mp_obj_type_t *type) {
59+
// CIRCUITPY-CHANGE
5860
mp_obj_base_t *base = (mp_obj_base_t *)m_malloc_helper(num_bytes, M_MALLOC_RAISE_ERROR | M_MALLOC_COLLECT | M_MALLOC_WITH_FINALISER);
5961
base->type = type;
6062
return base;

py/objarray.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ static mp_obj_array_t *array_new(char typecode, size_t n) {
106106
}
107107
int typecode_size = mp_binary_get_size('@', typecode, NULL);
108108

109+
// CIRCUITPY-CHANGE: refactor to use m_obj_malloc()
109110
const mp_obj_type_t *type;
110111
#if MICROPY_PY_BUILTINS_BYTEARRAY && MICROPY_PY_ARRAY
111112
type = (typecode == BYTEARRAY_TYPECODE) ? &mp_type_bytearray : &mp_type_array;
@@ -118,6 +119,7 @@ static mp_obj_array_t *array_new(char typecode, size_t n) {
118119
o->typecode = typecode;
119120
o->free = 0;
120121
o->len = n;
122+
// CIRCUITPY-CHANGE
121123
o->items = m_malloc(typecode_size * o->len);
122124
return o;
123125
}
@@ -227,6 +229,7 @@ static mp_obj_t bytearray_make_new(const mp_obj_type_t *type_in, size_t n_args,
227229
#if MICROPY_PY_BUILTINS_MEMORYVIEW
228230

229231
mp_obj_t mp_obj_new_memoryview(byte typecode, size_t nitems, void *items) {
232+
// CIRCUITPY-CHANGE
230233
mp_obj_array_t *self = mp_obj_malloc(mp_obj_array_t, &mp_type_memoryview);
231234
mp_obj_memoryview_init(self, typecode, 0, nitems, items);
232235
return MP_OBJ_FROM_PTR(self);
@@ -686,6 +689,7 @@ static mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value
686689
if (slice.start > memview_offset_max) {
687690
mp_raise_msg(&mp_type_OverflowError, MP_ERROR_TEXT("memoryview offset too large"));
688691
}
692+
// CIRCUITPY-CHANGE
689693
res = mp_obj_malloc(mp_obj_array_t, &mp_type_memoryview);
690694
*res = *o;
691695
res->memview_offset += slice.start;

py/objclosure.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ static mp_obj_t closure_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const
5050
return mp_call_function_n_kw(self->fun, self->n_closed + n_args, n_kw, args2);
5151
} else {
5252
// use heap to allocate temporary args array
53+
// CIRCUITPY-CHANGE
5354
mp_obj_t *args2 = m_malloc_items(n_total);
5455
memcpy(args2, self->closed, self->n_closed * sizeof(mp_obj_t));
5556
memcpy(args2 + self->n_closed, args, (n_args + 2 * n_kw) * sizeof(mp_obj_t));

py/objdeque.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ static mp_obj_t deque_make_new(const mp_obj_type_t *type, size_t n_args, size_t
5858
mp_obj_deque_t *o = mp_obj_malloc(mp_obj_deque_t, type);
5959
o->alloc = maxlen + 1;
6060
o->i_get = o->i_put = 0;
61+
// CIRCUITPY-CHANGE
6162
o->items = m_malloc_items(o->alloc);
6263

6364
if (n_args > 2) {

py/objexcept.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ mp_obj_t mp_alloc_emergency_exception_buf(mp_obj_t size_in) {
9494
mp_int_t size = mp_obj_get_int(size_in);
9595
void *buf = NULL;
9696
if (size > 0) {
97+
// CIRCUITPY-CHANGE
9798
buf = m_malloc_with_collect(size);
9899
}
99100

@@ -220,6 +221,7 @@ mp_obj_t mp_obj_exception_make_new(const mp_obj_type_t *type, size_t n_args, siz
220221
mp_arg_check_num(n_args, n_kw, 0, MP_OBJ_FUN_ARGS_MAX, false);
221222

222223
// Try to allocate memory for the exception, with fallback to emergency exception object
224+
// CIRCUITPY-CHANGE
223225
mp_obj_exception_t *o_exc = m_malloc_maybe_with_collect(sizeof(mp_obj_exception_t));
224226
if (o_exc == NULL) {
225227
o_exc = &MP_STATE_VM(mp_emergency_exception_obj);
@@ -544,6 +546,7 @@ mp_obj_t mp_obj_new_exception_msg_vlist(const mp_obj_type_t *exc_type, mp_rom_er
544546
// CIRCUITPY-CHANGE: here and more below
545547
size_t o_str_alloc = decompress_length(fmt);
546548
if (gc_alloc_possible()) {
549+
// CIRCUITPY-CHANGE
547550
o_str = m_malloc_maybe_with_collect(sizeof(mp_obj_str_t));
548551
o_str_buf = m_new_maybe(byte, o_str_alloc);
549552
}
@@ -661,6 +664,7 @@ void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, size_t line, qs
661664

662665
// Try to allocate memory for the traceback, with fallback to emergency traceback object
663666
if (self->traceback == NULL || self->traceback == (mp_obj_traceback_t *)&mp_const_empty_traceback_obj) {
667+
// CIRCUITPY-CHANGE
664668
self->traceback = m_malloc_maybe_with_collect(sizeof(mp_obj_traceback_t));
665669
if (self->traceback == NULL) {
666670
self->traceback = &MP_STATE_VM(mp_emergency_traceback_obj);

py/objtuple.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ static mp_obj_t mp_obj_tuple_make_new(const mp_obj_type_t *type_in, size_t n_arg
8686

8787
size_t alloc = 4;
8888
size_t len = 0;
89+
// CIRCUITPY-CHANGE
8990
mp_obj_t *items = m_malloc_items(alloc);
9091

9192
mp_obj_t iterable = mp_getiter(args[0], NULL);

py/objtype.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ static mp_obj_t native_base_init_wrapper(size_t n_args, const mp_obj_t *pos_args
9696
pos_args++;
9797
n_args--;
9898

99+
// CIRCUITPY-CHANGE
99100
mp_obj_t *args2 = m_malloc_items(n_args + 2 * n_kw);
100101
// copy in args
101102
memcpy(args2, pos_args, n_args * sizeof(mp_obj_t));
@@ -340,6 +341,7 @@ static mp_obj_t mp_obj_instance_make_new(const mp_obj_type_t *self, size_t n_arg
340341
mp_obj_t args2[1] = {MP_OBJ_FROM_PTR(self)};
341342
new_ret = mp_call_function_n_kw(init_fn[0], 1, 0, args2);
342343
} else {
344+
// CIRCUITPY-CHANGE
343345
mp_obj_t *args2 = m_malloc_items(1 + n_args + 2 * n_kw);
344346
args2[0] = MP_OBJ_FROM_PTR(self);
345347
memcpy(args2 + 1, args, (n_args + 2 * n_kw) * sizeof(mp_obj_t));
@@ -371,6 +373,7 @@ static mp_obj_t mp_obj_instance_make_new(const mp_obj_type_t *self, size_t n_arg
371373
if (n_args == 0 && n_kw == 0) {
372374
init_ret = mp_call_method_n_kw(0, 0, init_fn);
373375
} else {
376+
// CIRCUITPY-CHANGE
374377
mp_obj_t *args2 = m_malloc_items(2 + n_args + 2 * n_kw);
375378
args2[0] = init_fn[0];
376379
args2[1] = init_fn[1];
@@ -1513,6 +1516,7 @@ mp_obj_t mp_obj_cast_to_native_base(mp_obj_t self_in, mp_const_obj_t native_type
15131516
/******************************************************************************/
15141517
// staticmethod and classmethod types (probably should go in a different file)
15151518

1519+
// CIRCUITPY-CHANGE: better arg name
15161520
static mp_obj_t static_class_method_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
15171521
assert(type == &mp_type_staticmethod || type == &mp_type_classmethod);
15181522

py/parse.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ static void *parser_alloc(parser_t *parser, size_t num_bytes) {
286286
if (alloc < num_bytes) {
287287
alloc = num_bytes;
288288
}
289+
// CIRCUITPY-CHANGE
289290
chunk = (mp_parse_chunk_t *)m_malloc_with_collect(sizeof(mp_parse_chunk_t) + alloc);
290291
chunk->alloc = alloc;
291292
chunk->union_.used = 0;
@@ -1055,6 +1056,7 @@ mp_parse_tree_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
10551056
parser.result_stack_top = 0;
10561057
parser.result_stack = NULL;
10571058
while (parser.result_stack_alloc > 1) {
1059+
// CIRCUITPY-CHANGE
10581060
parser.result_stack = m_malloc_maybe_with_collect(sizeof(mp_parse_node_t) * parser.result_stack_alloc);
10591061
if (parser.result_stack != NULL) {
10601062
break;

py/scope.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ scope_t *scope_new(scope_kind_t kind, mp_parse_node_t pn, mp_uint_t emit_options
5151
MP_STATIC_ASSERT(MP_QSTR__lt_setcomp_gt_ <= UINT8_MAX);
5252
MP_STATIC_ASSERT(MP_QSTR__lt_genexpr_gt_ <= UINT8_MAX);
5353

54+
// CIRCUITPY-CHANGE
5455
scope_t *scope = m_new_struct_with_collect(scope_t, 1);
5556
scope->kind = kind;
5657
scope->pn = pn;

shared-bindings/displayio/Group.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ static mp_obj_t group_subscr(mp_obj_t self_in, mp_obj_t index_obj, mp_obj_t valu
317317
//|
318318
static mp_obj_t displayio_group_obj_sort(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
319319
displayio_group_t *self = native_group(pos_args[0]);
320+
// CIRCUITPY-CHANGE
320321
mp_obj_t *args = m_malloc_items(n_args);
321322
for (size_t i = 1; i < n_args; ++i) {
322323
args[i] = pos_args[i];

0 commit comments

Comments
 (0)