Skip to content

Commit 9af2f0e

Browse files
W-M-Rxiaoxiang781216
authored andcommitted
mempool: Support mempool address and size alignment by setting CONFIG_MM_NODE_GUARDSIZE
Setting a reasonable CONFIG_MM_NODE_GUARDSIZE can ensure absolute alignment of usersize Signed-off-by: wangmingrong1 <[email protected]>
1 parent e6f77d7 commit 9af2f0e

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

mm/mempool/mempool.c

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
* Pre-processor Definitions
4141
****************************************************************************/
4242

43+
#define MEMPOOL_HEADER_SIZE (sizeof(sq_entry_t) + CONFIG_MM_NODE_GUARDSIZE)
44+
4345
#if CONFIG_MM_BACKTRACE >= 0
4446
#define MEMPOOL_MAGIC_FREE 0x55555555
4547
#define MEMPOOL_MAGIC_ALLOC 0xAAAAAAAA
@@ -148,7 +150,7 @@ static void mempool_foreach(FAR struct mempool_s *pool,
148150

149151
sq_for_every(&pool->equeue, entry)
150152
{
151-
nblks = (pool->expandsize - sizeof(sq_entry_t)) / blocksize;
153+
nblks = (pool->expandsize - MEMPOOL_HEADER_SIZE) / blocksize;
152154
base = (FAR char *)entry - (nblks * blocksize);
153155

154156
while (nblks--)
@@ -280,10 +282,11 @@ int mempool_init(FAR struct mempool_s *pool, FAR const char *name)
280282
pool->ibase = NULL;
281283
}
282284

283-
if (pool->initialsize >= blocksize + sizeof(sq_entry_t))
285+
if (pool->initialsize >= blocksize + MEMPOOL_HEADER_SIZE)
284286
{
285-
size_t ninitial = (pool->initialsize - sizeof(sq_entry_t)) / blocksize;
286-
size_t size = ninitial * blocksize + sizeof(sq_entry_t);
287+
size_t ninitial = (pool->initialsize - MEMPOOL_HEADER_SIZE) /
288+
blocksize;
289+
size_t size = ninitial * blocksize + MEMPOOL_HEADER_SIZE;
287290
FAR char *base;
288291

289292
base = pool->alloc(pool, size);
@@ -363,11 +366,11 @@ FAR void *mempool_allocate(FAR struct mempool_s *pool)
363366
size_t blocksize = MEMPOOL_REALBLOCKSIZE(pool);
364367

365368
spin_unlock_irqrestore(&pool->lock, flags);
366-
if (pool->expandsize >= blocksize + sizeof(sq_entry_t))
369+
if (pool->expandsize >= blocksize + MEMPOOL_HEADER_SIZE)
367370
{
368-
size_t nexpand = (pool->expandsize - sizeof(sq_entry_t)) /
371+
size_t nexpand = (pool->expandsize - MEMPOOL_HEADER_SIZE) /
369372
blocksize;
370-
size_t size = nexpand * blocksize + sizeof(sq_entry_t);
373+
size_t size = nexpand * blocksize + MEMPOOL_HEADER_SIZE;
371374
FAR char *base = pool->alloc(pool, size);
372375

373376
if (base == NULL)
@@ -499,7 +502,7 @@ int mempool_info(FAR struct mempool_s *pool, FAR struct mempoolinfo_s *info)
499502
info->ordblks = sq_count(&pool->queue);
500503
info->iordblks = sq_count(&pool->iqueue);
501504
info->aordblks = pool->nalloc;
502-
info->arena = sq_count(&pool->equeue) * sizeof(sq_entry_t) +
505+
info->arena = sq_count(&pool->equeue) * MEMPOOL_HEADER_SIZE +
503506
(info->aordblks + info->ordblks + info->iordblks) * blocksize;
504507
spin_unlock_irqrestore(&pool->lock, flags);
505508
info->sizeblks = blocksize;
@@ -620,16 +623,16 @@ int mempool_deinit(FAR struct mempool_s *pool)
620623
return -EBUSY;
621624
}
622625

623-
if (pool->initialsize >= blocksize + sizeof(sq_entry_t))
626+
if (pool->initialsize >= blocksize + MEMPOOL_HEADER_SIZE)
624627
{
625-
count = (pool->initialsize - sizeof(sq_entry_t)) / blocksize;
628+
count = (pool->initialsize - MEMPOOL_HEADER_SIZE) / blocksize;
626629
}
627630

628631
if (count == 0)
629632
{
630-
if (pool->expandsize >= blocksize + sizeof(sq_entry_t))
633+
if (pool->expandsize >= blocksize + MEMPOOL_HEADER_SIZE)
631634
{
632-
count = (pool->expandsize - sizeof(sq_entry_t)) / blocksize;
635+
count = (pool->expandsize - MEMPOOL_HEADER_SIZE) / blocksize;
633636
}
634637
}
635638

@@ -641,11 +644,11 @@ int mempool_deinit(FAR struct mempool_s *pool)
641644
{
642645
blk = (FAR sq_entry_t *)((FAR char *)blk - count * blocksize);
643646

644-
blk = kasan_unpoison(blk, count * blocksize + sizeof(sq_entry_t));
647+
blk = kasan_unpoison(blk, count * blocksize + MEMPOOL_HEADER_SIZE);
645648
pool->free(pool, blk);
646-
if (pool->expandsize >= blocksize + sizeof(sq_entry_t))
649+
if (pool->expandsize >= blocksize + MEMPOOL_HEADER_SIZE)
647650
{
648-
count = (pool->expandsize - sizeof(sq_entry_t)) / blocksize;
651+
count = (pool->expandsize - MEMPOOL_HEADER_SIZE) / blocksize;
649652
}
650653
}
651654

0 commit comments

Comments
 (0)