2012-07-15 01:31:12 +02:00
|
|
|
/****************************************************************************
|
2014-09-22 18:53:50 +02:00
|
|
|
* mm/mm_heap/mm_memalign.c
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2014-08-31 16:10:15 +02:00
|
|
|
* Copyright (C) 2007, 2009, 2011, 2013-2014 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>
|
2012-07-15 01:31:12 +02:00
|
|
|
|
2014-09-24 15:29:09 +02:00
|
|
|
#include <nuttx/mm/mm.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
|
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_memalign
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2012-07-15 01:31:12 +02:00
|
|
|
* memalign requests more than enough space from malloc, finds a region
|
|
|
|
* within that chunk that meets the alignment request and then frees any
|
2014-04-13 22:32:20 +02:00
|
|
|
* leading or trailing space.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2012-07-15 01:31:12 +02:00
|
|
|
* The alignment argument must be a power of two (not checked). 8-byte
|
|
|
|
* alignment is guaranteed by normal malloc calls.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
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_memalign(FAR struct mm_heap_s *heap, size_t alignment,
|
|
|
|
size_t size)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2007-02-27 22:17:21 +01:00
|
|
|
FAR struct mm_allocnode_s *node;
|
2007-02-21 22:55:16 +01:00
|
|
|
size_t rawchunk;
|
|
|
|
size_t alignedchunk;
|
|
|
|
size_t mask = (size_t)(alignment - 1);
|
|
|
|
size_t allocsize;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2014-08-31 18:54:55 +02:00
|
|
|
/* If this requested alinement's less than or equal to the natural alignment
|
2012-07-15 01:31:12 +02:00
|
|
|
* of malloc, then just let malloc do the work.
|
2007-02-18 00:21:28 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (alignment <= MM_MIN_CHUNK)
|
|
|
|
{
|
2014-08-31 18:54:55 +02:00
|
|
|
return mm_malloc(heap, size);
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
2012-07-15 01:31:12 +02:00
|
|
|
/* Adjust the size to account for (1) the size of the allocated node, (2)
|
|
|
|
* to make sure that it is an even multiple of our granule size, and to
|
|
|
|
* include the alignment amount.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2012-07-15 01:31:12 +02:00
|
|
|
* Notice that we increase the allocation size by twice the requested
|
|
|
|
* alignment. We do this so that there will be at least two valid
|
|
|
|
* alignment points within the allocated memory.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2012-07-15 01:31:12 +02:00
|
|
|
* NOTE: These are sizes given to malloc and not chunk sizes. They do
|
|
|
|
* not include SIZEOF_MM_ALLOCNODE.
|
2007-02-18 00:21:28 +01:00
|
|
|
*/
|
|
|
|
|
2011-05-28 23:42:18 +02:00
|
|
|
size = MM_ALIGN_UP(size); /* Make multiples of our granule size */
|
2007-02-18 00:21:28 +01:00
|
|
|
allocsize = size + 2*alignment; /* Add double full alignment size */
|
|
|
|
|
|
|
|
/* Then malloc that size */
|
|
|
|
|
2014-08-31 18:54:55 +02:00
|
|
|
rawchunk = (size_t)mm_malloc(heap, allocsize);
|
2011-01-19 21:18:31 +01:00
|
|
|
if (rawchunk == 0)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-07-15 01:31:12 +02:00
|
|
|
/* We need to hold the MM semaphore while we muck with the chunks and
|
|
|
|
* 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 node associated with the allocation and the next node after
|
|
|
|
* the allocation.
|
2007-02-18 00:21:28 +01:00
|
|
|
*/
|
|
|
|
|
2015-10-08 17:10:22 +02:00
|
|
|
node = (FAR struct mm_allocnode_s *)(rawchunk - SIZEOF_MM_ALLOCNODE);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* Find the aligned subregion */
|
|
|
|
|
|
|
|
alignedchunk = (rawchunk + mask) & ~mask;
|
|
|
|
|
|
|
|
/* Check if there is free space at the beginning of the aligned chunk */
|
|
|
|
|
|
|
|
if (alignedchunk != rawchunk)
|
|
|
|
{
|
2007-02-27 22:17:21 +01:00
|
|
|
FAR struct mm_allocnode_s *newnode;
|
|
|
|
FAR struct mm_allocnode_s *next;
|
2007-02-21 22:55:16 +01:00
|
|
|
size_t precedingsize;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* Get the node the next node after the allocation. */
|
|
|
|
|
2015-10-08 17:10:22 +02:00
|
|
|
next = (FAR struct mm_allocnode_s *)((FAR char *)node + node->size);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* Make sure that there is space to convert the preceding mm_allocnode_s
|
|
|
|
* into an mm_freenode_s. I think that this should always be true
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(alignedchunk >= rawchunk + 8);
|
|
|
|
|
2020-02-13 14:58:07 +01:00
|
|
|
newnode = (FAR struct mm_allocnode_s *)
|
|
|
|
(alignedchunk - SIZEOF_MM_ALLOCNODE);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* Preceding size is full size of the new 'node,' including
|
|
|
|
* SIZEOF_MM_ALLOCNODE
|
|
|
|
*/
|
|
|
|
|
2007-02-21 22:55:16 +01:00
|
|
|
precedingsize = (size_t)newnode - (size_t)node;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2012-07-15 01:31:12 +02:00
|
|
|
/* If we were unlucky, then the alignedchunk can lie in such a position
|
|
|
|
* that precedingsize < SIZEOF_NODE_FREENODE. We can't let that happen
|
|
|
|
* because we are going to cast 'node' to struct mm_freenode_s below.
|
|
|
|
* This is why we allocated memory large enough to support two
|
|
|
|
* alignment points. In this case, we will simply use the second
|
|
|
|
* alignment point.
|
2007-02-18 00:21:28 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (precedingsize < SIZEOF_MM_FREENODE)
|
|
|
|
{
|
|
|
|
alignedchunk += alignment;
|
2020-02-13 14:58:07 +01:00
|
|
|
newnode = (FAR struct mm_allocnode_s *)
|
|
|
|
(alignedchunk - SIZEOF_MM_ALLOCNODE);
|
2007-02-21 22:55:16 +01:00
|
|
|
precedingsize = (size_t)newnode - (size_t)node;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set up the size of the new node */
|
|
|
|
|
2007-02-21 22:55:16 +01:00
|
|
|
newnode->size = (size_t)next - (size_t)newnode;
|
2007-02-18 00:21:28 +01:00
|
|
|
newnode->preceding = precedingsize | MM_ALLOC_BIT;
|
|
|
|
|
|
|
|
/* Reduce the size of the original chunk and mark it not allocated, */
|
|
|
|
|
|
|
|
node->size = precedingsize;
|
|
|
|
node->preceding &= ~MM_ALLOC_BIT;
|
|
|
|
|
|
|
|
/* Fix the preceding size of the next node */
|
|
|
|
|
|
|
|
next->preceding = newnode->size | (next->preceding & MM_ALLOC_BIT);
|
|
|
|
|
2012-07-15 01:31:12 +02:00
|
|
|
/* Convert the newnode chunk size back into malloc-compatible size by
|
|
|
|
* subtracting the header size SIZEOF_MM_ALLOCNODE.
|
2007-02-18 00:21:28 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
allocsize = newnode->size - SIZEOF_MM_ALLOCNODE;
|
|
|
|
|
|
|
|
/* Add the original, newly freed node to the free nodelist */
|
|
|
|
|
2013-03-08 19:29:56 +01:00
|
|
|
mm_addfreechunk(heap, (FAR struct mm_freenode_s *)node);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* Replace the original node with the newlay realloaced,
|
|
|
|
* aligned node
|
|
|
|
*/
|
|
|
|
|
|
|
|
node = newnode;
|
|
|
|
}
|
|
|
|
|
2017-10-24 19:35:40 +02:00
|
|
|
/* Check if there is free space at the end of the aligned chunk. Convert
|
|
|
|
* malloc-compatible chunk size to include SIZEOF_MM_ALLOCNODE as needed
|
|
|
|
* for mm_shrinkchunk.
|
|
|
|
*/
|
|
|
|
|
|
|
|
size = MM_ALIGN_UP(size + SIZEOF_MM_ALLOCNODE);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
if (allocsize > size)
|
|
|
|
{
|
2012-07-15 01:31:12 +02:00
|
|
|
/* Shrink the chunk by that much -- remember, mm_shrinkchunk wants
|
2017-10-24 19:35:40 +02:00
|
|
|
* internal chunk sizes that include SIZEOF_MM_ALLOCNODE.
|
2007-02-18 00:21:28 +01:00
|
|
|
*/
|
|
|
|
|
2017-10-24 19:35:40 +02:00
|
|
|
mm_shrinkchunk(heap, node, size);
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
2013-03-08 19:29:56 +01:00
|
|
|
mm_givesemaphore(heap);
|
2015-10-08 17:10:22 +02:00
|
|
|
return (FAR void *)alignedchunk;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|