py: Fix GC realloc issue, where memory chunks were never shrunk.
Previously, a realloc to a smaller memory chunk size would not free the unused blocks in the tail of the chunk.
This commit is contained in:
parent
4859edb95b
commit
9b0b373e5e
32
py/gc.c
32
py/gc.c
|
@ -567,22 +567,28 @@ void *gc_realloc(void *ptr_in, mp_uint_t n_bytes) {
|
||||||
// compute number of new blocks that are requested
|
// compute number of new blocks that are requested
|
||||||
mp_uint_t new_blocks = (n_bytes + BYTES_PER_BLOCK - 1) / BYTES_PER_BLOCK;
|
mp_uint_t new_blocks = (n_bytes + BYTES_PER_BLOCK - 1) / BYTES_PER_BLOCK;
|
||||||
|
|
||||||
// get the number of consecutive tail blocks and
|
// Get the total number of consecutive blocks that are already allocated to
|
||||||
// the number of free blocks after last tail block
|
// this chunk of memory, and then count the number of free blocks following
|
||||||
// stop if we reach (or are at) end of heap
|
// it. Stop if we reach the end of the heap, or if we find enough extra
|
||||||
|
// free blocks to satisfy the realloc. Note that we need to compute the
|
||||||
|
// total size of the existing memory chunk so we can correctly and
|
||||||
|
// efficiently shrink it (see below for shrinking code).
|
||||||
mp_uint_t n_free = 0;
|
mp_uint_t n_free = 0;
|
||||||
mp_uint_t n_blocks = 1; // counting HEAD block
|
mp_uint_t n_blocks = 1; // counting HEAD block
|
||||||
mp_uint_t max_block = gc_alloc_table_byte_len * BLOCKS_PER_ATB;
|
mp_uint_t max_block = gc_alloc_table_byte_len * BLOCKS_PER_ATB;
|
||||||
while (block + n_blocks + n_free < max_block) {
|
for (mp_uint_t bl = block + n_blocks; bl < max_block; bl++) {
|
||||||
if (n_blocks + n_free >= new_blocks) {
|
byte block_type = ATB_GET_KIND(bl);
|
||||||
// stop as soon as we find enough blocks for n_bytes
|
if (block_type == AT_TAIL) {
|
||||||
break;
|
n_blocks++;
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
byte block_type = ATB_GET_KIND(block + n_blocks + n_free);
|
if (block_type == AT_FREE) {
|
||||||
switch (block_type) {
|
n_free++;
|
||||||
case AT_FREE: n_free++; continue;
|
if (n_blocks + n_free >= new_blocks) {
|
||||||
case AT_TAIL: n_blocks++; continue;
|
// stop as soon as we find enough blocks for n_bytes
|
||||||
case AT_MARK: assert(0);
|
break;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -595,7 +601,7 @@ void *gc_realloc(void *ptr_in, mp_uint_t n_bytes) {
|
||||||
// check if we can shrink the allocated area
|
// check if we can shrink the allocated area
|
||||||
if (new_blocks < n_blocks) {
|
if (new_blocks < n_blocks) {
|
||||||
// free unneeded tail blocks
|
// free unneeded tail blocks
|
||||||
for (mp_uint_t bl = block + new_blocks; ATB_GET_KIND(bl) == AT_TAIL; bl++) {
|
for (mp_uint_t bl = block + new_blocks, count = n_blocks - new_blocks; count > 0; bl++, count--) {
|
||||||
ATB_ANY_TO_FREE(bl);
|
ATB_ANY_TO_FREE(bl);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue