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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
/* Copyright (c) 2002-2004 krzYszcz and others.
* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution. */
#include <string.h>
#include "m_pd.h"
#include "common/loud.h"
#include "common/grow.h"
#define PREPEND_INISIZE 32 /* LATER rethink */
#define PREPEND_MAXSIZE 256
typedef struct _prepend
{
t_object x_ob;
t_symbol *x_selector;
int x_size; /* as allocated */
int x_natoms; /* as used */
t_atom *x_message;
t_atom x_messini[PREPEND_INISIZE];
int x_entered;
int x_auxsize;
t_atom *x_auxbuf;
t_pd *x_proxy;
} t_prepend;
typedef struct _prependxy
{
t_pd xy_pd;
t_prepend *xy_owner;
} t_prependxy;
static t_class *prepend_class;
static t_class *prependxy_class;
/* Usually a preallocation method is used, except in special cases of:
1) reentrant output request, or 2) an output request which would cause
resizing to more than MAXSIZE (no such limit for a 'set' message).
In both special cases, a temporary output buffer is allocated.
A separately preallocated output buffer is not used, thus avoiding
memcpying of the stored message (a small performance gain when the
preallocation method is chosen). Instead, self-invoked 'set'
messages are postponed, using an auxiliary buffer. */
/* called only from prepend_doanything() */
static void prepend_dooutput(t_prepend *x, int ac, t_atom *av)
{
if (x->x_selector == &s_float)
{
if (ac > 1)
outlet_list(((t_object *)x)->ob_outlet, &s_list, ac, av);
else
outlet_float(((t_object *)x)->ob_outlet, av->a_w.w_float);
}
else if (x->x_selector == &s_list)
outlet_list(((t_object *)x)->ob_outlet, &s_list, ac, av);
else /* x->x_selector guaranteed non-empty */
/* CHECKED: 'bang' is prepended -- we cannot do so...
('symbol' cannot be compatible too) */
outlet_anything(((t_object *)x)->ob_outlet, x->x_selector, ac, av);
}
static void prepend_doanything(t_prepend *x, t_symbol *s, int ac, t_atom *av)
{
int reentered = x->x_entered;
x->x_entered = 1;
if (s == &s_)
s = 0;
if (s || x->x_natoms)
{
int prealloc = !reentered;
int ntotal = x->x_natoms + ac;
t_atom *buf;
if (s)
ntotal++;
if (prealloc && ntotal > x->x_size)
{
if (ntotal > PREPEND_MAXSIZE)
prealloc = 0;
else
{
int nrequested = ntotal;
x->x_message = grow_withdata(&nrequested, &x->x_natoms,
&x->x_size, x->x_message,
PREPEND_INISIZE, x->x_messini,
sizeof(*x->x_message));
prealloc = (nrequested == ntotal);
}
}
if (prealloc)
{
buf = x->x_message + x->x_natoms;
if (s)
{
SETSYMBOL(buf, s);
buf++;
}
if (ac)
memcpy(buf, av, ac * sizeof(*buf));
prepend_dooutput(x, ntotal, x->x_message);
}
else
{
/* LATER consider using the stack if ntotal <= MAXSTACK */
if (buf = getbytes(ntotal * sizeof(*buf)))
{
t_atom *bp = buf + x->x_natoms;
if (x->x_natoms)
memcpy(buf, x->x_message, x->x_natoms * sizeof(*buf));
if (s)
{
SETSYMBOL(bp, s);
bp++;
}
if (ac)
memcpy(bp, av, ac * sizeof(*bp));
prepend_dooutput(x, ntotal, buf);
freebytes(buf, ntotal * sizeof(*buf));
}
}
}
else prepend_dooutput(x, ac, av);
if (!reentered)
{
x->x_entered = 0;
if (x->x_auxbuf)
{
if (x->x_auxsize <= x->x_size)
{
x->x_natoms = x->x_auxsize / 2;
memcpy(x->x_message, x->x_auxbuf,
x->x_natoms * sizeof(*x->x_message));
freebytes(x->x_auxbuf, x->x_auxsize * sizeof(*x->x_auxbuf));
}
else
{
if (x->x_message != x->x_messini)
freebytes(x->x_message, x->x_size * sizeof(*x->x_message));
x->x_size = x->x_auxsize;
x->x_message = x->x_auxbuf;
x->x_natoms = x->x_auxsize / 2;
}
x->x_auxbuf = 0;
}
}
}
static void prepend_bang(t_prepend *x)
{
if (x->x_selector)
{
t_atom at;
SETSYMBOL(&at, &s_bang); /* CHECKED */
prepend_doanything(x, 0, 1, &at);
}
else outlet_bang(((t_object *)x)->ob_outlet);
}
static void prepend_float(t_prepend *x, t_float f)
{
if (x->x_selector)
{
t_atom at;
SETFLOAT(&at, f);
prepend_doanything(x, 0, 1, &at);
}
else outlet_float(((t_object *)x)->ob_outlet, f);
}
static void prepend_symbol(t_prepend *x, t_symbol *s)
{
if (x->x_selector)
{
t_atom at;
SETSYMBOL(&at, s);
prepend_doanything(x, 0, 1, &at);
}
else outlet_symbol(((t_object *)x)->ob_outlet, s);
}
/* LATER gpointer */
static void prepend_list(t_prepend *x, t_symbol *s, int ac, t_atom *av)
{
if (x->x_selector)
prepend_doanything(x, 0, ac, av);
else
outlet_list(((t_object *)x)->ob_outlet, s, ac, av);
}
static void prepend_anything(t_prepend *x, t_symbol *s, int ac, t_atom *av)
{
if (x->x_selector)
prepend_doanything(x, s, ac, av);
else
outlet_anything(((t_object *)x)->ob_outlet, s, ac, av);
}
static void prepend_doset(t_prepend *x, t_symbol *s, int ac, t_atom *av)
{
if (s)
x->x_selector = s;
else if (ac)
{
if (av->a_type == A_SYMBOL)
{
x->x_selector = av->a_w.w_symbol;
ac--; av++;
}
else if (av->a_type == A_FLOAT)
x->x_selector = (ac > 1 ? &s_list : &s_float);
else
return; /* LATER rethink */
}
else x->x_selector = 0;
if (ac)
{
int newsize = ac * 2;
if (x->x_entered)
{
if (x->x_auxbuf)
{
loud_warning((t_pd *)x, 0, "'set' message overridden");
freebytes(x->x_auxbuf, x->x_auxsize * sizeof(*x->x_auxbuf));
x->x_auxsize = 0;
}
if (x->x_auxbuf = getbytes(newsize * sizeof(*x->x_auxbuf)))
{
memcpy(x->x_auxbuf, av, ac * sizeof(*x->x_auxbuf));
x->x_auxsize = newsize;
}
}
else
{
t_atom *ap;
if (newsize > x->x_size)
{
int sz = newsize;
x->x_message = grow_nodata(&sz, &x->x_size, x->x_message,
PREPEND_INISIZE, x->x_messini,
sizeof(*x->x_message));
if (sz != newsize)
ac = sz / 2; /* LATER rethink */
}
x->x_natoms = ac;
ap = x->x_message;
while (ac--) *ap++ = *av++;
}
}
else x->x_natoms = 0;
}
static void prepend_set(t_prepend *x, t_symbol *s, int ac, t_atom *av)
{
if (shared_getmaxcompatibility())
prepend_doset(x, 0, ac, av);
else
prepend_anything(x, s, ac, av);
}
static void prependxy_bang(t_prependxy *xy)
{
prepend_doset(xy->xy_owner, 0, 0, 0); /* LATER rethink */
}
static void prependxy_float(t_prependxy *xy, t_float f)
{
t_atom at;
SETFLOAT(&at, f);
prepend_doset(xy->xy_owner, &s_float, 1, &at);
}
static void prependxy_symbol(t_prependxy *xy, t_symbol *s)
{
prepend_doset(xy->xy_owner,
(s && s != &s_ ? s : &s_symbol), 0, 0); /* LATER rethink */
}
static void prependxy_list(t_prependxy *xy, t_symbol *s, int ac, t_atom *av)
{
prepend_doset(xy->xy_owner, &s_list, ac, av); /* LATER rethink */
}
static void prependxy_anything(t_prependxy *xy, t_symbol *s, int ac, t_atom *av)
{
prepend_doset(xy->xy_owner, s, ac, av);
}
static void prepend_free(t_prepend *x)
{
if (x->x_message != x->x_messini)
freebytes(x->x_message, x->x_size * sizeof(*x->x_message));
if (x->x_auxbuf)
{
bug("prepend_free"); /* LATER rethink */
freebytes(x->x_auxbuf, x->x_auxsize * sizeof(*x->x_auxbuf));
}
if (x->x_proxy)
pd_free(x->x_proxy);
}
static void *prepend_new(t_symbol *s, int ac, t_atom *av)
{
t_prepend *x = (t_prepend *)pd_new(prepend_class);
x->x_selector = 0;
x->x_size = PREPEND_INISIZE;
x->x_natoms = 0;
x->x_message = x->x_messini;
x->x_auxbuf = 0;
x->x_entered = 0;
shared_usecompatibility();
if (ac)
{
x->x_proxy = 0;
prepend_doset(x, 0, ac, av);
}
else
{
if (shared_getmaxcompatibility())
/* CHECKED: this is still not compatible -- in max an object
without an outlet is created, and there is no warning when
loading from a file. */
loud_incompatible(prepend_class,
"creating an object without an argument");
x->x_proxy = pd_new(prependxy_class);
((t_prependxy *)x->x_proxy)->xy_owner = x;
inlet_new((t_object *)x, x->x_proxy, 0, 0);
}
outlet_new((t_object *)x, &s_anything);
return (x);
}
void prepend_setup(void)
{
prepend_class = class_new(gensym("prepend"),
(t_newmethod)prepend_new,
(t_method)prepend_free,
sizeof(t_prepend), 0,
A_GIMME, 0);
class_addbang(prepend_class, prepend_bang);
class_addfloat(prepend_class, prepend_float);
class_addsymbol(prepend_class, prepend_symbol);
class_addlist(prepend_class, prepend_list);
class_addanything(prepend_class, prepend_anything);
class_addmethod(prepend_class, (t_method)prepend_set,
gensym("set"), A_GIMME, 0);
prependxy_class = class_new(gensym("prepend"), 0, 0, sizeof(t_prependxy),
CLASS_PD | CLASS_NOINLET, 0);
class_addbang(prependxy_class, prependxy_bang);
class_addfloat(prependxy_class, prependxy_float);
class_addsymbol(prependxy_class, prependxy_symbol);
class_addlist(prependxy_class, prependxy_list);
class_addanything(prependxy_class, prependxy_anything);
}
|