Skip to content

Commit

Permalink
cleanup: include/: move misc/mempool.h to sys/mempool.h
Browse files Browse the repository at this point in the history
move misc/mempool.h to sys/mempool.h and
create a shim for backward-compatibility.

No functional changes to the headers.
A warning in the shim can be controlled with CONFIG_COMPAT_INCLUDES.

Related to #16539

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
  • Loading branch information
nashif committed Jun 28, 2019
1 parent 1ed300b commit 08ee8b0
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 97 deletions.
98 changes: 6 additions & 92 deletions include/misc/mempool.h
Original file line number Diff line number Diff line change
@@ -1,101 +1,15 @@
/*
* Copyright (c) 2018 Intel Corporation
* Copyright (c) 2019 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef ZEPHYR_INCLUDE_MISC_MEMPOOL_H_
#define ZEPHYR_INCLUDE_MISC_MEMPOOL_H_

#include <kernel.h>
#include <sys/mempool_base.h>
#include <misc/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;
u32_t level : 4;
u32_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(align) Z_GENERIC_SECTION(section) \
_mpool_buf_##name[_ALIGN4(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 = _ALIGN4(maxsz), \
.n_max = nmax, \
.n_levels = Z_MPOOL_LVLS(maxsz, minsz), \
.levels = _mpool_lvls_##name, \
.flags = SYS_MEM_POOL_USER \
} \
}

/**
* @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);
#ifndef CONFIG_COMPAT_INCLUDES
#warning "This header file has moved, include <sys/mempool.h> instead."
#endif

/**
* @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);
#include <sys/mempool.h>

#endif
#endif /* ZEPHYR_INCLUDE_MISC_MEMPOOL_H_ */
101 changes: 101 additions & 0 deletions include/sys/mempool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* 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 <misc/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;
u32_t level : 4;
u32_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(align) Z_GENERIC_SECTION(section) \
_mpool_buf_##name[_ALIGN4(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 = _ALIGN4(maxsz), \
.n_max = nmax, \
.n_levels = Z_MPOOL_LVLS(maxsz, minsz), \
.levels = _mpool_lvls_##name, \
.flags = SYS_MEM_POOL_USER \
} \
}

/**
* @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);

#endif
2 changes: 1 addition & 1 deletion lib/gui/lvgl/lvgl_mem_kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "lvgl_mem.h"
#include <zephyr.h>
#include <init.h>
#include <misc/mempool.h>
#include <sys/mempool.h>

K_MEM_POOL_DEFINE(lvgl_mem_pool,
CONFIG_LVGL_MEM_POOL_MIN_SIZE,
Expand Down
2 changes: 1 addition & 1 deletion lib/gui/lvgl/lvgl_mem_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "lvgl_mem.h"
#include <zephyr.h>
#include <init.h>
#include <misc/mempool.h>
#include <sys/mempool.h>

SYS_MEM_POOL_DEFINE(lvgl_mem_pool, NULL,
CONFIG_LVGL_MEM_POOL_MIN_SIZE,
Expand Down
2 changes: 1 addition & 1 deletion lib/libc/minimal/source/stdlib/malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <init.h>
#include <errno.h>
#include <sys/math_extras.h>
#include <misc/mempool.h>
#include <sys/mempool.h>
#include <string.h>
#include <app_memory/app_memdomain.h>

Expand Down
2 changes: 1 addition & 1 deletion lib/os/mempool.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <string.h>
#include <sys/__assert.h>
#include <sys/mempool_base.h>
#include <misc/mempool.h>
#include <sys/mempool.h>

#ifdef CONFIG_MISRA_SANE
#define LVL_ARRAY_SZ(n) (8 * sizeof(void *) / 2)
Expand Down
2 changes: 1 addition & 1 deletion tests/kernel/mem_pool/sys_mem_pool/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

#include <ztest.h>
#include <misc/mempool.h>
#include <sys/mempool.h>


#define BLK_SIZE_MIN 256
Expand Down

0 comments on commit 08ee8b0

Please sign in to comment.