Skip to content

Commit d977d26

Browse files
committed
py/gc: Use size_t instead of mp_uint_t to count things related to heap.
size_t is the correct type to use to count things related to the size of the address space. Using size_t (instead of mp_uint_t) is important for the efficiency of ports that configure mp_uint_t to larger than the machine word size.
1 parent f7782f8 commit d977d26

2 files changed

Lines changed: 33 additions & 33 deletions

File tree

py/gc.c

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ void gc_init(void *start, void *end) {
105105
// F = A * BLOCKS_PER_ATB / BLOCKS_PER_FTB
106106
// P = A * BLOCKS_PER_ATB * BYTES_PER_BLOCK
107107
// => T = A * (1 + BLOCKS_PER_ATB / BLOCKS_PER_FTB + BLOCKS_PER_ATB * BYTES_PER_BLOCK)
108-
mp_uint_t total_byte_len = (byte*)end - (byte*)start;
108+
size_t total_byte_len = (byte*)end - (byte*)start;
109109
#if MICROPY_ENABLE_FINALISER
110110
MP_STATE_MEM(gc_alloc_table_byte_len) = total_byte_len * BITS_PER_BYTE / (BITS_PER_BYTE + BITS_PER_BYTE * BLOCKS_PER_ATB / BLOCKS_PER_FTB + BITS_PER_BYTE * BLOCKS_PER_ATB * BYTES_PER_BLOCK);
111111
#else
@@ -115,11 +115,11 @@ void gc_init(void *start, void *end) {
115115
MP_STATE_MEM(gc_alloc_table_start) = (byte*)start;
116116

117117
#if MICROPY_ENABLE_FINALISER
118-
mp_uint_t gc_finaliser_table_byte_len = (MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB + BLOCKS_PER_FTB - 1) / BLOCKS_PER_FTB;
118+
size_t gc_finaliser_table_byte_len = (MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB + BLOCKS_PER_FTB - 1) / BLOCKS_PER_FTB;
119119
MP_STATE_MEM(gc_finaliser_table_start) = MP_STATE_MEM(gc_alloc_table_start) + MP_STATE_MEM(gc_alloc_table_byte_len);
120120
#endif
121121

122-
mp_uint_t gc_pool_block_len = MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB;
122+
size_t gc_pool_block_len = MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB;
123123
MP_STATE_MEM(gc_pool_start) = (byte*)end - gc_pool_block_len * BYTES_PER_BLOCK;
124124
MP_STATE_MEM(gc_pool_end) = end;
125125

@@ -175,7 +175,7 @@ bool gc_is_locked(void) {
175175
#define VERIFY_MARK_AND_PUSH(ptr) \
176176
do { \
177177
if (VERIFY_PTR(ptr)) { \
178-
mp_uint_t _block = BLOCK_FROM_PTR(ptr); \
178+
size_t _block = BLOCK_FROM_PTR(ptr); \
179179
if (ATB_GET_KIND(_block) == AT_HEAD) { \
180180
/* an unmarked head, mark it, and push it on gc stack */ \
181181
ATB_HEAD_TO_MARK(_block); \
@@ -194,7 +194,7 @@ STATIC void gc_drain_stack(void) {
194194
size_t block = *--MP_STATE_MEM(gc_sp);
195195

196196
// work out number of consecutive blocks in the chain starting with this one
197-
mp_uint_t n_blocks = 0;
197+
size_t n_blocks = 0;
198198
do {
199199
n_blocks += 1;
200200
} while (ATB_GET_KIND(block + n_blocks) == AT_TAIL);
@@ -214,7 +214,7 @@ STATIC void gc_deal_with_stack_overflow(void) {
214214
MP_STATE_MEM(gc_sp) = MP_STATE_MEM(gc_stack);
215215

216216
// scan entire memory looking for blocks which have been marked but not their children
217-
for (mp_uint_t block = 0; block < MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB; block++) {
217+
for (size_t block = 0; block < MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB; block++) {
218218
// trace (again) if mark bit set
219219
if (ATB_GET_KIND(block) == AT_MARK) {
220220
*MP_STATE_MEM(gc_sp)++ = block;
@@ -303,8 +303,8 @@ void gc_info(gc_info_t *info) {
303303
info->num_1block = 0;
304304
info->num_2block = 0;
305305
info->max_block = 0;
306-
for (mp_uint_t block = 0, len = 0; block < MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB; block++) {
307-
mp_uint_t kind = ATB_GET_KIND(block);
306+
for (size_t block = 0, len = 0; block < MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB; block++) {
307+
size_t kind = ATB_GET_KIND(block);
308308
if (kind == AT_FREE || kind == AT_HEAD) {
309309
if (len == 1) {
310310
info->num_1block += 1;
@@ -342,7 +342,7 @@ void gc_info(gc_info_t *info) {
342342
}
343343

344344
void *gc_alloc(size_t n_bytes, bool has_finaliser) {
345-
mp_uint_t n_blocks = ((n_bytes + BYTES_PER_BLOCK - 1) & (~(BYTES_PER_BLOCK - 1))) / BYTES_PER_BLOCK;
345+
size_t n_blocks = ((n_bytes + BYTES_PER_BLOCK - 1) & (~(BYTES_PER_BLOCK - 1))) / BYTES_PER_BLOCK;
346346
DEBUG_printf("gc_alloc(" UINT_FMT " bytes -> " UINT_FMT " blocks)\n", n_bytes, n_blocks);
347347

348348
// check if GC is locked
@@ -355,10 +355,10 @@ void *gc_alloc(size_t n_bytes, bool has_finaliser) {
355355
return NULL;
356356
}
357357

358-
mp_uint_t i;
359-
mp_uint_t end_block;
360-
mp_uint_t start_block;
361-
mp_uint_t n_free = 0;
358+
size_t i;
359+
size_t end_block;
360+
size_t start_block;
361+
size_t n_free = 0;
362362
int collected = !MP_STATE_MEM(gc_auto_collect_enabled);
363363
for (;;) {
364364

@@ -400,7 +400,7 @@ void *gc_alloc(size_t n_bytes, bool has_finaliser) {
400400

401401
// mark rest of blocks as used tail
402402
// TODO for a run of many blocks can make this more efficient
403-
for (mp_uint_t bl = start_block + 1; bl <= end_block; bl++) {
403+
for (size_t bl = start_block + 1; bl <= end_block; bl++) {
404404
ATB_FREE_TO_TAIL(bl);
405405
}
406406

@@ -454,7 +454,7 @@ void gc_free(void *ptr) {
454454
DEBUG_printf("gc_free(%p)\n", ptr);
455455

456456
if (VERIFY_PTR(ptr)) {
457-
mp_uint_t block = BLOCK_FROM_PTR(ptr);
457+
size_t block = BLOCK_FROM_PTR(ptr);
458458
if (ATB_GET_KIND(block) == AT_HEAD) {
459459
#if MICROPY_ENABLE_FINALISER
460460
FTB_CLEAR(block);
@@ -483,10 +483,10 @@ void gc_free(void *ptr) {
483483

484484
size_t gc_nbytes(const void *ptr) {
485485
if (VERIFY_PTR(ptr)) {
486-
mp_uint_t block = BLOCK_FROM_PTR(ptr);
486+
size_t block = BLOCK_FROM_PTR(ptr);
487487
if (ATB_GET_KIND(block) == AT_HEAD) {
488488
// work out number of consecutive blocks in the chain starting with this on
489-
mp_uint_t n_blocks = 0;
489+
size_t n_blocks = 0;
490490
do {
491491
n_blocks += 1;
492492
} while (ATB_GET_KIND(block + n_blocks) == AT_TAIL);
@@ -551,26 +551,26 @@ void *gc_realloc(void *ptr_in, size_t n_bytes, bool allow_move) {
551551
}
552552

553553
// get first block
554-
mp_uint_t block = BLOCK_FROM_PTR(ptr);
554+
size_t block = BLOCK_FROM_PTR(ptr);
555555

556556
// sanity check the ptr is pointing to the head of a block
557557
if (ATB_GET_KIND(block) != AT_HEAD) {
558558
return NULL;
559559
}
560560

561561
// compute number of new blocks that are requested
562-
mp_uint_t new_blocks = (n_bytes + BYTES_PER_BLOCK - 1) / BYTES_PER_BLOCK;
562+
size_t new_blocks = (n_bytes + BYTES_PER_BLOCK - 1) / BYTES_PER_BLOCK;
563563

564564
// Get the total number of consecutive blocks that are already allocated to
565565
// this chunk of memory, and then count the number of free blocks following
566566
// it. Stop if we reach the end of the heap, or if we find enough extra
567567
// free blocks to satisfy the realloc. Note that we need to compute the
568568
// total size of the existing memory chunk so we can correctly and
569569
// efficiently shrink it (see below for shrinking code).
570-
mp_uint_t n_free = 0;
571-
mp_uint_t n_blocks = 1; // counting HEAD block
572-
mp_uint_t max_block = MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB;
573-
for (mp_uint_t bl = block + n_blocks; bl < max_block; bl++) {
570+
size_t n_free = 0;
571+
size_t n_blocks = 1; // counting HEAD block
572+
size_t max_block = MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB;
573+
for (size_t bl = block + n_blocks; bl < max_block; bl++) {
574574
byte block_type = ATB_GET_KIND(bl);
575575
if (block_type == AT_TAIL) {
576576
n_blocks++;
@@ -595,7 +595,7 @@ void *gc_realloc(void *ptr_in, size_t n_bytes, bool allow_move) {
595595
// check if we can shrink the allocated area
596596
if (new_blocks < n_blocks) {
597597
// free unneeded tail blocks
598-
for (mp_uint_t bl = block + new_blocks, count = n_blocks - new_blocks; count > 0; bl++, count--) {
598+
for (size_t bl = block + new_blocks, count = n_blocks - new_blocks; count > 0; bl++, count--) {
599599
ATB_ANY_TO_FREE(bl);
600600
}
601601

@@ -614,7 +614,7 @@ void *gc_realloc(void *ptr_in, size_t n_bytes, bool allow_move) {
614614
// check if we can expand in place
615615
if (new_blocks <= n_blocks + n_free) {
616616
// mark few more blocks as used tail
617-
for (mp_uint_t bl = block + n_blocks; bl < block + new_blocks; bl++) {
617+
for (size_t bl = block + n_blocks; bl < block + new_blocks; bl++) {
618618
assert(ATB_GET_KIND(bl) == AT_FREE);
619619
ATB_FREE_TO_TAIL(bl);
620620
}
@@ -665,18 +665,18 @@ void gc_dump_info(void) {
665665
}
666666

667667
void gc_dump_alloc_table(void) {
668-
static const mp_uint_t DUMP_BYTES_PER_LINE = 64;
668+
static const size_t DUMP_BYTES_PER_LINE = 64;
669669
#if !EXTENSIVE_HEAP_PROFILING
670670
// When comparing heap output we don't want to print the starting
671671
// pointer of the heap because it changes from run to run.
672672
mp_printf(&mp_plat_print, "GC memory layout; from %p:", MP_STATE_MEM(gc_pool_start));
673673
#endif
674-
for (mp_uint_t bl = 0; bl < MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB; bl++) {
674+
for (size_t bl = 0; bl < MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB; bl++) {
675675
if (bl % DUMP_BYTES_PER_LINE == 0) {
676676
// a new line of blocks
677677
{
678678
// check if this line contains only free blocks
679-
mp_uint_t bl2 = bl;
679+
size_t bl2 = bl;
680680
while (bl2 < MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB && ATB_GET_KIND(bl2) == AT_FREE) {
681681
bl2++;
682682
}

py/mpstate.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,27 +48,27 @@ typedef struct _mp_state_mem_t {
4848
#endif
4949

5050
byte *gc_alloc_table_start;
51-
mp_uint_t gc_alloc_table_byte_len;
51+
size_t gc_alloc_table_byte_len;
5252
#if MICROPY_ENABLE_FINALISER
5353
byte *gc_finaliser_table_start;
5454
#endif
5555
byte *gc_pool_start;
5656
byte *gc_pool_end;
5757

5858
int gc_stack_overflow;
59-
mp_uint_t gc_stack[MICROPY_ALLOC_GC_STACK_SIZE];
60-
mp_uint_t *gc_sp;
59+
size_t gc_stack[MICROPY_ALLOC_GC_STACK_SIZE];
60+
size_t *gc_sp;
6161
uint16_t gc_lock_depth;
6262

6363
// This variable controls auto garbage collection. If set to 0 then the
6464
// GC won't automatically run when gc_alloc can't find enough blocks. But
6565
// you can still allocate/free memory and also explicitly call gc_collect.
6666
uint16_t gc_auto_collect_enabled;
6767

68-
mp_uint_t gc_last_free_atb_index;
68+
size_t gc_last_free_atb_index;
6969

7070
#if MICROPY_PY_GC_COLLECT_RETVAL
71-
mp_uint_t gc_collected;
71+
size_t gc_collected;
7272
#endif
7373
} mp_state_mem_t;
7474

0 commit comments

Comments
 (0)