Skip to content
Draft
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
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,4 @@ add_subdirectory(order)
add_subdirectory(log)
add_subdirectory(cxx)
add_subdirectory(traces)
add_subdirectory(mempool)
10 changes: 10 additions & 0 deletions test/mempool/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (C) 2025 Huawei Technologies Co., Ltd.
# SPDX-License-Identifier: 0BSD

add_executable(mempool_reuse_test mempool_reuse_test.c)
target_link_libraries(mempool_reuse_test PRIVATE dice.h dice.o)
add_test(NAME mempool-reuse-test COMMAND mempool_reuse_test)

add_executable(mempool_realloc_growth_test mempool_realloc_growth_test.c)
target_link_libraries(mempool_realloc_growth_test PRIVATE dice.h dice.o)
add_test(NAME mempool-realloc-growth-test COMMAND mempool_realloc_growth_test)
49 changes: 49 additions & 0 deletions test/mempool/mempool_realloc_growth_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (C) 2025 Huawei Technologies Co., Ltd.
* SPDX-License-Identifier: 0BSD
*/
#include <assert.h>
#include <stdint.h>
#include <string.h>

#include <dice/mempool.h>

static void
fill_pattern(uint8_t *buf, size_t len, uint8_t base)
{
for (size_t i = 0; i < len; ++i)
buf[i] = base + (uint8_t)i;
}

static void
check_pattern(const uint8_t *buf, size_t len, uint8_t base)
{
for (size_t i = 0; i < len; ++i)
assert(buf[i] == (uint8_t)(base + i));
}

int
main(void)
{
/* Seed the pool with an allocation that will be recycled. */
uint8_t *seed = mempool_alloc(16);
assert(seed != NULL);
fill_pattern(seed, 16, 0x10);
mempool_free(seed);

/* Allocate a larger block from the same bucket and populate it fully. */
const size_t original = 24;
uint8_t *block = mempool_alloc(original);
assert(block != NULL);
fill_pattern(block, original, 0x40);
check_pattern(block, original, 0x40);

/* Grow the block and verify the original payload is preserved. */
const size_t grown = 48;
uint8_t *bigger = mempool_realloc(block, grown);
assert(bigger != NULL);
check_pattern(bigger, original, 0x40);

mempool_free(bigger);
return 0;
}
30 changes: 30 additions & 0 deletions test/mempool/mempool_reuse_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (C) 2025 Huawei Technologies Co., Ltd.
* SPDX-License-Identifier: 0BSD
*/
#include <assert.h>
#include <stdint.h>
#include <string.h>

#include <dice/mempool.h>

int
main(void)
{
const size_t size = 64;
uint8_t *first = mempool_alloc(size);
assert(first != NULL);

memset(first, 0xAB, size);
mempool_free(first);

uint8_t *second = mempool_alloc(size);
assert(second != NULL);
assert(second == first);

for (size_t i = 0; i < size; ++i)
assert(second[i] == 0xAB);

mempool_free(second);
return 0;
}
Loading