2022-07-20 15:37:26 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* mm/mempool/mempool.c
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-11-06 16:27:44 +01:00
|
|
|
#include <execinfo.h>
|
2022-07-20 15:37:26 +02:00
|
|
|
#include <stdbool.h>
|
2022-11-06 16:27:44 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <syslog.h>
|
2022-07-20 15:37:26 +02:00
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/mm/mempool.h>
|
|
|
|
|
2022-10-24 11:29:34 +02:00
|
|
|
#include "kasan/kasan.h"
|
|
|
|
|
2022-11-06 16:27:44 +01:00
|
|
|
#if UINTPTR_MAX <= UINT32_MAX
|
|
|
|
# define MM_PTR_FMT_WIDTH 11
|
|
|
|
#elif UINTPTR_MAX <= UINT64_MAX
|
|
|
|
# define MM_PTR_FMT_WIDTH 19
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef ALIGN_UP
|
|
|
|
# define ALIGN_UP(x, a) (((x) + ((a) - 1)) & (~((a) - 1)))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if CONFIG_MM_BACKTRACE >= 0
|
|
|
|
struct mempool_backtrace_s
|
|
|
|
{
|
|
|
|
FAR struct list_node node;
|
|
|
|
pid_t pid;
|
|
|
|
# if CONFIG_MM_BACKTRACE > 0
|
|
|
|
FAR void *backtrace[CONFIG_MM_BACKTRACE];
|
|
|
|
# endif
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2022-07-20 15:37:26 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-10-30 02:41:08 +02:00
|
|
|
static inline void mempool_add_list(FAR struct list_node *list,
|
|
|
|
FAR void *base, size_t nblks,
|
2022-10-28 16:42:14 +02:00
|
|
|
size_t blocksize)
|
2022-07-20 15:37:26 +02:00
|
|
|
{
|
|
|
|
while (nblks-- > 0)
|
|
|
|
{
|
2022-10-30 02:41:08 +02:00
|
|
|
list_add_head(list, ((FAR struct list_node *)((FAR char *)base +
|
2022-10-28 16:42:14 +02:00
|
|
|
blocksize * nblks)));
|
2022-07-20 15:37:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-24 11:29:34 +02:00
|
|
|
static inline FAR void *mempool_malloc(FAR struct mempool_s *pool,
|
2022-10-31 08:09:47 +01:00
|
|
|
size_t alignment, size_t size)
|
2022-08-20 17:20:04 +02:00
|
|
|
{
|
|
|
|
if (pool->alloc != NULL)
|
|
|
|
{
|
2022-10-31 08:09:47 +01:00
|
|
|
return pool->alloc(pool, alignment, size);
|
2022-08-20 17:20:04 +02:00
|
|
|
}
|
2022-10-31 08:09:47 +01:00
|
|
|
else if (alignment == 0)
|
2022-08-20 17:20:04 +02:00
|
|
|
{
|
|
|
|
return kmm_malloc(size);
|
|
|
|
}
|
2022-10-31 08:09:47 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return kmm_memalign(alignment, size);
|
|
|
|
}
|
2022-08-20 17:20:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void mempool_mfree(FAR struct mempool_s *pool, FAR void *addr)
|
|
|
|
{
|
|
|
|
if (pool->free != NULL)
|
|
|
|
{
|
|
|
|
return pool->free(pool, addr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return kmm_free(addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-06 16:27:44 +01:00
|
|
|
#if CONFIG_MM_BACKTRACE >= 0
|
|
|
|
static inline void mempool_add_backtrace(FAR struct mempool_s *pool,
|
|
|
|
FAR struct mempool_backtrace_s *buf)
|
|
|
|
{
|
|
|
|
list_add_head(&pool->alist, &buf->node);
|
|
|
|
buf->pid = gettid();
|
|
|
|
# if CONFIG_MM_BACKTRACE > 0
|
|
|
|
if (pool->procfs.backtrace)
|
|
|
|
{
|
|
|
|
int result = backtrace(buf->backtrace, CONFIG_MM_BACKTRACE);
|
2022-11-24 12:36:21 +01:00
|
|
|
if (result < CONFIG_MM_BACKTRACE)
|
2022-11-06 16:27:44 +01:00
|
|
|
{
|
2022-11-24 12:36:21 +01:00
|
|
|
buf->backtrace[result] = NULL;
|
2022-11-06 16:27:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buf->backtrace[0] = NULL;
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-07-20 15:37:26 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mempool_init
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize a memory pool.
|
2022-08-14 04:28:05 +02:00
|
|
|
* The user needs to specify the initialization information of mempool
|
2022-10-28 16:42:14 +02:00
|
|
|
* including blocksize, initialsize, expandsize, interruptsize.
|
2022-07-20 15:37:26 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
2022-08-14 04:28:05 +02:00
|
|
|
* pool - Address of the memory pool to be used.
|
|
|
|
* name - The name of memory pool.
|
2022-07-20 15:37:26 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero on success; A negated errno value is returned on any failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-08-14 04:28:05 +02:00
|
|
|
int mempool_init(FAR struct mempool_s *pool, FAR const char *name)
|
2022-07-20 15:37:26 +02:00
|
|
|
{
|
2022-11-06 16:27:44 +01:00
|
|
|
#if CONFIG_MM_BACKTRACE >= 0
|
|
|
|
size_t blocksize = pool->blocksize + sizeof(struct mempool_backtrace_s);
|
|
|
|
#else
|
|
|
|
size_t blocksize = pool->blocksize;
|
|
|
|
#endif
|
|
|
|
size_t alignment = 0;
|
2022-10-28 16:42:14 +02:00
|
|
|
size_t ninterrupt;
|
|
|
|
size_t ninitial;
|
2022-08-14 04:28:05 +02:00
|
|
|
size_t count;
|
2022-07-20 15:37:26 +02:00
|
|
|
|
2022-11-06 16:27:44 +01:00
|
|
|
DEBUGASSERT(pool->blocksize != 0);
|
2022-07-20 15:37:26 +02:00
|
|
|
|
|
|
|
pool->nused = 0;
|
2022-10-30 02:41:08 +02:00
|
|
|
list_initialize(&pool->list);
|
|
|
|
list_initialize(&pool->ilist);
|
|
|
|
list_initialize(&pool->elist);
|
2022-08-14 04:28:05 +02:00
|
|
|
|
2022-11-06 16:27:44 +01:00
|
|
|
#if CONFIG_MM_BACKTRACE >= 0
|
|
|
|
list_initialize(&pool->alist);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if ((pool->blocksize & (pool->blocksize - 1)) == 0)
|
|
|
|
{
|
|
|
|
alignment = pool->blocksize;
|
|
|
|
blocksize = ALIGN_UP(blocksize, alignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
ninitial = pool->initialsize / blocksize;
|
|
|
|
ninterrupt = pool->interruptsize / blocksize;
|
2022-10-28 16:42:14 +02:00
|
|
|
count = ninitial + ninterrupt;
|
2022-09-16 10:55:09 +02:00
|
|
|
if (count != 0)
|
2022-07-20 15:37:26 +02:00
|
|
|
{
|
2022-10-31 08:09:47 +01:00
|
|
|
FAR char *base;
|
|
|
|
|
2022-11-06 16:27:44 +01:00
|
|
|
base = mempool_malloc(pool, alignment,
|
|
|
|
blocksize * count + sizeof(struct list_node));
|
2022-09-16 10:55:09 +02:00
|
|
|
if (base == NULL)
|
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2022-11-06 16:27:44 +01:00
|
|
|
mempool_add_list(&pool->ilist, base, ninterrupt, blocksize);
|
|
|
|
mempool_add_list(&pool->list, base + ninterrupt * blocksize,
|
|
|
|
ninitial, blocksize);
|
2022-10-31 08:09:47 +01:00
|
|
|
list_add_head(&pool->elist, (FAR struct list_node *)
|
2022-11-06 16:27:44 +01:00
|
|
|
(base + count * blocksize));
|
|
|
|
kasan_poison(base, blocksize * count);
|
2022-07-20 15:37:26 +02:00
|
|
|
}
|
|
|
|
|
2022-10-28 16:42:14 +02:00
|
|
|
if (pool->wait && pool->expandsize == 0)
|
2022-08-14 04:28:05 +02:00
|
|
|
{
|
|
|
|
nxsem_init(&pool->waitsem, 0, 0);
|
|
|
|
}
|
2022-07-20 15:37:26 +02:00
|
|
|
|
2022-11-05 17:44:53 +01:00
|
|
|
#if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MEMPOOL)
|
2022-07-29 06:17:14 +02:00
|
|
|
mempool_procfs_register(&pool->procfs, name);
|
2022-11-06 16:27:44 +01:00
|
|
|
# ifdef CONFIG_MM_BACKTRACE_DEFAULT
|
|
|
|
pool->procfs.backtrace = true;
|
|
|
|
# endif
|
2022-07-29 06:17:14 +02:00
|
|
|
#endif
|
|
|
|
|
2022-07-20 15:37:26 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mempool_alloc
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Allocate an block from a specific memory pool.
|
|
|
|
*
|
|
|
|
* If there isn't enough memory blocks, This function will expand memory
|
2022-10-28 16:42:14 +02:00
|
|
|
* pool if expandsize isn't zero.
|
2022-07-20 15:37:26 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* pool - Address of the memory pool to be used.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The pointer to the allocated block on success; NULL on any failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR void *mempool_alloc(FAR struct mempool_s *pool)
|
|
|
|
{
|
2022-10-30 02:41:08 +02:00
|
|
|
FAR struct list_node *blk;
|
2022-07-20 15:37:26 +02:00
|
|
|
irqstate_t flags;
|
|
|
|
|
|
|
|
retry:
|
|
|
|
flags = spin_lock_irqsave(&pool->lock);
|
2022-10-30 02:41:08 +02:00
|
|
|
blk = list_remove_head(&pool->list);
|
2022-07-20 15:37:26 +02:00
|
|
|
if (blk == NULL)
|
|
|
|
{
|
|
|
|
if (up_interrupt_context())
|
|
|
|
{
|
2022-10-30 02:41:08 +02:00
|
|
|
blk = list_remove_head(&pool->ilist);
|
2022-07-20 15:37:26 +02:00
|
|
|
if (blk == NULL)
|
|
|
|
{
|
|
|
|
goto out_with_lock;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
spin_unlock_irqrestore(&pool->lock, flags);
|
2022-10-28 16:42:14 +02:00
|
|
|
if (pool->expandsize != 0)
|
2022-07-20 15:37:26 +02:00
|
|
|
{
|
2022-11-06 16:27:44 +01:00
|
|
|
#if CONFIG_MM_BACKTRACE >= 0
|
|
|
|
size_t blocksize = pool->blocksize +
|
|
|
|
sizeof(struct mempool_backtrace_s);
|
|
|
|
#else
|
|
|
|
size_t blocksize = pool->blocksize;
|
|
|
|
#endif
|
2022-10-31 08:09:47 +01:00
|
|
|
size_t alignment = 0;
|
2022-11-06 16:27:44 +01:00
|
|
|
size_t nexpand;
|
2022-10-31 08:09:47 +01:00
|
|
|
|
|
|
|
if ((pool->blocksize & (pool->blocksize - 1)) == 0)
|
|
|
|
{
|
|
|
|
alignment = pool->blocksize;
|
2022-11-06 16:27:44 +01:00
|
|
|
blocksize = ALIGN_UP(blocksize, alignment);
|
2022-10-31 08:09:47 +01:00
|
|
|
}
|
|
|
|
|
2022-11-06 16:27:44 +01:00
|
|
|
nexpand = pool->expandsize / blocksize;
|
2022-10-31 08:09:47 +01:00
|
|
|
blk = mempool_malloc(pool, alignment,
|
2022-11-06 16:27:44 +01:00
|
|
|
blocksize * nexpand + sizeof(*blk));
|
2022-07-20 15:37:26 +02:00
|
|
|
if (blk == NULL)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-11-06 16:27:44 +01:00
|
|
|
kasan_poison(blk, blocksize * nexpand);
|
2022-07-20 15:37:26 +02:00
|
|
|
flags = spin_lock_irqsave(&pool->lock);
|
2022-11-06 16:27:44 +01:00
|
|
|
mempool_add_list(&pool->list, blk, nexpand, blocksize);
|
2022-10-31 08:09:47 +01:00
|
|
|
list_add_head(&pool->elist, (FAR struct list_node *)
|
2022-11-06 16:27:44 +01:00
|
|
|
((FAR char *)blk + nexpand * blocksize));
|
2022-10-30 02:41:08 +02:00
|
|
|
blk = list_remove_head(&pool->list);
|
2022-07-20 15:37:26 +02:00
|
|
|
}
|
2022-08-14 04:28:05 +02:00
|
|
|
else if (!pool->wait ||
|
|
|
|
nxsem_wait_uninterruptible(&pool->waitsem) < 0)
|
2022-07-20 15:37:26 +02:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pool->nused++;
|
2022-11-06 16:27:44 +01:00
|
|
|
#if CONFIG_MM_BACKTRACE >= 0
|
|
|
|
mempool_add_backtrace(pool, (FAR struct mempool_backtrace_s *)
|
|
|
|
((FAR char *)blk + pool->blocksize));
|
|
|
|
#endif
|
2022-10-28 16:42:14 +02:00
|
|
|
kasan_unpoison(blk, pool->blocksize);
|
2022-07-20 15:37:26 +02:00
|
|
|
out_with_lock:
|
|
|
|
spin_unlock_irqrestore(&pool->lock, flags);
|
|
|
|
return blk;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mempool_free
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Release an memory block to the pool.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* pool - Address of the memory pool to be used.
|
|
|
|
* blk - The pointer of memory block.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void mempool_free(FAR struct mempool_s *pool, FAR void *blk)
|
|
|
|
{
|
|
|
|
irqstate_t flags;
|
2022-11-06 16:27:44 +01:00
|
|
|
#if CONFIG_MM_BACKTRACE >= 0
|
|
|
|
size_t blocksize = pool->blocksize + sizeof(struct mempool_backtrace_s);
|
|
|
|
FAR struct mempool_backtrace_s *buf =
|
|
|
|
(FAR struct mempool_backtrace_s *)((FAR char *)blk + pool->blocksize);
|
|
|
|
|
|
|
|
list_delete(&buf->node);
|
|
|
|
#else
|
|
|
|
size_t blocksize = pool->blocksize;
|
|
|
|
#endif
|
2022-07-20 15:37:26 +02:00
|
|
|
|
|
|
|
flags = spin_lock_irqsave(&pool->lock);
|
2022-11-06 16:27:44 +01:00
|
|
|
if ((pool->blocksize & (pool->blocksize - 1)) == 0)
|
|
|
|
{
|
|
|
|
blocksize = ALIGN_UP(blocksize, pool->blocksize);
|
|
|
|
}
|
|
|
|
|
2022-10-28 16:42:14 +02:00
|
|
|
if (pool->interruptsize != 0)
|
2022-07-20 15:37:26 +02:00
|
|
|
{
|
2022-09-09 10:01:49 +02:00
|
|
|
FAR char *base;
|
2022-10-28 16:42:14 +02:00
|
|
|
size_t ninterrupt;
|
2022-09-09 10:01:49 +02:00
|
|
|
|
2022-10-30 02:41:08 +02:00
|
|
|
base = (FAR char *)(list_peek_head(&pool->elist) + 1);
|
2022-11-06 16:27:44 +01:00
|
|
|
ninterrupt = pool->interruptsize / blocksize;
|
2022-09-09 10:01:49 +02:00
|
|
|
if ((FAR char *)blk >= base &&
|
2022-11-06 16:27:44 +01:00
|
|
|
(FAR char *)blk < base + ninterrupt * blocksize)
|
2022-09-09 10:01:49 +02:00
|
|
|
{
|
2022-10-30 02:41:08 +02:00
|
|
|
list_add_head(&pool->ilist, blk);
|
2022-09-09 10:01:49 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-10-30 02:41:08 +02:00
|
|
|
list_add_head(&pool->list, blk);
|
2022-09-09 10:01:49 +02:00
|
|
|
}
|
2022-07-20 15:37:26 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-10-30 02:41:08 +02:00
|
|
|
list_add_head(&pool->list, blk);
|
2022-07-20 15:37:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pool->nused--;
|
2022-10-28 16:42:14 +02:00
|
|
|
kasan_poison(blk, pool->blocksize);
|
2022-07-20 15:37:26 +02:00
|
|
|
spin_unlock_irqrestore(&pool->lock, flags);
|
2022-10-28 16:42:14 +02:00
|
|
|
if (pool->wait && pool->expandsize == 0)
|
2022-07-20 15:37:26 +02:00
|
|
|
{
|
|
|
|
int semcount;
|
|
|
|
|
2022-08-14 04:28:05 +02:00
|
|
|
nxsem_get_value(&pool->waitsem, &semcount);
|
2022-07-20 15:37:26 +02:00
|
|
|
if (semcount < 1)
|
|
|
|
{
|
2022-08-14 04:28:05 +02:00
|
|
|
nxsem_post(&pool->waitsem);
|
2022-07-20 15:37:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-29 06:17:14 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: mempool_info
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* mempool_info returns a copy of updated current mempool information.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* pool - Address of the memory pool to be used.
|
|
|
|
* info - The pointer of mempoolinfo.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK on success; A negated errno value on any failure.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int mempool_info(FAR struct mempool_s *pool, FAR struct mempoolinfo_s *info)
|
|
|
|
{
|
|
|
|
irqstate_t flags;
|
|
|
|
|
2022-09-09 10:01:49 +02:00
|
|
|
DEBUGASSERT(pool != NULL && info != NULL);
|
2022-07-29 06:17:14 +02:00
|
|
|
|
|
|
|
flags = spin_lock_irqsave(&pool->lock);
|
2022-10-30 02:41:08 +02:00
|
|
|
info->ordblks = list_length(&pool->list);
|
|
|
|
info->iordblks = list_length(&pool->ilist);
|
2022-07-29 06:17:14 +02:00
|
|
|
info->aordblks = pool->nused;
|
2022-10-28 16:42:14 +02:00
|
|
|
info->arena = (pool->nused + info->ordblks + info->iordblks) *
|
|
|
|
pool->blocksize;
|
2022-07-29 06:17:14 +02:00
|
|
|
spin_unlock_irqrestore(&pool->lock, flags);
|
2022-10-28 16:42:14 +02:00
|
|
|
info->sizeblks = pool->blocksize;
|
|
|
|
if (pool->wait && pool->expandsize == 0)
|
2022-07-29 06:17:14 +02:00
|
|
|
{
|
|
|
|
int semcount;
|
|
|
|
|
2022-08-14 04:28:05 +02:00
|
|
|
nxsem_get_value(&pool->waitsem, &semcount);
|
2022-07-29 06:17:14 +02:00
|
|
|
info->nwaiter = -semcount;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
info->nwaiter = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-11-06 16:27:44 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: mempool_info_task
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int mempool_info_task(FAR struct mempool_s *pool,
|
|
|
|
FAR struct mempoolinfo_task *info)
|
|
|
|
{
|
|
|
|
DEBUGASSERT(info);
|
|
|
|
if (info->pid == -2)
|
|
|
|
{
|
|
|
|
size_t count = list_length(&pool->list);
|
|
|
|
|
|
|
|
info->aordblks += count;
|
|
|
|
info->uordblks += count * pool->blocksize;
|
|
|
|
}
|
|
|
|
else if (info->pid == -1)
|
|
|
|
{
|
|
|
|
#if CONFIG_MM_BACKTRACE >= 0
|
|
|
|
size_t blocksize = pool->blocksize +
|
|
|
|
sizeof(struct mempool_backtrace_s);
|
|
|
|
#else
|
|
|
|
size_t blocksize = pool->blocksize;
|
|
|
|
#endif
|
|
|
|
size_t count;
|
|
|
|
|
|
|
|
if ((pool->blocksize & (pool->blocksize - 1)) == 0)
|
|
|
|
{
|
|
|
|
blocksize = ALIGN_UP(blocksize, pool->blocksize);
|
|
|
|
}
|
|
|
|
|
|
|
|
count = (pool->initialsize + pool->interruptsize) / blocksize +
|
|
|
|
(list_length(&pool->elist) - 1) - list_length(&pool->list);
|
|
|
|
|
|
|
|
info->aordblks += count;
|
|
|
|
info->uordblks += count * pool->blocksize;
|
|
|
|
}
|
|
|
|
#if CONFIG_MM_BACKTRACE >= 0
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FAR struct mempool_backtrace_s *buf;
|
|
|
|
list_for_every_entry(&pool->alist, buf, struct mempool_backtrace_s,
|
|
|
|
node)
|
|
|
|
{
|
|
|
|
if (buf->pid == info->pid)
|
|
|
|
{
|
|
|
|
info->aordblks++;
|
|
|
|
info->uordblks += pool->blocksize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mempool_memdump
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* mempool_memdump returns a memory info about specified pid of
|
|
|
|
* task/thread. if pid equals -1, this function will dump all allocated
|
|
|
|
* node and output backtrace for every allocated node for this mempool,
|
|
|
|
* if pid equals -2, this function will dump all free node for this
|
|
|
|
* mempool, and if pid is greater than or equal to 0, will dump pid
|
|
|
|
* allocated node and output backtrace.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* pool - Address of the memory pool to be used.
|
|
|
|
* pid - The task of pid.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK on success; A negated errno value on any failure.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void mempool_memdump(FAR struct mempool_s *pool, pid_t pid)
|
|
|
|
{
|
|
|
|
if (pid == -2)
|
|
|
|
{
|
|
|
|
FAR struct list_node *node;
|
|
|
|
list_for_every(&pool->list, node)
|
|
|
|
{
|
|
|
|
syslog(LOG_INFO, "%12zu%*p\n",
|
|
|
|
pool->blocksize, MM_PTR_FMT_WIDTH,
|
|
|
|
(FAR char *)node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#if CONFIG_MM_BACKTRACE >= 0
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FAR struct mempool_backtrace_s *buf;
|
|
|
|
list_for_every_entry(&pool->alist, buf, struct mempool_backtrace_s,
|
|
|
|
node)
|
|
|
|
{
|
|
|
|
if (buf->pid == pid || pid == -1)
|
|
|
|
{
|
|
|
|
# if CONFIG_MM_BACKTRACE > 0
|
|
|
|
int i;
|
|
|
|
FAR const char *format = " %0*p";
|
|
|
|
# endif
|
|
|
|
char bt[CONFIG_MM_BACKTRACE * MM_PTR_FMT_WIDTH + 1];
|
|
|
|
|
|
|
|
bt[0] = '\0';
|
|
|
|
# if CONFIG_MM_BACKTRACE > 0
|
|
|
|
for (i = 0; i < CONFIG_MM_BACKTRACE && buf->backtrace[i]; i++)
|
|
|
|
{
|
|
|
|
sprintf(bt + i * MM_PTR_FMT_WIDTH, format,
|
|
|
|
MM_PTR_FMT_WIDTH - 1, buf->backtrace[i]);
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
|
|
|
|
syslog(LOG_INFO, "%6d%12zu%*p%s\n",
|
|
|
|
(int)buf->pid, pool->blocksize, MM_PTR_FMT_WIDTH,
|
|
|
|
((FAR char *)buf - pool->blocksize), bt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-07-20 15:37:26 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: mempool_deinit
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Deallocate a memory pool.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* pool - Address of the memory pool to be used.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int mempool_deinit(FAR struct mempool_s *pool)
|
|
|
|
{
|
2022-11-06 16:27:44 +01:00
|
|
|
#if CONFIG_MM_BACKTRACE >= 0
|
|
|
|
size_t blocksize = pool->blocksize + sizeof(struct mempool_backtrace_s);
|
|
|
|
#else
|
|
|
|
size_t blocksize = pool->blocksize;
|
|
|
|
#endif
|
2022-10-30 02:41:08 +02:00
|
|
|
FAR struct list_node *blk;
|
2022-10-31 08:09:47 +01:00
|
|
|
size_t ninterrupt;
|
|
|
|
size_t ninitial;
|
|
|
|
size_t count;
|
2022-07-20 15:37:26 +02:00
|
|
|
|
|
|
|
if (pool->nused != 0)
|
|
|
|
{
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
2022-11-05 17:44:53 +01:00
|
|
|
#if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MEMPOOL)
|
2022-07-29 06:17:14 +02:00
|
|
|
mempool_procfs_unregister(&pool->procfs);
|
|
|
|
#endif
|
|
|
|
|
2022-11-06 16:27:44 +01:00
|
|
|
if ((pool->blocksize & (pool->blocksize - 1)) == 0)
|
|
|
|
{
|
|
|
|
blocksize = ALIGN_UP(blocksize, pool->blocksize);
|
|
|
|
}
|
|
|
|
|
|
|
|
ninitial = pool->initialsize / blocksize;
|
|
|
|
ninterrupt = pool->interruptsize / blocksize;
|
2022-10-31 08:09:47 +01:00
|
|
|
count = ninitial + ninterrupt;
|
|
|
|
if (count == 0)
|
|
|
|
{
|
2022-11-06 16:27:44 +01:00
|
|
|
count = pool->expandsize / blocksize;
|
2022-10-31 08:09:47 +01:00
|
|
|
}
|
|
|
|
|
2022-10-30 02:41:08 +02:00
|
|
|
while ((blk = list_remove_head(&pool->elist)) != NULL)
|
2022-07-20 15:37:26 +02:00
|
|
|
{
|
2022-10-31 08:09:47 +01:00
|
|
|
blk = (FAR struct list_node *)((FAR char *)blk -
|
2022-11-06 16:27:44 +01:00
|
|
|
count * blocksize);
|
2022-11-24 06:46:53 +01:00
|
|
|
kasan_unpoison(blk, blocksize);
|
2022-08-20 17:20:04 +02:00
|
|
|
mempool_mfree(pool, blk);
|
2022-11-06 16:27:44 +01:00
|
|
|
count = pool->expandsize / blocksize;
|
2022-07-20 15:37:26 +02:00
|
|
|
}
|
|
|
|
|
2022-10-28 16:42:14 +02:00
|
|
|
if (pool->wait && pool->expandsize == 0)
|
2022-08-14 04:28:05 +02:00
|
|
|
{
|
|
|
|
nxsem_destroy(&pool->waitsem);
|
|
|
|
}
|
|
|
|
|
2022-07-20 15:37:26 +02:00
|
|
|
return 0;
|
|
|
|
}
|