lwmem: add available bytes check

This commit is contained in:
Tilen Majerle 2025-01-04 23:05:09 +01:00
parent 16532470a4
commit 551a6bd623
3 changed files with 58 additions and 0 deletions

View File

@ -12,6 +12,7 @@ else()
${CMAKE_CURRENT_LIST_DIR}/tests/lwmem_test.c
${CMAKE_CURRENT_LIST_DIR}/tests/lwmem_test_simple.c
${CMAKE_CURRENT_LIST_DIR}/tests/lwmem_test_region.c
${CMAKE_CURRENT_LIST_DIR}/tests/lwmem_test_available_mem.c
# win32 port
${CMAKE_CURRENT_LIST_DIR}/lwmem/src/system/lwmem_sys_win32.c

View File

@ -8,12 +8,15 @@ extern "C" void lwmem_test_run(void);
extern "C" void lwmem_test_simple_run(void);
extern "C" void lwmem_test_memory_structure(void);
extern "C" void lwmem_test_region(void);
extern "C" void lwmem_test_available_mem(void);
/* Setup manager */
static Lwmem::LwmemLight<1024> manager;
int
main(void) {
lwmem_test_available_mem();
return 0;
lwmem_test_region();
return 0;
#if LWMEM_CFG_FULL

View File

@ -0,0 +1,54 @@
#include <stdio.h>
#include "lwmem/lwmem.h"
#if LWMEM_CFG_ALIGN_NUM != 4
#error "Test shall run with LWMEM_CFG_ALIGN_NUM == 4"
#endif
/* Test assert information */
#define TEST_ASSERT(condition) \
do { \
if (!(condition)) { \
printf("Assert failed on the line %d\r\n", __LINE__); \
return; \
} \
} while (0)
void
lwmem_test_available_mem(void) {
static lwmem_t lwmem;
uint32_t data_buff[256 >> 2];
void *par1 = NULL, *par2 = NULL, *par3 = NULL;
const lwmem_region_t regions[] = {
{data_buff, sizeof(data_buff)},
{NULL, 0},
};
/* Setup the values */
lwmem_assignmem_ex(&lwmem, regions);
TEST_ASSERT(lwmem.mem_available_bytes == 248);
/* Allocate 10 bytes, that shall be properly aligned + header used*/
par1 = lwmem_malloc_ex(&lwmem, NULL, 10);
TEST_ASSERT(par1 != NULL);
TEST_ASSERT(lwmem.mem_available_bytes == (228));
par2 = lwmem_malloc_ex(&lwmem, NULL, 10);
TEST_ASSERT(par2 != NULL);
TEST_ASSERT(lwmem.mem_available_bytes == (208));
par3 = lwmem_malloc_ex(&lwmem, NULL, 10);
TEST_ASSERT(par3 != NULL);
TEST_ASSERT(lwmem.mem_available_bytes == (188));
/* Free the entry and check the new available bytes */
lwmem_free_s_ex(&lwmem, &par3);
TEST_ASSERT(par3 == NULL);
TEST_ASSERT(lwmem.mem_available_bytes == (208));
/* Free remaining 2 */
lwmem_free_s_ex(&lwmem, &par2);
TEST_ASSERT(par2 == NULL);
TEST_ASSERT(lwmem.mem_available_bytes == (228));
lwmem_free_s_ex(&lwmem, &par1);
TEST_ASSERT(par1 == NULL);
TEST_ASSERT(lwmem.mem_available_bytes == (248));
}