2012-07-15 01:31:12 +02:00
|
|
|
/****************************************************************************
|
2014-09-22 18:53:50 +02:00
|
|
|
* mm/mm_heap/mm_malloc.c
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2017-10-17 14:37:09 +02:00
|
|
|
* Copyright (C) 2007, 2009, 2013-2014, 2017 Gregory Nutt. All rights
|
|
|
|
* reserved.
|
2012-07-15 01:31:12 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
2009-12-15 02:39:03 +01:00
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
2007-02-18 00:21:28 +01:00
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
2012-07-15 01:31:12 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2012-07-15 01:31:12 +02:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Included Files
|
2012-07-15 01:31:12 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2013-03-08 21:36:18 +01:00
|
|
|
#include <nuttx/config.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
#include <assert.h>
|
2013-04-18 19:40:38 +02:00
|
|
|
#include <debug.h>
|
2018-11-12 16:36:35 +01:00
|
|
|
#include <string.h>
|
2012-07-15 01:31:12 +02:00
|
|
|
|
2020-04-07 16:21:42 +02:00
|
|
|
#include <nuttx/arch.h>
|
2014-09-24 15:29:09 +02:00
|
|
|
#include <nuttx/mm/mm.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2012-07-15 01:31:12 +02:00
|
|
|
/****************************************************************************
|
2009-12-15 02:39:03 +01:00
|
|
|
* Pre-processor Definitions
|
2012-07-15 01:31:12 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
#ifndef NULL
|
2015-10-08 17:10:22 +02:00
|
|
|
# define NULL ((void *)0)
|
2007-02-18 00:21:28 +01:00
|
|
|
#endif
|
|
|
|
|
2020-04-07 16:21:42 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef __KERNEL__
|
|
|
|
static void mm_free_delaylist(FAR struct mm_heap_s *heap)
|
|
|
|
{
|
|
|
|
FAR struct mm_delaynode_s *tmp;
|
|
|
|
irqstate_t flags;
|
|
|
|
|
|
|
|
/* Move the delay list to local */
|
|
|
|
|
|
|
|
flags = enter_critical_section();
|
|
|
|
|
|
|
|
tmp = heap->mm_delaylist.flink;
|
|
|
|
heap->mm_delaylist.flink = NULL;
|
|
|
|
|
|
|
|
leave_critical_section(flags);
|
|
|
|
|
|
|
|
/* Test if the delayed is empty */
|
|
|
|
|
|
|
|
while (tmp)
|
|
|
|
{
|
|
|
|
FAR void *address;
|
|
|
|
|
|
|
|
/* Get the first delayed deallocation */
|
|
|
|
|
|
|
|
address = tmp;
|
|
|
|
tmp = tmp->flink;
|
|
|
|
|
|
|
|
/* The address should always be non-NULL since that was checked in the
|
|
|
|
* 'while' condition above.
|
|
|
|
*/
|
|
|
|
|
|
|
|
mm_free(heap, address);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-08-31 18:54:55 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-07-15 01:31:12 +02:00
|
|
|
/****************************************************************************
|
2013-03-08 21:36:18 +01:00
|
|
|
* Name: mm_malloc
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2012-07-15 01:31:12 +02:00
|
|
|
* Find the smallest chunk that satisfies the request. Take the memory from
|
|
|
|
* that chunk, save the remaining, smaller chunk (if any).
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* 8-byte alignment of the allocated data is assured.
|
|
|
|
*
|
2012-07-15 01:31:12 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2013-03-08 21:36:18 +01:00
|
|
|
FAR void *mm_malloc(FAR struct mm_heap_s *heap, size_t size)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2007-02-27 22:17:21 +01:00
|
|
|
FAR struct mm_freenode_s *node;
|
2017-11-21 14:24:10 +01:00
|
|
|
size_t alignsize;
|
2007-02-18 00:21:28 +01:00
|
|
|
void *ret = NULL;
|
|
|
|
int ndx;
|
|
|
|
|
2020-04-07 16:21:42 +02:00
|
|
|
#ifdef __KERNEL__
|
|
|
|
/* Firstly, free mm_delaylist */
|
|
|
|
|
|
|
|
mm_free_delaylist(heap);
|
|
|
|
#endif
|
|
|
|
|
2017-11-21 14:24:10 +01:00
|
|
|
/* Ignore zero-length allocations */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2017-11-21 14:24:10 +01:00
|
|
|
if (size < 1)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-07-15 01:31:12 +02:00
|
|
|
/* Adjust the size to account for (1) the size of the allocated node and
|
|
|
|
* (2) to make sure that it is an even multiple of our granule size.
|
2007-02-18 00:21:28 +01:00
|
|
|
*/
|
|
|
|
|
2017-11-21 14:24:10 +01:00
|
|
|
alignsize = MM_ALIGN_UP(size + SIZEOF_MM_ALLOCNODE);
|
2017-11-22 14:30:48 +01:00
|
|
|
DEBUGASSERT(alignsize >= size); /* Check for integer overflow */
|
2020-02-13 09:06:05 +01:00
|
|
|
DEBUGASSERT(alignsize >= MM_MIN_CHUNK);
|
|
|
|
DEBUGASSERT(alignsize >= SIZEOF_MM_FREENODE);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2012-07-15 01:31:12 +02:00
|
|
|
/* We need to hold the MM semaphore while we muck with the nodelist. */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2013-03-08 19:29:56 +01:00
|
|
|
mm_takesemaphore(heap);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2012-07-15 01:31:12 +02:00
|
|
|
/* Get the location in the node list to start the search. Special case
|
|
|
|
* really big allocations
|
2007-02-18 00:21:28 +01:00
|
|
|
*/
|
|
|
|
|
2017-11-21 14:24:10 +01:00
|
|
|
if (alignsize >= MM_MAX_CHUNK)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2020-02-13 14:58:07 +01:00
|
|
|
ndx = MM_NNODES - 1;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Convert the request size into a nodelist index */
|
|
|
|
|
2017-11-21 14:24:10 +01:00
|
|
|
ndx = mm_size2ndx(alignsize);
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
2012-07-15 01:31:12 +02:00
|
|
|
/* Search for a large enough chunk in the list of nodes. This list is
|
|
|
|
* ordered by size, but will have occasional zero sized nodes as we visit
|
2013-03-08 19:29:56 +01:00
|
|
|
* other mm_nodelist[] entries.
|
2007-02-18 00:21:28 +01:00
|
|
|
*/
|
|
|
|
|
2013-03-08 19:29:56 +01:00
|
|
|
for (node = heap->mm_nodelist[ndx].flink;
|
2017-11-21 14:24:10 +01:00
|
|
|
node && node->size < alignsize;
|
2020-02-15 14:21:43 +01:00
|
|
|
node = node->flink)
|
2020-02-13 09:06:05 +01:00
|
|
|
{
|
|
|
|
DEBUGASSERT(node->blink->flink == node);
|
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2012-07-15 01:31:12 +02:00
|
|
|
/* If we found a node with non-zero size, then this is one to use. Since
|
|
|
|
* the list is ordered, we know that is must be best fitting chunk
|
|
|
|
* available.
|
2007-02-18 00:21:28 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (node)
|
|
|
|
{
|
2007-02-27 22:17:21 +01:00
|
|
|
FAR struct mm_freenode_s *remainder;
|
|
|
|
FAR struct mm_freenode_s *next;
|
2007-02-21 22:55:16 +01:00
|
|
|
size_t remaining;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2012-07-15 01:31:12 +02:00
|
|
|
/* Remove the node. There must be a predecessor, but there may not be
|
|
|
|
* a successor node.
|
2007-02-18 00:21:28 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(node->blink);
|
|
|
|
node->blink->flink = node->flink;
|
|
|
|
if (node->flink)
|
|
|
|
{
|
|
|
|
node->flink->blink = node->blink;
|
|
|
|
}
|
|
|
|
|
2012-07-15 01:31:12 +02:00
|
|
|
/* Check if we have to split the free node into one of the allocated
|
|
|
|
* size and another smaller freenode. In some cases, the remaining
|
|
|
|
* bytes can be smaller (they may be SIZEOF_MM_ALLOCNODE). In that
|
|
|
|
* case, we will just carry the few wasted bytes at the end of the
|
|
|
|
* allocation.
|
2007-02-18 00:21:28 +01:00
|
|
|
*/
|
|
|
|
|
2017-11-21 14:24:10 +01:00
|
|
|
remaining = node->size - alignsize;
|
2007-02-18 00:21:28 +01:00
|
|
|
if (remaining >= SIZEOF_MM_FREENODE)
|
|
|
|
{
|
|
|
|
/* Get a pointer to the next node in physical memory */
|
|
|
|
|
2020-04-09 14:46:24 +02:00
|
|
|
next = (FAR struct mm_freenode_s *)
|
|
|
|
(((FAR char *)node) + node->size);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* Create the remainder node */
|
|
|
|
|
2017-11-21 14:24:10 +01:00
|
|
|
remainder = (FAR struct mm_freenode_s *)
|
|
|
|
(((FAR char *)node) + alignsize);
|
|
|
|
|
|
|
|
remainder->size = remaining;
|
|
|
|
remainder->preceding = alignsize;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* Adjust the size of the node under consideration */
|
|
|
|
|
2017-11-21 14:24:10 +01:00
|
|
|
node->size = alignsize;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2012-07-15 01:31:12 +02:00
|
|
|
/* Adjust the 'preceding' size of the (old) next node, preserving
|
|
|
|
* the allocated flag.
|
2007-02-18 00:21:28 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
next->preceding = remaining | (next->preceding & MM_ALLOC_BIT);
|
|
|
|
|
|
|
|
/* Add the remainder back into the nodelist */
|
|
|
|
|
2013-03-08 19:29:56 +01:00
|
|
|
mm_addfreechunk(heap, remainder);
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle the case of an exact size match */
|
|
|
|
|
|
|
|
node->preceding |= MM_ALLOC_BIT;
|
2015-10-08 17:10:22 +02:00
|
|
|
ret = (void *)((FAR char *)node + SIZEOF_MM_ALLOCNODE);
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
2020-02-13 09:06:05 +01:00
|
|
|
DEBUGASSERT(ret == NULL || mm_heapmember(heap, ret));
|
2013-03-08 19:29:56 +01:00
|
|
|
mm_givesemaphore(heap);
|
2013-02-28 16:31:58 +01:00
|
|
|
|
2018-11-12 16:36:35 +01:00
|
|
|
#ifdef CONFIG_MM_FILL_ALLOCATIONS
|
|
|
|
if (ret)
|
|
|
|
{
|
2020-02-13 14:58:07 +01:00
|
|
|
memset(ret, 0xaa, alignsize - SIZEOF_MM_ALLOCNODE);
|
2018-11-12 16:36:35 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-02-28 16:31:58 +01:00
|
|
|
/* If CONFIG_DEBUG_MM is defined, then output the result of the allocation
|
|
|
|
* to the SYSLOG.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG_MM
|
|
|
|
if (!ret)
|
|
|
|
{
|
2017-11-21 14:24:10 +01:00
|
|
|
mwarn("WARNING: Allocation failed, size %d\n", alignsize);
|
2013-02-28 16:31:58 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-11-21 14:24:10 +01:00
|
|
|
minfo("Allocated %p, size %d\n", ret, alignsize);
|
2013-02-28 16:31:58 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
return ret;
|
|
|
|
}
|