dmalloc icon indicating copy to clipboard operation
dmalloc copied to clipboard

Mulitple discontinuous memory region support

Open xiaoxiang781216 opened this issue 4 years ago • 0 comments

In the embeded system without MMU, it's very common that many memory regions exist, so It's will be very useful if dmalloc can support it. Actually, we have sucessfully ported dmalloc to a popular RTOS(https://github.com/apache/incubator-nuttx) which has a clean multiple heap interface(https://github.com/apache/incubator-nuttx/blob/master/include/nuttx/mm/mm.h):

void mm_initialize(FAR struct mm_heap_s *heap, FAR void *heap_start,
                   size_t heap_size);
void mm_addregion(FAR struct mm_heap_s *heap, FAR void *heapstart,
                  size_t heapsize);
FAR void *mm_malloc(FAR struct mm_heap_s *heap, size_t size);
void mm_free(FAR struct mm_heap_s *heap, FAR void *mem);
FAR void *mm_realloc(FAR struct mm_heap_s *heap, FAR void *oldmem,
                     size_t size);
FAR void *mm_calloc(FAR struct mm_heap_s *heap, size_t n, size_t elem_size);
FAR void *mm_zalloc(FAR struct mm_heap_s *heap, size_t size);
FAR void *mm_memalign(FAR struct mm_heap_s *heap, size_t alignment,
                      size_t size);
bool mm_heapmember(FAR struct mm_heap_s *heap, FAR void *mem);
FAR void *mm_brkaddr(FAR struct mm_heap_s *heap, int region);
FAR void *mm_sbrk(FAR struct mm_heap_s *heap, intptr_t incr,
                  uintptr_t maxbreak);
void mm_extend(FAR struct mm_heap_s *heap, FAR void *mem, size_t size,
               int region);
int mm_mallinfo(FAR struct mm_heap_s *heap, FAR struct mallinfo *info);

The idea is that:

  1. Move heap related global variables into one struct(e.g. dlmalloc_heap)
  2. Add a new set of API accept dlmalloc_heap as argument, e.g.:
extern
DMALLOC_PNT	dmalloc_heap_malloc(dlmalloc_heap *heap, const char *file, const int line, 
			       const DMALLOC_SIZE size, const int func_id,
			       const DMALLOC_SIZE alignment,
			       const int xalloc_b);
  1. Define a global default heap and implement dmalloc_malloc like this:
static dlmalloc_heap heap:

DMALLOC_PNT	dmalloc_malloc(const char *file, const int line,
			       const DMALLOC_SIZE size, const int func_id,
			       const DMALLOC_SIZE alignment,
			       const int xalloc_b)
{
	return dmalloc_heap_malloc(&heap, file, line, size, func_id, alignment, xalloc_b);
}

With this change install, it will be easy to replace dlmalloc in the embeded environment.

xiaoxiang781216 avatar Mar 31 '21 12:03 xiaoxiang781216