mm/mempool: add new api: xx_realloc and xx_alloc_size

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
This commit is contained in:
dongjiuzhu1 2022-09-16 15:35:08 +08:00 committed by Xiang Xiao
parent 4e179e8a85
commit 5828e5bb88
2 changed files with 165 additions and 1 deletions

View File

@ -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
*

View File

@ -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
*