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
|
|
|
*
|
2021-02-08 13:50:10 +01:00
|
|
|
* 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
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2021-02-08 13:50:10 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2021-02-08 13:50:10 +01:00
|
|
|
* 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.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
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
|
|
|
|
2021-03-02 09:03:00 +01:00
|
|
|
#include "mm_heap/mm.h"
|
2021-10-09 09:22:28 +02:00
|
|
|
#include "kasan/kasan.h"
|
2021-03-02 09:03:00 +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
|
|
|
*
|
2022-10-30 04:34:24 +01:00
|
|
|
* The alignment argument must be a power of two. 16-byte alignment is
|
2021-01-31 09:08:36 +01:00
|
|
|
* 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;
|
2023-01-05 18:28:13 +01:00
|
|
|
uintptr_t rawchunk;
|
|
|
|
uintptr_t alignedchunk;
|
|
|
|
size_t mask = alignment - 1;
|
2007-02-21 22:55:16 +01:00
|
|
|
size_t allocsize;
|
2021-01-31 09:08:36 +01:00
|
|
|
size_t newsize;
|
|
|
|
|
|
|
|
/* Make sure that alignment is less than half max size_t */
|
|
|
|
|
|
|
|
if (alignment >= (SIZE_MAX / 2))
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure that alignment is a power of 2 */
|
|
|
|
|
|
|
|
if ((alignment & -alignment) != alignment)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2022-10-30 04:34:24 +01:00
|
|
|
#if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
|
2022-11-26 16:44:38 +01:00
|
|
|
node = mempool_multiple_memalign(heap->mm_mpool, alignment, size);
|
2022-10-30 04:34:24 +01:00
|
|
|
if (node != NULL)
|
|
|
|
{
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-02-08 16:19:12 +01:00
|
|
|
/* If this requested alinement's less than or equal to the natural
|
|
|
|
* alignment of malloc, then just let malloc do the work.
|
2007-02-18 00:21:28 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (alignment <= MM_MIN_CHUNK)
|
|
|
|
{
|
2022-10-31 13:09:23 +01:00
|
|
|
FAR void *ptr = mm_malloc(heap, size);
|
|
|
|
DEBUGASSERT(ptr == NULL || ((uintptr_t)ptr) % alignment == 0);
|
|
|
|
return ptr;
|
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
|
|
|
*/
|
|
|
|
|
2023-01-05 18:28:13 +01:00
|
|
|
newsize = MM_ALIGN_UP(size); /* Make multiples of our granule size */
|
2021-01-31 09:08:36 +01:00
|
|
|
allocsize = newsize + 2 * alignment; /* Add double full alignment size */
|
|
|
|
|
2023-01-05 18:28:13 +01:00
|
|
|
if (newsize < size || allocsize < newsize)
|
2021-01-31 09:08:36 +01:00
|
|
|
{
|
|
|
|
/* Integer overflow */
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* Then malloc that size */
|
|
|
|
|
2023-01-05 18:28:13 +01:00
|
|
|
rawchunk = (uintptr_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;
|
|
|
|
}
|
|
|
|
|
2022-11-24 06:46:53 +01:00
|
|
|
kasan_poison((FAR void *)rawchunk,
|
|
|
|
mm_malloc_size(heap, (FAR void *)rawchunk));
|
2021-10-09 09:22:28 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* We need to hold the MM mutex while we muck with the chunks and
|
2012-07-15 01:31:12 +02:00
|
|
|
* nodelist.
|
2007-02-18 00:21:28 +01:00
|
|
|
*/
|
|
|
|
|
2022-11-05 23:41:26 +01:00
|
|
|
DEBUGVERIFY(mm_lock(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;
|
2022-12-15 12:55:16 +01:00
|
|
|
size_t newnodesize;
|
2023-01-28 11:34:30 +01:00
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
/* Get the node the next node after the allocation. */
|
|
|
|
|
2022-12-15 12:55:16 +01:00
|
|
|
next = (FAR struct mm_allocnode_s *)
|
|
|
|
((FAR char *)node + SIZEOF_MM_NODE(node));
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2021-02-08 16:19:12 +01:00
|
|
|
/* Make sure that there is space to convert the preceding
|
2021-01-31 09:08:36 +01:00
|
|
|
* mm_allocnode_s into an mm_freenode_s. I think that this should
|
|
|
|
* always be true
|
2007-02-18 00:21:28 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
2023-01-05 18:28:13 +01:00
|
|
|
precedingsize = (uintptr_t)newnode - (uintptr_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);
|
2023-01-05 18:28:13 +01:00
|
|
|
precedingsize = (uintptr_t)newnode - (uintptr_t)node;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
2023-01-28 11:34:30 +01:00
|
|
|
/* If the previous node is free, merge node and previous node, then
|
|
|
|
* set up the node size.
|
|
|
|
*/
|
|
|
|
|
2022-12-22 09:02:42 +01:00
|
|
|
if ((node->size & MM_PREVFREE_BIT) != 0)
|
2023-01-28 11:34:30 +01:00
|
|
|
{
|
2022-12-22 09:02:42 +01:00
|
|
|
FAR struct mm_freenode_s *prev =
|
|
|
|
(FAR struct mm_freenode_s *)((FAR char *)node - node->preceding);
|
|
|
|
|
2023-01-28 11:34:30 +01:00
|
|
|
/* Remove the node. There must be a predecessor, but there may
|
|
|
|
* not be a successor node.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(prev->blink);
|
|
|
|
prev->blink->flink = prev->flink;
|
|
|
|
if (prev->flink)
|
|
|
|
{
|
|
|
|
prev->flink->blink = prev->blink;
|
|
|
|
}
|
|
|
|
|
2022-12-22 09:02:42 +01:00
|
|
|
precedingsize += SIZEOF_MM_NODE(prev);
|
2023-01-28 11:34:30 +01:00
|
|
|
node = (FAR struct mm_allocnode_s *)prev;
|
|
|
|
}
|
|
|
|
|
|
|
|
node->size = precedingsize;
|
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
/* Set up the size of the new node */
|
|
|
|
|
2022-12-15 12:55:16 +01:00
|
|
|
newnodesize = (uintptr_t)next - (uintptr_t)newnode;
|
2022-12-22 09:02:42 +01:00
|
|
|
newnode->size = newnodesize | MM_ALLOC_BIT | MM_PREVFREE_BIT;
|
2022-12-15 12:55:16 +01:00
|
|
|
newnode->preceding = precedingsize;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2022-12-22 09:02:42 +01:00
|
|
|
/* Clear the previous free bit of the next node */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2022-12-22 09:02:42 +01:00
|
|
|
next->size &= ~MM_PREVFREE_BIT;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2012-07-15 01:31:12 +02:00
|
|
|
/* Convert the newnode chunk size back into malloc-compatible size by
|
2022-12-22 09:02:42 +01:00
|
|
|
* subtracting the header size OVERHEAD_MM_ALLOCNODE.
|
2007-02-18 00:21:28 +01:00
|
|
|
*/
|
|
|
|
|
2022-12-22 09:02:42 +01:00
|
|
|
allocsize = newnodesize - OVERHEAD_MM_ALLOCNODE;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* 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
|
2022-12-22 09:02:42 +01:00
|
|
|
* malloc-compatible chunk size to include OVERHEAD_MM_ALLOCNODE as needed
|
2017-10-24 19:35:40 +02:00
|
|
|
* for mm_shrinkchunk.
|
|
|
|
*/
|
|
|
|
|
2022-12-22 09:02:42 +01:00
|
|
|
size = MM_ALIGN_UP(size + OVERHEAD_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
|
2022-12-22 09:02:42 +01:00
|
|
|
* internal chunk sizes that include OVERHEAD_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
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
mm_unlock(heap);
|
2021-10-09 09:22:28 +02:00
|
|
|
|
2022-08-01 09:59:37 +02:00
|
|
|
MM_ADD_BACKTRACE(heap, node);
|
|
|
|
|
2021-10-09 09:22:28 +02:00
|
|
|
kasan_unpoison((FAR void *)alignedchunk,
|
2022-11-24 06:46:53 +01:00
|
|
|
mm_malloc_size(heap, (FAR void *)alignedchunk));
|
2021-10-09 09:22:28 +02:00
|
|
|
|
2022-10-31 13:09:23 +01:00
|
|
|
DEBUGASSERT(alignedchunk % alignment == 0);
|
2015-10-08 17:10:22 +02:00
|
|
|
return (FAR void *)alignedchunk;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|