Skip to content

Commit

Permalink
Realloc Tests | Realloc map put bug fix (PR #51).
Browse files Browse the repository at this point in the history
Fixes #44
  • Loading branch information
mkirchner committed Feb 13, 2020
2 parents e5ebe36 + 551ef2a commit 3797674
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/gc.c
Expand Up @@ -445,7 +445,7 @@ void* gc_realloc(GarbageCollector* gc, void* p, size_t size)
} else {
// successful reallocation w/ copy
gc_allocation_map_remove(gc->allocs, p, true);
gc_allocation_map_put(gc->allocs, p, size, alloc->dtor);
gc_allocation_map_put(gc->allocs, q, size, alloc->dtor);
}
return q;
}
Expand Down
44 changes: 44 additions & 0 deletions test/test_gc.c
Expand Up @@ -353,6 +353,49 @@ static char* test_gc_static_allocation()
return NULL;
}

static char* test_gc_realloc()
{
GarbageCollector gc_;
void *bos = __builtin_frame_address(0);
gc_start(&gc_, bos);

/* manually allocate some memory */
{
void *unmarked = malloc(sizeof(char));
void *re_unmarked = gc_realloc(&gc_, unmarked, sizeof(char) * 2);
mu_assert(!re_unmarked, "GC should not realloc pointers unknown to it");
free(unmarked);
}

/* reallocing NULL pointer */
{
void *unmarked = NULL;
void *re_marked = gc_realloc(&gc_, unmarked, sizeof(char) * 42);
mu_assert(re_marked, "GC should not realloc NULL pointers");
Allocation* a = gc_allocation_map_get(gc_.allocs, re_marked);
mu_assert(a->size == 42, "Wrong allocation size");
}

/* realloc a valid pointer with same size to enforce same pointer is used*/
{
int** ints = gc_calloc(&gc_, 16, sizeof(int*));
ints = gc_realloc(&gc_, ints, 16*sizeof(int*));
Allocation* a = gc_allocation_map_get(gc_.allocs, ints);
mu_assert(a->size == 16*sizeof(int*), "Wrong allocation size");
}

/* realloc with size greater than before */
{
int** ints = gc_calloc(&gc_, 16, sizeof(int*));
ints = gc_realloc(&gc_, ints, 42*sizeof(int*));
Allocation* a = gc_allocation_map_get(gc_.allocs, ints);
mu_assert(a->size == 42*sizeof(int*), "Wrong allocation size");
}

gc_stop(&gc_);
return NULL;
}

static void _create_allocs(GarbageCollector* gc,
size_t count,
size_t size)
Expand Down Expand Up @@ -424,6 +467,7 @@ static char* test_suite()
mu_run_test(test_gc_allocation_map_cleanup);
mu_run_test(test_gc_static_allocation);
mu_run_test(test_primes);
mu_run_test(test_gc_realloc);
mu_run_test(test_gc_pause_resume);
mu_run_test(test_gc_strdup);
return 0;
Expand Down

0 comments on commit 3797674

Please sign in to comment.