aboutsummaryrefslogtreecommitdiff
path: root/system/kernel/pdp_list.c
blob: ed2ad7611d7b0e83fa360238ac6f8c028756f51c (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
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663

/*
 *   Pure Data Packet header file. List class
 *   Copyright (c) by Tom Schouten <pdp@zzz.kotnet.org>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 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 General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

/* who can live without a list, hmm? */

/* rethink the list of list thing:
   should free and copy really copy the lists, or should
   references to lists be allowed too?

   maybe references to lists as generic pointers? */

#include "pdp_list.h"
#include "pdp_types.h"
#include "pdp.h"
#include <pthread.h>
#include <stdlib.h>

#define D if (0)

#define PDP_LIST_BLOCKSIZE 4096

#define PDP_ATOM_ALLOC _pdp_atom_reuse
#define PDP_ATOM_DEALLOC _pdp_atom_save
//#define PDP_ATOM_ALLOC _pdp_atom_alloc
//#define PDP_ATOM_DEALLOC _pdp_atom_dealloc

static t_pdp_atom *freelist;
static t_pdp_list *blocklist;
static pthread_mutex_t mut;

/* memory allocation (for ease of wrapping) */
static inline t_pdp_list* _pdp_list_alloc(void){return (t_pdp_list *)pdp_alloc(sizeof(t_pdp_list));}
static inline void _pdp_list_dealloc(t_pdp_list *list){pdp_dealloc(list);}
static inline t_pdp_atom* _pdp_atom_alloc(void){return (t_pdp_atom *)pdp_alloc(sizeof(t_pdp_atom));}
static inline void _pdp_atom_dealloc(t_pdp_atom *a){pdp_dealloc(a);}



/* some private helper methods */

/* list pool setup */
void pdp_list_setup(void)
{
    freelist = 0;
    blocklist = _pdp_list_alloc();
    blocklist->elements = 0;
    blocklist->first = 0;
    pthread_mutex_init(&mut, NULL);

#if 0
    {
	char *c = "(test 1 2 (23 4)) (een (zes (ze)ven ()))) (())";
	while (*c){
	    t_pdp_list *l = pdp_list_from_char(c, &c);
	    if (l) pdp_list_print(l); 
	    else{
		post("parse error: remaining input: %s", c); break;
	    }
	}
    }
#endif

}

/* allocate a block */
static t_pdp_atom* _pdp_atom_block_alloc(int size)
{
    int i = size;
    t_pdp_atom *atom_block, *atom;
    atom_block = (t_pdp_atom *)pdp_alloc(sizeof(t_pdp_atom) * size);
    atom = atom_block;
    while(i--){
        atom->next = atom + 1;
        atom->t = a_undef;
	atom->w.w_int = 0;
        atom++;
    }
    atom_block[size-1].next = 0;
    return atom_block;
}

static void _pdp_atomlist_fprint(FILE* f, t_pdp_atom *a);

static void _pdp_atom_refill_freelist(void)
{
    t_pdp_atom *atom;

    if (freelist != 0) post("ERROR:_pdp_atom_do_reuse: freelist != 0");

    /* get a new block */
    freelist = _pdp_atom_block_alloc(PDP_LIST_BLOCKSIZE);
    //post("new block");
    //_pdp_atomlist_fprint(stderr, freelist);

    /* take the first element from the block to serve as a blocklist element */
    atom = freelist;
    atom->t = a_pointer;
    atom->w.w_pointer = freelist;
    freelist = freelist->next;
    atom->next = blocklist->first;
    blocklist->first = atom;
    blocklist->elements++;
    
}


/* reuse an old element from the freelist, or allocate a new block */
static inline t_pdp_atom* _pdp_atom_reuse(void)
{
    t_pdp_atom *atom;

    pthread_mutex_lock(&mut);

    while (!(atom = freelist)){
	_pdp_atom_refill_freelist();
    }
    /* delete the element from the freelist */
    freelist = freelist->next;
    atom->next = 0;

    pthread_mutex_unlock(&mut);

    return atom;
}

static inline void _pdp_atom_save(t_pdp_atom *atom)
{
    pthread_mutex_lock(&mut);

    atom->next = freelist;
    freelist = atom;

    pthread_mutex_unlock(&mut);
}






/* create a list */
t_pdp_list* pdp_list_new(int elements)
{
    t_pdp_atom *a = 0;
    t_pdp_list *l = _pdp_list_alloc();
    l->elements = 0;
    if (elements){
	a = PDP_ATOM_ALLOC();
	l->elements++;
	a->t = a_undef;
	a->w.w_int = 0;
	a->next = 0;
	elements--;
    }
    l->first = a;
    l->last = a;

    while (elements--){
	a = PDP_ATOM_ALLOC();
	l->elements++;
	a->t = a_undef;
	a->w.w_int = 0;
	a->next = l->first;
	l->first = a;
    }
    return l;
}

/* clear a list */
void pdp_list_clear(t_pdp_list *l)
{
    t_pdp_atom *a = l->first; 
    t_pdp_atom *next_a;

    while(a){
	next_a = a->next;
	PDP_ATOM_DEALLOC(a);
	a = next_a;
    }
    l->first = 0;
    l->last = 0;
    l->elements = 0;
}

/* destroy a list */
void pdp_list_free(t_pdp_list *l)
{
    if (l){
	pdp_list_clear(l);
	_pdp_list_dealloc(l);
    }
}


/* destroy a (sub)tree */
void pdp_tree_free(t_pdp_list *l)
{
  if (l) {
    pdp_tree_clear(l);
    _pdp_list_dealloc(l);
  }
}

/* clear a tree */
void pdp_tree_clear(t_pdp_list *l)
{
    t_pdp_atom *a = l->first; 
    t_pdp_atom *next_a;

    while(a){
      if (a->t == a_list) pdp_tree_free(a->w.w_list);
      next_a = a->next;
      PDP_ATOM_DEALLOC(a);
      a = next_a;
    }
    l->first = 0;
    l->last = 0;
    l->elements = 0;
}


static inline int _is_whitespace(char c){return (c == ' ' || c == '\n' || c == '\t');}
static inline void _skip_whitespace(char **c){while (_is_whitespace(**c)) (*c)++;}
static inline int _is_left_separator(char c) {return (c == '(');}
static inline int _is_right_separator(char c) {return (c == ')');}
static inline int _is_separator(char c) {return (_is_left_separator(c) || _is_right_separator(c) || _is_whitespace(c));}


static inline void _parse_atom(t_pdp_word_type *t, t_pdp_word *w, char *c, int n)
{
    char tmp[n+1];
    //post("_parse_atom(%d, %x, %s, %d)", *t, w, c, n); 
    strncpy(tmp, c, n);
    tmp[n] = 0;


    /* check if it's a number */
    if (tmp[0] >= '0' && tmp[0] <= '9'){
	int is_float = 0;
	char *t2;
	for(t2 = tmp; *t2; t2++){
	    if (*t2 == '.') {
		is_float = 1;
		break;
	    }
	}
	/* it's a float */
	if (is_float){
	    float f = strtod(tmp, 0);
	    D post("adding float %f", f);
	    *t = a_float;
	    *w = (t_pdp_word)f;
	}

	/* it's an int */
	else {
	    int i = strtol(tmp, 0, 0);
	    D post("adding int %d", i);
	    *t = a_int;
	    *w = (t_pdp_word)i;
	}
	
    }
    /* it's a symbol */
    else {
	D post("adding symbol %s", tmp);
	*t = a_symbol;
	*w = (t_pdp_word)pdp_gensym(tmp);
    }

    D post("done parsing atom");

}


/* create a list from a character string */
t_pdp_list *pdp_list_from_cstring(char *chardef, char **nextchar)
{
    t_pdp_list *l = pdp_list_new(0);
    char *c = chardef;
    char *lastname = c;
    int n = 0;

    D post ("creating list from char: %s", chardef);

    /* find opening parenthesis and skip it*/
    _skip_whitespace(&c);
    if (!_is_left_separator(*c)) goto error; else c++;

    /* start counting at the first non-whitespace
       character after opening parenthesis */
    _skip_whitespace(&c);
    lastname = c;
    n = 0;

    while(*c){
	if (!_is_separator(*c)){
	    /* item not terminated: keep counting */
	    c++;
	    n++;
	}
	else {
	    /* separator encountered whitespace or parenthesis */

	    if (n){
		/* if there was something between this and the previous
		   separator, we've found and atom and parse it */
		pdp_list_add_back(l, a_undef, (t_pdp_word)0);
		_parse_atom(&l->last->t, &l->last->w, lastname, n);

		/* skip the whitespace after the parsed word, if any */
		_skip_whitespace(&c);
	    }

	    /* if we're at a right separator, we're done */
	    if (_is_right_separator(*c)) {c++; goto done;}

	    /* if it's a left separater, we have a sublist */
	    if (_is_left_separator(*c)){
		t_pdp_list *sublist = pdp_list_from_cstring(c, &c);
		if (!sublist) goto error; //sublist had parse error
		pdp_list_add_back(l, a_list, (t_pdp_word)sublist);
	    }

	    /* prepare for next atom */
	    lastname = c;
	    n = 0;
	}

    }

 error:
    /* end of string encountered: parse error */
    D post("parse error: %s", c);
    if (nextchar) *nextchar = c;
    pdp_tree_free(l); //this will free all sublists too
    return 0;

	
 done:
    _skip_whitespace(&c);
    if (nextchar) *nextchar = c;
    return l;
    
    

}


/* traversal */
void pdp_list_apply(t_pdp_list *l, t_pdp_atom_method m)
{
  t_pdp_atom *a;
  if (!l) return;
  for (a=l->first; a; a=a->next) m(a);
}

void pdp_tree_apply(t_pdp_list *l, t_pdp_atom_method m) 
{
  t_pdp_atom *a;
  if (!l) return;
  for (a=l->first; a; a=a->next){
    if (a->t == a_list) pdp_tree_apply(a->w.w_list, m);
    else m(a);
  }
}

void pdp_list_apply_word_method(t_pdp_list *l, 
				t_pdp_word_type type, t_pdp_word_method wm)
{
  t_pdp_atom *a;
  if (!l) return;
  for (a=l->first; a; a=a->next){
    if (a->t == type) wm(a->w);
  }
}
void pdp_list_apply_pword_method(t_pdp_list *l, 
				t_pdp_word_type type, t_pdp_pword_method pwm)
{
  t_pdp_atom *a;
  if (!l) return;
  for (a=l->first; a; a=a->next){
    if (a->t == type) pwm(&a->w);
  }
}

void pdp_tree_apply_word_method(t_pdp_list *l, 
				t_pdp_word_type type, t_pdp_word_method wm) 
{
  t_pdp_atom *a;
  if (!l) return;
  for (a=l->first; a; a=a->next){
    if (a->t == a_list) pdp_tree_apply_word_method(a->w.w_list, type, wm);
    else if (a->t == type) wm(a->w);
  }
}
void pdp_tree_apply_pword_method(t_pdp_list *l, 
				t_pdp_word_type type, t_pdp_pword_method pwm) 
{
  t_pdp_atom *a;
  if (!l) return;
  for (a=l->first; a; a=a->next){
    if (a->t == a_list) pdp_tree_apply_pword_method(a->w.w_list, type ,pwm);
    else if (a->t == type) pwm(&a->w);
  }
}

static void _atom_packet_mark_unused(t_pdp_atom *a)
{
  if (a->t == a_packet){
    pdp_packet_mark_unused(a->w.w_packet);
    a->t = a_undef;
    a->w.w_int = 0;
  }
}

void pdp_tree_strip_packets  (t_pdp_list *l)
{
   pdp_tree_apply(l, _atom_packet_mark_unused);
}


/* debug */
static void _pdp_atomlist_fprint(FILE* f, t_pdp_atom *a)
{
    fprintf(f, "(");
    while (a){
	switch(a->t){
	case a_symbol:   fprintf(f, "%s",a->w.w_symbol->s_name); break;
	case a_float:    fprintf(f, "%f",a->w.w_float); break;
	case a_int:      fprintf(f, "%d",a->w.w_int); break;
	case a_packet:   fprintf(f, "#<pdp %d %s>",a->w.w_packet,
				 pdp_packet_get_description(a->w.w_packet)->s_name); break;
	case a_pointer:  fprintf(f, "#<0x%08x>",(unsigned int)a->w.w_pointer); break;
	case a_list:     _pdp_atomlist_fprint(f, a->w.w_list->first); break;
	case a_undef:    fprintf(f, "undef"); break;

	default:         fprintf(f, "unknown"); break;
	}
       	a = a->next;
	if (a) fprintf(f, " ");
    }
    fprintf(f, ")");
}

void _pdp_list_fprint(FILE* f, t_pdp_list *l)
{
    _pdp_atomlist_fprint(f, l->first);
    fprintf(f, "\n");
}

void pdp_list_print(t_pdp_list *l)
{
    _pdp_list_fprint(stderr, l);
}


/* public list operations */


/* add a word to the start of the list */
void pdp_list_add(t_pdp_list *l, t_pdp_word_type t, t_pdp_word w)
{
    t_pdp_atom *new_a = PDP_ATOM_ALLOC();
    l->elements++;
    new_a->next = l->first;
    new_a->w = w;
    new_a->t = t;
    l->first = new_a;
    if (!l->last) l->last = new_a;
}


/* add a word to the end of the list */
void pdp_list_add_back(t_pdp_list *l, t_pdp_word_type t, t_pdp_word w)
{
    t_pdp_atom *new_a = PDP_ATOM_ALLOC();
    l->elements++;
    new_a->next = 0;
    new_a->w = w;
    new_a->t = t;
    if (l->last){
	l->last->next = new_a;
    }
    else{
	l->first = new_a;
    }
    l->last = new_a;
}

/* get list size */
int pdp_list_size(t_pdp_list *l)
{
    return l->elements;
}




/* pop: return first item and remove */
t_pdp_word pdp_list_pop(t_pdp_list *l)
{
    t_pdp_atom *a = l->first;
    t_pdp_word w;

    w = a->w;
    l->first = a->next;
    PDP_ATOM_DEALLOC(a);
    l->elements--;
    if (!l->first) l->last = 0;

    return w;
}


/* return element at index */
t_pdp_word pdp_list_index(t_pdp_list *l, int index)
{
    t_pdp_atom *a;
    for (a = l->first; index--; a = a->next);
    return a->w;
}





/* remove an element from a list */
void pdp_list_remove(t_pdp_list *l, t_pdp_word_type t, t_pdp_word w)
{
    t_pdp_atom head;
    t_pdp_atom *a;
    t_pdp_atom *kill_a;
    head.next = l->first;

    for(a = &head; a->next; a = a->next){
	if (a->next->w.w_int == w.w_int && a->next->t == t){
	    kill_a = a->next;        // element to be killed
	    a->next = a->next->next; // remove link
	    PDP_ATOM_DEALLOC(kill_a);
	    l->elements--;
	    l->first = head.next;    // restore the start pointer
	    if (l->last == kill_a) { // restore the end pointer
		l->last = (a != &head) ? a : 0;
	    }

	    break;
	}
    }
    
}





/* copy a list */
t_pdp_list* pdp_tree_copy_reverse(t_pdp_list *list)
{
    t_pdp_list *newlist = pdp_list_new(0);
    t_pdp_atom *a;
    for (a = list->first; a; a = a->next)
	if (a->t == a_list){
	    pdp_list_add(newlist, a->t, 
			 (t_pdp_word)pdp_tree_copy_reverse(a->w.w_list));
	}
	else{
	    pdp_list_add(newlist, a->t, a->w);
	}
    return newlist;
}
t_pdp_list* pdp_list_copy_reverse(t_pdp_list *list)
{
    t_pdp_list *newlist = pdp_list_new(0);
    t_pdp_atom *a;
    for (a = list->first; a; a = a->next)
      pdp_list_add(newlist, a->t, a->w);
    return newlist;
}

t_pdp_list* pdp_tree_copy(t_pdp_list *list)
{
    t_pdp_list *newlist = pdp_list_new(list->elements);
    t_pdp_atom *a_src = list->first;
    t_pdp_atom *a_dst = newlist->first;

    while(a_src){
	a_dst->t = a_src->t;
	if (a_dst->t == a_list){ //recursively copy sublists (tree copy)
	    a_dst->w.w_list = pdp_tree_copy(a_src->w.w_list);
	}
	else{
	    a_dst->w = a_src->w;
	}
	a_src = a_src->next;
	a_dst = a_dst->next;
    }

    return newlist;
}
t_pdp_list* pdp_list_copy(t_pdp_list *list)
{
    t_pdp_list *newlist = pdp_list_new(list->elements);
    t_pdp_atom *a_src = list->first;
    t_pdp_atom *a_dst = newlist->first;

    while(a_src){
	a_dst->t = a_src->t;
	a_dst->w = a_src->w;
	a_src = a_src->next;
	a_dst = a_dst->next;
    }
    return newlist;
}

void pdp_list_cat (t_pdp_list *l, t_pdp_list *tail)
{
    t_pdp_list *tmp = pdp_list_copy(tail);
    l->elements += tmp->elements;
    l->last->next = tmp->first;
    l->last = tmp->last;
    _pdp_list_dealloc(tmp); //delete the list stub
    
}


/* check if a list contains an element */
int pdp_list_contains(t_pdp_list *list, t_pdp_word_type t, t_pdp_word w)
{
    t_pdp_atom *a;
    for(a = list->first; a; a=a->next){
	if (a->w.w_int == w.w_int && a->t == t) return 1;
    }
    return 0;
}

/* add a thing to the start of the list if it's not in there already */
void pdp_list_add_to_set(t_pdp_list *list, t_pdp_word_type t, t_pdp_word w)
{
    if (!pdp_list_contains(list, t, w))
	pdp_list_add(list, t, w);
}