Skip to content

Commit

Permalink
mm/mm_heap: Optimizing heap performance, changing the mm_size2ndx and…
Browse files Browse the repository at this point in the history
… mm-addfreechunk functions to inline functions can improve by about 7%

Signed-off-by: liwenxiang1 <[email protected]>
  • Loading branch information
xianglyc committed Oct 12, 2024
1 parent a2b129f commit fe61bf0
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 151 deletions.
2 changes: 0 additions & 2 deletions mm/mm_heap/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion mm/mm_heap/Make.defs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
61 changes: 52 additions & 9 deletions mm/mm_heap/mm.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 */
82 changes: 0 additions & 82 deletions mm/mm_heap/mm_addfreechunk.c

This file was deleted.

57 changes: 0 additions & 57 deletions mm/mm_heap/mm_size2ndx.c

This file was deleted.

0 comments on commit fe61bf0

Please sign in to comment.