forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests/cmsis_rtos_v2: Introduce tests to make use of mempool APIs
Some test programs to illustrate the usage of mempool APIs. Signed-off-by: Rajavardhan Gundi <[email protected]>
- Loading branch information
1 parent
e5c8488
commit 00ffaed
Showing
2 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Copyright (c) 2018 Intel Corporation | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <ztest.h> | ||
#include <kernel.h> | ||
#include <cmsis_os2.h> | ||
|
||
#define MAX_BLOCKS 10 | ||
#define TIMEOUT_TICKS 10 | ||
|
||
struct mem_block { | ||
int member1; | ||
int member2; | ||
}; | ||
|
||
static char __aligned(4) sample_mem[sizeof(struct mem_block) * MAX_BLOCKS]; | ||
static const osMemoryPoolAttr_t mp_attrs = { | ||
.name = "TestMempool", | ||
.attr_bits = 0, | ||
.cb_mem = NULL, | ||
.cb_size = 0, | ||
.mp_mem = sample_mem, | ||
.mp_size = sizeof(struct mem_block) * MAX_BLOCKS, | ||
}; | ||
|
||
/** | ||
* @brief Test memory pool allocation and free | ||
* | ||
* @see osMemoryPoolNew(), osMemoryPoolAlloc(), osMemoryPoolFree(), | ||
*/ | ||
void test_mempool(void) | ||
{ | ||
int i; | ||
osMemoryPoolId_t mp_id; | ||
const char *name; | ||
struct mem_block *addr_list[MAX_BLOCKS + 1]; | ||
osStatus_t status; | ||
|
||
mp_id = osMemoryPoolNew(MAX_BLOCKS, sizeof(struct mem_block), | ||
&mp_attrs); | ||
zassert_true(mp_id != NULL, "mempool creation failed"); | ||
|
||
name = osMemoryPoolGetName(mp_id); | ||
zassert_true(strcmp(mp_attrs.name, name) == 0, | ||
"Error getting mempool name"); | ||
|
||
zassert_equal(osMemoryPoolGetCapacity(mp_id), MAX_BLOCKS, | ||
"Something's wrong with osMemoryPoolGetCapacity!"); | ||
|
||
zassert_equal(osMemoryPoolGetBlockSize(mp_id), | ||
sizeof(struct mem_block), | ||
"Something's wrong with osMemoryPoolGetBlockSize!"); | ||
|
||
/* The memory pool should be completely available at this point */ | ||
zassert_equal(osMemoryPoolGetCount(mp_id), 0, | ||
"Something's wrong with osMemoryPoolGetCount!"); | ||
zassert_equal(osMemoryPoolGetSpace(mp_id), MAX_BLOCKS, | ||
"Something's wrong with osMemoryPoolGetSpace!"); | ||
|
||
for (i = 0; i < MAX_BLOCKS; i++) { | ||
addr_list[i] = (struct mem_block *)osMemoryPoolAlloc(mp_id, | ||
osWaitForever); | ||
zassert_true(addr_list[i] != NULL, "mempool allocation failed"); | ||
} | ||
|
||
/* The memory pool should be completely in use at this point */ | ||
zassert_equal(osMemoryPoolGetCount(mp_id), MAX_BLOCKS, | ||
"Something's wrong with osMemoryPoolGetCount!"); | ||
zassert_equal(osMemoryPoolGetSpace(mp_id), 0, | ||
"Something's wrong with osMemoryPoolGetSpace!"); | ||
|
||
/* All blocks in mempool are allocated, any more allocation | ||
* without free should fail | ||
*/ | ||
addr_list[i] = (struct mem_block *)osMemoryPoolAlloc(mp_id, | ||
TIMEOUT_TICKS); | ||
zassert_true(addr_list[i] == NULL, "allocation happened." | ||
" Something's wrong!"); | ||
|
||
for (i = 0; i < MAX_BLOCKS; i++) { | ||
status = osMemoryPoolFree(mp_id, addr_list[i]); | ||
zassert_true(status == osOK, "mempool free failed"); | ||
} | ||
|
||
status = osMemoryPoolDelete(mp_id); | ||
zassert_true(status == osOK, "mempool delete failure"); | ||
} |