Skip to content
Draft
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
29 changes: 0 additions & 29 deletions Documentation/os/memory/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,35 +74,6 @@ parameter.
In fact, the standard ``malloc()``, ``realloc()``, ``free()`` use this
same mechanism, but with a global heap structure called ``g_mmheap``.

Heap Memory Pool Initial Allocation
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

``mm_initialize_pool()`` can reserve backing chunks for small-allocation
pools. ``mm_initialize_pool_initial(config, init, initialsize)`` allows the
first backing chunk to differ from later chunks. It accepts the same heap
and pool configuration; passing ``NULL`` for ``init`` keeps the allocator's
default pool sizes and growth policy. The existing interface is equivalent
to passing zero for ``initialsize``.

A positive initial size is rounded up to pointer alignment and enlarged to
fit the first request if necessary. It excludes the backing chunk header.
Subsequent chunks use the configured normal chunk size. When the normal chunk
size is smaller than the per-pool expansion size, the allocator requests each
allocation directly from the backing heap and ignores the initial size.
The lower-level ``mempool_multiple_init_initial()`` provides the same final
``initialsize`` argument for users of ``mempool_multiple_init()``.

An independent filesystem heap can select this policy with
``CONFIG_FS_HEAP_MEMPOOL_INIT_CHUNK_SIZE``. Its default, zero, preserves the
normal ``CONFIG_MM_HEAP_MEMPOOL_CHUNK_SIZE`` for the first chunk. The option
requires ``CONFIG_FS_HEAPSIZE`` and heap memory pools with backing chunks.
It applies to both the standard allocator and TLSF, and does not change the
size of the filesystem heap or the policies of the user and kernel heaps.

A smaller first chunk can leave a larger contiguous region available for
requests that bypass the pools. It can also cause earlier growth; it does
not guarantee lower peak memory use or better allocation throughput.

User/Kernel Heaps
~~~~~~~~~~~~~~~~~

Expand Down
13 changes: 3 additions & 10 deletions fs/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -299,18 +299,11 @@ config FS_HEAPSIZE
default 0 to use kmm directly. independent heap disabled

config FS_HEAP_MEMPOOL_INIT_CHUNK_SIZE
int "FS heap initial mempool chunk size"
int "FS heap mempool init chunk size"
default 0
range 0 2147483647
depends on FS_HEAPSIZE > 0 && MM_HEAP_MEMPOOL_THRESHOLD > 0
depends on MM_HEAP_MEMPOOL_CHUNK_SIZE >= MM_HEAP_MEMPOOL_EXPAND_SIZE
depends on FS_HEAPSIZE > 0
---help---
Payload bytes in the first backing allocation of the FS heap pool.
Zero preserves the normal chunk size. A positive value is rounded
up to pointer alignment and enlarged for the first request if needed.
Later allocations retain MM_HEAP_MEMPOOL_CHUNK_SIZE. A smaller
initial chunk leaves more of the FS heap available for large requests,
but can cause the pool to grow sooner. This does not resize FS_HEAPSIZE.
Size of the mempool allocated when fs heap init.

config FS_HEAPBUF_SECTION
string "FS heap use Userheap section"
Expand Down
11 changes: 5 additions & 6 deletions fs/fs_heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ static FAR struct mm_heap_s *g_fs_heap;
void fs_heap_initialize(void)
{
struct mm_heap_config_s config;
struct mm_pool_config_s poolconfig;

#ifdef FS_HEAPBUF_SECTION
static uint8_t buf[CONFIG_FS_HEAPSIZE]
locate_data(CONFIG_FS_HEAPBUF_SECTION);
Expand All @@ -55,12 +57,9 @@ void fs_heap_initialize(void)
config.name = "heapfs";
config.start = buf;
config.size = CONFIG_FS_HEAPSIZE;
#if CONFIG_FS_HEAP_MEMPOOL_INIT_CHUNK_SIZE > 0
g_fs_heap = mm_initialize_pool_initial(&config, NULL,
CONFIG_FS_HEAP_MEMPOOL_INIT_CHUNK_SIZE);
#else
g_fs_heap = mm_initialize_pool(&config, NULL);
#endif
memset(&poolconfig, 0, sizeof(struct mm_pool_config_s));
poolconfig.init_chunksize = CONFIG_FS_HEAP_MEMPOOL_INIT_CHUNK_SIZE;
g_fs_heap = mm_initialize_pool(&config, &poolconfig);
}

FAR void *fs_heap_zalloc(size_t size)
Expand Down
27 changes: 3 additions & 24 deletions include/nuttx/mm/mempool.h
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ void mempool_procfs_unregister(FAR struct mempool_procfs_entry_s *entry);
* free - The free memory function for multiples pool.
* arg - The alloc & free memory functions used arg.
* chunksize - The multiples pool chunk size.
* init_chunksize - The multiples pool initial chunk size.
* expandsize - The expend memory for all pools in multiples pool.
* dict_expendsize - The expend size for multiple dictnoary.
* Returned Value:
Expand All @@ -418,30 +419,8 @@ mempool_multiple_init(FAR const char *name,
mempool_multiple_alloc_t alloc,
mempool_multiple_alloc_size_t alloc_size,
mempool_multiple_free_t free, FAR void *arg,
size_t chunksize, size_t expandsize,
size_t dict_expendsize);

/****************************************************************************
* Name: mempool_multiple_init_initial
*
* Description:
* A smaller first backing chunk can leave more memory available to other
* users of the backing heap without reducing later growth increments.
* The parameters match mempool_multiple_init, with an additional
* initialsize. Zero selects chunksize. A positive value is rounded up to
* pointer alignment and increased if needed for the first request.
* It is ignored when chunksize is smaller than expandsize.
*
****************************************************************************/

FAR struct mempool_multiple_s *
mempool_multiple_init_initial(FAR const char *name,
FAR const size_t *poolsize, size_t npools,
mempool_multiple_alloc_t alloc,
mempool_multiple_alloc_size_t alloc_size,
mempool_multiple_free_t free, FAR void *arg,
size_t chunksize, size_t expandsize,
size_t dict_expendsize, size_t initialsize);
size_t chunksize, size_t init_chunksize,
size_t expandsize, size_t dict_expendsize);

/****************************************************************************
* Name: mempool_multiple_alloc
Expand Down
14 changes: 4 additions & 10 deletions include/nuttx/mm/mm.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,13 @@ struct mm_heap_config_s
bool allocheap;
};

struct mempool_init_s
struct mm_pool_config_s
{
FAR const size_t *poolsize;
size_t npools;
size_t threshold;
size_t chunksize;
size_t init_chunksize;
size_t expandsize;
size_t dict_expendsize;
};
Expand Down Expand Up @@ -307,17 +308,10 @@ mm_initialize(FAR const char *name, FAR void *heapstart, size_t heapsize)
#ifdef CONFIG_MM_HEAP_MEMPOOL
FAR struct mm_heap_s *
mm_initialize_pool(FAR const struct mm_heap_config_s *config,
FAR const struct mempool_init_s *init);

FAR struct mm_heap_s *
mm_initialize_pool_initial(FAR const struct mm_heap_config_s *config,
FAR const struct mempool_init_s *init,
size_t initialsize);
FAR const struct mm_pool_config_s *poolconfig);

#else
# define mm_initialize_pool(config, init) mm_initialize_heap(config)
# define mm_initialize_pool_initial(config, init, initialsize) \
mm_initialize_heap(config)
# define mm_initialize_pool(config, poolconfig) mm_initialize_heap(config)
#endif

void mm_addregion(FAR struct mm_heap_s *heap, FAR void *heapstart,
Expand Down
8 changes: 8 additions & 0 deletions mm/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,14 @@ config MM_HEAP_MEMPOOL_CHUNK_SIZE
---help---
This size describes the multiple mempool chunk size.

config MM_HEAP_MEMPOOL_INIT_CHUNK_SIZE
int "The multiples pool chunk initial size"
default MM_HEAP_MEMPOOL_CHUNK_SIZE
---help---
This size describes the multiples pool chunk initial size.
Set the size of the first allocation when mempool is initialized
to avoid fragmentation caused by multiple allocations

config MM_MIN_BLKSIZE
int "Minimum memory block size"
default 0
Expand Down
67 changes: 17 additions & 50 deletions mm/mempool/mempool_multiple.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
****************************************************************************/

#include <assert.h>
#include <stdint.h>
#include <strings.h>
#include <syslog.h>
#include <sys/param.h>
Expand Down Expand Up @@ -89,8 +88,8 @@ struct mempool_multiple_s

rmutex_t lock;
sq_queue_t chunk_queue;
size_t chunk_size;
size_t initial_size;
size_t chunksize;
size_t init_chunksize;
size_t dict_used;
size_t dict_col_num_log2;
size_t dict_row_num;
Expand Down Expand Up @@ -152,11 +151,11 @@ mempool_multiple_alloc_chunk(FAR struct mempool_multiple_s *mpool,
size_t align, size_t size)
{
FAR struct mpool_chunk_s *chunk;
size_t chunksize;
FAR char *tmp;
FAR void *ret;
size_t chunksize;

if (mpool->chunk_size < mpool->expandsize)
if (mpool->chunksize < mpool->expandsize)
{
ret = mpool->alloc(mpool->arg, align, size);
if (ret)
Expand All @@ -171,8 +170,8 @@ mempool_multiple_alloc_chunk(FAR struct mempool_multiple_s *mpool,
if (chunk == NULL)
{
retry:
chunksize = mpool->initial_size != 0 ?
MAX(mpool->initial_size, size) : mpool->chunk_size;
chunksize = mpool->init_chunksize != mpool->chunksize ?
mpool->init_chunksize : mpool->chunksize;
tmp = mpool->alloc(mpool->arg, mpool->expandsize,
chunksize + sizeof(struct mpool_chunk_s));

Expand All @@ -181,7 +180,7 @@ mempool_multiple_alloc_chunk(FAR struct mempool_multiple_s *mpool,
return NULL;
}

mpool->initial_size = 0;
mpool->init_chunksize = mpool->chunksize;
mpool->alloced += mpool->alloc_size(mpool->arg, tmp);
chunk = (FAR struct mpool_chunk_s *)(tmp + chunksize);
chunk->end = tmp + chunksize;
Expand All @@ -192,11 +191,7 @@ mempool_multiple_alloc_chunk(FAR struct mempool_multiple_s *mpool,
}

ret = (FAR void *)ALIGN_UP((uintptr_t)chunk->next, align);

/* A small first chunk may end before the next aligned address. */

if ((uintptr_t)ret > (uintptr_t)chunk->end ||
(uintptr_t)chunk->end - (uintptr_t)ret < size)
if ((uintptr_t)chunk->end - (uintptr_t)ret < size)
{
goto retry;
}
Expand All @@ -213,7 +208,7 @@ mempool_multiple_free_chunk(FAR struct mempool_multiple_s *mpool,
FAR struct mpool_chunk_s *chunk;
FAR sq_entry_t *entry;

if (mpool->chunk_size < mpool->expandsize)
if (mpool->chunksize < mpool->expandsize)
{
mpool->free(mpool->arg, ptr);
return;
Expand Down Expand Up @@ -405,28 +400,10 @@ FAR struct mempool_multiple_s *
mempool_multiple_init(FAR const char *name,
FAR const size_t *poolsize, size_t npools,
mempool_multiple_alloc_t alloc,
mempool_multiple_alloc_size_t alloc_size,
mempool_multiple_alloc_size_t allocsize,
mempool_multiple_free_t free, FAR void *arg,
size_t chunksize, size_t expandsize,
size_t dict_expendsize)
{
return mempool_multiple_init_initial(name, poolsize, npools, alloc,
alloc_size, free, arg, chunksize,
expandsize, dict_expendsize, 0);
}

/****************************************************************************
* Name: mempool_multiple_init_initial
****************************************************************************/

FAR struct mempool_multiple_s *
mempool_multiple_init_initial(FAR const char *name,
FAR const size_t *poolsize, size_t npools,
mempool_multiple_alloc_t alloc,
mempool_multiple_alloc_size_t alloc_size,
mempool_multiple_free_t free, FAR void *arg,
size_t chunksize, size_t expandsize,
size_t dict_expendsize, size_t initialsize)
size_t chunksize, size_t init_chunksize,
size_t expandsize, size_t dict_expendsize)
{
FAR struct mempool_multiple_s *mpool;
FAR struct mempool_s *pools;
Expand All @@ -441,16 +418,6 @@ mempool_multiple_init_initial(FAR const char *name,
return NULL;
}

if (chunksize < expandsize)
{
initialsize = 0;
}
else if (initialsize > SIZE_MAX - sizeof(struct mpool_chunk_s) -
(sizeof(uintptr_t) - 1))
{
return NULL;
}

maxpoolszie = poolsize[0];
minpoolsize = poolsize[0];
for (i = 0; i < npools; i++)
Expand Down Expand Up @@ -479,14 +446,14 @@ mempool_multiple_init_initial(FAR const char *name,
pools = (FAR struct mempool_s *)
((uintptr_t)mpool + sizeof(struct mempool_multiple_s));

mpool->alloc_size = alloc_size;
mpool->alloc_size = allocsize;
mpool->expandsize = expandsize;
mpool->chunk_size = chunksize;
mpool->initial_size = ALIGN_UP(initialsize, sizeof(uintptr_t));
mpool->chunksize = chunksize;
mpool->init_chunksize = init_chunksize;
mpool->alloc = alloc;
mpool->free = free;
mpool->arg = arg;
mpool->alloced = alloc_size(arg, mpool);
mpool->alloced = allocsize(arg, mpool);
sq_init(&mpool->chunk_queue);
mpool->pools = pools;
mpool->npools = npools;
Expand Down Expand Up @@ -808,7 +775,7 @@ mempool_multiple_mallinfo(FAR struct mempool_multiple_s *mpool)
nxrmutex_lock(&mpool->lock);
info.arena = mpool->alloced;

if (mpool->chunk_size >= mpool->expandsize)
if (mpool->chunksize >= mpool->expandsize)
{
FAR struct mpool_chunk_s *chunk;

Expand Down
Loading
Loading