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
|
/*
flext - C++ layer for Max/MSP and pd (pure data) externals
Copyright (c) 2001-2004 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.
*/
/*! \file flsupport.cpp
\brief flext support functions and classes.
*/
#include "flext.h"
#include <stdio.h>
#include <stdarg.h>
#include <new>
const t_symbol *flext::sym_float = NULL;
const t_symbol *flext::sym_symbol = NULL;
const t_symbol *flext::sym_bang = NULL;
const t_symbol *flext::sym_list = NULL;
const t_symbol *flext::sym_pointer = NULL;
const t_symbol *flext::sym_int = NULL;
#if FLEXT_SYS != FLEXT_SYS_JMAX
const t_symbol *flext::sym_anything = NULL;
#endif
#if FLEXT_SYS == FLEXT_SYS_PD
const t_symbol *flext::sym_signal = NULL;
#endif
int flext::Version() { return FLEXT_VERSION; }
const char *flext::VersionStr() { return FLEXT_VERSTR; }
void flext::Setup()
{
static bool issetup = false;
if(issetup)
return;
else issetup = true;
#if FLEXT_SYS == FLEXT_SYS_PD
sym_anything = gensym("anything");
sym_pointer = gensym("pointer");
sym_float = gensym("float");
sym_symbol = gensym("symbol");
sym_bang = gensym("bang");
sym_list = gensym("list");
sym_signal = gensym("signal");
#elif FLEXT_SYS == FLEXT_SYS_MAX
sym_int = gensym("int");
sym_float = gensym("float");
sym_symbol = gensym("symbol");
sym_bang = gensym("bang");
sym_list = gensym("list");
sym_anything = gensym("anything");
#elif FLEXT_SYS == FLEXT_SYS_JMAX
sym_int = fts_s_int;
sym_float = fts_s_float;
sym_symbol = fts_s_symbol;
sym_bang = fts_s_bang;
sym_list = fts_s_list;
sym_pointer = fts_s_pointer;
#else
#endif
#ifdef FLEXT_THREADS
#if FLEXT_SYS == FLEXT_SYS_MAX && FLEXT_OS == FLEXT_OS_MAC && FLEXT_THREADS == FLEXT_THR_POSIX
// for Max OSX/CFM MachO functions have to be imported
CFBundleRef bundle = NULL;
if (bundle = CFBundleGetBundleWithIdentifier(CFSTR("com.apple.Carbon")))
{
//load pthread function pointers
pthread_self = CFBundleGetFunctionPointerForName(bundle, CFSTR("pthread_self"));
pthread_equal = CFBundleGetFunctionPointerForName(bundle, CFSTR("pthread_equal"));
pthread_create = CFBundleGetFunctionPointerForName(bundle, CFSTR("pthread_create"));
pthread_cancel = CFBundleGetFunctionPointerForName(bundle, CFSTR("pthread_cancel"));
pthread_testcancel = CFBundleGetFunctionPointerForName(bundle, CFSTR("pthread_testcancel"));
pthread_join = CFBundleGetFunctionPointerForName(bundle, CFSTR("pthread_join"));
pthread_exit = CFBundleGetFunctionPointerForName(bundle, CFSTR("pthread_exit"));
pthread_attr_init = CFBundleGetFunctionPointerForName(bundle, CFSTR("pthread_attr_init"));
pthread_attr_destroy = CFBundleGetFunctionPointerForName(bundle, CFSTR("pthread_attr_destroy"));
pthread_attr_setdetachstate = CFBundleGetFunctionPointerForName(bundle, CFSTR("pthread_attr_setdetachstate"));
pthread_attr_getschedparam = CFBundleGetFunctionPointerForName(bundle, CFSTR("pthread_attr_getschedparam"));
pthread_attr_setschedparam = CFBundleGetFunctionPointerForName(bundle, CFSTR("pthread_attr_setschedparam"));
pthread_mutex_init = CFBundleGetFunctionPointerForName(bundle, CFSTR("pthread_mutex_init"));
pthread_mutex_destroy = CFBundleGetFunctionPointerForName(bundle, CFSTR("pthread_mutex_destroy"));
pthread_mutex_lock = CFBundleGetFunctionPointerForName(bundle, CFSTR("pthread_mutex_lock"));
pthread_mutex_unlock = CFBundleGetFunctionPointerForName(bundle, CFSTR("pthread_mutex_unlock"));
pthread_mutex_trylock = CFBundleGetFunctionPointerForName(bundle, CFSTR("pthread_mutex_trylock"));
pthread_cond_init = CFBundleGetFunctionPointerForName(bundle, CFSTR("pthread_cond_init"));
pthread_cond_destroy = CFBundleGetFunctionPointerForName(bundle, CFSTR("pthread_cond_destroy"));
pthread_cond_signal = CFBundleGetFunctionPointerForName(bundle, CFSTR("pthread_cond_signal"));
pthread_cond_wait = CFBundleGetFunctionPointerForName(bundle, CFSTR("pthread_cond_wait"));
pthread_cond_timedwait = CFBundleGetFunctionPointerForName(bundle, CFSTR("pthread_cond_timedwait"));
sched_yield = CFBundleGetFunctionPointerForName(bundle, CFSTR("sched_yield"));
sched_get_priority_min = CFBundleGetFunctionPointerForName(bundle, CFSTR("sched_get_priority_min"));
sched_get_priority_max = CFBundleGetFunctionPointerForName(bundle, CFSTR("sched_get_priority_max"));
}
#endif
thrid = GetThreadId();
StartHelper();
#endif
}
#if FLEXT_SYS == FLEXT_SYS_PD && defined(FLEXT_THREADED) && defined(FLEXT_PDLOCK)
#define SYSLOCK() sys_lock()
#define SYSUNLOCK() sys_unlock()
#else
#define SYSLOCK() (void)0
#define SYSUNLOCK() (void)0
#endif
/////////////////////////////////////////////////////////
// overloaded new/delete memory allocation methods
//
/////////////////////////////////////////////////////////
#define LARGEALLOC 32000
void *flext_root::operator new(size_t bytes)
{
bytes += sizeof(size_t);
char *blk;
if(bytes >= LARGEALLOC) {
#if FLEXT_SYS == FLEXT_SYS_MAX
blk = (char *)::sysmem_newptr(bytes);
#else
// use C library function for large memory blocks
blk = (char *)::malloc(bytes);
#endif
}
else {
//! We need system locking here for secondary threads!
SYSLOCK();
#if FLEXT_SYS == FLEXT_SYS_JMAX
blk = (char *)::fts_malloc(bytes);
#else
blk = (char *)::getbytes(bytes);
#endif
SYSUNLOCK();
}
*(size_t *)blk = bytes;
return blk+sizeof(size_t);
}
void flext_root::operator delete(void *blk)
{
char *ori = (char *)blk-sizeof(size_t);
size_t bytes = *(size_t *)ori;
if(bytes >= LARGEALLOC) {
#if FLEXT_SYS == FLEXT_SYS_MAX
::sysmem_freeptr(ori);
#else
// use C library function for large memory blocks
::free(ori);
#endif
}
else {
//! We need system locking here for secondary threads!
SYSLOCK();
#if FLEXT_SYS == FLEXT_SYS_JMAX
::fts_free(ori);
#else
::freebytes(ori,bytes);
#endif
SYSUNLOCK();
}
}
void *flext_root::NewAligned(size_t bytes,int bitalign)
{
const size_t ovh = sizeof(size_t)+sizeof(char *);
const unsigned long alignovh = bitalign/8-1;
bytes += ovh+alignovh;
char *blk;
if(bytes >= LARGEALLOC) {
#if FLEXT_SYS == FLEXT_SYS_MAX
blk = (char *)::sysmem_newptr(bytes);
#else
// use C library function for large memory blocks
blk = (char *)::malloc(bytes);
#endif
}
else {
//! We need system locking here for secondary threads!
SYSLOCK();
#if FLEXT_SYS == FLEXT_SYS_JMAX
blk = (char *)::fts_malloc(bytes);
#else
blk = (char *)::getbytes(bytes);
#endif
SYSUNLOCK();
}
char *ablk = reinterpret_cast<char *>((reinterpret_cast<unsigned long>(blk)+ovh+alignovh) & ~alignovh);
*(char **)(ablk-sizeof(size_t)-sizeof(char *)) = blk;
*(size_t *)(ablk-sizeof(size_t)) = bytes;
return ablk;
}
void flext_root::FreeAligned(void *blk)
{
char *ori = *(char **)((char *)blk-sizeof(size_t)-sizeof(char *));
size_t bytes = *(size_t *)((char *)blk-sizeof(size_t));
if(bytes >= LARGEALLOC) {
#if FLEXT_SYS == FLEXT_SYS_MAX
::sysmem_freeptr(ori);
#else
// use C library function for large memory blocks
::free(ori);
#endif
}
else {
//! We need system locking here for secondary threads!
SYSLOCK();
#if FLEXT_SYS == FLEXT_SYS_JMAX
::fts_free(ori);
#else
::freebytes(ori,bytes);
#endif
SYSUNLOCK();
}
}
// ------------------------------------------
/*! \todo there is probably also a shortcut for Max and jMax
\todo size checking
*/
void flext::GetAString(const t_atom &a,char *buf,int szbuf)
{
#if FLEXT_SYS == FLEXT_SYS_PD
atom_string(const_cast<t_atom *>(&a),buf,szbuf);
#else
// no checking for size here
if(IsSymbol(a)) STD::sprintf(buf,GetString(a));
else if(IsFloat(a)) STD::sprintf(buf,"%f",GetFloat(a));
else if(IsInt(a)) STD::sprintf(buf,"%i",GetInt(a));
else *buf = 0;
#endif
}
unsigned long flext::AtomHash(const t_atom &a)
{
#if FLEXT_SYS == FLEXT_SYS_MAX || FLEXT_SYS == FLEXT_SYS_PD
return ((unsigned long)a.a_type<<28)^*(unsigned long *)&a.a_w;
#else
#error Not implemented
#endif
}
void flext_root::post(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
char buf[1024]; // \TODO this is quite unsafe.....
vsprintf(buf, fmt, ap);
::post(buf);
va_end(ap);
}
void flext_root::error(const char *fmt,...)
{
va_list ap;
va_start(ap, fmt);
char buf[1024]; // \TODO this is quite unsafe.....
STD::sprintf(buf,"error: ");
vsprintf(buf+7, fmt, ap);
::post(buf);
va_end(ap);
}
AnyMap::AnyMap() {}
AnyMap::~AnyMap() {}
AnyMap::iterator AnyMap::find(unsigned int k) { return Parent::find(k); }
unsigned int &AnyMap::operator [](unsigned int k) { return Parent::operator [](k); }
|