X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=bertos%2Fstruct%2Fheap.c;h=4c00bd7ca0ed1ebd3b24a6048b6b8f26b8112d24;hb=ed914dcd14bb340d61a8bc011ec0cd7a0312ed79;hp=33c6de2acadca599ee7057a4dc6710df16a4fba8;hpb=f2bfb561625f0efcaef53cb57276967e01fa20bd;p=bertos.git diff --git a/bertos/struct/heap.c b/bertos/struct/heap.c index 33c6de2a..4c00bd7c 100644 --- a/bertos/struct/heap.c +++ b/bertos/struct/heap.c @@ -32,7 +32,6 @@ * * \brief Heap subsystem (public interface). * - * \version $Id$ * \author Bernie Innocenti */ @@ -48,7 +47,7 @@ /* * This function prototype is deprecated, will change in: * void heap_init(struct Heap* h, heap_buf_t* memory, size_t size) - * in the nex BeRTOS release. + * in the next BeRTOS release. */ void heap_init(struct Heap* h, void* memory, size_t size) { @@ -127,9 +126,9 @@ void heap_freemem(struct Heap* h, void *mem, size_t size) if (!size) size = sizeof(MemChunk); - /* Special case: first chunk in the free list */ + /* Special cases: first chunk in the free list or memory completely full */ ASSERT((uint8_t*)mem != (uint8_t*)h->FreeList); - if (((uint8_t *)mem) < ((uint8_t *)h->FreeList)) + if (((uint8_t *)mem) < ((uint8_t *)h->FreeList) || !h->FreeList) { /* Insert memory block before the current free list head */ prev = (MemChunk *)mem; @@ -184,8 +183,30 @@ 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 (MemChunk *chunk = h->FreeList; chunk; chunk = chunk->next) + free_mem += chunk->size; + + return free_mem; +} + #if CONFIG_HEAP_MALLOC +/** + * Standard malloc interface + */ void *heap_malloc(struct Heap* h, size_t size) { size_t *mem; @@ -197,6 +218,9 @@ void *heap_malloc(struct Heap* h, size_t size) return mem; } +/** + * Standard calloc interface + */ void *heap_calloc(struct Heap* h, size_t size) { void *mem;