mempool - Allow mempool with 0 blocks. This is useful in cases when the mempool is sized according to an application setting. If the user wants 0 of something, it is easier to just initialize the mempool with 0 blocks and a NULL buffer, and just let any attempt to allocate from it to fail.
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/9d5aa01d Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/9d5aa01d Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/9d5aa01d Branch: refs/heads/develop Commit: 9d5aa01d3e1e46a4a78365c45680c8693747235c Parents: 4965b05 Author: Christopher Collins <[email protected]> Authored: Thu Aug 4 16:24:47 2016 -0700 Committer: Christopher Collins <[email protected]> Committed: Thu Aug 4 16:24:47 2016 -0700 ---------------------------------------------------------------------- libs/os/src/os_mempool.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9d5aa01d/libs/os/src/os_mempool.c ---------------------------------------------------------------------- diff --git a/libs/os/src/os_mempool.c b/libs/os/src/os_mempool.c index 04a18a1..e940ba4 100644 --- a/libs/os/src/os_mempool.c +++ b/libs/os/src/os_mempool.c @@ -41,21 +41,29 @@ STAILQ_HEAD(, os_mempool) g_os_mempool_list = * @return os_error_t */ os_error_t -os_mempool_init(struct os_mempool *mp, int blocks, int block_size, void *membuf, - char *name) +os_mempool_init(struct os_mempool *mp, int blocks, int block_size, + void *membuf, char *name) { int true_block_size; uint8_t *block_addr; struct os_memblock *block_ptr; /* Check for valid parameters */ - if ((!mp) || (!membuf) || (blocks <= 0) || (block_size <= 0)) { + if ((!mp) || (blocks < 0) || (block_size <= 0)) { return OS_INVALID_PARM; } - /* Blocks need to be sized properly and memory buffer should be aligned */ - if (((uint32_t)membuf & (OS_ALIGNMENT - 1)) != 0) { - return OS_MEM_NOT_ALIGNED; + if ((!membuf) && (blocks != 0)) { + return OS_INVALID_PARM; + } + + if (membuf != NULL) { + /* Blocks need to be sized properly and memory buffer should be + * aligned + */ + if (((uint32_t)membuf & (OS_ALIGNMENT - 1)) != 0) { + return OS_MEM_NOT_ALIGNED; + } } true_block_size = OS_MEMPOOL_TRUE_BLOCK_SIZE(block_size);
