shithub: leaf

Download patch

ref: 45cc438e24bfadaf1b412364492237fe65f977f1
parent: b40f3a777e9dacbfc30351349a1c7d3a632f1d8e
author: spiricom <jeff@snyderphonics.com>
date: Mon May 18 16:52:42 EDT 2020

updated to include mempool overrun error reporting to user

binary files a/.DS_Store b/.DS_Store differ
--- a/LEAF/Src/leaf-mempool.c
+++ b/LEAF/Src/leaf-mempool.c
@@ -87,7 +87,11 @@
 void* mpool_alloc(size_t asize, _tMempool* pool)
 {
     // If the head is NULL, the mempool is full
-    if (pool->head == NULL) return NULL;
+    if (pool->head == NULL)
+    {
+        leaf_mempool_overrun();
+        return NULL;
+    }
     
     // Should we alloc the first block large enough or check all blocks and pick the one closest in size?
     size_t size_to_alloc = mpool_align(asize);
@@ -100,7 +104,11 @@
         
         // If we reach the end of the free list, there
         // are no blocks large enough, return NULL
-        if (node_to_alloc == NULL) return NULL;
+        if (node_to_alloc == NULL)
+        {
+            leaf_mempool_overrun();
+            return NULL;
+        }
     }
     
     // Create a new node after the node to be allocated if there is enough space
@@ -153,7 +161,11 @@
 void* mpool_calloc(size_t asize, _tMempool* pool)
 {
     // If the head is NULL, the mempool is full
-    if (pool->head == NULL) return NULL;
+    if (pool->head == NULL)
+    {
+        leaf_mempool_overrun();
+        return NULL;
+    }
     
     // Should we alloc the first block large enough or check all blocks and pick the one closest in size?
     size_t size_to_alloc = mpool_align(asize);
@@ -166,7 +178,11 @@
         
         // If we reach the end of the free list, there
         // are no blocks large enough, return NULL
-        if (node_to_alloc == NULL) return NULL;
+        if (node_to_alloc == NULL)
+        {
+            leaf_mempool_overrun();
+            return NULL;
+        }
     }
     
     // Create a new node after the node to be allocated if there is enough space
@@ -362,7 +378,8 @@
 
 void leaf_mempool_overrun(void)
 {
-    //TODO: we should set up some kind of leaf_error method that reaches user space to notify library users of things that failed.
+    LEAF_error(1);
+    //TODO: we should make a set of real error codes that are in an enum type
 }
 
 void tMempool_init(tMempool* const mp, char* memory, size_t size)
--- a/LEAF/Src/leaf.c
+++ b/LEAF/Src/leaf.c
@@ -52,3 +52,9 @@
     return leaf.sampleRate;
 }
 
+
+//implement a function called this in your user code to catch errors
+__attribute__((weak)) uint8_t LEAF_error(uint8_t whichone)
+{
+    return whichone;
+}
--- a/LEAF/leaf.h
+++ b/LEAF/leaf.h
@@ -125,6 +125,9 @@
      */
     float       LEAF_getSampleRate   (void);
     
+
+    __attribute__((weak)) uint8_t LEAF_error(uint8_t whichone);
+    
     /*!
      * @}
      */