aboutsummaryrefslogtreecommitdiff
path: root/cyclone/hammer/testmess.c
blob: cc7aa2bb416add3af76c40ae51fec0bf220f7e9a (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
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
356
357
358
359
/* Copyright (c) 2002-2005 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 <stdio.h>
#include <string.h>
#include "m_pd.h"
#include "unstable/fragile.h"

#ifdef _WIN32
#define snprintf  _snprintf
#endif

#define TESTMESS_INISIZE      4  /* LATER rethink */
#define TESTMESS_STACKSIZE  256

typedef struct _testmess
{
    t_object     x_ob;
    t_symbol    *x_method;
    void       (*x_messfun)(struct _testmess *, t_symbol *s, int, t_atom *);
    int          x_appendmode;
    int          x_size;      /* as allocated */
    int          x_natoms;    /* as used */
    int          x_tailwise;  /* data is moved to the end of a buffer */
    t_atom      *x_message;
    t_atom      *x_messbuf;
    t_atom       x_messini[TESTMESS_INISIZE];
} t_testmess;

static t_class *testmess_class;

static void testmess_setnatoms(t_testmess *x, int natoms)
{
    if (x->x_tailwise)
	x->x_message = x->x_messbuf + x->x_size - natoms;
    else
	x->x_message = x->x_messbuf;
    x->x_natoms = natoms;
}

static int testmess_makeroom(t_testmess *x, int natoms, int preserve)
{
    if (x->x_size < natoms)
    {
	int newsize = x->x_size * 2;
	while (newsize < natoms) newsize *= 2;
/*	post("makeroom %s %d %d %d", x->x_method->s_name,
	     preserve, natoms, newsize);*/
	if (x->x_messbuf == x->x_messini)
	{
	    if (!(x->x_messbuf =
		  (t_atom *)getbytes(newsize * sizeof(*x->x_messbuf))))
	    {
		x->x_messbuf = x->x_messini;
		testmess_setnatoms(x, preserve ? x->x_natoms : 0);
		return (0);
	    }
	    x->x_size = newsize;
	    testmess_setnatoms(x, preserve ? x->x_natoms : 0);
	    if (x->x_natoms)
	    {
		if (x->x_tailwise)
		    memcpy(x->x_message,
			   x->x_messini + TESTMESS_INISIZE - x->x_natoms,
			   x->x_natoms * sizeof(*x->x_message));
		else
		    memcpy(x->x_message,
			   x->x_messini, x->x_natoms * sizeof(*x->x_message));
	    }
	}
	else
	{
	    int oldsize = x->x_size;
	    if (!(x->x_messbuf =
		  (t_atom *)resizebytes(x->x_messbuf,
					x->x_size * sizeof(*x->x_messbuf),
					newsize * sizeof(*x->x_messbuf))))
	    {
		x->x_messbuf = x->x_messini;
		x->x_size = TESTMESS_INISIZE;
		testmess_setnatoms(x, 0);
		return (0);
	    }
	    x->x_size = newsize;
	    testmess_setnatoms(x, preserve ? x->x_natoms : 0);
	    if (x->x_natoms && x->x_tailwise)
		memmove(x->x_message, x->x_messbuf + oldsize - x->x_natoms,
			x->x_natoms * sizeof(*x->x_message));
	}
    }
    return (1);
}

static void testmess_stackmess(t_testmess *x, t_symbol *s, int ac, t_atom *av)
{
    t_atom buf[TESTMESS_STACKSIZE];
    int natoms = x->x_natoms;
    if (x->x_appendmode)
    {
	int left = TESTMESS_STACKSIZE - ac;
	if (left < 0) ac = TESTMESS_STACKSIZE, natoms = 0;
	else if (natoms > left) natoms = left;
	if (ac)
	    memcpy(buf, av, ac * sizeof(*buf));
	if (natoms)
	    memcpy(buf + ac, x->x_message, natoms * sizeof(*buf));
    }
    else
    {
	int left = TESTMESS_STACKSIZE - natoms;
	if (left < 0) natoms = TESTMESS_STACKSIZE, ac = 0;
	else if (ac > left) ac = left;
	if (natoms)
	    memcpy(buf, x->x_message, natoms * sizeof(*buf));
	if (ac)
	    memcpy(buf + natoms, av, ac * sizeof(*buf));
    }
    outlet_anything(((t_object *)x)->ob_outlet, s, natoms + ac, buf);
}

static void testmess_heapmess(t_testmess *x, t_symbol *s, int ac, t_atom *av)
{
    int ntotal = x->x_natoms + ac;
    t_atom *buf = getbytes(ntotal * sizeof(*buf));
    if (buf)
    {
	if (x->x_appendmode)
	{
	    if (ac)
		memcpy(buf, av, ac * sizeof(*buf));
	    if (x->x_natoms)
		memcpy(buf + ac, x->x_message, x->x_natoms * sizeof(*buf));
	}
	else
	{
	    if (x->x_natoms)
		memcpy(buf, x->x_message, x->x_natoms * sizeof(*buf));
	    if (ac)
		memcpy(buf + x->x_natoms, av, ac * sizeof(*buf));
	}
	outlet_anything(((t_object *)x)->ob_outlet, s, ntotal, buf);
	freebytes(buf, ntotal * sizeof(*buf));
    }
}

static void testmess_premess(t_testmess *x, t_symbol *s, int ac, t_atom *av)
{
    int ntotal = x->x_natoms + ac;
    if (testmess_makeroom(x, ntotal, 1))
    {
	t_atom *buf;
	if (x->x_appendmode)
	{
	    buf = x->x_messbuf + x->x_size - ntotal;
	    if (ac)
		memcpy(buf, av, ac * sizeof(*buf));
	}
	else
	{
	    buf = x->x_messbuf;
	    if (ac)
		memcpy(buf + x->x_natoms, av, ac * sizeof(*buf));
	}
	outlet_anything(((t_object *)x)->ob_outlet, s, ntotal, buf);
    }
}

static void testmess_bang(t_testmess *x)
{
    if (x->x_natoms)
	x->x_messfun(x, &s_list, 0, 0);
}

static void testmess_float(t_testmess *x, t_float f)
{
    t_atom at;
    SETFLOAT(&at, f);
    x->x_messfun(x, (x->x_natoms ? &s_list : &s_float), 1, &at);
}

static void testmess_symbol(t_testmess *x, t_symbol *s)
{
    x->x_messfun(x, s, 0, 0);
}

static void testmess_anything(t_testmess *x, t_symbol *s, int ac, t_atom *av)
{
    x->x_messfun(x, s, ac, av);
}

static void testmess_setnumbers(t_testmess *x, int natoms, int start)
{
    if (natoms <= 0)
	natoms = 100;
    if (testmess_makeroom(x, natoms * 2, 0))
    {
	t_atom *ap;
	testmess_setnatoms(x, natoms);
	ap = x->x_message;
	while (natoms--)
	{
	    SETFLOAT(ap, (t_float)start);
	    start++; ap++;
	}
    }
}

#define FRAGILE_HASHSIZE  1024

static int fragile_hash(t_symbol *s)
{
    unsigned int hash1 = 0,  hash2 = 0;
    char *ptr = s->s_name;
    while (*ptr)
    {
        hash1 += *ptr++;
        hash2 += hash1;
    }
    return (hash2 & (FRAGILE_HASHSIZE-1));
}

int fragile_symbol_count(void)
{
    return (100);
}

void fragile_getsymbols(t_atom *av)
{
    t_symbol *s = gensym("#N");
    int i;
    for (i = 0, s -= fragile_hash(s); i < FRAGILE_HASHSIZE; i++, s++)
    {
	if (s->s_name)
	{
	    t_symbol *s1;
	    for (s1 = s; s1; s1 = s1->s_next)
		printf("%s\n", s1->s_name);
	}
    }
}

static void testmess_setnames(t_testmess *x, t_symbol *s,
			      int natoms, int nchars)
{
    if (!s)
	s = &s_;
    if (*s->s_name == 'c')
    {
	natoms = fragile_class_count();
	if (natoms > 0 && testmess_makeroom(x, natoms * 2, 0))
	{
	    testmess_setnatoms(x, natoms);
	    fragile_class_getnames(x->x_message, natoms);
	}
    }
    else
    {
	if (natoms <= 0)
	    natoms = 100;
	if (nchars <= 0)
	    nchars = 10;
	if (testmess_makeroom(x, natoms * 2, 0))
	{
	    char buf[MAXPDSTRING], fmt[16];
	    int i = 0;
	    t_atom *ap;
	    testmess_setnatoms(x, natoms);
	    ap = x->x_message;
	    sprintf(fmt, "%%.%dx", nchars);
	    while (natoms--)
	    {
		snprintf(buf, MAXPDSTRING-1, fmt, i);
		SETSYMBOL(ap, gensym(buf));
		i++; ap++;
	    }
	}
    }
}

static void testmess_set(t_testmess *x, t_symbol *s, int ac, t_atom *av)
{
    t_symbol *csym = 0, *msym = 0;
    t_float f1 = 0., f2 = 0.;
    if (ac)
    {
	if (av->a_type == A_SYMBOL)
	    csym = av->a_w.w_symbol;
	else if (av->a_type == A_FLOAT)
	    f1 = av->a_w.w_float;
	if (ac > 1)
	{
	    if (av[1].a_type == A_SYMBOL)
		msym = av[1].a_w.w_symbol;
	    else if (av[1].a_type == A_FLOAT)
	    {
		if (csym)
		    f1 = av[1].a_w.w_float;
		else
		    f2 = av[1].a_w.w_float;
		if (ac > 2)
		{
		    if (av[2].a_type == A_SYMBOL)
			msym = av[2].a_w.w_symbol;
		    else if (csym && av[2].a_type == A_FLOAT)
		    f2 = av[2].a_w.w_float;
		}
	    }
	}
    }
    if (msym == gensym("stack"))
	x->x_method = msym, x->x_messfun = testmess_stackmess;
    else if (msym == gensym("heap"))
	x->x_method = msym, x->x_messfun = testmess_heapmess;
    else
    {
	x->x_method = gensym("prealloc");
	x->x_messfun = testmess_premess;
	x->x_tailwise = x->x_appendmode;
    }
    testmess_setnatoms(x, 0);
    if (csym)
	testmess_setnames(x, csym, (int)f1, (int)f2);
    else
	testmess_setnumbers(x, (int)f1, (int)f2);
}

static void testmess_free(t_testmess *x)
{
    if (x->x_messbuf != x->x_messini)
	freebytes(x->x_messbuf, x->x_size * sizeof(*x->x_messbuf));
}

static void *testmess_new(t_symbol *s, int ac, t_atom *av)
{
    t_testmess *x = (t_testmess *)pd_new(testmess_class);
    x->x_appendmode = 1;
    x->x_tailwise = 0;
    x->x_size = TESTMESS_INISIZE;
    x->x_messbuf = x->x_messini;
    outlet_new((t_object *)x, &s_anything);
    testmess_set(x, s, ac, av);
    return (x);
}

void testmess_setup(void)
{
    testmess_class = class_new(gensym("testmess"),
			       (t_newmethod)testmess_new,
			       (t_method)testmess_free,
			       sizeof(t_testmess), 0,
			       A_GIMME, 0);
    class_addbang(testmess_class, testmess_bang);
    class_addfloat(testmess_class, testmess_float);
    class_addsymbol(testmess_class, testmess_symbol);
    class_addanything(testmess_class, testmess_anything);
    class_addmethod(testmess_class, (t_method)testmess_set,
		    gensym("set"), A_GIMME, 0);
}