Skip to content

Commit da8c7dd

Browse files
committed
py/malloc: Add m_tracked_realloc.
Signed-off-by: Andrew Leech <[email protected]>
1 parent 838f212 commit da8c7dd

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

ports/unix/coverage.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,13 @@ static mp_obj_t extra_coverage(void) {
292292
gc_collect();
293293
}
294294

295+
// resize one of the blocks
296+
void *before = ptrs[1];
297+
ptrs[1] = FLIP_POINTER(m_tracked_realloc(FLIP_POINTER(ptrs[1]), 2 * NUM_BYTES));
298+
void *after = ptrs[1];
299+
bool location_changed = before != after;
300+
mp_printf(&mp_plat_print, "%d\n", location_changed);
301+
295302
// check the memory blocks have the correct content
296303
for (size_t i = 0; i < NUM_PTRS; ++i) {
297304
bool correct_contents = true;

py/malloc.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,41 @@ void *m_tracked_calloc(size_t nmemb, size_t size) {
263263
return &node->data[0];
264264
}
265265

266+
void *m_tracked_realloc(void *ptr_in, size_t n_bytes) {
267+
if (ptr_in == NULL) {
268+
return NULL;
269+
}
270+
m_tracked_node_t *node = (m_tracked_node_t *)((uint8_t *)ptr_in - sizeof(m_tracked_node_t));
271+
size_t prev_bytes;
272+
#if MICROPY_TRACKED_ALLOC_STORE_SIZE
273+
prev_bytes = node->size;
274+
#else
275+
prev_bytes = gc_nbytes(node);
276+
#endif
277+
#if MICROPY_DEBUG_VERBOSE
278+
size_t nb;
279+
size_t n = m_tracked_count_links(&nb);
280+
DEBUG_printf("m_tracked_realloc(%p, [%p, %p], pbytes=%u, nbytes=%u, links=%u;%u)\n", node, node->prev, node->next, (int)prev_bytes, (int)n_bytes, (int)n, (int)nb);
281+
#endif
282+
node = m_realloc(node,
283+
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
284+
sizeof(m_tracked_node_t) + prev_bytes,
285+
#endif
286+
sizeof(m_tracked_node_t) + n_bytes
287+
);
288+
if (node == NULL) {
289+
node->prev->next = node->next;
290+
node->next->prev = node->prev;
291+
return NULL;
292+
}
293+
#if MICROPY_TRACKED_ALLOC_STORE_SIZE
294+
node->size = n_bytes;
295+
#endif
296+
node->prev->next = node;
297+
node->next->prev = node;
298+
return &node->data[0];
299+
}
300+
266301
void m_tracked_free(void *ptr_in) {
267302
if (ptr_in == NULL) {
268303
return;

py/misc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ NORETURN void m_malloc_fail(size_t num_bytes);
111111
// These alloc/free functions track the pointers in a linked list so the GC does not reclaim
112112
// them. They can be used by code that requires traditional C malloc/free semantics.
113113
void *m_tracked_calloc(size_t nmemb, size_t size);
114+
void *m_tracked_realloc(void *ptr_in, size_t n_bytes);
114115
void m_tracked_free(void *ptr_in);
115116
#endif
116117

tests/ports/unix/extra_coverage.py.exp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ m_tracked_head = 0
2929
5 1
3030
6 1
3131
7 1
32+
1
3233
0 1
3334
1 1
3435
2 1

tests/run-tests.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,10 @@ def main():
937937
run-tests.py -i async - exclude all, then include tests containing "async" anywhere
938938
run-tests.py -e '/big.+int' - include all, then exclude by regex
939939
run-tests.py -e async -i async_foo - include all, exclude async, yet still include async_foo
940+
941+
For tests that use cpython, unix micropython and mpy-cross, the default executables
942+
vcan be overridden with the environment variables MICROPY_CPYTHON3, MICROPY_MICROPYTHON and
943+
MICROPY_MPYCROSS respectively.
940944
""",
941945
)
942946
cmd_parser.add_argument("--target", default="unix", help="the target platform")

0 commit comments

Comments
 (0)