aboutsummaryrefslogtreecommitdiff
path: root/externals/grill/vasp/source/buflib.cpp
blob: 41754a205d4811db4f7bfdceccceee1eab0383b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/* 

VASP modular - vector assembling signal processor / objects for Max/MSP and PD

Copyright (c) 2002 Thomas Grill (xovo@gmx.net)
For information on usage and redistribution, and for a DISCLAIMER OF ALL
WARRANTIES, see the file, "license.txt," in this distribution.  

*/

#include "main.h"
#include "buflib.h"
#include <stdio.h>

#define LIBTICK 0.1 // tick time in s
#define LIBTOL 3  // how many ticks till release

#define REUSE_MAXLOSEREL 0.1  // max. fraction of lost buffer size 
#define REUSE_MAXLOSEABS 10000 // max. lost buffer size


#ifdef __MWERKS__
#define STD	std
#else
#define STD
#endif


class FreeEntry:
    public flext
{
public:
	FreeEntry(const t_symbol *s): sym(s),nxt(NULL) {}

	const t_symbol *sym;
	FreeEntry *nxt;
};

class BufEntry:
    public flext
{
public:
	BufEntry(const t_symbol *s,I fr,BL zero = true);
	~BufEntry();

	V IncRef();
	V DecRef();

	const t_symbol *sym;
	I refcnt,tick;
	BufEntry *nxt;

	I alloc,len;
	S *data;
};


static BufEntry *libhead = NULL,*libtail = NULL;
static FreeEntry *freehead = NULL,*freetail = NULL;
static I libcnt = 0,libtick = 0;

#ifdef FLEXT_THREADS
static flext::ThrMutex libmtx;
#endif

static V FreeLibSym(const t_symbol *s);




BufEntry::BufEntry(const t_symbol *s,I fr,BL zero): 
	sym(s), 
	alloc(fr),len(fr),
	refcnt(0),nxt(NULL) 
{
    data = (S *)NewAligned(fr*sizeof(S));
	if(zero) flext::ZeroMem(data,len*sizeof(*data));
}

BufEntry::~BufEntry()
{
	if(sym) FreeLibSym(sym);
	if(data) FreeAligned(data);
}

V BufEntry::IncRef() { ++refcnt; }
V BufEntry::DecRef() { --refcnt; tick = libtick; }

static BufEntry *FindInLib(const t_symbol *s) 
{
	BufEntry *e;
	for(e = libhead; e && e->sym != s; e = e->nxt) (void)0;
	return e?e:NULL;
}

#ifdef FLEXT_DEBUG
static V DumpLib() 
{
	post("Dump {");
	BufEntry *e;
	for(e = libhead; e; e = e->nxt) {
		post("\t%s -> refs:%i, alloc:%i, len:%i -> %p",flext::GetString(e->sym),e->refcnt,e->alloc,e->len,e->data);
	}
	post("}");
}
#endif

VBuffer *BufLib::Get(const VSymbol &s,I chn,I len,I offs)
{
	BufEntry *e = FindInLib(s.Symbol());
	if(e) 
		return new ImmBuf(e,len,offs);
	else
		return new SysBuf(s,chn,len,offs);
}

V BufLib::IncRef(const t_symbol *s) 
{ 
	if(s) {
		BufEntry *e = FindInLib(s);
		if(e) e->IncRef();
	}
}

V BufLib::DecRef(const t_symbol *s)
{ 
	if(s) {
		BufEntry *e = FindInLib(s);
		if(e) e->DecRef();
	}
}

static V Collect()
{
#ifdef FLEXT_THREADS
	libmtx.Lock();
#endif

	// collect garbage
	BufEntry *e,*p;
	for(p = NULL,e = libhead; e; ) {
		if(e->refcnt <= 0 && e->tick+LIBTOL < libtick) {
			FLEXT_ASSERT(e->refcnt == 0);

			BufEntry *n = e->nxt;

			if(p) p->nxt = n;
			else libhead = n;

			if(!n) libtail = p;
			else e->nxt = NULL;

			delete e;

			e = n;
		}
		else
			p = e,e = e->nxt;
	}

#ifdef FLEXT_THREADS
	libmtx.Unlock();
#endif
}


#ifdef FLEXT_THREADS
static bool libthractive = false;
//static flext::thrid_t libthrid;
static bool libthrexit = false; // currently not used
static flext::ThrCond *libthrcond = NULL;

static V LibThr(flext::thr_params *)
{
	flext::RelPriority(-2);

	while(!libthrexit) {
		libthrcond->TimedWait(1); // don't go below 1 here as TimedWait might not support fractions of seconds!!!
		// TODO - should process return value of TimedWait
		Collect();	
	}
}
#endif

static flext::Timer *libclk = NULL;

static V LibTick(V *)
{
#ifdef FLEXT_THREADS
	libthrcond->Signal();
#else
	Collect();
#endif

	++libtick;
}

static const t_symbol *GetLibSym()
{
	if(freehead) {
		// reuse from free-list
		FreeEntry *r = freehead;
		freehead = r->nxt;
		if(!freehead) freetail = NULL;
		const t_symbol *s = r->sym;
		delete r;
		return s;
	}
	else {
		// allocate new symbol
		char tmp[20];
		if(libcnt > 0xffff)
			STD::sprintf(tmp,"vasp!%08x",libcnt); 
		else // better hash lookup for 4 digits
			STD::sprintf(tmp,"vasp!%04x",libcnt); 
		libcnt++;
		return gensym(tmp);
	}
}

static V FreeLibSym(const t_symbol *sym)
{
#ifdef FLEXT_DEBUG
//	post("free %s",flext::GetString(sym));
#endif

	FreeEntry *f = new FreeEntry(sym);
	if(!freehead) freehead = f;
	else freetail->nxt = f;
	freetail = f;
}


BufEntry *BufLib::NewImm(I fr,BL zero)
{
#ifdef FLEXT_THREADS
	if(!libthractive) {
		bool ret = flext::LaunchThread(LibThr,NULL);
		if(!ret)
			error("vasp - Could not launch helper thread");
		else {
			libthrcond = new flext::ThrCond;
			libthractive = true;
		}
	}
#endif
	if(!libclk) {
		libclk = new flext::Timer(true);
		libclk->SetCallback(LibTick);
		libclk->Periodic(LIBTICK);
	}

	const t_symbol *s = GetLibSym();
	BufEntry *entry = new BufEntry(s,fr,zero);

#ifdef FLEXT_THREADS
	libmtx.Lock();
#endif

	if(libtail) libtail->nxt = entry; 
	else libhead = entry;
	libtail = entry;

#ifdef FLEXT_DEBUG
//	DumpLib();
#endif

#ifdef FLEXT_THREADS
	libmtx.Unlock();
#endif

	return entry;
}

static F reuse_maxloserel = (F)REUSE_MAXLOSEREL;
static I reuse_maxloseabs = REUSE_MAXLOSEABS;

BufEntry *BufLib::Resize(BufEntry *e,I fr,BL keep,BL zero)
{ 
	if(e->alloc >= fr && fr >= e->alloc*(1-reuse_maxloserel) && fr >= (e->alloc-reuse_maxloseabs)) {
		// reuse buffer
		e->len = fr;
	}
	else {
		S *nd = new S[fr]; 
		if(keep) {
			I l = fr;
			if(e->len < l) {
				l = e->len;
				if(zero) flext::ZeroMem(nd+l,(fr-l)*sizeof(*nd));
			}
			flext::CopyMem(nd,e->data,l*sizeof(*nd));
		}

		delete[] e->data;
		e->data = nd;
		e->len = e->alloc = fr;
	}
	return e;
}



ImmBuf::ImmBuf(I len,BL zero):
	VBuffer(0,len),
	entry(BufLib::NewImm(len,zero))
{}

ImmBuf::ImmBuf(BufEntry *e,I len,I offs): 
	VBuffer(0,len,offs),
	entry(e) 
{
	if(Length() > e->alloc) {
		Length(e->alloc);
		post("vasp - buffer %s: Length (%i) is out of range, corrected to %i",GetString(e->sym),len,e->alloc);
	}
}

VSymbol ImmBuf::Symbol() const { return entry->sym; }

I ImmBuf::Frames() const { return entry->len; }

V ImmBuf::Frames(I fr,BL keep,BL zero) { entry = BufLib::Resize(entry,fr,keep,zero); }

S *ImmBuf::Data() { return entry->data; }