History

add dynamic memory allocation config option, which turns mpool_alloc/free into malloc/free wrappers

Matthew Wang committed 5 years ago

Commit 98913b41 parent 4c8de0f8 files · +81 −30Browse files

Unified · Split

8 files
  1. leaf/Src/leaf-analysis.c +18 −11
  2. leaf/Src/leaf-distortion.c +4 −3
  3. leaf/Src/leaf-effects.c +3 −3
  4. leaf/Src/leaf-envelopes.c +0 −2
  5. leaf/Src/leaf-mempool.c +48 −6
  6. leaf/Src/leaf-midi.c +2 −2
  7. leaf/leaf-config.h +3 −0
  8. leaf/leaf.h +3 −3
leaf/Src/leaf-analysis.c+18−11
@@ -18,6 +18,10 @@ · 17 unmodified lines
1818
1919 #endif
2020
21+#if LEAF_DEBUG
22+#include "../../TestPlugin/JuceLibraryCode/JuceHeader.h"
23+#endif
24+
2125 //===========================================================================
2226 /* Envelope Follower */
2327 //===========================================================================
@@ -52,8 +56,8 @@ float tEnvelopeFollower_tick(tEnvelopeFollower* const ef, float x) · 28 unmodified lines
5256 if (x < 0.0f ) x = -x; /* Absolute value. */
5357
5458 if (isnan(x)) return 0.0f;
55− if ((x >= e->y) && (x > e->a_thresh)) e->y = x; /* If we hit a peak, ride the peak to the top. */
56− else e->y = e->y * e->d_coeff; /* Else, exponential decay of output. */
59+ if ((x >= e->y) && (x > e->a_thresh)) e->y = x; /* If we hit a peak, ride the peak to the top. */
60+ else e->y = e->y * e->d_coeff; /* Else, exponential decay of output. */
5761
5862 //ef->y = envelope_pow[(uint16_t)(ef->y * (float)UINT16_MAX)] * ef->d_coeff; //not quite the right behavior - too much loss of precision?
5963 //ef->y = powf(ef->y, 1.000009f) * ef->d_coeff; // too expensive
@@ -961,7 +965,7 @@ void tZeroCrossingInfo_init (tZeroCrossingInfo* const zc, LEAF* const leaf) · 901 unmodified lines
961965 void tZeroCrossingInfo_initToPool (tZeroCrossingInfo* const zc, tMempool* const mp)
962966 {
963967 _tMempool* m = *mp;
964− _tZeroCrossingInfo* z = *zc = (_tZeroCrossingInfo*) mpool_alloc(sizeof(_tZeroCrossingInfo), m);
968+ _tZeroCrossingInfo* z = *zc = (_tZeroCrossingInfo*) mpool_calloc(sizeof(_tZeroCrossingInfo), m);
965969 z->mempool = m;
966970
967971 z->_leading_edge = INT_MIN;
@@ -1050,7 +1054,9 @@ void tZeroCrossingCollector_initToPool (tZeroCrossingCollector* const zc, · 82 unmodified lines
10501054 z->_info = (tZeroCrossingInfo*) mpool_calloc(sizeof(tZeroCrossingInfo) * z->_size, m);
10511055
10521056 for (unsigned i = 0; i < z->_size; i++)
1053− tZeroCrossingInfo_initToPool(&z->_info[i], mp);
1057+ {
1058+ tZeroCrossingInfo_initToPool(&z->_info[i], mp);
1059+ }
10541060
10551061 z->_pos = 0;
10561062
@@ -1068,8 +1074,9 @@ void tZeroCrossingCollector_free (tZeroCrossingCollector* const zc) · 11 unmodified lines
10681074 _tZeroCrossingCollector* z = *zc;
10691075
10701076 for (unsigned i = 0; i < z->_size; i++)
1071−
1072− tZeroCrossingInfo_free(&z->_info[i]);
1077+ {
1078+ tZeroCrossingInfo_free(&z->_info[i]);
1079+ }
10731080
10741081 mpool_free((char*)z->_info, z->mempool);
10751082 mpool_free((char*)z, z->mempool);
@@ -1198,7 +1205,7 @@ static inline void update_state(tZeroCrossingCollector* const zc, float s) · 122 unmodified lines
11981205 {
11991206 --z->_pos;
12001207 z->_pos &= z->_mask;
1201− tZeroCrossingInfo crossing = z->_info[z->_pos];
1208+ tZeroCrossingInfo crossing = z->_info[z->_pos & z->_mask];
12021209 crossing->_before_crossing = z->_prev;
12031210 crossing->_after_crossing = s;
12041211 crossing->_peak = s;
@@ -1210,7 +1217,7 @@ static inline void update_state(tZeroCrossingCollector* const zc, float s) · 5 unmodified lines
12101217 }
12111218 else
12121219 {
1213− tZeroCrossingInfo_updatePeak(&z->_info[z->_pos], s, z->_frame);
1220+ tZeroCrossingInfo_updatePeak(&z->_info[z->_pos & z->_mask], s, z->_frame);
12141221 }
12151222 if (s > z->_peak_update)
12161223 {
@@ -1220,14 +1227,14 @@ static inline void update_state(tZeroCrossingCollector* const zc, float s) · 3 unmodified lines
12201227 else if (z->_state && (s < z->_hysteresis))
12211228 {
12221229 z->_state = 0;
1223− z->_info[z->_pos]->_trailing_edge = z->_frame;
1230+ z->_info[z->_pos & z->_mask]->_trailing_edge = z->_frame;
12241231 if (z->_peak == 0.0f)
12251232 z->_peak = z->_peak_update;
12261233 }
12271234
12281235 if (z->_frame > z->_window_size * 2)
12291236 reset(zc);
1230−
1237+
12311238 z->_prev = s;
12321239 }
12331240
@@ -1235,7 +1242,7 @@ static inline void shift(tZeroCrossingCollector* const zc, int n) · 1 unmodified line
12351242 {
12361243 _tZeroCrossingCollector* z = *zc;
12371244
1238− tZeroCrossingInfo crossing = z->_info[z->_pos];
1245+ tZeroCrossingInfo crossing = z->_info[z->_pos & z->_mask];
12391246
12401247 crossing->_leading_edge -= n;
12411248 if (!z->_state)
leaf/Src/leaf-distortion.c+4−3
@@ -80,14 +80,15 @@ void tOversampler_init (tOversampler* const osr, int ratio, int extraQuality, LE · 79 unmodified lines
8080 void tOversampler_initToPool (tOversampler* const osr, int ratio, int extraQuality, tMempool* const mp)
8181 {
8282 _tMempool* m = *mp;
83− _tOversampler* os = *osr = (_tOversampler*) mpool_alloc(sizeof(_tOversampler), m);
84− os->mempool = m;
85−
8683 uint8_t offset = 0;
8784 if (extraQuality) offset = 6;
8885 if (ratio == 2 || ratio == 4 ||
8986 ratio == 8 || ratio == 16 ||
9087 ratio == 32 || ratio == 64) {
88+
89+ _tOversampler* os = *osr = (_tOversampler*) mpool_alloc(sizeof(_tOversampler), m);
90+ os->mempool = m;
91+
9192 os->ratio = ratio;
9293 int idx = (int)(log2f(os->ratio))-1+offset;
9394 os->numTaps = __leaf_tablesize_firNumTaps[idx];
leaf/Src/leaf-effects.c+3−3
@@ -904,9 +904,8 @@ void tRosenbergGlottalPulse_initToPool (tRosenbergGlottalPulse* const gp, tMempo · 903 unmodified lines
904904 g->pulseLength = 0.0f;
905905 g->freq = 0.0f;
906906 g->inc = 0.0f;
907−
908−
909907 }
908+
910909 void tRosenbergGlottalPulse_free (tRosenbergGlottalPulse* const gp)
911910 {
912911 _tRosenbergGlottalPulse* g = *gp;
@@ -977,6 +976,7 @@ void tRosenbergGlottalPulse_setFreq (tRosenbergGlottalPulse* const g · 64 unmodified lines
977976
978977 g->freq = freq;
979978 g->inc = freq * leaf->invSampleRate;
979+ g->inc -= (int) g->inc;
980980 }
981981
982982 void tRosenbergGlottalPulse_setOpenLength (tRosenbergGlottalPulse* const gp, float openLength)
@@ -1066,7 +1066,7 @@ void tSOLAD_ioSamples(tSOLAD* const wp, float* in, float* out, int blocksize) · 83 unmodified lines
10661066 {
10671067 float sample = tHighpass_tick(&w->hp, in[0]);
10681068 w->delaybuf[0] = sample;
1069− w->delaybuf[w->loopSize] = sample; // copy one sample for interpolation
1069+ w->delaybuf[w->loopSize-1] = sample; // copy one sample for interpolation
10701070 n--;
10711071 i++;
10721072 in++;
leaf/Src/leaf-envelopes.c+0−2
@@ -746,8 +746,6 @@ void tADSRT_setRelease(tADSRT* const adsrenv, float release) · 745 unmodified lines
746746 void tADSRT_setLeakFactor(tADSRT* const adsrenv, float leakFactor)
747747 {
748748 _tADSRT* adsr = *adsrenv;
749−
750−
751749 adsr->leakFactor = leakFactor;
752750 }
753751
leaf/Src/leaf-mempool.c+48−6
@@ -48,6 +48,12 @@ · 47 unmodified lines
4848
4949 #endif
5050
51+#include <stdlib.h>
52+
53+#if LEAF_DEBUG
54+#include "../../TestPlugin/JuceLibraryCode/JuceHeader.h"
55+#endif
56+
5157 /**
5258 * private function
5359 */
@@ -89,6 +95,23 @@ void leaf_pool_init(LEAF* const leaf, char* memory, size_t size) · 35 unmodified lines
8995 */
9096 char* mpool_alloc(size_t asize, _tMempool* pool)
9197 {
98+#if LEAF_DEBUG
99+ DBG("malloc " + String(asize));
100+#endif
101+#if LEAF_USE_DYNAMIC_ALLOCATION
102+ char* temp = (char*) malloc(asize);
103+ if (temp == NULL)
104+ {
105+ // allocation failed, exit from the program
106+ fprintf(stderr, "Out of memory.\n");
107+ exit(1);
108+ }
109+ if (pool->leaf->clearOnAllocation > 0)
110+ {
111+ memset(temp, 0, asize);
112+ }
113+ return temp;
114+#else
92115 // If the head is NULL, the mempool is full
93116 if (pool->head == NULL)
94117 {
@@ -169,6 +192,7 @@ char* mpool_alloc(size_t asize, _tMempool* pool) · 74 unmodified lines
169192
170193 // Return the pool of the allocated node;
171194 return node_to_alloc->pool;
195+#endif
172196 }
173197
174198
@@ -177,6 +201,20 @@ char* mpool_alloc(size_t asize, _tMempool* pool) · 2 unmodified lines
177201 */
178202 char* mpool_calloc(size_t asize, _tMempool* pool)
179203 {
204+#if LEAF_DEBUG
205+ DBG("calloc " + String(asize));
206+#endif
207+#if LEAF_USE_DYNAMIC_ALLOCATION
208+ char* ret = (char*) malloc(asize);
209+ if (ret == NULL)
210+ {
211+ // allocation failed, exit from the program
212+ fprintf(stderr, "Out of memory.\n");
213+ exit(1);
214+ }
215+ memset(ret, 0, asize);
216+ return ret;
217+#else
180218 // If the head is NULL, the mempool is full
181219 if (pool->head == NULL)
182220 {
@@ -252,26 +290,29 @@ char* mpool_calloc(size_t asize, _tMempool* pool) · 69 unmodified lines
252290 for (int i = 0; i < node_to_alloc->size; i++) node_to_alloc->pool[i] = 0;
253291 // Return the pool of the allocated node;
254292 return node_to_alloc->pool;
293+#endif
255294 }
256295
257296 char* leaf_alloc(LEAF* const leaf, size_t size)
258297 {
259298 //printf("alloc %i\n", size);
260− char* block = mpool_alloc(size, &leaf->_internal_mempool);
261−
262− return block;
299+ return mpool_alloc(size, &leaf->_internal_mempool);
263300 }
264301
265302 char* leaf_calloc(LEAF* const leaf, size_t size)
266303 {
267304 //printf("alloc %i\n", size);
268− char* block = mpool_calloc(size, &leaf->_internal_mempool);
269−
270− return block;
305+ return mpool_calloc(size, &leaf->_internal_mempool);
271306 }
272307
273308 void mpool_free(char* ptr, _tMempool* pool)
274309 {
310+#if LEAF_DEBUG
311+ DBG("free");
312+#endif
313+#if LEAF_USE_DYNAMIC_ALLOCATION
314+ free(ptr);
315+#else
275316 //if (ptr < pool->mpool || ptr >= pool->mpool + pool->msize)
276317 // Get the node at the freed space
277318 mpool_node_t* freed_node = (mpool_node_t*) (ptr - pool->leaf->header_size);
@@ -336,6 +377,7 @@ void mpool_free(char* ptr, _tMempool* pool) · 58 unmodified lines
336377 // Format the freed pool
337378 // char* freed_pool = (char*)freed_node->pool;
338379 // for (int i = 0; i < freed_node->size; i++) freed_pool[i] = 0;
380+#endif
339381 }
340382
341383 void leaf_free(LEAF* const leaf, char* ptr)
leaf/Src/leaf-midi.c+2−2
@@ -633,11 +633,11 @@ void tSimplePoly_initToPool (tSimplePoly* const polyh, int maxNumV · 632 unmodified lines
633633 }
634634 poly->stealing_on = 1;
635635 poly->recover_stolen = 1;
636− poly->voices = (int**) mpool_alloc(sizeof(int*) * poly->maxNumVoices, m);
636+ poly->voices = (int**) mpool_calloc(sizeof(int*) * poly->maxNumVoices, m);
637637
638638 for (int i = 0; i < poly->maxNumVoices; ++i)
639639 {
640− poly->voices[i] = (int*) mpool_alloc(sizeof(int) * 3, m);
640+ poly->voices[i] = (int*) mpool_calloc(sizeof(int) * 3, m);
641641 poly->voices[i][0] = -1;
642642 }
643643 tStack_initToPool(&poly->stack, mp);
leaf/leaf-config.h+3−0
@@ -58,6 +58,9 @@ · 57 unmodified lines
5858
5959 #define LEAF_USE_CMSIS 0
6060
61+//! Use stdlib malloc() and free() internally instead of LEAF's normal mempool behavior for when you want to avoid being limited to and managing mempool a fixed mempool size. Usage of all object remains essentially the same.
62+#define LEAF_USE_DYNAMIC_ALLOCATION 0
63+
6164 //==============================================================================
6265
6366 #endif // LEAF_CONFIG_H_INCLUDED
leaf/leaf.h+3−3
@@ -27,9 +27,9 @@ · 26 unmodified lines
2727
2828 #define LEAF_DEBUG 0
2929
30−#if LEAF_DEBUG
31−#include "../LEAF_JUCEPlugin/JuceLibraryCode/JuceHeader.h"
32−#endif
30+//#if LEAF_DEBUG
31+//#include "../TestPlugin/JuceLibraryCode/JuceHeader.h"
32+//#endif
3333
3434 #if _WIN32 || _WIN64
3535

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