Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion include/nuttx/mm/mempool.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#include <sys/types.h>
#include <stdbool.h>

#include <nuttx/list.h>
#include <nuttx/queue.h>
#include <nuttx/mm/mm.h>
#include <nuttx/nuttx.h>
Expand Down Expand Up @@ -227,6 +226,29 @@ int mempool_init(FAR struct mempool_s *pool);

FAR void *mempool_allocate(FAR struct mempool_s *pool, unsigned int timeout);

/****************************************************************************
* Name: mempool_zallocate
*
* Description:
* Allocate an block from a specific memory pool and zero the allocated
* block.
*
* If there isn't enough memory blocks, This function will expand memory
* pool if expandsize isn't zero.
*
* Input Parameters:
* pool - Address of the memory pool to be used.
* timeout - The maximum time (ms) to wait for a buffer to become
* available.
*
* Returned Value:
* The pointer to the allocated block on success; NULL on any failure.
*
****************************************************************************/

FAR void *mempool_zallocate(FAR struct mempool_s *pool,
unsigned int timeout);

/****************************************************************************
* Name: mempool_release
*
Expand Down
52 changes: 44 additions & 8 deletions mm/mempool/mempool.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ typedef void (*mempool_callback_t)(FAR struct mempool_s *pool,
* Private Data
****************************************************************************/

static mutex_t g_mempool_init_lock = NXMUTEX_INITIALIZER;

/****************************************************************************
* Private Functions
****************************************************************************/
Expand All @@ -85,8 +83,7 @@ mempool_remove_queue(FAR struct mempool_s *pool, FAR sq_queue_t *queue)

if (ret)
{
queue->head = ret->flink;
if (!queue->head)
if (!ret->flink)
{
queue->tail = NULL;
}
Expand All @@ -97,6 +94,7 @@ mempool_remove_queue(FAR struct mempool_s *pool, FAR sq_queue_t *queue)
pool->check(pool, addr);
}

queue->head = ret->flink;
ret->flink = NULL;
}

Expand Down Expand Up @@ -331,6 +329,7 @@ static void mempool_check_callback(FAR struct mempool_s *pool,

int mempool_init(FAR struct mempool_s *pool)
{
static mutex_t g_mempool_init_lock = NXMUTEX_INITIALIZER;
size_t blocksize = MEMPOOL_REALBLOCKSIZE(pool->blocksize);

if (pool->init)
Expand Down Expand Up @@ -555,6 +554,38 @@ FAR void *mempool_allocate(FAR struct mempool_s *pool, unsigned int timeout)
return blk;
}

/****************************************************************************
* Name: mempool_zallocate
*
* Description:
* Allocate an block from a specific memory pool and zero the allocated
* block.
*
* If there isn't enough memory blocks, This function will expand memory
* pool if expandsize isn't zero.
*
* Input Parameters:
* pool - Address of the memory pool to be used.
* timeout - The maximum time (ms) to wait for a buffer to become
* available.
*
* Returned Value:
* The pointer to the allocated block on success; NULL on any failure.
*
****************************************************************************/

FAR void *mempool_zallocate(FAR struct mempool_s *pool, unsigned int timeout)
{
FAR void *blk = mempool_allocate(pool, timeout);

if (blk != NULL)
{
memset(blk, 0, pool->blocksize);
}

return blk;
}

/****************************************************************************
* Name: mempool_release
*
Expand Down Expand Up @@ -607,7 +638,7 @@ void mempool_release(FAR struct mempool_s *pool, FAR void *blk)
spin_unlock_irqrestore(&pool->lock, flags);
if (mempool_can_wait(pool))
{
int semcount;
int semcount = 0;

nxsem_get_value(&pool->waitsem, &semcount);
if (semcount < 1)
Expand Down Expand Up @@ -685,7 +716,7 @@ int mempool_info(FAR struct mempool_s *pool, FAR struct mempoolinfo_s *info)
info->sizeblks = blocksize;
if (mempool_can_wait(pool))
{
int semcount;
int semcount = 0;

nxsem_get_value(&pool->waitsem, &semcount);
info->nwaiter = -semcount;
Expand Down Expand Up @@ -726,8 +757,13 @@ mempool_info_task(FAR struct mempool_s *pool,
}
else if (task->pid == PID_MM_ALLOC)
{
info.aordblks += pool->nalloc;
info.uordblks += pool->nalloc * blocksize;
irqstate_t flags = spin_lock_irqsave(&pool->lock);
size_t nalloc = pool->nalloc;

spin_unlock_irqrestore(&pool->lock, flags);

info.aordblks += nalloc;
info.uordblks += nalloc * blocksize;
}
#if CONFIG_MM_BACKTRACE >= 0
else
Expand Down
9 changes: 5 additions & 4 deletions mm/mempool/mempool_multiple.c
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ mempool_multiple_init(FAR const char *name,
FAR struct mempool_s *pools;
size_t maxpoolszie;
size_t minpoolsize;
size_t mempoolsize;
int ret;
int i;

Expand All @@ -428,15 +429,15 @@ mempool_multiple_init(FAR const char *name,
}
}

ret = sizeof(struct mempool_multiple_s) +
npools * sizeof(struct mempool_s);
mpool = alloc(arg, sizeof(uintptr_t), ret);
mempoolsize = sizeof(struct mempool_multiple_s) +
npools * sizeof(struct mempool_s);
mpool = alloc(arg, sizeof(uintptr_t), mempoolsize);
if (mpool == NULL)
{
return NULL;
}

memset(mpool, 0, ret);
memset(mpool, 0, mempoolsize);

pools = (FAR struct mempool_s *)
((uintptr_t)mpool + sizeof(struct mempool_multiple_s));
Expand Down
Loading