History

First commit.

mulshine committed 7 years ago

Commit 127a1131 parent 824e8d1147 files · +49021 −-7Browse files

Showing the changes to LEAF/Src_cpp/leaf-mempool.cpp (1 of 147 files). Show all

Unified · Split

LEAF/Src_cpp/leaf-mempool.cpp+193−0
@@ -0,0 +1,193 @@
1+
2+/** mpool source significantly modified by Mike Mulshine, Jeff Snyder, et al., Princeton University Music Department **/
3+
4+/**
5+ In short, mpool is distributed under so called "BSD license",
6+
7+ Copyright (c) 2009-2010 Tatsuhiko Kubo <cubicdaiya@gmail.com>
8+ All rights reserved.
9+
10+ Redistribution and use in source and binary forms, with or without modification,
11+ are permitted provided that the following conditions are met:
12+
13+ * Redistributions of source code must retain the above copyright notice,
14+ this list of conditions and the following disclaimer.
15+
16+ * Redistributions in binary form must reproduce the above copyright notice,
17+ this list of conditions and the following disclaimer in the documentation
18+ and/or other materials provided with the distribution.
19+
20+ * Neither the name of the authors nor the names of its contributors
21+ may be used to endorse or promote products derived from this software
22+ without specific prior written permission.
23+
24+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28+ OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
30+ TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35+*/
36+
37+/* written with C99 style */
38+
39+#include "../Inc/leaf-mempool.h"
40+
41+
42+#define NUM_BLOCKS 50
43+
44+char memory[MPOOL_POOL_SIZE];
45+mpool_pool_t blocks[NUM_BLOCKS]; //
46+mpool_t leaf_pool;
47+
48+/**
49+ * private function
50+ */
51+static inline size_t mpool_align(size_t size);
52+
53+
54+/**
55+ * create memory pool
56+ */
57+void mpool_create (size_t size, mpool_t* pool)
58+{
59+ if (size > MPOOL_POOL_SIZE) size = MPOOL_POOL_SIZE;
60+
61+ pool->mpool = &blocks[0];
62+
63+ pool->mpool->pool = (void*)memory;
64+ pool->mpool->size = size;
65+ pool->mpool->used = 0;
66+
67+ pool->usize = 0;
68+ pool->msize = size;
69+
70+ for (int i = 0; i < MPOOL_POOL_SIZE; i++) memory[i]=0;
71+}
72+
73+void leaf_pool_init(size_t size)
74+{
75+ mpool_create(size, &leaf_pool);
76+}
77+
78+/**
79+ * allocate memory from memory pool
80+ */
81+void* mpool_alloc(size_t size, mpool_t* pool)
82+{
83+ pool->next += 1;
84+
85+ if (pool->next >= NUM_BLOCKS) return NULL;
86+
87+ mpool_pool_t* new_chunk = &blocks[pool->next];
88+ new_chunk->size = mpool_align(size);
89+
90+ int idx = pool->next - 1;
91+ for (mpool_pool_t *old_chunk = &blocks[idx]; idx >= 0; old_chunk = &blocks[--idx])
92+ {
93+ if (old_chunk->used == 0)
94+ {
95+ if (new_chunk->size < old_chunk->size)
96+ {
97+ // set memory start pointer of this chunk to that of old chunk, mark new chunk used
98+ new_chunk->pool = old_chunk->pool;
99+ new_chunk->used = 1;
100+
101+ // increment memory start pointer of old chunk
102+ char* ptr = (char*)old_chunk->pool;
103+ ptr += new_chunk->size;
104+ old_chunk->pool = (void*)ptr;
105+
106+ // decrement size of old chunk by size of new chunk
107+ old_chunk->size -= new_chunk->size;
108+
109+ // increment used size of whole mempool by size of new chunk
110+ pool->usize += new_chunk->size;
111+
112+ // return memory pointer of new chunk
113+ return new_chunk->pool;
114+ }
115+ }
116+ }
117+
118+ // if not enough space anywhere, return NULL
119+ return NULL;
120+}
121+
122+void* leaf_alloc(size_t size)
123+{
124+ return mpool_alloc(size, &leaf_pool);
125+}
126+
127+void mpool_free(void* ptr, mpool_t* pool)
128+{
129+ //printf("finding %p\n", ptr);
130+
131+ int idx = 0;
132+ for (mpool_pool_t *block = &blocks[0]; idx < NUM_BLOCKS; block = &blocks[++idx])
133+ {
134+ if (block->pool == ptr)
135+ {
136+ // Mark unused
137+ block->used = 0;
138+
139+
140+ // Format to all zeros (kinda arbitrary, but possibly handy / needed)
141+ char* buff = (char*)block->pool;
142+ for (int i = 0; i < block->size; i++)
143+ {
144+ buff[i] = 0;
145+ }
146+
147+ // decrement overall pool size
148+ pool->usize -= block->size;
149+
150+ break;
151+ }
152+ }
153+}
154+
155+void leaf_free(void* ptr)
156+{
157+ mpool_free(ptr, &leaf_pool);
158+}
159+
160+size_t mpool_get_size(mpool_t* pool)
161+{
162+ return pool->msize;
163+}
164+
165+size_t leaf_pool_get_size(void)
166+{
167+ return mpool_get_size(&leaf_pool);
168+}
169+
170+size_t mpool_get_used(mpool_t* pool)
171+{
172+ return pool->usize;
173+}
174+
175+size_t leaf_pool_get_used(void)
176+{
177+ return mpool_get_used(&leaf_pool);
178+}
179+
180+void* leaf_pool_get_pool(void)
181+{
182+ float* buff = (float*)memory;
183+
184+ return buff;
185+}
186+
187+/**
188+ * align byte boundary
189+ */
190+static inline size_t mpool_align(size_t size) {
191+ return (size + (MPOOL_ALIGN_SIZE - 1)) & ~(MPOOL_ALIGN_SIZE - 1);
192+}
193+

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t