summaryrefslogtreecommitdiff
path: root/ports/unix/coverage.c
diff options
context:
space:
mode:
authorJeff Epler <jepler@gmail.com>2021-09-15 10:54:48 -0500
committerDamien George <damien@micropython.org>2022-12-08 14:35:08 +1100
commitd75ff422972189232345108ecd2037ac148acda1 (patch)
treeaf6f447117c60e3d0416072442ccb98ee16c0a1b /ports/unix/coverage.c
parent9f434dd8de3c327c194d531c5496f603d6288d98 (diff)
unix/coverage: Add extra GC coverage test for ATB gap byte.
The assertion that is added here (to gc.c) fails when running this new test if ALLOC_TABLE_GAP_BYTE is set to 0. Signed-off-by: Jeff Epler <jepler@gmail.com> Signed-off-by: Damien George <damien@micropython.org>
Diffstat (limited to 'ports/unix/coverage.c')
-rw-r--r--ports/unix/coverage.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/ports/unix/coverage.c b/ports/unix/coverage.c
index 8b7fc7de6..f545674ce 100644
--- a/ports/unix/coverage.c
+++ b/ports/unix/coverage.c
@@ -1,4 +1,5 @@
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include "py/obj.h"
@@ -224,6 +225,42 @@ STATIC mp_obj_t extra_coverage(void) {
mp_printf(&mp_plat_print, "%p\n", gc_nbytes(NULL));
}
+ // GC initialisation and allocation stress test, to check the logic behind ALLOC_TABLE_GAP_BYTE
+ // (the following test should fail when ALLOC_TABLE_GAP_BYTE=0)
+ {
+ mp_printf(&mp_plat_print, "# GC part 2\n");
+
+ // check the GC is unlocked and save its state
+ assert(MP_STATE_THREAD(gc_lock_depth) == 0);
+ mp_state_mem_t mp_state_mem_orig = mp_state_ctx.mem;
+
+ // perform the test
+ unsigned heap_size = 64 * MICROPY_BYTES_PER_GC_BLOCK;
+ for (unsigned j = 0; j < 256 * MP_BYTES_PER_OBJ_WORD; ++j) {
+ char *heap = calloc(heap_size, 1);
+ gc_init(heap, heap + heap_size);
+
+ m_malloc(MICROPY_BYTES_PER_GC_BLOCK);
+ void *o = gc_alloc(MICROPY_BYTES_PER_GC_BLOCK, GC_ALLOC_FLAG_HAS_FINALISER);
+ ((mp_obj_base_t *)o)->type = NULL; // ensure type is cleared so GC doesn't look for finaliser
+ for (unsigned i = 0; i < heap_size / MICROPY_BYTES_PER_GC_BLOCK; ++i) {
+ void *p = m_malloc_maybe(MICROPY_BYTES_PER_GC_BLOCK);
+ if (!p) {
+ break;
+ }
+ *(void **)p = o;
+ o = p;
+ }
+ gc_collect();
+ free(heap);
+ heap_size += MICROPY_BYTES_PER_GC_BLOCK / 16;
+ }
+ mp_printf(&mp_plat_print, "pass\n");
+
+ // restore the GC state (the original heap)
+ mp_state_ctx.mem = mp_state_mem_orig;
+ }
+
// tracked allocation
{
#define NUM_PTRS (8)