shithub: leaf

Download patch

ref: d1e38aa8820d43c4fda3dbb399805a232822a597
parent: 7618da1b969d617ad1e339fcabcf3a73afb33ceb
author: Matthew Wang <mjw7@princeton.edu>
date: Wed Feb 5 10:55:02 EST 2020

trying wrapping initToPool in init, freeFromPool in free

--- a/LEAF/Src/leaf-mempool.c
+++ b/LEAF/Src/leaf-mempool.c
@@ -2,37 +2,37 @@
 /** mpool source significantly modified by Mike Mulshine, Jeff Snyder, et al., Princeton University Music Department **/
 
 /**
-   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.
+ 
+ * 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 with C99 style */
 
@@ -48,7 +48,7 @@
 
 #endif
 
-mpool_t leaf_pool;
+tMempool leaf_pool;
 size_t header_size;
 
 /**
@@ -72,14 +72,14 @@
     pool->head = create_node(pool->mpool, NULL, NULL, pool->msize-header_size);
     
     for (int i = 0; i < pool->head->size; i++)
-	{
-		memory[i+header_size]=0;
-	}
+    {
+        memory[i+header_size]=0;
+    }
 }
 
 void leaf_pool_init(char* memory, size_t size)
 {
-    mpool_create(memory, size, &leaf_pool);
+    mpool_create(memory, size, &leaf_pool->pool);
 }
 
 /**
@@ -136,7 +136,7 @@
     delink_node(node_to_alloc);
     
     pool->usize += header_size + node_to_alloc->size;
-
+    
     // Return the pool of the allocated node;
     return node_to_alloc->pool;
 }
@@ -149,21 +149,21 @@
 {
     // If the head is NULL, the mempool is full
     if (pool->head == NULL) 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);
     mpool_node_t* node_to_alloc = pool->head;
-
+    
     // Traverse the free list for a large enough block
     while (node_to_alloc->size < size_to_alloc)
     {
         node_to_alloc = node_to_alloc->next;
-
+        
         // If we reach the end of the free list, there
         // are no blocks large enough, return NULL
         if (node_to_alloc == NULL) return NULL;
     }
-
+    
     // Create a new node after the node to be allocated if there is enough space
     mpool_node_t* new_node;
     size_t leftover = node_to_alloc->size - size_to_alloc;
@@ -181,10 +181,10 @@
     {
         // Add any leftover space to the allocated node to avoid fragmentation
         node_to_alloc->size += leftover;
-
+        
         new_node = node_to_alloc->next;
     }
-
+    
     // Update the head if we are allocating the first node of the free list
     // The head will be NULL if there is no space left
     if (pool->head == node_to_alloc)
@@ -191,10 +191,10 @@
     {
         pool->head = new_node;
     }
-
+    
     // Remove the allocated node from the free list
     delink_node(node_to_alloc);
-
+    
     pool->usize += header_size + node_to_alloc->size;
     // Format the new pool
     char* new_pool = (char*)node_to_alloc->pool;
@@ -206,10 +206,10 @@
 void* leaf_alloc(size_t size)
 {
     //printf("alloc %i\n", size);
-	void* block = mpool_alloc(size, &leaf_pool);
-
-	if (block == NULL) leaf_mempool_overrun();
-
+    void* block = mpool_alloc(size, &leaf_pool->pool);
+    
+    if (block == NULL) leaf_mempool_overrun();
+    
     return block;
 }
 
@@ -216,11 +216,11 @@
 void* leaf_allocAndClear(size_t size)
 {
     //printf("alloc %i\n", size);
-	void* block = mpool_allocAndClear(size, &leaf_pool);
-
-	if (block == NULL) leaf_mempool_overrun();
-
-
+    void* block = mpool_allocAndClear(size, &leaf_pool->pool);
+    
+    if (block == NULL) leaf_mempool_overrun();
+    
+    
     return block;
 }
 
@@ -282,13 +282,13 @@
     pool->head = freed_node;
     
     // Format the freed pool
-//    char* freed_pool = (char*)freed_node->pool;
-//    for (int i = 0; i < freed_node->size; i++) freed_pool[i] = 0;
+    //    char* freed_pool = (char*)freed_node->pool;
+    //    for (int i = 0; i < freed_node->size; i++) freed_pool[i] = 0;
 }
 
 void leaf_free(void* ptr)
 {
-    mpool_free(ptr, &leaf_pool);
+    mpool_free(ptr, &leaf_pool->pool);
 }
 
 size_t mpool_get_size(mpool_t* pool)
@@ -303,17 +303,17 @@
 
 size_t leaf_pool_get_size(void)
 {
-    return mpool_get_size(&leaf_pool);
+    return mpool_get_size(&leaf_pool->pool);
 }
 
 size_t leaf_pool_get_used(void)
 {
-    return mpool_get_used(&leaf_pool);
+    return mpool_get_used(&leaf_pool->pool);
 }
 
 void* leaf_pool_get_pool(void)
 {
-    float* buff = (float*)leaf_pool.mpool;
+    float* buff = (float*)leaf_pool->pool.mpool;
     
     return buff;
 }
@@ -357,21 +357,17 @@
 
 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.
+    //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);
+    tMempool_initToPool(mp, memory, size, &leaf_pool);
 }
 
 void tMempool_free(tMempool* const mp)
 {
-    _tMempool* m = *mp;
-    
-    leaf_free(m);
+    tMempool_freeFromPool(mp, &leaf_pool);
 }
 
 void    tMempool_initToPool     (tMempool* const mp, char* memory, size_t size, tMempool* const mem)