Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions core/shared/mem-alloc/ems/ems_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,12 @@ gc_realloc_vo_internal(void *vheap, void *ptr, gc_size_t size, const char *file,
hmu_mark_pinuse(hmu_next);
}
UNLOCK_HEAP(heap);
heap->total_free_size -= (tot_size - tot_size_old);
if ((heap->current_size - heap->total_free_size)
> heap->highmark_size) {
heap->highmark_size =
heap->current_size - heap->total_free_size;
}
return obj_old;
}
}
Expand Down
51 changes: 51 additions & 0 deletions tests/unit/mem-alloc/mem_alloc_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -787,3 +787,54 @@ test_mixed_alloc_until_oom(void **state)

mem_allocator_destroy(allocator);
}

/* Test: Memory usage statistics for in-place reallocations */
static void
test_memory_usage_statistics_in_place_realloc(void **state)
{
mem_allocator_t allocator;
char heap_buf[64 * 1024];
void *ptr;
void *realloc_ptr;
mem_alloc_info_t info;

allocator = mem_allocator_create(heap_buf, sizeof(heap_buf));
assert_non_null(allocator);

/* Initial memory usage statistics */
mem_allocator_get_alloc_info(allocator, &info);
assert_int_equal(info.total_size - info.total_free_size, 0);
assert_int_equal(info.highmark_size, 0);

/* Allocation */
ptr = mem_allocator_malloc(allocator, 128);
assert_non_null(ptr);

/* Memory usage statistics after allocation */
mem_allocator_get_alloc_info(allocator, &info);
assert_int_equal(info.total_size - info.total_free_size,
GC_ALIGN_8(128 + OBJ_EXTRA_SIZE));
assert_int_equal(info.highmark_size, GC_ALIGN_8(128 + OBJ_EXTRA_SIZE));

/* Reallocation */
realloc_ptr = mem_allocator_realloc(allocator, ptr, 256);
assert_non_null(realloc_ptr);
assert_ptr_equal(realloc_ptr, ptr); /* Should be in-place realloc */
ptr = realloc_ptr;

/* Memory usage statistics after reallocation */
mem_allocator_get_alloc_info(allocator, &info);
assert_int_equal(info.total_size - info.total_free_size,
GC_ALIGN_8(256 + OBJ_EXTRA_SIZE));
assert_int_equal(info.highmark_size, GC_ALIGN_8(256 + OBJ_EXTRA_SIZE));

/* Free */
mem_allocator_free(allocator, ptr);

/* Memory usage statistics after free */
mem_allocator_get_alloc_info(allocator, &info);
assert_int_equal(info.total_size - info.total_free_size, 0);
assert_int_equal(info.highmark_size, GC_ALIGN_8(256 + OBJ_EXTRA_SIZE));

mem_allocator_destroy(allocator);
}
1 change: 1 addition & 0 deletions tests/unit/mem-alloc/test_runner.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ main(void)
cmocka_unit_test(test_normal_alloc_until_oom),
cmocka_unit_test(test_aligned_alloc_until_oom),
cmocka_unit_test(test_mixed_alloc_until_oom),
cmocka_unit_test(test_memory_usage_statistics_in_place_realloc),
};

return cmocka_run_group_tests(tests, NULL, NULL);
Expand Down
Loading