Reformat.
[bertos.git] / bertos / struct / heap.c
index b8d06701cbc04c9dd8766cf71743d1d3ae019f49..1faacf3c8a9078f60f0c4ab7611596bd51507262 100644 (file)
@@ -32,7 +32,6 @@
  *
  * \brief Heap subsystem (public interface).
  *
- * \version $Id$
  * \author Bernie Innocenti <bernie@codewiz.org>
  */
 
@@ -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)
 {
@@ -184,6 +183,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)