mempool - fn to check if block came from pool.
Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/4965b05c Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/4965b05c Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/4965b05c Branch: refs/heads/develop Commit: 4965b05c5f6c469fc356c195e3bfe0da50385f96 Parents: b187e6d Author: Christopher Collins <[email protected]> Authored: Thu Aug 4 16:24:14 2016 -0700 Committer: Christopher Collins <[email protected]> Committed: Thu Aug 4 16:24:14 2016 -0700 ---------------------------------------------------------------------- libs/os/include/os/os_mempool.h | 3 ++ libs/os/src/os_mempool.c | 54 ++++++++++++++++++++++++------------ 2 files changed, 40 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4965b05c/libs/os/include/os/os_mempool.h ---------------------------------------------------------------------- diff --git a/libs/os/include/os/os_mempool.h b/libs/os/include/os/os_mempool.h index 6b0f24c..a731618 100644 --- a/libs/os/include/os/os_mempool.h +++ b/libs/os/include/os/os_mempool.h @@ -82,6 +82,9 @@ typedef uint64_t os_membuf_t; os_error_t os_mempool_init(struct os_mempool *mp, int blocks, int block_size, void *membuf, char *name); +/* Checks if a memory block was allocated from the specified mempool. */ +int os_memblock_from(struct os_mempool *mp, void *block_addr); + /* Get a memory block from the pool */ void *os_memblock_get(struct os_mempool *mp); http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4965b05c/libs/os/src/os_mempool.c ---------------------------------------------------------------------- diff --git a/libs/os/src/os_mempool.c b/libs/os/src/os_mempool.c index 1e7428c..04a18a1 100644 --- a/libs/os/src/os_mempool.c +++ b/libs/os/src/os_mempool.c @@ -86,6 +86,42 @@ os_mempool_init(struct os_mempool *mp, int blocks, int block_size, void *membuf, } /** + * Checks if a memory block was allocated from the specified mempool. + * + * @param mp The mempool to check as parent. + * @param block_addr The memory block to check as child. + * + * @return 0 if the block does not belong to the mempool; + * 1 if the block does belong to the mempool. + */ +int +os_memblock_from(struct os_mempool *mp, void *block_addr) +{ + uint32_t true_block_size; + uint32_t baddr32; + uint32_t end; + + _Static_assert(sizeof block_addr == sizeof baddr32, + "Pointer to void must be 32-bits."); + + baddr32 = (uint32_t)block_addr; + true_block_size = OS_MEMPOOL_TRUE_BLOCK_SIZE(mp->mp_block_size); + end = mp->mp_membuf_addr + (mp->mp_num_blocks * true_block_size); + + /* Check that the block is in the memory buffer range. */ + if ((baddr32 < mp->mp_membuf_addr) || (baddr32 >= end)) { + return 0; + } + + /* All freed blocks should be on true block size boundaries! */ + if (((baddr32 - mp->mp_membuf_addr) % true_block_size) != 0) { + return 0; + } + + return 1; +} + +/** * os memblock get * * Get a memory block from a memory pool @@ -135,9 +171,6 @@ os_error_t os_memblock_put(struct os_mempool *mp, void *block_addr) { os_sr_t sr; - uint32_t end; - uint32_t true_block_size; - uint32_t baddr32; struct os_memblock *block; /* Make sure parameters are valid */ @@ -146,23 +179,10 @@ os_memblock_put(struct os_mempool *mp, void *block_addr) } /* Check that the block we are freeing is a valid block! */ - baddr32 = (uint32_t)block_addr; - true_block_size = OS_MEMPOOL_TRUE_BLOCK_SIZE(mp->mp_block_size); - end = mp->mp_membuf_addr + (mp->mp_num_blocks * true_block_size); - if ((baddr32 < mp->mp_membuf_addr) || (baddr32 >= end)) { - return OS_INVALID_PARM; - } - - /* All freed blocks should be on true block size boundaries! */ - if (((baddr32 - mp->mp_membuf_addr) % true_block_size) != 0) { + if (!os_memblock_from(mp, block_addr)) { return OS_INVALID_PARM; } - /* - * XXX: we should do boundary checks here! The block had better be within - * the pool. If it fails, do we return an error or assert()? Add this when - * we add the memory debug. - */ block = (struct os_memblock *)block_addr; OS_ENTER_CRITICAL(sr);
