tests: lib/heap: add test for heap listeners
This adds test to make heap listeners are working correctly. Signed-off-by: Daniel Leung <daniel.leung@intel.com>
This commit is contained in:
parent
f6e2705719
commit
be9779753f
2 changed files with 103 additions and 1 deletions
|
@ -1,3 +1,4 @@
|
|||
CONFIG_ZTEST=y
|
||||
CONFIG_SYS_HEAP_VALIDATE=y
|
||||
CONFIG_SYS_HEAP_RUNTIME_STATS=y
|
||||
CONFIG_SYS_HEAP_LISTENER=y
|
||||
|
|
|
@ -6,6 +6,8 @@
|
|||
#include <zephyr.h>
|
||||
#include <ztest.h>
|
||||
#include <sys/sys_heap.h>
|
||||
#include <sys/heap_listener.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
/* Guess at a value for heap size based on available memory on the
|
||||
* platform, with workarounds.
|
||||
|
@ -385,6 +387,104 @@ static void test_realloc(void)
|
|||
"Realloc should have moved %p", p2);
|
||||
}
|
||||
|
||||
#ifdef CONFIG_SYS_HEAP_LISTENER
|
||||
static struct sys_heap listener_heap;
|
||||
static uintptr_t listener_heap_id;
|
||||
static void *listener_mem;
|
||||
|
||||
static void heap_alloc_cb(uintptr_t heap_id, void *mem, size_t bytes)
|
||||
{
|
||||
listener_heap_id = heap_id;
|
||||
listener_mem = mem;
|
||||
|
||||
TC_PRINT("Heap 0x%" PRIxPTR ", alloc %p, size %u\n",
|
||||
heap_id, mem, (uint32_t)bytes);
|
||||
}
|
||||
|
||||
static void heap_free_cb(uintptr_t heap_id, void *mem, size_t bytes)
|
||||
{
|
||||
listener_heap_id = heap_id;
|
||||
listener_mem = mem;
|
||||
|
||||
TC_PRINT("Heap 0x%" PRIxPTR ", free %p, size %u\n",
|
||||
heap_id, mem, (uint32_t)bytes);
|
||||
}
|
||||
#endif /* CONFIG_SYS_HEAP_LISTENER */
|
||||
|
||||
static void test_heap_listeners(void)
|
||||
{
|
||||
#ifdef CONFIG_SYS_HEAP_LISTENER
|
||||
void *mem;
|
||||
|
||||
HEAP_LISTENER_ALLOC_DEFINE(heap_event_alloc,
|
||||
HEAP_ID_FROM_POINTER(&listener_heap),
|
||||
heap_alloc_cb);
|
||||
|
||||
HEAP_LISTENER_FREE_DEFINE(heap_event_free,
|
||||
HEAP_ID_FROM_POINTER(&listener_heap),
|
||||
heap_free_cb);
|
||||
|
||||
sys_heap_init(&listener_heap, heapmem, SMALL_HEAP_SZ);
|
||||
|
||||
/* Register listeners */
|
||||
heap_listener_register(&heap_event_alloc);
|
||||
heap_listener_register(&heap_event_free);
|
||||
|
||||
/*
|
||||
* Note that sys_heap may allocate a bigger size than requested
|
||||
* due to how sys_heap works. So checking whether the allocated
|
||||
* size equals to the requested size does not work.
|
||||
*/
|
||||
|
||||
/* Alloc/free operations without explicit alignment */
|
||||
mem = sys_heap_alloc(&listener_heap, 32U);
|
||||
|
||||
zassert_equal(listener_heap_id,
|
||||
HEAP_ID_FROM_POINTER(&listener_heap),
|
||||
"Heap ID mismatched: 0x%lx != %p", listener_heap_id,
|
||||
&listener_heap);
|
||||
zassert_equal(listener_mem, mem,
|
||||
"Heap allocated pointer mismatched: %p != %p",
|
||||
listener_mem, mem);
|
||||
|
||||
sys_heap_free(&listener_heap, mem);
|
||||
zassert_equal(listener_heap_id,
|
||||
HEAP_ID_FROM_POINTER(&listener_heap),
|
||||
"Heap ID mismatched: 0x%lx != %p", listener_heap_id,
|
||||
&listener_heap);
|
||||
zassert_equal(listener_mem, mem,
|
||||
"Heap allocated pointer mismatched: %p != %p",
|
||||
listener_mem, mem);
|
||||
|
||||
/* Alloc/free operations with explicit alignment */
|
||||
mem = sys_heap_aligned_alloc(&listener_heap, 128U, 32U);
|
||||
|
||||
zassert_equal(listener_heap_id,
|
||||
HEAP_ID_FROM_POINTER(&listener_heap),
|
||||
"Heap ID mismatched: 0x%lx != %p", listener_heap_id,
|
||||
&listener_heap);
|
||||
zassert_equal(listener_mem, mem,
|
||||
"Heap allocated pointer mismatched: %p != %p",
|
||||
listener_mem, mem);
|
||||
|
||||
sys_heap_free(&listener_heap, mem);
|
||||
zassert_equal(listener_heap_id,
|
||||
HEAP_ID_FROM_POINTER(&listener_heap),
|
||||
"Heap ID mismatched: 0x%lx != %p", listener_heap_id,
|
||||
&listener_heap);
|
||||
zassert_equal(listener_mem, mem,
|
||||
"Heap allocated pointer mismatched: %p != %p",
|
||||
listener_mem, mem);
|
||||
|
||||
/* Clean up */
|
||||
heap_listener_unregister(&heap_event_alloc);
|
||||
heap_listener_unregister(&heap_event_free);
|
||||
|
||||
#else /* CONFIG_SYS_HEAP_LISTENER */
|
||||
ztest_test_skip();
|
||||
#endif /* CONFIG_SYS_HEAP_LISTENER */
|
||||
}
|
||||
|
||||
void test_main(void)
|
||||
{
|
||||
ztest_test_suite(lib_heap_test,
|
||||
|
@ -392,7 +492,8 @@ void test_main(void)
|
|||
ztest_unit_test(test_small_heap),
|
||||
ztest_unit_test(test_fragmentation),
|
||||
ztest_unit_test(test_big_heap),
|
||||
ztest_unit_test(test_solo_free_header)
|
||||
ztest_unit_test(test_solo_free_header),
|
||||
ztest_unit_test(test_heap_listeners)
|
||||
);
|
||||
|
||||
ztest_run_test_suite(lib_heap_test);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue