diff options
| author | Damien George <damien@micropython.org> | 2022-05-04 12:12:11 +1000 |
|---|---|---|
| committer | Damien George <damien@micropython.org> | 2022-05-05 10:31:50 +1000 |
| commit | fca5701f74b784834d1af22d10988a1d0fddddaf (patch) | |
| tree | f98c1d6c9e1473a13455857af0563760abcd08ec /ports/unix/coverage.c | |
| parent | 965747bd97183a85db56e8d79c15c589df87cdd6 (diff) | |
py/malloc: Introduce m_tracked_calloc, m_tracked_free functions.
Enabled by MICROPY_TRACKED_ALLOC.
Signed-off-by: Damien George <damien@micropython.org>
Diffstat (limited to 'ports/unix/coverage.c')
| -rw-r--r-- | ports/unix/coverage.c | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/ports/unix/coverage.c b/ports/unix/coverage.c index 7409221ef..cf425ac43 100644 --- a/ports/unix/coverage.c +++ b/ports/unix/coverage.c @@ -220,6 +220,55 @@ STATIC mp_obj_t extra_coverage(void) { mp_printf(&mp_plat_print, "%p\n", gc_nbytes(NULL)); } + // tracked allocation + { + #define NUM_PTRS (8) + #define NUM_BYTES (128) + #define FLIP_POINTER(p) ((uint8_t *)((uintptr_t)(p) ^ 0x0f)) + + mp_printf(&mp_plat_print, "# tracked allocation\n"); + mp_printf(&mp_plat_print, "m_tracked_head = %p\n", MP_STATE_VM(m_tracked_head)); + + uint8_t *ptrs[NUM_PTRS]; + + // allocate memory blocks + for (size_t i = 0; i < NUM_PTRS; ++i) { + ptrs[i] = m_tracked_calloc(1, NUM_BYTES); + bool all_zero = true; + for (size_t j = 0; j < NUM_BYTES; ++j) { + if (ptrs[i][j] != 0) { + all_zero = false; + break; + } + ptrs[i][j] = j; + } + mp_printf(&mp_plat_print, "%d %d\n", i, all_zero); + + // hide the pointer from the GC and collect + ptrs[i] = FLIP_POINTER(ptrs[i]); + gc_collect(); + } + + // check the memory blocks have the correct content + for (size_t i = 0; i < NUM_PTRS; ++i) { + bool correct_contents = true; + for (size_t j = 0; j < NUM_BYTES; ++j) { + if (FLIP_POINTER(ptrs[i])[j] != j) { + correct_contents = false; + break; + } + } + mp_printf(&mp_plat_print, "%d %d\n", i, correct_contents); + } + + // free the memory blocks + for (size_t i = 0; i < NUM_PTRS; ++i) { + m_tracked_free(FLIP_POINTER(ptrs[i])); + } + + mp_printf(&mp_plat_print, "m_tracked_head = %p\n", MP_STATE_VM(m_tracked_head)); + } + // vstr { mp_printf(&mp_plat_print, "# vstr\n"); |
