From: batt Date: Mon, 19 Apr 2010 22:49:56 +0000 (+0000) Subject: Heap: add heap_freeSpace(), a new function. X-Git-Tag: 2.5.0~438 X-Git-Url: https://codewiz.org/gitweb?a=commitdiff_plain;h=f884c67ed85598875ef683987323fd6085e01e14;p=bertos.git Heap: add heap_freeSpace(), a new function. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@3470 38d2e660-2303-0410-9eaa-f027e97ec537 --- diff --git a/bertos/struct/heap.c b/bertos/struct/heap.c index b8d06701..1e4d6053 100644 --- a/bertos/struct/heap.c +++ b/bertos/struct/heap.c @@ -184,6 +184,25 @@ void heap_freemem(struct Heap* h, void *mem, size_t size) } } +/** + * Returns the number of free bytes in a heap. + * \param h the heap to check. + * + * \note The returned value is the sum of all free memory regions + * in the heap. + * Those regions are likely to be *not* contiguous, + * so a successive allocation may fail even if the + * requested amount of memory is lower than the current free space. + */ +size_t heap_freeSpace(struct Heap *h) +{ + size_t free_mem = 0; + for (MemChunck *chunk = h->FreeList; hunk; chunk = chunk->next) + free_mem += chunck->size; + + return free_mem; +} + #if CONFIG_HEAP_MALLOC void *heap_malloc(struct Heap* h, size_t size) diff --git a/bertos/struct/heap.h b/bertos/struct/heap.h index ad362a6b..efd02c28 100644 --- a/bertos/struct/heap.h +++ b/bertos/struct/heap.h @@ -85,6 +85,7 @@ void *heap_allocmem(struct Heap* heap, size_t size); /// Free a chunk of memory of \a size bytes from the heap void heap_freemem(struct Heap* heap, void *mem, size_t size); +size_t heap_freeSpace(struct Heap *h); #define HNEW(heap, type) \ (type*)heap_allocmem(heap, sizeof(type))