lib: sbi: Support multiple heaps
PATCH 1/2 lib: sbi: Support multiple heaps
The upcoming SMMTT implementation will require some larger contiguous memory regions for the memory tracking tables. We plan to specify the memory region for these tables as a reserved-memory node in the device tree, and then dynamically allocate individual tables out of this region. These changes to the SBI heap allocator will allow us to explicitly create and allocate from a dedicated heap tied to the table memory region.
--- include/sbi/sbi_heap.h | 18 +++++ lib/sbi/sbi_heap.c | 146 +++++++++++++++++++++++++++-------------- 2 files changed, 113 insertions(+), 51 deletions(-)
include/sbi/sbi_heap.h +18 -0
@@ -12,6 +12,9 @@#include <sbi/sbi_types.h> +/* Opaque declaration of heap control struct */ +struct heap_control ; +/* Alignment of heap base address and size */ #define HEAP_BASE_ALIGN 1024
@@ -19,9 +22,11 @@ struct sbi_scratch;/** Allocate from heap area */ void *sbi_malloc (size_t size); +void *sbi_malloc_from (struct heap_control *hpctrl,size_t size);/** Zero allocate from heap area */ void *sbi_zalloc (size_t size); +void *sbi_zalloc_from (struct heap_control *hpctrl,size_t size);/** Allocate array from heap area */ static inline void *sbi_calloc (size_t nitems,size_t size)
@@ -29,19 +34,32 @@ static inline void *sbi_calloc(size_t nitems, size_t size)return sbi_zalloc (nitems * size); } +static inline void *sbi_calloc_from (struct heap_control *hpctrl, +size_t nitems,size_t size) +{ +return sbi_zalloc_from (hpctrl, nitems * size); +} +/** Free-up to heap area */ void sbi_free (void *ptr); +void sbi_free_from (struct heap_control *hpctrl,void *ptr);/** Amount (in bytes) of free space in the heap area */ unsigned long sbi_heap_free_space (void ); +unsigned long sbi_heap_free_space_from (struct heap_control *hpctrl);/** Amount (in bytes) of used space in the heap area */ unsigned long sbi_heap_used_space (void ); +unsigned long sbi_heap_used_space_from (struct heap_control *hpctrl);/** Amount (in bytes) of reserved space in the heap area */ unsigned long sbi_heap_reserved_space (void ); +unsigned long sbi_heap_reserved_space_from (struct heap_control *hpctrl);/** Initialize heap area */ int sbi_heap_init (struct sbi_scratch *scratch); +int sbi_heap_init_new (struct heap_control *hpctrl,unsigned long base, +unsigned long size); +int sbi_heap_alloc_new (struct heap_control **hpctrl);#endif
lib/sbi/sbi_heap.c +95 -51
257 lines not on this page · raw message
Signature · 1 line
2.45.2
PATCH 2/2 lib: sbi: Implement aligned memory allocators
This change adds a simple implementation of sbi_memalign(), for future use in allocating aligned memory for SMMTT tables.
--- include/sbi/sbi_heap.h | 5 +++++ lib/sbi/sbi_heap.c | 37 ++++++++++++++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 3 deletions(-)
include/sbi/sbi_heap.h +5 -0
@@ -24,6 +24,11 @@ struct sbi_scratch;void *sbi_malloc (size_t size);void *sbi_malloc_from (struct heap_control *hpctrl,size_t size); +/** Allocate aligned from heap area */ +void *sbi_memalign (size_t alignment,size_t size); +void *sbi_memalign_from (struct heap_control *hpctrl,size_t alignment, +size_t size); +/** Zero allocate from heap area */ void *sbi_zalloc (size_t size);void *sbi_zalloc_from (struct heap_control *hpctrl,size_t size);
lib/sbi/sbi_heap.c +34 -3
@@ -37,7 +37,8 @@ struct heap_control {static struct heap_control global_hpctrl; -void *sbi_malloc_from (struct heap_control *hpctrl,size_t size) +static void *alloc_with_align (struct heap_control *hpctrl, +size_t align,size_t size) {void *ret =NULL ;struct heap_node *n, *np;
@@ -45,8 +46,8 @@ void *sbi_malloc_from(struct heap_control *hpctrl, size_t size)if (!size)return NULL ; - size +=HEAP_ALLOC_ALIGN -1 ; - size &= ~((unsigned long )HEAP_ALLOC_ALIGN -1 ); + size += align -1 ; + size &= ~((unsigned long )align -1 );spin_lock (&hpctrl->lock);
@@ -80,11 +81,41 @@ void *sbi_malloc_from(struct heap_control *hpctrl, size_t size)return ret; } +void *sbi_malloc_from (struct heap_control *hpctrl,size_t size) +{ +return alloc_with_align (hpctrl,HEAP_ALLOC_ALIGN , size); +} +void *sbi_malloc (size_t size) {return sbi_malloc_from (&global_hpctrl, size); } +void *sbi_memalign_from (struct heap_control *hpctrl,size_t alignment, +size_t size) +{ +if (alignment <HEAP_ALLOC_ALIGN ) { + alignment =HEAP_ALLOC_ALIGN ; + } + +// Make sure alignment is power of two +if ((alignment & (alignment -1 )) !=0 ) { +return NULL ; + } + +// Make sure size is multiple of alignment +if (size % alignment !=0 ) { +return NULL ; + } + +return alloc_with_align (hpctrl, alignment, size); +} + +void *sbi_memalign (size_t alignment,size_t size) +{ +return sbi_memalign_from (&global_hpctrl, alignment, size); +} +void *sbi_zalloc_from (struct heap_control *hpctrl,size_t size) {void *ret =sbi_malloc_from (hpctrl, size);
Signature · 1 line
2.45.2