2020-04-04 06:39:25 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020 Intel Corporation
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
#ifndef ZEPHYR_INCLUDE_MEMPOOL_HEAP_H_
|
|
|
|
|
|
|
|
/* Compatibility implementation of a k_mem_pool backend in terms of a
|
|
|
|
* k_heap
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* The "ID" of a k_heap-based mempool is just the tuple of the data
|
|
|
|
* block pointer and the heap that allocated it
|
|
|
|
*/
|
|
|
|
struct k_mem_block_id {
|
|
|
|
void *data;
|
|
|
|
struct k_heap *heap;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Note the data pointer gets unioned with the same value stored in
|
|
|
|
* the ID field to save space.
|
|
|
|
*/
|
|
|
|
struct k_mem_block {
|
|
|
|
union {
|
|
|
|
void *data;
|
|
|
|
struct k_mem_block_id id;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
struct k_mem_pool {
|
|
|
|
struct k_heap *heap;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Sizing is a heuristic, as k_mem_pool made promises about layout
|
|
|
|
* that k_heap does not. We make space for the number of maximum
|
|
|
|
* objects defined, and include extra so there's enough metadata space
|
|
|
|
* available for the maximum number of minimum-sized objects to be
|
2019-10-24 07:43:16 +08:00
|
|
|
* stored: 8 bytes for each desired chunk header, and a 15 word block
|
|
|
|
* to reserve room for a "typical" set of bucket list heads and the heap
|
2019-09-26 13:59:35 +08:00
|
|
|
* footer(this size was picked more to conform with existing test
|
|
|
|
* expectations than any rigorous theory -- we have tests that rely on being
|
|
|
|
* able to allocate the blocks promised and ones that make assumptions about
|
2020-04-04 06:39:25 +08:00
|
|
|
* when memory will run out).
|
|
|
|
*/
|
|
|
|
#define Z_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
|
|
|
|
K_HEAP_DEFINE(poolheap_##name, \
|
|
|
|
((maxsz) * (nmax)) \
|
|
|
|
+ 8 * ((maxsz) * (nmax) / (minsz)) \
|
2019-10-24 07:43:16 +08:00
|
|
|
+ 15 * sizeof(void *)); \
|
2020-04-04 06:39:25 +08:00
|
|
|
struct k_mem_pool name = { \
|
|
|
|
.heap = &poolheap_##name \
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* ZEPHYR_INCLUDE_MEMPOOL_HEAP_H_ */
|