shithub: leaf

Download patch

ref: b86da099957faf3a3a2c6d30c8b65f13d69f38d6
parent: bc99f48aca15efd07cb81853172b95e83076314f
author: Matthew Wang <mjw7@princeton.edu>
date: Thu Jan 23 05:47:31 EST 2020

tMempool object for allocating objects to other memory locations

--- a/LEAF/Inc/leaf-mempool.h
+++ b/LEAF/Inc/leaf-mempool.h
@@ -1,38 +1,38 @@
 /*==============================================================================
  
-   In short, mpool is distributed under so called "BSD license",
-   
-   Copyright (c) 2009-2010 Tatsuhiko Kubo <cubicdaiya@gmail.com>
-   All rights reserved.
-   
-   Redistribution and use in source and binary forms, with or without modification,
-   are permitted provided that the following conditions are met:
-   
-   * Redistributions of source code must retain the above copyright notice,
-   this list of conditions and the following disclaimer.
-   
-   * Redistributions in binary form must reproduce the above copyright notice,
-   this list of conditions and the following disclaimer in the documentation
-   and/or other materials provided with the distribution.
-   
-   * Neither the name of the authors nor the names of its contributors
-   may be used to endorse or promote products derived from this software 
-   without specific prior written permission.
-   
-   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
-   TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
-   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
-   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
-   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
-   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ In short, mpool is distributed under so called "BSD license",
+ 
+ Copyright (c) 2009-2010 Tatsuhiko Kubo <cubicdaiya@gmail.com>
+ All rights reserved.
+ 
+ Redistribution and use in source and binary forms, with or without modification,
+ are permitted provided that the following conditions are met:
+ 
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
  
-   written by C99 style
-==============================================================================*/
+ * Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+ 
+ * Neither the name of the authors nor the names of its contributors
+ may be used to endorse or promote products derived from this software
+ without specific prior written permission.
+ 
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
+ TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ 
+ written by C99 style
+ ==============================================================================*/
 
 #ifndef LEAF_MPOOL_H_INCLUDED
 #define LEAF_MPOOL_H_INCLUDED
@@ -41,60 +41,72 @@
 extern "C" {
 #endif
     
-//==============================================================================
-
+    //==============================================================================
+    
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdbool.h>
     
-//==============================================================================
-
+    //==============================================================================
+    
 #define MPOOL_ALIGN_SIZE (8)
-
-//#define size_t unsigned long
-      
-/**
- * memory pool structure
- */
-
-// node of free list
-typedef struct mpool_node_t {
-    void                *pool;     // memory pool field
-    struct mpool_node_t *next;     // next node pointer
-    struct mpool_node_t *prev;     // prev node pointer
-    size_t size;
-} mpool_node_t;
-
-typedef struct mpool_t {
-    void*         mpool;       // start of the mpool
-    size_t        usize;       // used size of the pool
-    size_t        msize;       // max size of the pool
-    mpool_node_t* head;        // first node of memory pool free list
-} mpool_t;
-
-void mpool_create (char* memory, size_t size, mpool_t* pool);
-
-void *mpool_alloc(size_t size, mpool_t* pool);
-void mpool_free(void* ptr, mpool_t* pool);
-
-size_t mpool_get_size(mpool_t* pool);
-size_t mpool_get_used(mpool_t* pool);
-
-void leaf_pool_init(char* memory, size_t size);
-
-void* leaf_alloc(size_t size);
-void leaf_free(void* ptr);
-
-size_t leaf_pool_get_size(void);
-size_t leaf_pool_get_used(void);
-
-void* leaf_pool_get_pool(void);
-
-void leaf_mempool_overrun(void);
     
-//==============================================================================
-
+    //#define size_t unsigned long
+    
+    /**
+     * memory pool structure
+     */
+    
+    // node of free list
+    typedef struct mpool_node_t {
+        void                *pool;     // memory pool field
+        struct mpool_node_t *next;     // next node pointer
+        struct mpool_node_t *prev;     // prev node pointer
+        size_t size;
+    } mpool_node_t;
+    
+    typedef struct mpool_t {
+        void*         mpool;       // start of the mpool
+        size_t        usize;       // used size of the pool
+        size_t        msize;       // max size of the pool
+        mpool_node_t* head;        // first node of memory pool free list
+    } mpool_t;
+    
+    void mpool_create (char* memory, size_t size, mpool_t* pool);
+    
+    void *mpool_alloc(size_t size, mpool_t* pool);
+    void mpool_free(void* ptr, mpool_t* pool);
+    
+    size_t mpool_get_size(mpool_t* pool);
+    size_t mpool_get_used(mpool_t* pool);
+    
+    void leaf_pool_init(char* memory, size_t size);
+    
+    void* leaf_alloc(size_t size);
+    void leaf_free(void* ptr);
+    
+    size_t leaf_pool_get_size(void);
+    size_t leaf_pool_get_used(void);
+    
+    void* leaf_pool_get_pool(void);
+    
+    void leaf_mempool_overrun(void);
+    
+    // User object for creating additional mempools
+    
+    typedef struct _tMempool
+    {
+        mpool_t* pool;
+    } _tMempool;
+    
+    typedef _tMempool* tMempool;
+    
+    void        tMempool_init      (tMempool* const, char* memory, size_t size);
+    void        tMempool_free      (tMempool* const);
+    
+    //==============================================================================
+    
 #ifdef __cplusplus
 }
 #endif
@@ -102,5 +114,6 @@
 #endif // LEAF_MPOOL_H
 
 //==============================================================================
+
 
 
--- a/LEAF/Inc/leaf-sampling.h
+++ b/LEAF/Inc/leaf-sampling.h
@@ -46,9 +46,9 @@
     typedef _tBuffer* tBuffer;
     
     void  tBuffer_init (tBuffer* const, uint32_t length);
-    void  tBuffer_init_locate (tBuffer* const sb, uint32_t length, mpool_t* pool);
+    void  tBuffer_initToPool (tBuffer* const sb, uint32_t length, tMempool* mp);
     void  tBuffer_free (tBuffer* const);
-    void  tBuffer_free_locate (tBuffer* const sb, mpool_t* pool);
+    void  tBuffer_freeFromPool (tBuffer* const sb, tMempool* mp);
     
     void  tBuffer_tick (tBuffer* const, float sample);
     
--- a/LEAF/Src/leaf-mempool.c
+++ b/LEAF/Src/leaf-mempool.c
@@ -77,8 +77,6 @@
 	}
 }
 
-
-
 void leaf_pool_init(char* memory, size_t size)
 {
     mpool_create(memory, size, &leaf_pool);
@@ -289,3 +287,16 @@
 	//TODO: we should set up some kind of leaf_error method that reaches user space to notify library users of things that failed.
 }
 
+void tMempool_init(tMempool* const mp, char* memory, size_t size)
+{
+    _tMempool* m = *mp = (_tMempool*) leaf_alloc(sizeof(_tMempool));
+    
+    mpool_create (memory, size, m->pool);
+}
+
+void tMempool_free(tMempool* const mp)
+{
+    _tMempool* m = *mp;
+    
+    leaf_free(m);
+}
--- a/LEAF/Src/leaf-sampling.c
+++ b/LEAF/Src/leaf-sampling.c
@@ -39,11 +39,12 @@
     tBuffer_clear(sb);
 }
 
-void  tBuffer_init_locate (tBuffer* const sb, uint32_t length, mpool_t* pool)
+void  tBuffer_initToPool (tBuffer* const sb, uint32_t length, tMempool* mp)
 {
-    _tBuffer* s = *sb = (_tBuffer*) mpool_alloc(sizeof(_tBuffer), pool);
+    _tMempool* m = *mp;
+    _tBuffer* s = *sb = (_tBuffer*) mpool_alloc(sizeof(_tBuffer), m->pool);
 
-    s->buff = (float*) mpool_alloc( sizeof(float) * length, pool);
+    s->buff = (float*) mpool_alloc( sizeof(float) * length, m->pool);
 
     s->bufferLength = length;
     s->recordedLength = 0;
@@ -61,12 +62,13 @@
     leaf_free(s);
 }
 
-void  tBuffer_free_locate (tBuffer* const sb, mpool_t* pool)
+void  tBuffer_freeFromPool (tBuffer* const sb, tMempool* mp)
 {
+    _tMempool* m = *mp;
     _tBuffer* s = *sb;
 
-    mpool_free(s->buff, pool);
-    mpool_free(s, pool);
+    mpool_free(s->buff, m->pool);
+    mpool_free(s, m->pool);
 }