mirror of
https://github.com/MaJerle/lwmem.git
synced 2025-01-13 21:42:53 +08:00
lwmem: add available bytes check
This commit is contained in:
parent
16532470a4
commit
551a6bd623
@ -12,6 +12,7 @@ else()
|
|||||||
${CMAKE_CURRENT_LIST_DIR}/tests/lwmem_test.c
|
${CMAKE_CURRENT_LIST_DIR}/tests/lwmem_test.c
|
||||||
${CMAKE_CURRENT_LIST_DIR}/tests/lwmem_test_simple.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_region.c
|
||||||
|
${CMAKE_CURRENT_LIST_DIR}/tests/lwmem_test_available_mem.c
|
||||||
|
|
||||||
# win32 port
|
# win32 port
|
||||||
${CMAKE_CURRENT_LIST_DIR}/lwmem/src/system/lwmem_sys_win32.c
|
${CMAKE_CURRENT_LIST_DIR}/lwmem/src/system/lwmem_sys_win32.c
|
||||||
|
@ -8,12 +8,15 @@ extern "C" void lwmem_test_run(void);
|
|||||||
extern "C" void lwmem_test_simple_run(void);
|
extern "C" void lwmem_test_simple_run(void);
|
||||||
extern "C" void lwmem_test_memory_structure(void);
|
extern "C" void lwmem_test_memory_structure(void);
|
||||||
extern "C" void lwmem_test_region(void);
|
extern "C" void lwmem_test_region(void);
|
||||||
|
extern "C" void lwmem_test_available_mem(void);
|
||||||
|
|
||||||
/* Setup manager */
|
/* Setup manager */
|
||||||
static Lwmem::LwmemLight<1024> manager;
|
static Lwmem::LwmemLight<1024> manager;
|
||||||
|
|
||||||
int
|
int
|
||||||
main(void) {
|
main(void) {
|
||||||
|
lwmem_test_available_mem();
|
||||||
|
return 0;
|
||||||
lwmem_test_region();
|
lwmem_test_region();
|
||||||
return 0;
|
return 0;
|
||||||
#if LWMEM_CFG_FULL
|
#if LWMEM_CFG_FULL
|
||||||
|
54
tests/lwmem_test_available_mem.c
Normal file
54
tests/lwmem_test_available_mem.c
Normal 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));
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user