From f4e1b9fed77359b10424f1be0ee459d4818b78e5 Mon Sep 17 00:00:00 2001 From: liwenxiang1 Date: Mon, 22 Apr 2024 10:52:09 +0800 Subject: [PATCH] mm/mm_heap: Optimizing heap performance, changing the mm_size2ndx and mm-addfreechunk functions to inline functions can improve by about 7% Signed-off-by: liwenxiang1 --- mm/mm_heap/CMakeLists.txt | 2 - mm/mm_heap/Make.defs | 2 +- mm/mm_heap/mm.h | 61 +++++++++++++++++++++++---- mm/mm_heap/mm_addfreechunk.c | 82 ------------------------------------ mm/mm_heap/mm_size2ndx.c | 57 ------------------------- 5 files changed, 53 insertions(+), 151 deletions(-) delete mode 100644 mm/mm_heap/mm_addfreechunk.c delete mode 100644 mm/mm_heap/mm_size2ndx.c diff --git a/mm/mm_heap/CMakeLists.txt b/mm/mm_heap/CMakeLists.txt index 234feb5dee457..ab32bbeb0bb24 100644 --- a/mm/mm_heap/CMakeLists.txt +++ b/mm/mm_heap/CMakeLists.txt @@ -27,8 +27,6 @@ if(CONFIG_MM_DEFAULT_MANAGER) set(SRCS mm_initialize.c mm_lock.c - mm_addfreechunk.c - mm_size2ndx.c mm_malloc_size.c mm_shrinkchunk.c mm_brkaddr.c diff --git a/mm/mm_heap/Make.defs b/mm/mm_heap/Make.defs index bfa4cd421e94c..099d89c966690 100644 --- a/mm/mm_heap/Make.defs +++ b/mm/mm_heap/Make.defs @@ -24,7 +24,7 @@ ifeq ($(CONFIG_MM_DEFAULT_MANAGER),y) -CSRCS += mm_initialize.c mm_lock.c mm_addfreechunk.c mm_size2ndx.c +CSRCS += mm_initialize.c mm_lock.c CSRCS += mm_malloc_size.c mm_shrinkchunk.c mm_brkaddr.c mm_calloc.c CSRCS += mm_extend.c mm_free.c mm_mallinfo.c mm_malloc.c mm_foreach.c CSRCS += mm_memalign.c mm_realloc.c mm_zalloc.c mm_heapmember.c mm_memdump.c diff --git a/mm/mm_heap/mm.h b/mm/mm_heap/mm.h index 80c59c316ea87..360f450babc4c 100644 --- a/mm/mm_heap/mm.h +++ b/mm/mm_heap/mm.h @@ -292,15 +292,6 @@ void mm_unlock(FAR struct mm_heap_s *heap); void mm_shrinkchunk(FAR struct mm_heap_s *heap, FAR struct mm_allocnode_s *node, size_t size); -/* Functions contained in mm_addfreechunk.c *********************************/ - -void mm_addfreechunk(FAR struct mm_heap_s *heap, - FAR struct mm_freenode_s *node); - -/* Functions contained in mm_size2ndx.c *************************************/ - -int mm_size2ndx(size_t size); - /* Functions contained in mm_foreach.c **************************************/ void mm_foreach(FAR struct mm_heap_s *heap, mm_node_handler_t handler, @@ -310,4 +301,56 @@ void mm_foreach(FAR struct mm_heap_s *heap, mm_node_handler_t handler, void mm_delayfree(FAR struct mm_heap_s *heap, FAR void *mem, bool delay); +/**************************************************************************** + * Inline Functions + ****************************************************************************/ + +static inline_function int mm_size2ndx(size_t size) +{ + DEBUGASSERT(size >= MM_MIN_CHUNK); + if (size >= MM_MAX_CHUNK) + { + return MM_NNODES - 1; + } + + size >>= MM_MIN_SHIFT; + return flsl(size) - 1; +} + +static inline_function void mm_addfreechunk(FAR struct mm_heap_s *heap, + FAR struct mm_freenode_s *node) +{ + FAR struct mm_freenode_s *next; + FAR struct mm_freenode_s *prev; + size_t nodesize = MM_SIZEOF_NODE(node); + int ndx; + + DEBUGASSERT(nodesize >= MM_MIN_CHUNK); + DEBUGASSERT(MM_NODE_IS_FREE(node)); + + /* Convert the size to a nodelist index */ + + ndx = mm_size2ndx(nodesize); + + /* Now put the new node into the next */ + + for (prev = &heap->mm_nodelist[ndx], + next = heap->mm_nodelist[ndx].flink; + next && next->size && MM_SIZEOF_NODE(next) < nodesize; + 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; + } +} + #endif /* __MM_MM_HEAP_MM_H */ diff --git a/mm/mm_heap/mm_addfreechunk.c b/mm/mm_heap/mm_addfreechunk.c deleted file mode 100644 index 1e2de70495035..0000000000000 --- a/mm/mm_heap/mm_addfreechunk.c +++ /dev/null @@ -1,82 +0,0 @@ -/**************************************************************************** - * mm/mm_heap/mm_addfreechunk.c - * - * SPDX-License-Identifier: Apache-2.0 - * - * 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 - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * 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. - * - ****************************************************************************/ - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include - -#include - -#include - -#include "mm_heap/mm.h" - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: mm_addfreechunk - * - * Description: - * Add a free chunk to the nodes list. It is assumed that the caller holds - * the mm mutex. - * - ****************************************************************************/ - -void mm_addfreechunk(FAR struct mm_heap_s *heap, - FAR struct mm_freenode_s *node) -{ - FAR struct mm_freenode_s *next; - FAR struct mm_freenode_s *prev; - size_t nodesize = MM_SIZEOF_NODE(node); - int ndx; - - DEBUGASSERT(nodesize >= MM_MIN_CHUNK); - DEBUGASSERT(MM_NODE_IS_FREE(node)); - - /* Convert the size to a nodelist index */ - - ndx = mm_size2ndx(nodesize); - - /* Now put the new node into the next */ - - for (prev = &heap->mm_nodelist[ndx], - next = heap->mm_nodelist[ndx].flink; - next && next->size && MM_SIZEOF_NODE(next) < nodesize; - 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; - } -} diff --git a/mm/mm_heap/mm_size2ndx.c b/mm/mm_heap/mm_size2ndx.c deleted file mode 100644 index 00fafc7c26dde..0000000000000 --- a/mm/mm_heap/mm_size2ndx.c +++ /dev/null @@ -1,57 +0,0 @@ -/**************************************************************************** - * mm/mm_heap/mm_size2ndx.c - * - * SPDX-License-Identifier: Apache-2.0 - * - * 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 - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * 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. - * - ****************************************************************************/ - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include - -#include - -#include - -#include "mm_heap/mm.h" - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: mm_size2ndx - * - * Description: - * Convert the size to a nodelist index. - * - ****************************************************************************/ - -int mm_size2ndx(size_t size) -{ - DEBUGASSERT(size >= MM_MIN_CHUNK); - if (size >= MM_MAX_CHUNK) - { - return MM_NNODES - 1; - } - - size >>= MM_MIN_SHIFT; - return flsl(size) - 1; -}