From 84db21f62f852c7d775d09279ed7c88a8378d677 Mon Sep 17 00:00:00 2001 From: wangmingrong1 Date: Fri, 10 Jan 2025 11:47:42 +0800 Subject: [PATCH] mm: Add pending configuration for mm node struct and precding pending memory block node precding and the entire struct, after turning on this bit, the size of each memory block wil be aligned to MM_ALIGN Signed-off-by: wangmingrong1 --- mm/Kconfig | 8 ++++++++ mm/mm_heap/mm.h | 40 +++++++++++++++++++++++++++++++++------- mm/mm_heap/mm_realloc.c | 2 +- 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/mm/Kconfig b/mm/Kconfig index 356daa2196978..779b9d08165f1 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -69,6 +69,14 @@ config MM_DEFAULT_ALIGNMENT memory default alignment is equal to sizoef(uintptr), if this value is not 0, this value must be 2^n and at least sizeof(uintptr). +config MM_NODE_PENDING + bool "Enable pending memory node" + default n + ---help--- + Pending memory block node precding and the entire struct, + After turning on this bit, the size of each memory block + will be aligned to MM_ALIGN + config MM_SMALL bool "Small memory model" default n diff --git a/mm/mm_heap/mm.h b/mm/mm_heap/mm.h index 80e225afcb17d..0230f25d2027b 100644 --- a/mm/mm_heap/mm.h +++ b/mm/mm_heap/mm.h @@ -143,7 +143,15 @@ * previous freenode */ -#define MM_ALLOCNODE_OVERHEAD (MM_SIZEOF_ALLOCNODE - sizeof(mmsize_t)) +#ifdef CONFIG_MM_NODE_PENDING +# define MM_NODE_PENDING aligned_data(MM_ALIGN) +# define MMSIZE_T_LEN MM_ALIGN +#else +# define MM_NODE_PENDING +# define MMSIZE_T_LEN sizeof(mmsize_t) +#endif + +#define MM_ALLOCNODE_OVERHEAD (MM_SIZEOF_ALLOCNODE - MMSIZE_T_LEN) /* Get the node size */ @@ -173,8 +181,17 @@ typedef size_t mmsize_t; struct mm_allocnode_s { - mmsize_t preceding; /* Physical preceding chunk size */ - mmsize_t size; /* Size of this chunk */ + union + { + mmsize_t preceding; /* Physical preceding chunk size */ + uint8_t preceding_align[MMSIZE_T_LEN]; + }; + union + { + mmsize_t size; /* Physical preceding chunk size */ + uint8_t size_align[MMSIZE_T_LEN]; + }; + #if CONFIG_MM_BACKTRACE >= 0 pid_t pid; /* The pid for caller */ unsigned long seqno; /* The sequence of memory malloc */ @@ -182,14 +199,23 @@ struct mm_allocnode_s FAR void *backtrace[CONFIG_MM_BACKTRACE]; /* The backtrace buffer for caller */ # endif #endif -}; +}MM_NODE_PENDING; /* This describes a free chunk */ struct mm_freenode_s { - mmsize_t preceding; /* Physical preceding chunk size */ - mmsize_t size; /* Size of this chunk */ + union + { + mmsize_t preceding; /* Physical preceding chunk size */ + uint8_t preceding_align[MMSIZE_T_LEN]; + }; + union + { + mmsize_t size; /* Physical preceding chunk size */ + uint8_t size_align[MMSIZE_T_LEN]; + }; + #if CONFIG_MM_BACKTRACE >= 0 pid_t pid; /* The pid for caller */ unsigned long seqno; /* The sequence of memory malloc */ @@ -199,7 +225,7 @@ struct mm_freenode_s #endif FAR struct mm_freenode_s *flink; /* Supports a doubly linked list */ FAR struct mm_freenode_s *blink; -}; +}MM_NODE_PENDING; static_assert(MM_SIZEOF_ALLOCNODE <= MM_MIN_CHUNK, "Error size for struct mm_allocnode_s\n"); diff --git a/mm/mm_heap/mm_realloc.c b/mm/mm_heap/mm_realloc.c index d41619a66ad58..e1bcdef6abd45 100644 --- a/mm/mm_heap/mm_realloc.c +++ b/mm/mm_heap/mm_realloc.c @@ -153,7 +153,7 @@ FAR void *mm_realloc(FAR struct mm_heap_s *heap, FAR void *oldmem, heap->mm_curused += newsize - oldsize; mm_shrinkchunk(heap, oldnode, newsize); kasan_poison((FAR char *)oldnode + MM_SIZEOF_NODE(oldnode) + - sizeof(mmsize_t), oldsize - MM_SIZEOF_NODE(oldnode)); + MMSIZE_T_LEN, oldsize - MM_SIZEOF_NODE(oldnode)); } /* Then return the original address */