2014-08-31 22:42:45 +02:00
|
|
|
/****************************************************************************
|
2014-09-22 18:53:50 +02:00
|
|
|
* mm/mm_heap/mm_extend.c
|
2014-08-31 22:42:45 +02:00
|
|
|
*
|
2024-09-12 16:44:55 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
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
|
2014-08-31 22:42:45 +02:00
|
|
|
*
|
2021-02-08 13:50:10 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-08-31 22:42:45 +02: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.
|
2014-08-31 22:42:45 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
2014-09-24 15:29:09 +02:00
|
|
|
#include <nuttx/mm/mm.h>
|
2014-08-31 22:42:45 +02:00
|
|
|
|
2021-03-02 09:03:00 +01:00
|
|
|
#include "mm_heap/mm.h"
|
|
|
|
|
2014-08-31 22:42:45 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-09-06 05:48:30 +02:00
|
|
|
#define MIN_EXTEND (2 * MM_SIZEOF_ALLOCNODE)
|
2014-08-31 22:42:45 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mm_extend
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Extend a heap region by add a block of (virtually) contiguous memory
|
|
|
|
* to the end of the heap.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void mm_extend(FAR struct mm_heap_s *heap, FAR void *mem, size_t size,
|
|
|
|
int region)
|
|
|
|
{
|
2021-03-02 09:03:00 +01:00
|
|
|
FAR struct mm_allocnode_s *oldnode;
|
|
|
|
FAR struct mm_allocnode_s *newnode;
|
2014-08-31 22:42:45 +02:00
|
|
|
uintptr_t blockstart;
|
|
|
|
uintptr_t blockend;
|
|
|
|
|
|
|
|
/* Make sure that we were passed valid parameters */
|
|
|
|
|
|
|
|
DEBUGASSERT(heap && mem);
|
2014-09-03 19:43:23 +02:00
|
|
|
#if CONFIG_MM_REGIONS > 1
|
2023-01-05 18:28:13 +01:00
|
|
|
DEBUGASSERT(size >= MIN_EXTEND && region >= 0 &&
|
|
|
|
region < heap->mm_nregions);
|
2014-09-03 19:43:23 +02:00
|
|
|
#else
|
2016-05-26 18:01:15 +02:00
|
|
|
DEBUGASSERT(size >= MIN_EXTEND && region == 0);
|
2014-09-03 19:43:23 +02:00
|
|
|
#endif
|
2014-08-31 22:42:45 +02:00
|
|
|
|
|
|
|
/* Make sure that the memory region are properly aligned */
|
|
|
|
|
|
|
|
blockstart = (uintptr_t)mem;
|
|
|
|
blockend = blockstart + size;
|
|
|
|
|
|
|
|
DEBUGASSERT(MM_ALIGN_UP(blockstart) == blockstart);
|
|
|
|
DEBUGASSERT(MM_ALIGN_DOWN(blockend) == blockend);
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* Take the memory manager mutex */
|
2014-08-31 22:42:45 +02:00
|
|
|
|
2022-11-05 23:41:26 +01:00
|
|
|
DEBUGVERIFY(mm_lock(heap));
|
2014-08-31 22:42:45 +02:00
|
|
|
|
|
|
|
/* Get the terminal node in the old heap. The block to extend must
|
|
|
|
* immediately follow this node.
|
|
|
|
*/
|
|
|
|
|
2021-07-03 16:16:25 +02:00
|
|
|
oldnode = heap->mm_heapend[region];
|
2023-09-06 05:48:30 +02:00
|
|
|
DEBUGASSERT((uintptr_t)oldnode + MM_SIZEOF_ALLOCNODE == blockstart);
|
2014-08-31 22:42:45 +02:00
|
|
|
|
|
|
|
/* The size of the old node now extends to the new terminal node.
|
2023-09-06 05:48:30 +02:00
|
|
|
* This is the old size (MM_SIZEOF_ALLOCNODE) plus the size of
|
2014-08-31 22:42:45 +02:00
|
|
|
* the block (size) minus the size of the new terminal node
|
2023-09-06 05:48:30 +02:00
|
|
|
* (MM_SIZEOF_ALLOCNODE) or simply:
|
2014-08-31 22:42:45 +02:00
|
|
|
*/
|
|
|
|
|
2022-12-15 12:55:16 +01:00
|
|
|
oldnode->size = size | (oldnode->size & MM_MASK_BIT);
|
2014-08-31 22:42:45 +02:00
|
|
|
|
|
|
|
/* The old node should already be marked as allocated */
|
|
|
|
|
2023-09-06 06:30:24 +02:00
|
|
|
DEBUGASSERT(MM_NODE_IS_ALLOC(oldnode));
|
2014-08-31 22:42:45 +02:00
|
|
|
|
|
|
|
/* Get and initialize the new terminal node in the heap */
|
|
|
|
|
2022-12-22 09:02:42 +01:00
|
|
|
newnode = (FAR struct mm_allocnode_s *)
|
2023-09-06 05:48:30 +02:00
|
|
|
(blockend - MM_SIZEOF_ALLOCNODE);
|
|
|
|
newnode->size = MM_SIZEOF_ALLOCNODE | MM_ALLOC_BIT;
|
2014-08-31 22:42:45 +02:00
|
|
|
|
2021-07-03 16:16:25 +02:00
|
|
|
heap->mm_heapend[region] = newnode;
|
2023-01-19 07:32:56 +01:00
|
|
|
|
|
|
|
/* Finally, increase the total heap size accordingly */
|
|
|
|
|
|
|
|
heap->mm_heapsize += size;
|
2022-09-06 08:18:45 +02:00
|
|
|
mm_unlock(heap);
|
2014-08-31 22:42:45 +02:00
|
|
|
|
|
|
|
/* Finally "free" the new block of memory where the old terminal node was
|
|
|
|
* located.
|
|
|
|
*/
|
|
|
|
|
2023-01-05 18:28:13 +01:00
|
|
|
mm_free(heap, mem);
|
2014-08-31 22:42:45 +02:00
|
|
|
}
|