lib: Remove sys_mem_pool implementation
This has been replaced by sys_heap now and all dependencies are gone. Remove. Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
This commit is contained in:
parent
e7436f7c55
commit
0c15627cc1
|
@ -265,24 +265,6 @@ used as provided in ``app_macro_support.h``:
|
|||
|
||||
FOR_EACH(K_APPMEM_PARTITION_DEFINE, part0, part1, part2);
|
||||
|
||||
There are some kernel objects which are defined by macros and take an argument
|
||||
for a destination section. A good example of these are sys_mem_pools, which
|
||||
are heap objects. The destination section name for an automatic partition
|
||||
can be obtained with :c:macro:`K_APP_DMEM_SECTION()` and
|
||||
:c:macro:`K_APP_BMEM_SECTION()` respectively for initialized data and BSS:
|
||||
|
||||
.. code-block:: c
|
||||
|
||||
/* Declare automatic memory partition foo_partition */
|
||||
K_APPMEM_PARTITION_DEFINE(foo_partition);
|
||||
|
||||
/* Section argument for the destination section obtained via
|
||||
* K_APP_DMEM_SECTION()
|
||||
*/
|
||||
SYS_MEM_POOL_DEFINE(foo_pool, NULL, BLK_SIZE_MIN, BLK_SIZE_MAX,
|
||||
BLK_NUM_MAX, BLK_ALIGN,
|
||||
K_APP_DMEM_SECTION(foo_partition));
|
||||
|
||||
Automatic Partitions for Static Library Globals
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
|
@ -315,7 +297,7 @@ There are a few memory partitions which are pre-defined by the system:
|
|||
- ``z_malloc_partition`` - This partition contains the system-wide pool of
|
||||
memory used by libc malloc(). Due to possible starvation issues, it is
|
||||
not recommended to draw heap memory from a global pool, instead
|
||||
it is better to define various sys_mem_pool objects and assign them
|
||||
it is better to define various sys_heap objects and assign them
|
||||
to specific memory domains.
|
||||
|
||||
- ``z_libc_partition`` - Contains globals required by the C library and runtime.
|
||||
|
|
|
@ -25,7 +25,6 @@
|
|||
#include <sys/slist.h>
|
||||
#include <sys/sflist.h>
|
||||
#include <sys/util.h>
|
||||
#include <sys/mempool_base.h>
|
||||
#include <kernel_structs.h>
|
||||
#include <mempool_heap.h>
|
||||
#include <kernel_version.h>
|
||||
|
|
|
@ -1,115 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2018 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef ZEPHYR_INCLUDE_SYS_MEMPOOL_H_
|
||||
#define ZEPHYR_INCLUDE_SYS_MEMPOOL_H_
|
||||
|
||||
#include <kernel.h>
|
||||
#include <sys/mempool_base.h>
|
||||
#include <sys/mutex.h>
|
||||
|
||||
struct sys_mem_pool {
|
||||
struct sys_mem_pool_base base;
|
||||
struct sys_mutex mutex;
|
||||
};
|
||||
|
||||
struct sys_mem_pool_block {
|
||||
struct sys_mem_pool *pool;
|
||||
uint32_t level : 4;
|
||||
uint32_t block : 28;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Statically define system memory pool
|
||||
*
|
||||
* The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
|
||||
* long. The memory pool allows blocks to be repeatedly partitioned into
|
||||
* quarters, down to blocks of @a min_size bytes long. The buffer is aligned
|
||||
* to a @a align -byte boundary.
|
||||
*
|
||||
* If the pool is to be accessed outside the module where it is defined, it
|
||||
* can be declared via
|
||||
*
|
||||
* @code extern struct sys_mem_pool <name>; @endcode
|
||||
*
|
||||
* This pool will not be in an initialized state. You will still need to
|
||||
* run sys_mem_pool_init() on it before using any other APIs.
|
||||
*
|
||||
* @param name Name of the memory pool.
|
||||
* @param ignored ignored, any value
|
||||
* @param minsz Size of the smallest blocks in the pool (in bytes).
|
||||
* @param maxsz Size of the largest blocks in the pool (in bytes).
|
||||
* @param nmax Number of maximum sized blocks in the pool.
|
||||
* @param align Alignment of the pool's buffer (power of 2).
|
||||
* @param section Destination binary section for pool data
|
||||
*/
|
||||
#define SYS_MEM_POOL_DEFINE(name, ignored, minsz, maxsz, nmax, align, section) \
|
||||
char __aligned(WB_UP(align)) Z_GENERIC_SECTION(section) \
|
||||
_mpool_buf_##name[WB_UP(maxsz) * nmax \
|
||||
+ _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
|
||||
struct sys_mem_pool_lvl Z_GENERIC_SECTION(section) \
|
||||
_mpool_lvls_##name[Z_MPOOL_LVLS(maxsz, minsz)]; \
|
||||
Z_GENERIC_SECTION(section) struct sys_mem_pool name = { \
|
||||
.base = { \
|
||||
.buf = _mpool_buf_##name, \
|
||||
.max_sz = WB_UP(maxsz), \
|
||||
.n_max = nmax, \
|
||||
.n_levels = Z_MPOOL_LVLS(maxsz, minsz), \
|
||||
.levels = _mpool_lvls_##name, \
|
||||
.flags = SYS_MEM_POOL_USER \
|
||||
} \
|
||||
}; \
|
||||
BUILD_ASSERT(WB_UP(maxsz) >= _MPOOL_MINBLK)
|
||||
|
||||
/**
|
||||
* @brief Initialize a memory pool
|
||||
*
|
||||
* This is intended to complete initialization of memory pools that have been
|
||||
* declared with SYS_MEM_POOL_DEFINE().
|
||||
*
|
||||
* @param p Memory pool to initialize
|
||||
*/
|
||||
static inline void sys_mem_pool_init(struct sys_mem_pool *p)
|
||||
{
|
||||
z_sys_mem_pool_base_init(&p->base);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Allocate a block of memory
|
||||
*
|
||||
* Allocate a chunk of memory from a memory pool. This cannot be called from
|
||||
* interrupt context.
|
||||
*
|
||||
* @param p Address of the memory pool
|
||||
* @param size Requested size of the memory block
|
||||
* @return A pointer to the requested memory, or NULL if none is available
|
||||
*/
|
||||
void *sys_mem_pool_alloc(struct sys_mem_pool *p, size_t size);
|
||||
|
||||
/**
|
||||
* @brief Free memory allocated from a memory pool
|
||||
*
|
||||
* Free memory previously allocated by sys_mem_pool_alloc().
|
||||
* It is safe to pass NULL to this function, in which case it is a no-op.
|
||||
*
|
||||
* @param ptr Pointer to previously allocated memory
|
||||
*/
|
||||
void sys_mem_pool_free(void *ptr);
|
||||
|
||||
/**
|
||||
* @brief Try to perform in-place expansion of memory allocated from a pool
|
||||
*
|
||||
* Return 0 if memory previously allocated by sys_mem_pool_alloc()
|
||||
* can accommodate a new size, otherwise return the size of data that
|
||||
* needs to be copied over to new memory.
|
||||
*
|
||||
* @param ptr Pointer to previously allocated memory
|
||||
* @param new_size New size requested for the memory block
|
||||
* @return A 0 if OK, or size of data to copy elsewhere
|
||||
*/
|
||||
size_t sys_mem_pool_try_expand_inplace(void *ptr, size_t new_size);
|
||||
|
||||
#endif
|
|
@ -1,106 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2018 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef ZEPHYR_INCLUDE_SYS_MEMPOOL_BASE_H_
|
||||
#define ZEPHYR_INCLUDE_SYS_MEMPOOL_BASE_H_
|
||||
|
||||
#include <zephyr/types.h>
|
||||
#include <stddef.h>
|
||||
|
||||
/*
|
||||
* Definitions and macros used by both the IRQ-safe k_mem_pool and user-mode
|
||||
* compatible sys_mem_pool implementations
|
||||
*/
|
||||
|
||||
struct sys_mem_pool_lvl {
|
||||
union {
|
||||
uint32_t *bits_p;
|
||||
uint32_t bits[sizeof(uint32_t *)/4];
|
||||
};
|
||||
sys_dlist_t free_list;
|
||||
};
|
||||
|
||||
#define SYS_MEM_POOL_KERNEL BIT(0)
|
||||
#define SYS_MEM_POOL_USER BIT(1)
|
||||
|
||||
struct sys_mem_pool_base {
|
||||
void *buf;
|
||||
size_t max_sz;
|
||||
uint16_t n_max;
|
||||
uint8_t n_levels;
|
||||
int8_t max_inline_level;
|
||||
struct sys_mem_pool_lvl *levels;
|
||||
uint8_t flags;
|
||||
};
|
||||
|
||||
#define _MPOOL_MINBLK sizeof(sys_dnode_t)
|
||||
|
||||
#define Z_MPOOL_HAVE_LVL(maxsz, minsz, l) \
|
||||
(((maxsz) >> (2*(l))) >= MAX((minsz), _MPOOL_MINBLK) ? 1 : 0)
|
||||
|
||||
#define Z_MPOOL_LVLS(maxsz, minsz) \
|
||||
(Z_MPOOL_HAVE_LVL((maxsz), (minsz), 0) + \
|
||||
Z_MPOOL_HAVE_LVL((maxsz), (minsz), 1) + \
|
||||
Z_MPOOL_HAVE_LVL((maxsz), (minsz), 2) + \
|
||||
Z_MPOOL_HAVE_LVL((maxsz), (minsz), 3) + \
|
||||
Z_MPOOL_HAVE_LVL((maxsz), (minsz), 4) + \
|
||||
Z_MPOOL_HAVE_LVL((maxsz), (minsz), 5) + \
|
||||
Z_MPOOL_HAVE_LVL((maxsz), (minsz), 6) + \
|
||||
Z_MPOOL_HAVE_LVL((maxsz), (minsz), 7) + \
|
||||
Z_MPOOL_HAVE_LVL((maxsz), (minsz), 8) + \
|
||||
Z_MPOOL_HAVE_LVL((maxsz), (minsz), 9) + \
|
||||
Z_MPOOL_HAVE_LVL((maxsz), (minsz), 10) + \
|
||||
Z_MPOOL_HAVE_LVL((maxsz), (minsz), 11) + \
|
||||
Z_MPOOL_HAVE_LVL((maxsz), (minsz), 12) + \
|
||||
Z_MPOOL_HAVE_LVL((maxsz), (minsz), 13) + \
|
||||
Z_MPOOL_HAVE_LVL((maxsz), (minsz), 14) + \
|
||||
Z_MPOOL_HAVE_LVL((maxsz), (minsz), 15))
|
||||
|
||||
/* Rounds the needed bits up to integer multiples of uint32_t */
|
||||
#define Z_MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) \
|
||||
((((n_max) << (2*(l))) + 31) / 32)
|
||||
|
||||
/* One or two 32-bit words gets stored free unioned with the pointer,
|
||||
* otherwise the calculated unclamped value
|
||||
*/
|
||||
#define Z_MPOOL_LBIT_WORDS(n_max, l) \
|
||||
(Z_MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) <= sizeof(uint32_t *)/4 ? 0 \
|
||||
: Z_MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l))
|
||||
|
||||
/* How many bytes for the bitfields of a single level? */
|
||||
#define Z_MPOOL_LBIT_BYTES(maxsz, minsz, l, n_max) \
|
||||
(Z_MPOOL_HAVE_LVL((maxsz), (minsz), (l)) ? \
|
||||
4 * Z_MPOOL_LBIT_WORDS((n_max), l) : 0)
|
||||
|
||||
/* Size of the bitmap array that follows the buffer in allocated memory */
|
||||
#define _MPOOL_BITS_SIZE(maxsz, minsz, n_max) \
|
||||
(Z_MPOOL_LBIT_BYTES(maxsz, minsz, 0, n_max) + \
|
||||
Z_MPOOL_LBIT_BYTES(maxsz, minsz, 1, n_max) + \
|
||||
Z_MPOOL_LBIT_BYTES(maxsz, minsz, 2, n_max) + \
|
||||
Z_MPOOL_LBIT_BYTES(maxsz, minsz, 3, n_max) + \
|
||||
Z_MPOOL_LBIT_BYTES(maxsz, minsz, 4, n_max) + \
|
||||
Z_MPOOL_LBIT_BYTES(maxsz, minsz, 5, n_max) + \
|
||||
Z_MPOOL_LBIT_BYTES(maxsz, minsz, 6, n_max) + \
|
||||
Z_MPOOL_LBIT_BYTES(maxsz, minsz, 7, n_max) + \
|
||||
Z_MPOOL_LBIT_BYTES(maxsz, minsz, 8, n_max) + \
|
||||
Z_MPOOL_LBIT_BYTES(maxsz, minsz, 9, n_max) + \
|
||||
Z_MPOOL_LBIT_BYTES(maxsz, minsz, 10, n_max) + \
|
||||
Z_MPOOL_LBIT_BYTES(maxsz, minsz, 11, n_max) + \
|
||||
Z_MPOOL_LBIT_BYTES(maxsz, minsz, 12, n_max) + \
|
||||
Z_MPOOL_LBIT_BYTES(maxsz, minsz, 13, n_max) + \
|
||||
Z_MPOOL_LBIT_BYTES(maxsz, minsz, 14, n_max) + \
|
||||
Z_MPOOL_LBIT_BYTES(maxsz, minsz, 15, n_max))
|
||||
|
||||
|
||||
void z_sys_mem_pool_base_init(struct sys_mem_pool_base *p);
|
||||
|
||||
int z_sys_mem_pool_block_alloc(struct sys_mem_pool_base *p, size_t size,
|
||||
uint32_t *level_p, uint32_t *block_p, void **data_p);
|
||||
|
||||
void z_sys_mem_pool_block_free(struct sys_mem_pool_base *p, uint32_t level,
|
||||
uint32_t block);
|
||||
|
||||
#endif /* ZEPHYR_INCLUDE_SYS_MEMPOOL_BASE_H_ */
|
|
@ -94,9 +94,8 @@ config MINIMAL_LIBC_MALLOC_ARENA_SIZE
|
|||
default 0
|
||||
depends on MINIMAL_LIBC_MALLOC
|
||||
help
|
||||
Indicate the size of the memory arena used for minimal libc's
|
||||
malloc() implementation. This size value must be compatible with
|
||||
a sys_mem_pool definition with nmax of 1 and minsz of 16.
|
||||
Indicate the size in bytes of the memory arena used for
|
||||
minimal libc's malloc() implementation.
|
||||
|
||||
config MINIMAL_LIBC_CALLOC
|
||||
bool "Enable minimal libc trivial calloc implementation"
|
||||
|
|
|
@ -9,9 +9,11 @@
|
|||
#include <init.h>
|
||||
#include <errno.h>
|
||||
#include <sys/math_extras.h>
|
||||
#include <sys/mempool.h>
|
||||
#include <string.h>
|
||||
#include <app_memory/app_memdomain.h>
|
||||
#include <sys/sys_heap.h>
|
||||
|
||||
#define LOG_LEVEL CONFIG_KERNEL_LOG_LEVEL
|
||||
#include <logging/log.h>
|
||||
LOG_MODULE_DECLARE(os, CONFIG_KERNEL_LOG_LEVEL);
|
||||
|
||||
|
|
|
@ -11,7 +11,6 @@ zephyr_sources(
|
|||
dec.c
|
||||
fdtable.c
|
||||
hex.c
|
||||
mempool.c
|
||||
notify.c
|
||||
printk.c
|
||||
onoff.c
|
||||
|
|
404
lib/os/mempool.c
404
lib/os/mempool.c
|
@ -1,404 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2018 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <kernel.h>
|
||||
#include <string.h>
|
||||
#include <sys/__assert.h>
|
||||
#include <sys/mempool_base.h>
|
||||
#include <sys/mempool.h>
|
||||
#include <sys/check.h>
|
||||
|
||||
#ifdef CONFIG_MISRA_SANE
|
||||
#define LVL_ARRAY_SZ(n) (8 * sizeof(void *) / 2)
|
||||
#else
|
||||
#define LVL_ARRAY_SZ(n) (n)
|
||||
#endif
|
||||
|
||||
static void *block_ptr(struct sys_mem_pool_base *p, size_t lsz, int block)
|
||||
{
|
||||
return (uint8_t *)p->buf + lsz * block;
|
||||
}
|
||||
|
||||
static int block_num(struct sys_mem_pool_base *p, void *block, int sz)
|
||||
{
|
||||
return ((uint8_t *)block - (uint8_t *)p->buf) / sz;
|
||||
}
|
||||
|
||||
/* Places a 32 bit output pointer in word, and an integer bit index
|
||||
* within that word as the return value
|
||||
*/
|
||||
static int get_bit_ptr(struct sys_mem_pool_base *p, int level, int bn,
|
||||
uint32_t **word)
|
||||
{
|
||||
uint32_t *bitarray = level <= p->max_inline_level ?
|
||||
p->levels[level].bits : p->levels[level].bits_p;
|
||||
|
||||
*word = &bitarray[bn / 32];
|
||||
|
||||
return bn & 0x1f;
|
||||
}
|
||||
|
||||
static void set_alloc_bit(struct sys_mem_pool_base *p, int level, int bn)
|
||||
{
|
||||
uint32_t *word;
|
||||
int bit = get_bit_ptr(p, level, bn, &word);
|
||||
|
||||
*word |= (1<<bit);
|
||||
}
|
||||
|
||||
static void clear_alloc_bit(struct sys_mem_pool_base *p, int level, int bn)
|
||||
{
|
||||
uint32_t *word;
|
||||
int bit = get_bit_ptr(p, level, bn, &word);
|
||||
|
||||
*word &= ~(1<<bit);
|
||||
}
|
||||
|
||||
#ifdef CONFIG_ASSERT
|
||||
static inline bool alloc_bit_is_set(struct sys_mem_pool_base *p,
|
||||
int level, int bn)
|
||||
{
|
||||
uint32_t *word;
|
||||
int bit = get_bit_ptr(p, level, bn, &word);
|
||||
|
||||
return (*word >> bit) & 1U;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Returns all four of the allocated bits for the specified blocks
|
||||
* "partners" in the bottom 4 bits of the return value
|
||||
*/
|
||||
static int partner_alloc_bits(struct sys_mem_pool_base *p, int level, int bn)
|
||||
{
|
||||
uint32_t *word;
|
||||
int bit = get_bit_ptr(p, level, bn, &word);
|
||||
|
||||
return (*word >> (4*(bit / 4))) & 0xfU;
|
||||
}
|
||||
|
||||
void z_sys_mem_pool_base_init(struct sys_mem_pool_base *p)
|
||||
{
|
||||
int i;
|
||||
size_t buflen = p->n_max * p->max_sz, sz = p->max_sz;
|
||||
uint32_t *bits = (uint32_t *)((uint8_t *)p->buf + buflen);
|
||||
|
||||
p->max_inline_level = -1;
|
||||
|
||||
for (i = 0; i < p->n_levels; i++) {
|
||||
size_t nblocks = buflen / sz;
|
||||
|
||||
sys_dlist_init(&p->levels[i].free_list);
|
||||
|
||||
if (nblocks <= sizeof(p->levels[i].bits)*8) {
|
||||
p->max_inline_level = i;
|
||||
} else {
|
||||
p->levels[i].bits_p = bits;
|
||||
bits += (nblocks + 31)/32;
|
||||
}
|
||||
|
||||
sz = WB_DN(sz / 4);
|
||||
}
|
||||
|
||||
for (i = 0; i < p->n_max; i++) {
|
||||
void *block = block_ptr(p, p->max_sz, i);
|
||||
|
||||
sys_dlist_append(&p->levels[0].free_list, block);
|
||||
}
|
||||
}
|
||||
|
||||
/* A note on synchronization:
|
||||
*
|
||||
* For k_mem_pools which are interrupt safe, all manipulation of the actual
|
||||
* pool data happens in one of alloc_block()/free_block() or break_block().
|
||||
* All of these transition between a state where the caller "holds" a block
|
||||
* pointer that is marked used in the store and one where she doesn't (or else
|
||||
* they will fail, e.g. if there isn't a free block). So that is the basic
|
||||
* operation that needs synchronization, which we can do piecewise as needed in
|
||||
* small one-block chunks to preserve latency. At most (in free_block) a
|
||||
* single locked operation consists of four bit sets and dlist removals. If the
|
||||
* overall allocation operation fails, we just free the block we have (putting
|
||||
* a block back into the list cannot fail) and return failure.
|
||||
*
|
||||
* For user mode compatible sys_mem_pool pools, a semaphore is used at the API
|
||||
* level since using that does not introduce latency issues like locking
|
||||
* interrupts does.
|
||||
*/
|
||||
|
||||
static inline int pool_irq_lock(struct sys_mem_pool_base *p)
|
||||
{
|
||||
if (p->flags & SYS_MEM_POOL_KERNEL) {
|
||||
return irq_lock();
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static inline void pool_irq_unlock(struct sys_mem_pool_base *p, int key)
|
||||
{
|
||||
if (p->flags & SYS_MEM_POOL_KERNEL) {
|
||||
irq_unlock(key);
|
||||
}
|
||||
}
|
||||
|
||||
static void *block_alloc(struct sys_mem_pool_base *p, int l, size_t lsz)
|
||||
{
|
||||
sys_dnode_t *block;
|
||||
|
||||
block = sys_dlist_get(&p->levels[l].free_list);
|
||||
if (block != NULL) {
|
||||
set_alloc_bit(p, l, block_num(p, block, lsz));
|
||||
}
|
||||
return block;
|
||||
}
|
||||
|
||||
/* Called with lock held */
|
||||
static unsigned int bfree_recombine(struct sys_mem_pool_base *p, int level,
|
||||
size_t *lsizes, int bn, unsigned int key)
|
||||
{
|
||||
while (level >= 0) {
|
||||
int i, lsz = lsizes[level];
|
||||
void *block = block_ptr(p, lsz, bn);
|
||||
|
||||
/* Detect common double-free occurrences */
|
||||
__ASSERT(alloc_bit_is_set(p, level, bn),
|
||||
"mempool double-free detected at %p", block);
|
||||
|
||||
/* Put it back */
|
||||
clear_alloc_bit(p, level, bn);
|
||||
sys_dlist_append(&p->levels[level].free_list, block);
|
||||
|
||||
/* Relax the lock (might result in it being taken, which is OK!) */
|
||||
pool_irq_unlock(p, key);
|
||||
key = pool_irq_lock(p);
|
||||
|
||||
/* Check if we can recombine its superblock, and repeat */
|
||||
if (level == 0 || partner_alloc_bits(p, level, bn) != 0) {
|
||||
return key;
|
||||
}
|
||||
|
||||
for (i = 0; i < 4; i++) {
|
||||
int b = (bn & ~3) + i;
|
||||
|
||||
sys_dlist_remove(block_ptr(p, lsz, b));
|
||||
}
|
||||
|
||||
/* Free the larger block */
|
||||
level = level - 1;
|
||||
bn = bn / 4;
|
||||
}
|
||||
__ASSERT(0, "out of levels");
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void block_free(struct sys_mem_pool_base *p, int level,
|
||||
size_t *lsizes, int bn)
|
||||
{
|
||||
unsigned int key = pool_irq_lock(p);
|
||||
|
||||
key = bfree_recombine(p, level, lsizes, bn, key);
|
||||
pool_irq_unlock(p, key);
|
||||
}
|
||||
|
||||
/* Takes a block of a given level, splits it into four blocks of the
|
||||
* next smaller level, puts three into the free list as in
|
||||
* block_free() but without the need to check adjacent bits or
|
||||
* recombine, and returns the remaining smaller block.
|
||||
*/
|
||||
static void *block_break(struct sys_mem_pool_base *p, void *block, int l,
|
||||
size_t *lsizes)
|
||||
{
|
||||
int i, bn;
|
||||
|
||||
bn = block_num(p, block, lsizes[l]);
|
||||
set_alloc_bit(p, l + 1, 4*bn);
|
||||
|
||||
for (i = 1; i < 4; i++) {
|
||||
int lsz = lsizes[l + 1];
|
||||
void *block2 = (lsz * i) + (char *)block;
|
||||
|
||||
sys_dlist_append(&p->levels[l + 1].free_list, block2);
|
||||
}
|
||||
|
||||
return block;
|
||||
}
|
||||
|
||||
int z_sys_mem_pool_block_alloc(struct sys_mem_pool_base *p, size_t size,
|
||||
uint32_t *level_p, uint32_t *block_p, void **data_p)
|
||||
{
|
||||
int i, from_l, alloc_l = -1;
|
||||
unsigned int key;
|
||||
void *data = NULL;
|
||||
size_t lsizes[LVL_ARRAY_SZ(p->n_levels)];
|
||||
|
||||
/* Walk down through levels, finding the one from which we
|
||||
* want to allocate and the smallest one with a free entry
|
||||
* from which we can split an allocation if needed. Along the
|
||||
* way, we populate an array of sizes for each level so we
|
||||
* don't need to waste RAM storing it.
|
||||
*/
|
||||
lsizes[0] = p->max_sz;
|
||||
for (i = 0; i < p->n_levels; i++) {
|
||||
if (i > 0) {
|
||||
lsizes[i] = WB_DN(lsizes[i-1] / 4);
|
||||
}
|
||||
|
||||
if (lsizes[i] < size) {
|
||||
break;
|
||||
}
|
||||
|
||||
alloc_l = i;
|
||||
}
|
||||
|
||||
if (alloc_l < 0) {
|
||||
*data_p = NULL;
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
/* Now walk back down the levels (i.e. toward bigger sizes)
|
||||
* looking for an available block. Start at the smallest
|
||||
* enclosing block found above (note that because that loop
|
||||
* was done without synchronization, it may no longer be
|
||||
* available!) as a useful optimization. Note that the
|
||||
* removal of the block from the list and the re-addition of
|
||||
* its the three unused children needs to be performed
|
||||
* atomically, otherwise we open up a situation where we can
|
||||
* "steal" the top level block of the whole heap, causing a
|
||||
* spurious -ENOMEM.
|
||||
*/
|
||||
key = pool_irq_lock(p);
|
||||
for (i = alloc_l; i >= 0; i--) {
|
||||
data = block_alloc(p, i, lsizes[i]);
|
||||
|
||||
/* Found one. Iteratively break it down to the size
|
||||
* we need. Note that we relax the lock to allow a
|
||||
* pending interrupt to fire so we don't hurt latency
|
||||
* by locking the full loop.
|
||||
*/
|
||||
if (data != NULL) {
|
||||
for (from_l = i; from_l < alloc_l; from_l++) {
|
||||
data = block_break(p, data, from_l, lsizes);
|
||||
pool_irq_unlock(p, key);
|
||||
key = pool_irq_lock(p);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
pool_irq_unlock(p, key);
|
||||
|
||||
*data_p = data;
|
||||
|
||||
if (data == NULL) {
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
*level_p = alloc_l;
|
||||
*block_p = block_num(p, data, lsizes[alloc_l]);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void z_sys_mem_pool_block_free(struct sys_mem_pool_base *p, uint32_t level,
|
||||
uint32_t block)
|
||||
{
|
||||
size_t lsizes[LVL_ARRAY_SZ(p->n_levels)];
|
||||
uint32_t i;
|
||||
|
||||
/* As in z_sys_mem_pool_block_alloc(), we build a table of level sizes
|
||||
* to avoid having to store it in precious RAM bytes.
|
||||
* Overhead here is somewhat higher because block_free()
|
||||
* doesn't inherently need to traverse all the larger
|
||||
* sublevels.
|
||||
*/
|
||||
lsizes[0] = p->max_sz;
|
||||
for (i = 1; i <= level; i++) {
|
||||
lsizes[i] = WB_DN(lsizes[i-1] / 4);
|
||||
}
|
||||
|
||||
block_free(p, level, lsizes, block);
|
||||
}
|
||||
|
||||
/*
|
||||
* Functions specific to user-mode blocks
|
||||
*/
|
||||
|
||||
void *sys_mem_pool_alloc(struct sys_mem_pool *p, size_t size)
|
||||
{
|
||||
struct sys_mem_pool_block *blk;
|
||||
uint32_t level, block;
|
||||
char *ret;
|
||||
int lock_ret;
|
||||
|
||||
lock_ret = sys_mutex_lock(&p->mutex, K_FOREVER);
|
||||
CHECKIF(lock_ret != 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size += WB_UP(sizeof(struct sys_mem_pool_block));
|
||||
if (z_sys_mem_pool_block_alloc(&p->base, size, &level, &block,
|
||||
(void **)&ret)) {
|
||||
ret = NULL;
|
||||
goto out;
|
||||
}
|
||||
|
||||
blk = (struct sys_mem_pool_block *)ret;
|
||||
blk->level = level;
|
||||
blk->block = block;
|
||||
blk->pool = p;
|
||||
ret += WB_UP(sizeof(struct sys_mem_pool_block));
|
||||
out:
|
||||
sys_mutex_unlock(&p->mutex);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void sys_mem_pool_free(void *ptr)
|
||||
{
|
||||
struct sys_mem_pool_block *blk;
|
||||
struct sys_mem_pool *p;
|
||||
int lock_ret;
|
||||
|
||||
if (ptr == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
ptr = (char *)ptr - WB_UP(sizeof(struct sys_mem_pool_block));
|
||||
blk = (struct sys_mem_pool_block *)ptr;
|
||||
p = blk->pool;
|
||||
|
||||
lock_ret = sys_mutex_lock(&p->mutex, K_FOREVER);
|
||||
CHECKIF(lock_ret != 0) {
|
||||
return;
|
||||
}
|
||||
z_sys_mem_pool_block_free(&p->base, blk->level, blk->block);
|
||||
sys_mutex_unlock(&p->mutex);
|
||||
}
|
||||
|
||||
size_t sys_mem_pool_try_expand_inplace(void *ptr, size_t requested_size)
|
||||
{
|
||||
struct sys_mem_pool_block *blk;
|
||||
size_t struct_blk_size = WB_UP(sizeof(struct sys_mem_pool_block));
|
||||
size_t block_size, total_requested_size;
|
||||
|
||||
ptr = (char *)ptr - struct_blk_size;
|
||||
blk = (struct sys_mem_pool_block *)ptr;
|
||||
|
||||
/*
|
||||
* Determine size of previously allocated block by its level.
|
||||
* Most likely a bit larger than the original allocation
|
||||
*/
|
||||
block_size = blk->pool->base.max_sz;
|
||||
for (int i = 1; i <= blk->level; i++) {
|
||||
block_size = WB_DN(block_size / 4);
|
||||
}
|
||||
|
||||
/* We really need this much memory */
|
||||
total_requested_size = requested_size + struct_blk_size;
|
||||
|
||||
if (block_size >= total_requested_size) {
|
||||
/* size adjustment can occur in-place */
|
||||
return 0;
|
||||
}
|
||||
return block_size - struct_blk_size;
|
||||
}
|
|
@ -9,7 +9,6 @@
|
|||
#include <sys/printk.h>
|
||||
#include <app_memory/app_memdomain.h>
|
||||
#include <sys/libc-hooks.h>
|
||||
#include <sys/mempool.h>
|
||||
#include <logging/log.h>
|
||||
|
||||
#include "main.h"
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
*/
|
||||
|
||||
#include <sys/dlist.h>
|
||||
#include <sys/mempool_base.h>
|
||||
|
||||
#include <nrfx/hal/nrf_rtc.h>
|
||||
|
||||
|
|
|
@ -8,7 +8,6 @@
|
|||
#include <string.h>
|
||||
|
||||
#include <sys/dlist.h>
|
||||
#include <sys/mempool_base.h>
|
||||
|
||||
#include <nrfx/hal/nrf_ecb.h>
|
||||
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
*/
|
||||
|
||||
#include <sys/dlist.h>
|
||||
#include <sys/mempool_base.h>
|
||||
#include <toolchain.h>
|
||||
|
||||
#include <nrfx/hal/nrf_radio.h>
|
||||
|
|
|
@ -7,7 +7,6 @@
|
|||
|
||||
#include <stdbool.h>
|
||||
#include <sys/dlist.h>
|
||||
#include <sys/mempool_base.h>
|
||||
|
||||
#include "hal/cntr.h"
|
||||
|
||||
|
|
|
@ -7,7 +7,6 @@
|
|||
*/
|
||||
|
||||
#include <sys/dlist.h>
|
||||
#include <sys/mempool_base.h>
|
||||
|
||||
#include "hal/cntr.h"
|
||||
|
||||
|
|
|
@ -9,7 +9,6 @@
|
|||
#include <string.h>
|
||||
|
||||
#include <sys/dlist.h>
|
||||
#include <sys/mempool_base.h>
|
||||
#include <sys/byteorder.h>
|
||||
|
||||
#include "hal/ecb.h"
|
||||
|
|
|
@ -9,7 +9,6 @@
|
|||
#include <string.h>
|
||||
#include <sys/printk.h>
|
||||
#include <sys/dlist.h>
|
||||
#include <sys/mempool_base.h>
|
||||
#include <sys/byteorder.h>
|
||||
#include <bluetooth/addr.h>
|
||||
#include <toolchain.h>
|
||||
|
|
|
@ -7,7 +7,6 @@
|
|||
|
||||
#include <stdbool.h>
|
||||
#include <sys/dlist.h>
|
||||
#include <sys/mempool_base.h>
|
||||
|
||||
#include "hal/cntr.h"
|
||||
|
||||
|
|
Loading…
Reference in a new issue