diff --git a/include/nuttx/mm/mempool.h b/include/nuttx/mm/mempool.h index d27741f13f4..e06db286ade 100644 --- a/include/nuttx/mm/mempool.h +++ b/include/nuttx/mm/mempool.h @@ -258,6 +258,25 @@ int mempool_multiple_init(FAR struct mempool_multiple_s *mpool, FAR void *mempool_multiple_alloc(FAR struct mempool_multiple_s *mpool, size_t size); +/**************************************************************************** + * Name: mempool_multiple_realloc + * + * Description: + * Change the size of the block memory pointed to by oldblk to size bytes. + * + * Input Parameters: + * mpool - The handle of multiple memory pool to be used. + * oldblk - The pointer to change the size of the block memory. + * size - The size of alloc blk. + * + * Returned Value: + * The pointer to the allocated block on success; NULL on any failure. + * + ****************************************************************************/ + +FAR void *mempool_multiple_realloc(FAR struct mempool_multiple_s *mpool, + FAR void *oldblk, size_t size); + /**************************************************************************** * Name: mempool_multiple_free * @@ -273,6 +292,22 @@ FAR void *mempool_multiple_alloc(FAR struct mempool_multiple_s *mpool, void mempool_multiple_free(FAR struct mempool_multiple_s *mpool, FAR void *blk); +/**************************************************************************** + * Name: mempool_multiple_alloc_size + * + * Description: + * Get size of memory block from multiple memory. + * + * Input Parameters: + * blk - The pointer of memory block. + * + * Returned Value: + * The size of memory block. + * + ****************************************************************************/ + +size_t mempool_multiple_alloc_size(FAR void *blk); + /**************************************************************************** * Name: mempool_multiple_fixed_alloc * @@ -293,6 +328,28 @@ void mempool_multiple_free(FAR struct mempool_multiple_s *mpool, FAR void *mempool_multiple_fixed_alloc(FAR struct mempool_multiple_s *mpool, size_t size); +/**************************************************************************** + * Name: mempool_multiple_fixed_realloc + * + * Description: + * Change the size of the block memory pointed to by oldblk to size bytes. + * + * Input Parameters: + * mpool - The handle of multiple memory pool to be used. + * oldblk - The pointer to change the size of the block memory. + * oldsize - The size of block memory to oldblk. + * size - The size of alloc blk. + * + * Returned Value: + * The pointer to the allocated block on success; NULL on any failure. + * + ****************************************************************************/ + +FAR void * +mempool_multiple_fixed_realloc(FAR struct mempool_multiple_s *mpool, + FAR void *oldblk, size_t oldsize, + size_t size); + /**************************************************************************** * Name: mempool_multiple_fixed_free * diff --git a/mm/mempool/mempool_multiple.c b/mm/mempool/mempool_multiple.c index 1114c264541..092b84f393a 100644 --- a/mm/mempool/mempool_multiple.c +++ b/mm/mempool/mempool_multiple.c @@ -31,6 +31,7 @@ #define SIZEOF_HEAD sizeof(FAR struct mempool_s *) #define MAX(a, b) ((a) > (b) ? (a) : (b)) +#define MIN(a, b) ((a) < (b) ? (a) : (b)) /**************************************************************************** * Private Functions @@ -145,11 +146,52 @@ FAR void *mempool_multiple_alloc(FAR struct mempool_multiple_s *mpool, return (FAR char *)blk + SIZEOF_HEAD; } } - while (++pool< end); + while (++pool < end); return NULL; } +/**************************************************************************** + * Name: mempool_multiple_realloc + * + * Description: + * Change the size of the block memory pointed to by oldblk to size bytes. + * + * Input Parameters: + * mpool - The handle of multiple memory pool to be used. + * oldblk - The pointer to change the size of the block memory. + * size - The size of alloc blk. + * + * Returned Value: + * The pointer to the allocated block on success; NULL on any failure. + * + ****************************************************************************/ + +FAR void *mempool_multiple_realloc(FAR struct mempool_multiple_s *mpool, + FAR void *oldblk, size_t size) +{ + FAR void *blk; + + if (size < 1) + { + mempool_multiple_free(mpool, oldblk); + return NULL; + } + + blk = mempool_multiple_alloc(mpool, size); + if (blk != NULL && oldblk != NULL) + { + FAR struct mempool_s *oldpool; + + oldpool = *(FAR struct mempool_s **) + ((FAR char *)oldblk - SIZEOF_HEAD); + memcpy(blk, oldblk, MIN(oldpool->bsize, size)); + mempool_multiple_free(mpool, oldblk); + } + + return blk; +} + /**************************************************************************** * Name: mempool_multiple_free * @@ -175,6 +217,32 @@ void mempool_multiple_free(FAR struct mempool_multiple_s *mpool, mempool_free(pool, mem); } +/**************************************************************************** + * Name: mempool_multiple_alloc_size + * + * Description: + * Get size of memory block from multiple memory. + * + * Input Parameters: + * blk - The pointer of memory block. + * + * Returned Value: + * The size of memory block. + * + ****************************************************************************/ + +size_t mempool_multiple_alloc_size(FAR void *blk) +{ + FAR struct mempool_s *pool; + FAR void *mem; + + DEBUGASSERT(blk != NULL); + + mem = (FAR char *)blk - SIZEOF_HEAD; + pool = *(FAR struct mempool_s **)mem; + return pool->bsize; +} + /**************************************************************************** * Name: mempool_multiple_fixed_alloc * @@ -202,6 +270,45 @@ FAR void *mempool_multiple_fixed_alloc(FAR struct mempool_multiple_s *mpool, return mempool_alloc(pool); } +/**************************************************************************** + * Name: mempool_multiple_fixed_realloc + * + * Description: + * Change the size of the block memory pointed to by oldblk to size bytes. + * + * Input Parameters: + * mpool - The handle of multiple memory pool to be used. + * oldblk - The pointer to change the size of the block memory. + * oldsize - The size of block memory to oldblk. + * size - The size of alloc blk. + * + * Returned Value: + * The pointer to the allocated block on success; NULL on any failure. + * + ****************************************************************************/ + +FAR void * +mempool_multiple_fixed_realloc(FAR struct mempool_multiple_s *mpool, + FAR void *oldblk, size_t oldsize, size_t size) +{ + FAR void *blk; + + if (size < 1) + { + mempool_multiple_fixed_free(mpool, oldblk, oldsize); + return NULL; + } + + blk = mempool_multiple_fixed_alloc(mpool, size); + if (blk != NULL && oldblk != NULL) + { + memcpy(blk, oldblk, MIN(oldsize, size)); + mempool_multiple_fixed_free(mpool, oldblk, oldsize); + } + + return blk; +} + /**************************************************************************** * Name: mempool_multiple_fixed_free *