Add isc_mem_reget() function to realloc isc_mem_get allocations

The isc_mem_get() and isc_mem_put() functions are leaving the memory
allocation size tracking to the users of the API, while
isc_mem_allocate() and isc_mem_free() would track the sizes internally.
This allowed to have isc_mem_rellocate() to manipulate the memory
allocations by the later set, but not the former set of the functions.

This commit introduces isc_mem_reget(ctx, old_ptr, old_size, new_size)
function that operates on the memory allocations with external size
tracking completing the API.
This commit is contained in:
Ondřej Surý
2021-09-22 18:48:03 +02:00
committed by Evan Hunt
parent c5c6a76e8c
commit aeb3d1cab3
3 changed files with 92 additions and 11 deletions

View File

@@ -220,6 +220,40 @@ isc_mem_inuse_test(void **state) {
isc_mem_destroy(&mctx2);
}
#define REGET_INIT_SIZE 1024
#define REGET_GROW_SIZE 2048
#define REGET_SHRINK_SIZE 512
static void
isc_mem_reget_test(void **state) {
uint8_t *data = isc_mem_get(test_mctx, REGET_INIT_SIZE);
UNUSED(state);
for (size_t i = 0; i < REGET_INIT_SIZE; i++) {
data[i] = i % UINT8_MAX;
}
data = isc_mem_reget(test_mctx, data, REGET_INIT_SIZE, REGET_GROW_SIZE);
for (size_t i = 0; i < REGET_INIT_SIZE; i++) {
assert_int_equal(data[i], i % UINT8_MAX);
}
for (size_t i = REGET_GROW_SIZE; i > 0; i--) {
data[i - 1] = i % UINT8_MAX;
}
data = isc_mem_reget(test_mctx, data, REGET_GROW_SIZE,
REGET_SHRINK_SIZE);
for (size_t i = REGET_SHRINK_SIZE; i > 0; i--) {
assert_int_equal(data[i - 1], i % UINT8_MAX);
}
isc_mem_put(test_mctx, data, REGET_SHRINK_SIZE);
}
#if ISC_MEM_TRACKLINES
/* test mem with no flags */
@@ -436,6 +470,8 @@ main(void) {
_teardown),
cmocka_unit_test_setup_teardown(isc_mem_inuse_test, _setup,
_teardown),
cmocka_unit_test_setup_teardown(isc_mem_reget_test, _setup,
_teardown),
#if !defined(__SANITIZE_THREAD__)
cmocka_unit_test_setup_teardown(isc_mem_benchmark, _setup,