~kamalmostafa/ubuntu/lucid/pdp/fix-504941-ftbfs

« back to all changes in this revision

Viewing changes to system/kernel/pdp_type.c

  • Committer: Bazaar Package Importer
  • Author(s): Guenter Geiger (Debian/GNU)
  • Date: 2005-03-15 22:21:05 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050315222105-1q287rsihmd9j1tb
Tags: 1:0.12.4-2
* fixed the hardcoded depends
* added 3dp library

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *   Pure Data Packet system implementation. : code for handling different packet types
3
 
 *   Copyright (c) by Tom Schouten <pdp@zzz.kotnet.org>
4
 
 *
5
 
 *   This program is free software; you can redistribute it and/or modify
6
 
 *   it under the terms of the GNU General Public License as published by
7
 
 *   the Free Software Foundation; either version 2 of the License, or
8
 
 *   (at your option) any later version.
9
 
 *
10
 
 *   This program is distributed in the hope that it will be useful,
11
 
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 *   GNU General Public License for more details.
14
 
 *
15
 
 *   You should have received a copy of the GNU General Public License
16
 
 *   along with this program; if not, write to the Free Software
17
 
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
 
 *
19
 
 */
20
 
 
21
 
 
22
 
/* this file contains type handling routines */
23
 
 
24
 
#include <stdarg.h>
25
 
#include <string.h>
26
 
#include <pthread.h>
27
 
#include "pdp_list.h"
28
 
#include "pdp_symbol.h"
29
 
#include "pdp_packet.h"
30
 
#include "pdp_post.h"
31
 
#include "pdp_type.h"
32
 
#include "pdp_mem.h"
33
 
#include "pdp_debug.h"
34
 
 
35
 
 
36
 
// debug
37
 
#define D if (0)
38
 
 
39
 
 
40
 
static t_pdp_list *conversion_list;
41
 
 
42
 
#define INIT_MAX_CACHE_SIZE 32
43
 
 
44
 
static t_pdp_list *cached_conversion_list;
45
 
static int max_cache_size;
46
 
 
47
 
/* mutex */
48
 
static pthread_mutex_t pdp_conversion_mutex;
49
 
static pthread_mutex_t pdp_cache_mutex;
50
 
 
51
 
 
52
 
 
53
 
/* convert a type to a list */
54
 
t_pdp_list *pdp_type_to_list(t_pdp_symbol *type)
55
 
{
56
 
#define TMPSIZE 1024
57
 
 
58
 
    char *c = type->s_name;
59
 
    char *lastname = c;
60
 
    int n = 0;
61
 
    char tmp[strlen(type->s_name)+1];
62
 
    t_pdp_list *l = pdp_list_new(0);
63
 
 
64
 
    while(*c){
65
 
        if (*c == '/'){
66
 
            strncpy(tmp, lastname, n);
67
 
            tmp[n] = 0;
68
 
            pdp_list_add_back(l, a_symbol, (t_pdp_word)pdp_gensym(tmp));
69
 
            c++;
70
 
            lastname = c;
71
 
            n = 0;
72
 
        }
73
 
        else{
74
 
            c++;
75
 
            n++;
76
 
            PDP_ASSERT(n < TMPSIZE);
77
 
        }
78
 
    }
79
 
    pdp_list_add_back(l, a_symbol, (t_pdp_word)pdp_gensym(lastname));
80
 
 
81
 
    return l;
82
 
}
83
 
 
84
 
 
85
 
/* get the description symbol. */
86
 
t_pdp_symbol *pdp_packet_get_description(int packet)
87
 
{
88
 
    t_pdp *header = pdp_packet_header(packet);
89
 
 
90
 
    if (!header) return pdp_gensym("invalid");
91
 
    else if (!header->desc){
92
 
 
93
 
        /* since every packet is obliged to have a description, this is an error */
94
 
        pdp_post("ERROR: pdp_packet_get_description: packet %d has no description.", packet);
95
 
        pdp_packet_print_debug(packet);
96
 
        return pdp_gensym("unknown");
97
 
    }
98
 
    else return header->desc;
99
 
}
100
 
 
101
 
 
102
 
 
103
 
/* this runs a conversion program */
104
 
int _pdp_type_run_conversion_program(t_pdp_conversion_program *program,
105
 
                                    int packet, t_pdp_symbol *dest_template)
106
 
{
107
 
    /* run a conversion program:
108
 
       treat the source packet as readonly, and cleanup intermediates, such
109
 
       that the net result is the production of a new packet, with the
110
 
       source packet intact. */
111
 
 
112
 
    int p, tmp;
113
 
    t_pdp_atom *a;
114
 
    t_pdp_conversion_method m;
115
 
 
116
 
    // run the first line of the program
117
 
    a = program->first;
118
 
    m = a->w.w_pointer;
119
 
    D pdp_post("DEBUG: _pdp_type_run_conversion_program: method = %x", m);
120
 
    p = m(packet, dest_template);
121
 
    D pdp_post("DEBUG: _pdp_type_run_conversion_program:");
122
 
    D pdp_post("        packet returned = %d, type = %s", 
123
 
               p, pdp_packet_get_description(p)->s_name);
124
 
 
125
 
    // run the remaining lines + cleanup intermediates
126
 
    for (a=a->next; a; a=a->next){
127
 
        m = a->w.w_pointer;
128
 
        D pdp_post("DEBUG: _pdp_type_run_conversion_program: next method ptr = %x", m);
129
 
        tmp = m(p, dest_template);
130
 
        pdp_packet_mark_unused(p);
131
 
        p = tmp;
132
 
    }
133
 
    return p;
134
 
}
135
 
 
136
 
 
137
 
/* find a conversion program */
138
 
t_pdp_conversion_program *
139
 
_pdp_type_find_conversion_program(t_pdp_symbol *src_pattern, t_pdp_symbol *dst_pattern)
140
 
{
141
 
    t_pdp_conversion *c;
142
 
    t_pdp_atom *a;
143
 
    t_pdp_conversion_program *retval = 0;
144
 
 
145
 
    /* lock conversion list */
146
 
    pthread_mutex_lock(&pdp_conversion_mutex);
147
 
 
148
 
    for (a = conversion_list->first; a; a=a->next){
149
 
        c = a->w.w_pointer;
150
 
        /* can be a wildcard match */
151
 
        if (pdp_type_description_match(src_pattern, c->src_pattern) &&
152
 
            pdp_type_description_match(dst_pattern, c->dst_pattern)) {
153
 
            /* found a program */
154
 
            D pdp_post("DEBUG: _pdp_type_find_conversion_program: found: %s -> %s",
155
 
                       c->src_pattern->s_name, c->dst_pattern->s_name);
156
 
            retval = c->program;
157
 
            goto gotit;
158
 
        }
159
 
    }
160
 
 
161
 
    /* no conversion program was found */
162
 
    retval = 0;
163
 
 gotit:
164
 
 
165
 
    /* lock conversion list */
166
 
    pthread_mutex_unlock(&pdp_conversion_mutex);
167
 
    return retval;
168
 
}
169
 
 
170
 
/* find a cached conversion program 
171
 
 if one is found it will be moved to the back of the queue (MRU) */
172
 
t_pdp_conversion_program *
173
 
_pdp_type_find_cached_conversion_program(t_pdp_symbol *src_pattern, t_pdp_symbol *dst_pattern)
174
 
{
175
 
    t_pdp_conversion *c, *c_tmp;
176
 
    t_pdp_atom *a;
177
 
    t_pdp_conversion_program *retval = 0;
178
 
 
179
 
    /* lock cached list */
180
 
    pthread_mutex_lock(&pdp_cache_mutex);
181
 
 
182
 
    for (a = cached_conversion_list->first; a; a=a->next){
183
 
        c = a->w.w_pointer;
184
 
        /* must be exact match */
185
 
        if ((src_pattern == c->src_pattern) &&
186
 
            (dst_pattern == c->dst_pattern)) {
187
 
 
188
 
            /* found a program */
189
 
            D pdp_post("DEBUG: _pdp_type_find_cached_conversion_program: found: %s -> %s", 
190
 
                       c->src_pattern->s_name, c->dst_pattern->s_name);
191
 
            retval = c->program;
192
 
 
193
 
            /* make MRU (move to back) */
194
 
            c_tmp = cached_conversion_list->last->w.w_pointer;
195
 
            cached_conversion_list->last->w.w_pointer = c;
196
 
            a->w.w_pointer = c_tmp;
197
 
            goto gotit;
198
 
        }
199
 
    }
200
 
 
201
 
    retval = 0;
202
 
 
203
 
 gotit:
204
 
 
205
 
 
206
 
    /* un lock cached list */
207
 
    pthread_mutex_unlock(&pdp_cache_mutex);
208
 
 
209
 
    /* no conversion program was found */
210
 
    return retval;
211
 
}
212
 
 
213
 
 
214
 
/* conversion program manipulations */
215
 
void pdp_conversion_program_free(t_pdp_conversion_program *program)
216
 
{
217
 
    pdp_list_free(program);
218
 
}
219
 
 
220
 
/* debug print */
221
 
void _pdp_conversion_program_print(t_pdp_conversion_program *program)
222
 
{
223
 
    D pdp_post("_pdp_conversion_program_print %x", program);
224
 
    pdp_list_print(program);
225
 
}
226
 
 
227
 
t_pdp_conversion_program *pdp_conversion_program_new(t_pdp_conversion_method method, ...)
228
 
{
229
 
    t_pdp_conversion_program *p = pdp_list_new(0);
230
 
    t_pdp_conversion_method m = method;
231
 
    va_list ap;
232
 
 
233
 
    D pdp_post("DEBUG: pdp_conversion_program_new:BEGIN");
234
 
 
235
 
    pdp_list_add_back_pointer(p, m);
236
 
    va_start(ap, method);
237
 
    while (m = va_arg(ap, t_pdp_conversion_method)) pdp_list_add_back_pointer(p, m);
238
 
    va_end(ap);
239
 
 
240
 
    D pdp_post("DEBUG: pdp_conversion_program_new:END");
241
 
 
242
 
    return p;
243
 
}
244
 
 
245
 
t_pdp_conversion_program *pdp_conversion_program_copy(t_pdp_conversion_program *program)
246
 
{
247
 
    if (program) return pdp_list_copy(program);
248
 
    else return 0;
249
 
}
250
 
 
251
 
void pdp_conversion_program_add(t_pdp_conversion_program *program, 
252
 
                                t_pdp_conversion_program *tail)
253
 
254
 
    return pdp_list_cat(program, tail);
255
 
}
256
 
 
257
 
/* conversion registration */
258
 
void pdp_type_register_conversion (t_pdp_symbol *src_pattern, t_pdp_symbol *dst_pattern, 
259
 
                                   t_pdp_conversion_program *program)
260
 
{
261
 
    t_pdp_conversion *c = (t_pdp_conversion *)pdp_alloc(sizeof(*c));
262
 
    c->src_pattern = src_pattern;
263
 
    c->dst_pattern = dst_pattern;
264
 
    c->program = program;
265
 
 
266
 
    /* lock conversion list */
267
 
    pthread_mutex_lock(&pdp_conversion_mutex);
268
 
 
269
 
    pdp_list_add_back_pointer(conversion_list, c);
270
 
 
271
 
    /* unlock conversion list */
272
 
    pthread_mutex_unlock(&pdp_conversion_mutex);
273
 
    
274
 
}
275
 
 
276
 
/* register a cached conversion */
277
 
void pdp_type_register_cached_conversion (t_pdp_symbol *src_pattern, t_pdp_symbol *dst_pattern, t_pdp_conversion_program *program)
278
 
{
279
 
 
280
 
    /* create the new conversion */
281
 
    t_pdp_conversion *c = (t_pdp_conversion *)pdp_alloc(sizeof(*c));
282
 
    c->src_pattern = src_pattern;
283
 
    c->dst_pattern = dst_pattern;
284
 
    c->program = program;
285
 
 
286
 
    /* lock cached conversion list */
287
 
    pthread_mutex_lock(&pdp_cache_mutex);
288
 
 
289
 
    /* check size, and remove LRU (top) if the cache is full */
290
 
    while (cached_conversion_list->elements >= max_cache_size){
291
 
        t_pdp_conversion *c_old = pdp_list_pop(cached_conversion_list).w_pointer;
292
 
        if (c_old->program) pdp_conversion_program_free(c_old->program);
293
 
        pdp_dealloc(c_old);
294
 
    }
295
 
    
296
 
    /* add and make MRU (back) */
297
 
    pdp_list_add_back_pointer(cached_conversion_list, c);
298
 
 
299
 
    /* unlock cached conversion list */
300
 
    pthread_mutex_unlock(&pdp_cache_mutex);
301
 
}
302
 
 
303
 
/* convert a given packet to a certain type (template) */
304
 
int _pdp_packet_convert(int packet, t_pdp_symbol *dest_template)
305
 
{
306
 
    t_pdp_symbol *type = pdp_packet_get_description(packet);
307
 
    t_pdp_symbol *tmp_type = 0;
308
 
    int tmp_packet = -1;
309
 
 
310
 
    t_pdp_conversion_program *program = 0;
311
 
    t_pdp_conversion_program *program_last = 0;
312
 
    t_pdp_conversion_program *program_tail = 0;
313
 
 
314
 
    /* check if there is a program in the cached list, if so run it */
315
 
    if (program = _pdp_type_find_cached_conversion_program(type, dest_template))
316
 
        return _pdp_type_run_conversion_program(program, packet, dest_template);
317
 
 
318
 
    /* if it is not cached, iteratively convert 
319
 
       and save program on top of cache list if a conversion path (program) was found */
320
 
 
321
 
    // run first conversion that matches
322
 
    program = pdp_conversion_program_copy
323
 
        (_pdp_type_find_conversion_program(type, dest_template));
324
 
    program_last = program;
325
 
    if (!program){
326
 
        D pdp_post("DEBUG: pdp_type_convert: (1) can't convert %s to %s", 
327
 
                   type->s_name, dest_template->s_name);
328
 
        return -1;
329
 
    }
330
 
    tmp_packet = _pdp_type_run_conversion_program(program, packet, dest_template);
331
 
    tmp_type = pdp_packet_get_description(tmp_packet);
332
 
 
333
 
    // run more conversions if necessary, deleting intermediate packets
334
 
    while (!pdp_type_description_match(tmp_type, dest_template)){
335
 
        int new_packet;
336
 
        program_tail = _pdp_type_find_conversion_program(tmp_type, dest_template);
337
 
        if (!program_tail){
338
 
            D pdp_post("DEBUG: pdp_type_convert: (2) can't convert %s to %s", 
339
 
                       tmp_type->s_name, dest_template->s_name);
340
 
            pdp_packet_mark_unused(tmp_packet);
341
 
            pdp_conversion_program_free(program);
342
 
            return -1;
343
 
        }
344
 
        if (program_last == program_tail){
345
 
            pdp_post("ERROR: pdp_packet_convert: conversion loop detected");
346
 
        }
347
 
        program_last = program_tail;
348
 
 
349
 
        pdp_conversion_program_add(program, program_tail);
350
 
        new_packet = _pdp_type_run_conversion_program(program_tail, tmp_packet, dest_template);
351
 
        pdp_packet_mark_unused(tmp_packet);
352
 
        tmp_packet = new_packet;
353
 
        tmp_type = pdp_packet_get_description(tmp_packet);
354
 
    }
355
 
 
356
 
    // save the conversion program in the cache
357
 
    pdp_type_register_cached_conversion(type, dest_template, program);
358
 
 
359
 
    // return resulting packet
360
 
    return tmp_packet;
361
 
        
362
 
}
363
 
 
364
 
/* convert or copy ro */
365
 
int pdp_packet_convert_ro(int packet, t_pdp_symbol *dest_template)
366
 
{
367
 
    t_pdp_symbol *type = pdp_packet_get_description(packet);
368
 
 
369
 
    /* if it is compatible, return a ro copy */
370
 
    if (pdp_type_description_match(type, dest_template)) return pdp_packet_copy_ro(packet);
371
 
 
372
 
    /* if not, convert to a new type */
373
 
    else return _pdp_packet_convert(packet, dest_template);
374
 
}
375
 
 
376
 
/* convert or copy rw */
377
 
int pdp_packet_convert_rw(int packet, t_pdp_symbol *dest_template)
378
 
{
379
 
    t_pdp_symbol *type = pdp_packet_get_description(packet);
380
 
 
381
 
    /* if it is compatible, just return a rw copy */
382
 
    if (pdp_type_description_match(type, dest_template)) return pdp_packet_copy_rw(packet);
383
 
 
384
 
    /* if not, convert to a new type */
385
 
    else return _pdp_packet_convert(packet, dest_template);
386
 
}
387
 
 
388
 
 
389
 
/* this is a hack. type cache data: (a type description parsed into
390
 
   a pdp_list for fast comparison) should be setup when
391
 
   a packet symbol is created, but since that can be everywhere,
392
 
   we set it up on first use. type cache is permanent. */
393
 
 
394
 
 
395
 
static void _setup_type_cache(t_pdp_symbol *s)
396
 
{
397
 
    t_pdp_list *l = pdp_type_to_list(s); 
398
 
    if (!pdp_symbol_set_typelist(s, l))
399
 
        pdp_list_free(l); // list was already present -> free cached list
400
 
 
401
 
}
402
 
 
403
 
/* check if a type description fits a template
404
 
   this function is symmetric */
405
 
int pdp_type_description_match(t_pdp_symbol *description, t_pdp_symbol *pattern)
406
 
{
407
 
    int match = 0; // no match until all tests are passed
408
 
 
409
 
    t_pdp_atom *ad, *ap;
410
 
    t_pdp_symbol *wildcard = PDP_SYM_WILDCARD;
411
 
 
412
 
    PDP_ASSERT(pattern);
413
 
    PDP_ASSERT(description);
414
 
 
415
 
    /* same type symbol -> match */
416
 
    if (description == pattern) {match = 1; goto done;}
417
 
 
418
 
    /* check the description list */
419
 
    if (!(description->s_type)) _setup_type_cache(description);
420
 
    if (!(pattern->s_type))     _setup_type_cache(pattern);
421
 
 
422
 
    /* compare symbols of description list */
423
 
    for(ad=description->s_type->first, ap=pattern->s_type->first; 
424
 
        ad && ap; 
425
 
        ad=ad->next, ap=ap->next)
426
 
    {
427
 
 
428
 
        if (ad->w.w_symbol == wildcard) continue;
429
 
        if (ap->w.w_symbol == wildcard) continue;
430
 
        if (ad->w.w_symbol != ap->w.w_symbol) {goto done;} /* difference and not a wildcard */
431
 
    }
432
 
 
433
 
    /* ok if sizes are equal */
434
 
    if (! (ad || ap)) {match = 1; goto done;}
435
 
 
436
 
    /* one of the two is shorter, so the shortest list needs 
437
 
       to end with a wildcard to have a match */
438
 
 
439
 
    if (ap && description->s_type->last->w.w_symbol != wildcard) goto done;
440
 
    if (ad && pattern->s_type->last->w.w_symbol != wildcard) goto done;
441
 
    
442
 
    /* all tests passed: type templates match */
443
 
    match = 1;
444
 
 
445
 
 done:
446
 
    D pdp_post("DEBUG: testing match between %s and %s: %s", 
447
 
           description->s_name, pattern->s_name, match ? "match" : "no match");
448
 
    return match;
449
 
 
450
 
}
451
 
 
452
 
 
453
 
 
454
 
 
455
 
 
456
 
/* setup method */
457
 
void pdp_type_setup(void)
458
 
{
459
 
    int i;
460
 
 
461
 
    // create mutexes
462
 
    pthread_mutex_init(&pdp_conversion_mutex, NULL);
463
 
    pthread_mutex_init(&pdp_cache_mutex, NULL);
464
 
 
465
 
    // create conversion lists
466
 
    cached_conversion_list = pdp_list_new(0);
467
 
    conversion_list = pdp_list_new(0);
468
 
    max_cache_size = INIT_MAX_CACHE_SIZE;
469
 
 
470
 
 
471
 
 
472
 
 
473
 
}