This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 066b3259758ca24d41df8dcc5ae089461e0d6731 Author: Bowen Wang <[email protected]> AuthorDate: Wed Jul 30 10:00:11 2025 +0800 mm/mempool: add max allowed alloc number support for mempool Now mempool can init this value to control the max alloc number Signed-off-by: Bowen Wang <[email protected]> --- include/nuttx/mm/mempool.h | 2 ++ mm/mempool/mempool.c | 9 +++++++++ mm/mempool/mempool_procfs.c | 12 ++++++------ 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/include/nuttx/mm/mempool.h b/include/nuttx/mm/mempool.h index 7d20e650fc0..80cef518cec 100644 --- a/include/nuttx/mm/mempool.h +++ b/include/nuttx/mm/mempool.h @@ -96,6 +96,7 @@ struct mempool_s size_t initialsize; /* The initialize size in normal mempool */ size_t interruptsize; /* The initialize size in interrupt mempool */ size_t expandsize; /* The size of expand block every time for mempool */ + size_t maxalloc; /* The maximum allowed alloc number, 0 means not limit */ bool wait; /* The flag of need to wait when mempool is empty */ FAR void *priv; /* This pointer is used to store the user's private data */ mempool_alloc_t alloc; /* The alloc function for mempool */ @@ -140,6 +141,7 @@ struct mempoolinfo_s unsigned long aordblks; /* This is the number of used blocks */ unsigned long sizeblks; /* This is the size of a mempool blocks */ unsigned long nwaiter; /* This is the number of waiter for mempool */ + unsigned long maxalloc; /* This is the maximum allowed alloc number, 0 means not limit */ }; /**************************************************************************** diff --git a/mm/mempool/mempool.c b/mm/mempool/mempool.c index 27197f76946..e9e201f11d4 100644 --- a/mm/mempool/mempool.c +++ b/mm/mempool/mempool.c @@ -25,6 +25,7 @@ ****************************************************************************/ #include <assert.h> +#include <debug.h> #include <execinfo.h> #include <stdbool.h> #include <stdio.h> @@ -359,6 +360,13 @@ FAR void *mempool_allocate(FAR struct mempool_s *pool, unsigned int timeout) retry: flags = spin_lock_irqsave(&pool->lock); + if (pool->maxalloc > 0 && pool->nalloc >= pool->maxalloc) + { + spin_unlock_irqrestore(&pool->lock, flags); + merr("ERROR: mempool_allocate: maxalloc=%zu\n", pool->maxalloc); + return NULL; + } + blk = mempool_remove_queue(pool, &pool->queue); if (blk == NULL) { @@ -529,6 +537,7 @@ int mempool_info(FAR struct mempool_s *pool, FAR struct mempoolinfo_s *info) info->aordblks = pool->nalloc; info->arena = sq_count(&pool->equeue) * MEMPOOL_HEADER_SIZE + (info->aordblks + info->ordblks + info->iordblks) * blocksize; + info->maxalloc = pool->maxalloc; spin_unlock_irqrestore(&pool->lock, flags); info->sizeblks = blocksize; if (pool->wait && pool->expandsize == 0) diff --git a/mm/mempool/mempool_procfs.c b/mm/mempool/mempool_procfs.c index d415c01d561..42ebb02727a 100644 --- a/mm/mempool/mempool_procfs.c +++ b/mm/mempool/mempool_procfs.c @@ -42,7 +42,7 @@ * to handle the longest line generated by this logic. */ -#define MEMPOOLINFO_LINELEN 80 +#define MEMPOOLINFO_LINELEN 128 /**************************************************************************** * Private Types @@ -142,9 +142,9 @@ static ssize_t mempool_read(FAR struct file *filep, FAR char *buffer, offset = filep->f_pos; procfile = filep->f_priv; linesize = procfs_snprintf(procfile->line, MEMPOOLINFO_LINELEN, - "%13s%11s%9s%9s%9s%9s%9s\n", "", "total", + "%13s%11s%9s%9s%9s%9s%9s%9s\n", "", "total", "bsize", "nused", "nfree", "nifree", - "nwaiter"); + "nwaiter", "maxalloc"); copysize = procfs_memcpy(procfile->line, linesize, buffer, buflen, &offset); @@ -163,11 +163,11 @@ static ssize_t mempool_read(FAR struct file *filep, FAR char *buffer, mempool_info(pool, &minfo); linesize = procfs_snprintf(procfile->line, MEMPOOLINFO_LINELEN, - "%12s:%11lu%9lu%9lu%9lu%9lu%9lu\n", - entry->name, minfo.arena, + "%12s:%11lu%9lu%9lu%9lu%9lu%9lu%9lu" + "\n", entry->name, minfo.arena, minfo.sizeblks, minfo.aordblks, minfo.ordblks, minfo.iordblks, - minfo.nwaiter); + minfo.nwaiter, minfo.maxalloc); copysize = procfs_memcpy(procfile->line, linesize, buffer, buflen, &offset); totalsize += copysize;
