[go: up one dir, main page]

Menu

[205fcb]: / src / bundle.c  Maximize  Restore  History

Download this file

427 lines (349 with data), 8.8 kB

  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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
* Copyright (C) 2004 Steve Harris
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* $Id$
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "lo_types_internal.h"
#include "lo/lo.h"
lo_bundle lo_bundle_new(lo_timetag tt)
{
lo_bundle b = calloc(1, sizeof(struct _lo_bundle));
b->size = 4;
b->len = 0;
b->ts = tt;
b->elmnts = calloc(b->size, sizeof(lo_element));
b->refcount = 0;
return b;
}
void lo_bundle_incref(lo_bundle b)
{
b->refcount ++;
}
static
void lo_bundle_decref(lo_bundle b)
{
b->refcount --;
}
static lo_bundle *push_to_list(lo_bundle *list, lo_bundle ptr, size_t *len, size_t *size)
{
if (*len >= *size) {
*size *= 2;
list = realloc(list, *size * sizeof(lo_bundle));
}
list[*len] = ptr;
(*len)++;
return list;
}
static lo_bundle *pop_from_list(lo_bundle *list, size_t *len, size_t *size)
{
(*len)--;
return list;
}
static int is_in_list(lo_bundle *list, lo_bundle ptr, size_t *len)
{
size_t i;
for (i = 0; i < *len; i++)
if (list[i] == ptr)
return -1;
return 0;
}
static lo_bundle *walk_tree(lo_bundle *B, lo_bundle b, size_t *len, size_t *size, int *ret)
{
size_t i;
int res;
// check whether bundle is part of parents list
if (is_in_list(B, b, len)) {
*ret = -1;
return B;
}
// push bundle to parents list
B = push_to_list(B, b, len, size);
res = 0;
for (i = 0; i < b->len; i++) {
if (b->elmnts[i].type == LO_ELEMENT_BUNDLE) {
B = walk_tree(B, b->elmnts[i].content.bundle, len, size, &res);
if (res)
break;
}
}
// pop bundle from parents list
B = pop_from_list(B, len, size);
*ret = res;
return B;
}
static int lo_bundle_circular(lo_bundle b)
{
size_t len = 0;
size_t size = 4;
int res;
lo_bundle *B = calloc(size, sizeof(lo_bundle));
B = walk_tree(B, b, &len, &size, &res);
if (B)
free(B);
return res;
}
static int lo_bundle_add_element(lo_bundle b, int type, const char *path, void *elmnt)
{
if (b->len >= b->size) {
b->size *= 2;
b->elmnts = realloc(b->elmnts, b->size * sizeof(lo_element));
if (!b->elmnts)
return -1;
}
b->elmnts[b->len].type = type;
switch (type) {
case LO_ELEMENT_MESSAGE: {
lo_message msg = elmnt;
lo_message_incref(msg);
b->elmnts[b->len].content.message.msg = msg;
b->elmnts[b->len].content.message.path = path;
(b->len)++;
break;
}
case LO_ELEMENT_BUNDLE: {
lo_bundle bndl = elmnt;
lo_bundle_incref(bndl);
b->elmnts[b->len].content.bundle = bndl;
(b->len)++;
// do not add bundle if a circular reference is found
if (lo_bundle_circular(b))
{
// note that this is a special case where we _know_ that
// decrement should not result in a free, therefore we
// avoid it explicitly. otherwise double-free might
// happen if calling code still has a reference to the
// added bundle, since it's possible that refcount==0.
lo_bundle_decref(bndl);
(b->len)--;
return -1;
}
break;
}
}
return 0;
}
int lo_bundle_add_message(lo_bundle b, const char *path, lo_message m)
{
if (!m)
return 0;
return lo_bundle_add_element (b, LO_ELEMENT_MESSAGE, path, m);
}
int lo_bundle_add_bundle(lo_bundle b, lo_bundle n)
{
if (!n)
return 0;
return lo_bundle_add_element (b, LO_ELEMENT_BUNDLE, NULL, n);
}
lo_element_type lo_bundle_get_type(lo_bundle b, int index)
{
if (index < (int)b->len) {
return b->elmnts[index].type;
}
return 0;
}
lo_bundle lo_bundle_get_bundle(lo_bundle b, int index)
{
if ( (index < (int)b->len) && (b->elmnts[index].type == LO_ELEMENT_BUNDLE) ) {
return b->elmnts[index].content.bundle;
}
return 0;
}
lo_message lo_bundle_get_message(lo_bundle b, int index,
const char **path)
{
if ( (index < (int)b->len) && (b->elmnts[index].type == LO_ELEMENT_MESSAGE) ) {
if (path)
*path = b->elmnts[index].content.message.path;
return b->elmnts[index].content.message.msg;
}
return 0;
}
lo_timetag lo_bundle_get_timestamp(lo_bundle b)
{
return b->ts;
}
unsigned int lo_bundle_count(lo_bundle b)
{
return b->len;
}
size_t lo_bundle_length(lo_bundle b)
{
size_t size = 16; /* "#bundle" and the timetag */
size_t i;
if (!b) {
return 0;
}
size += b->len * 4; /* sizes */
for (i = 0; i < b->len; i++)
switch (b->elmnts[i].type) {
case LO_ELEMENT_BUNDLE:
size += lo_bundle_length(b->elmnts[i].content.bundle);
break;
case LO_ELEMENT_MESSAGE:
size += lo_message_length(b->elmnts[i].content.message.msg, b->elmnts[i].content.message.path);
break;
}
return size;
}
void *lo_bundle_serialise(lo_bundle b, void *to, size_t * size)
{
size_t s, skip;
int32_t *bes;
size_t i;
char *pos;
lo_pcast32 be;
if (!b) {
if (size)
*size = 0;
return NULL;
}
s = lo_bundle_length(b);
if (size) {
*size = s;
}
if (!to) {
to = calloc(1, s);
}
pos = to;
strcpy(pos, "#bundle");
pos += 8;
be.nl = lo_htoo32(b->ts.sec);
memcpy(pos, &be, 4);
pos += 4;
be.nl = lo_htoo32(b->ts.frac);
memcpy(pos, &be, 4);
pos += 4;
for (i = 0; i < b->len; i++) {
switch (b->elmnts[i].type) {
case LO_ELEMENT_MESSAGE:
lo_message_serialise(b->elmnts[i].content.message.msg, b->elmnts[i].content.message.path, pos + 4, &skip);
break;
case LO_ELEMENT_BUNDLE:
lo_bundle_serialise(b->elmnts[i].content.bundle, pos+4, &skip);
break;
}
bes = (int32_t *) (void *)pos;
*bes = lo_htoo32(skip);
pos += skip + 4;
if (pos > (char *) to + s) {
fprintf(stderr, "liblo: data integrity error at message %lu\n",
(long unsigned int)i);
return NULL;
}
}
if (pos != (char *) to + s) {
fprintf(stderr, "liblo: data integrity error\n");
if (to) {
free(to);
}
return NULL;
}
return to;
}
void lo_bundle_free(lo_bundle b)
{
if (!b) {
return;
}
lo_bundle_decref(b);
if (b->refcount > 0)
return;
free(b->elmnts);
free(b);
}
static void collect_element(lo_element *elmnt)
{
switch (elmnt->type) {
case LO_ELEMENT_MESSAGE: {
lo_message msg = elmnt->content.message.msg;
lo_message_free(msg);
break;
}
case LO_ELEMENT_BUNDLE: {
lo_bundle bndl = elmnt->content.bundle;
lo_bundle_free_recursive(bndl);
break;
}
}
}
void lo_bundle_free_messages(lo_bundle b)
{
lo_bundle_free_recursive(b);
}
void lo_bundle_free_recursive(lo_bundle b)
{
size_t i;
if (!b)
return;
lo_bundle_decref(b);
if (b->refcount > 0)
return;
for (i = 0; i < b->len; i++)
collect_element(&b->elmnts[i]);
free(b->elmnts);
free(b);
}
static void offset_pp(int offset, int *state)
{
size_t i;
for (i=0; i < offset; i++) {
if (!state[i])
printf("│ ");
else // last
printf(" ");
}
if (!state[offset])
printf("├─");
else // last
printf("└─");
}
static int *lo_bundle_pp_internal(lo_bundle b, int offset, int *state, size_t *len)
{
size_t i;
if (offset+2 > *len) {
*len *= 2;
state = realloc(state, *len*sizeof(int));
}
offset_pp(offset, state);
printf("bundle─┬─(%08x.%08x)\n", b->ts.sec, b->ts.frac);
for (i = 0; i < b->len; i++) {
state[offset+1] = i != b->len-1 ? 0 : 1;
switch(b->elmnts[i].type) {
case LO_ELEMENT_MESSAGE:
offset_pp(offset+1, state);
printf("%s ", b->elmnts[i].content.message.path);
lo_message_pp(b->elmnts[i].content.message.msg);
break;
case LO_ELEMENT_BUNDLE:
state = lo_bundle_pp_internal(b->elmnts[i].content.bundle, offset+1, state, len);
break;
}
}
return state;
}
void lo_bundle_pp(lo_bundle b)
{
size_t len;
int *state;
if (!b)
return;
len = 4;
state = calloc(len, sizeof(int));
state[0] = 1;
state = lo_bundle_pp_internal(b, 0, state, &len);
free (state);
}
/* vi:set ts=8 sts=4 sw=4: */