2012-07-15 07:31:12 +08:00
|
|
|
/****************************************************************************
|
2014-09-23 00:53:50 +08:00
|
|
|
* mm/mm_heap/mm_addfreechunk.c
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2024-09-12 22:44:55 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2021-02-08 20:50:10 +08: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 07:21:28 +08:00
|
|
|
*
|
2021-02-08 20:50:10 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2021-02-08 20:50:10 +08: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 07:21:28 +08:00
|
|
|
*
|
2012-07-15 07:31:12 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2012-07-15 07:31:12 +08:00
|
|
|
/****************************************************************************
|
2007-02-18 07:21:28 +08:00
|
|
|
* Included Files
|
2012-07-15 07:31:12 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2013-03-09 04:36:18 +08:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2020-02-13 16:06:05 +08:00
|
|
|
#include <assert.h>
|
|
|
|
|
2014-09-24 21:29:09 +08:00
|
|
|
#include <nuttx/mm/mm.h>
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2021-03-02 16:03:00 +08:00
|
|
|
#include "mm_heap/mm.h"
|
|
|
|
|
2012-07-15 07:31:12 +08:00
|
|
|
/****************************************************************************
|
2015-10-03 06:30:35 +08:00
|
|
|
* Public Functions
|
2012-07-15 07:31:12 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2012-07-15 07:31:12 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: mm_addfreechunk
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
|
|
|
* Description:
|
2021-07-31 23:17:21 +08:00
|
|
|
* Add a free chunk to the nodes list. It is assumed that the caller holds
|
2022-09-06 14:18:45 +08:00
|
|
|
* the mm mutex.
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2012-07-15 07:31:12 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2021-02-08 23:19:12 +08:00
|
|
|
void mm_addfreechunk(FAR struct mm_heap_s *heap,
|
|
|
|
FAR struct mm_freenode_s *node)
|
2007-02-18 07:21:28 +08:00
|
|
|
{
|
2007-02-28 05:17:21 +08:00
|
|
|
FAR struct mm_freenode_s *next;
|
|
|
|
FAR struct mm_freenode_s *prev;
|
2023-09-06 11:48:30 +08:00
|
|
|
size_t nodesize = MM_SIZEOF_NODE(node);
|
2020-02-20 05:23:26 +08:00
|
|
|
int ndx;
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2022-12-30 16:30:55 +08:00
|
|
|
DEBUGASSERT(nodesize >= MM_MIN_CHUNK);
|
2023-09-06 12:30:24 +08:00
|
|
|
DEBUGASSERT(MM_NODE_IS_FREE(node));
|
2020-02-13 16:06:05 +08:00
|
|
|
|
2007-02-18 07:21:28 +08:00
|
|
|
/* Convert the size to a nodelist index */
|
|
|
|
|
2022-12-15 19:55:16 +08:00
|
|
|
ndx = mm_size2ndx(nodesize);
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2020-02-12 12:03:22 +08:00
|
|
|
/* Now put the new node into the next */
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2021-07-03 22:16:25 +08:00
|
|
|
for (prev = &heap->mm_nodelist[ndx],
|
|
|
|
next = heap->mm_nodelist[ndx].flink;
|
2023-09-06 11:48:30 +08:00
|
|
|
next && next->size && MM_SIZEOF_NODE(next) < nodesize;
|
2007-02-18 07:21:28 +08:00
|
|
|
prev = next, next = next->flink);
|
|
|
|
|
|
|
|
/* Does it go in mid next or at the end? */
|
|
|
|
|
|
|
|
prev->flink = node;
|
|
|
|
node->blink = prev;
|
|
|
|
node->flink = next;
|
|
|
|
|
|
|
|
if (next)
|
|
|
|
{
|
|
|
|
/* The new node goes between prev and next */
|
|
|
|
|
|
|
|
next->blink = node;
|
|
|
|
}
|
|
|
|
}
|