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));

0 commit comments

Comments
 (0)