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

« back to all changes in this revision

Viewing changes to system/kernel/.#pdp_symbol.c.1.9

  • 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 implementing pdp's namespace (symbols)
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
 
#include <string.h>
22
 
#include <pthread.h>
23
 
#include "pdp_symbol.h"
24
 
#include "pdp_list.h"
25
 
#include "pdp_debug.h"
26
 
 
27
 
// some extra prototypes
28
 
void *pdp_alloc(int size);
29
 
void pdp_dealloc(void *data);
30
 
 
31
 
// the symbol hash mutex
32
 
static pthread_mutex_t pdp_hash_mutex;
33
 
 
34
 
#define HASHSIZE 1024
35
 
static t_pdp_symbol *pdp_symhash[HASHSIZE];
36
 
 
37
 
 
38
 
#define LOCK pthread_mutex_lock(&pdp_hash_mutex)
39
 
#define UNLOCK pthread_mutex_unlock(&pdp_hash_mutex)
40
 
 
41
 
 
42
 
static void _pdp_symbol_init(t_pdp_symbol *s)
43
 
{
44
 
    memset(s, 0, sizeof(*s));
45
 
    s->s_forth.t = a_undef;
46
 
}
47
 
 
48
 
 
49
 
/* shamelessly copied from pd src and made thread safe */
50
 
t_pdp_symbol *_pdp_dogensym(char *s, t_pdp_symbol *oldsym)
51
 
{
52
 
    t_pdp_symbol **sym1, *sym2;
53
 
    unsigned int hash1 = 0,  hash2 = 0;
54
 
    int length = 0;
55
 
    char *s2 = s;
56
 
    while (*s2)
57
 
    {
58
 
        hash1 += *s2;
59
 
        hash2 += hash1;
60
 
        length++;
61
 
        s2++;
62
 
    }
63
 
    sym1 = pdp_symhash + (hash2 & (HASHSIZE-1));
64
 
 
65
 
    /* lock hash */
66
 
    LOCK;
67
 
 
68
 
    while (sym2 = *sym1)
69
 
    {
70
 
        if (!strcmp(sym2->s_name, s)) goto gotit;
71
 
        sym1 = &sym2->s_next;
72
 
    }
73
 
    if (oldsym){
74
 
        sym2 = oldsym;
75
 
    }
76
 
    else
77
 
    {
78
 
        sym2 = (t_pdp_symbol *)pdp_alloc(sizeof(*sym2));
79
 
        _pdp_symbol_init(sym2);
80
 
        sym2->s_name = pdp_alloc(length+1);
81
 
        sym2->s_next = 0;
82
 
        strcpy(sym2->s_name, s);
83
 
    }
84
 
    *sym1 = sym2;
85
 
 
86
 
 gotit:
87
 
 
88
 
    /* unlock hash */
89
 
    UNLOCK;
90
 
    return (sym2);
91
 
}
92
 
 
93
 
t_pdp_symbol *pdp_gensym(char *s)
94
 
{
95
 
    return(_pdp_dogensym(s, 0));
96
 
}
97
 
 
98
 
 
99
 
/* connect a parsed typelist to a symbol type name
100
 
   1 = succes, 0 = error (symbol already connected) */
101
 
int pdp_symbol_set_typelist(t_pdp_symbol *s, t_pdp_list *typelist)
102
 
{
103
 
    int status = 0;
104
 
    LOCK;
105
 
    if (!s->s_type){
106
 
        s->s_type = typelist;
107
 
        status = 1;
108
 
    }
109
 
    UNLOCK;
110
 
    return status;
111
 
}
112
 
 
113
 
 
114
 
void pdp_symbol_apply_all(t_pdp_symbol_iterator it)
115
 
{
116
 
    int i;
117
 
    for (i=0; i<HASHSIZE; i++){
118
 
        t_pdp_symbol *s;
119
 
        for (s = pdp_symhash[i]; s; s=s->s_next){
120
 
            it(s);
121
 
        }
122
 
        
123
 
    }
124
 
}
125
 
 
126
 
t_pdp_symbol _pdp_sym_wildcard;
127
 
t_pdp_symbol _pdp_sym_float;
128
 
t_pdp_symbol _pdp_sym_int;
129
 
t_pdp_symbol _pdp_sym_symbol;
130
 
t_pdp_symbol _pdp_sym_packet;
131
 
t_pdp_symbol _pdp_sym_pointer;
132
 
t_pdp_symbol _pdp_sym_invalid;
133
 
t_pdp_symbol _pdp_sym_list;
134
 
t_pdp_symbol _pdp_sym_question_mark;
135
 
t_pdp_symbol _pdp_sym_atom;
136
 
t_pdp_symbol _pdp_sym_null;
137
 
t_pdp_symbol _pdp_sym_quote_start;
138
 
t_pdp_symbol _pdp_sym_quote_end;
139
 
t_pdp_symbol _pdp_sym_return;
140
 
t_pdp_symbol _pdp_sym_nreturn;
141
 
t_pdp_symbol _pdp_sym_defstart;
142
 
t_pdp_symbol _pdp_sym_defend;
143
 
t_pdp_symbol _pdp_sym_if;
144
 
t_pdp_symbol _pdp_sym_then;
145
 
t_pdp_symbol _pdp_sym_local;
146
 
t_pdp_symbol _pdp_sym_forth;
147
 
t_pdp_symbol _pdp_sym_call;
148
 
t_pdp_symbol _pdp_sym_push;
149
 
t_pdp_symbol _pdp_sym_pop;
150
 
 
151
 
static void _sym(char *name, t_pdp_symbol *s)
152
 
{
153
 
    _pdp_symbol_init(s);
154
 
    s->s_name = name;
155
 
    PDP_ASSERT(_pdp_dogensym(name, s) == s);
156
 
    
157
 
}
158
 
 
159
 
void pdp_symbol_setup(void)
160
 
{
161
 
    // create mutexes
162
 
    pthread_mutex_init(&pdp_hash_mutex, NULL);
163
 
 
164
 
    // init symbol hash
165
 
    memset(pdp_symhash, 0, HASHSIZE * sizeof(t_pdp_symbol *));
166
 
 
167
 
    // setup predefined symbols
168
 
    _sym("*",        &_pdp_sym_wildcard);
169
 
    _sym("float",    &_pdp_sym_float);
170
 
    _sym("int",      &_pdp_sym_int);
171
 
    _sym("symbol",   &_pdp_sym_symbol);
172
 
    _sym("packet",   &_pdp_sym_packet);
173
 
    _sym("pointer",  &_pdp_sym_pointer);
174
 
    _sym("invalid",  &_pdp_sym_invalid);
175
 
    _sym("list",     &_pdp_sym_list);
176
 
    _sym("?",        &_pdp_sym_question_mark);
177
 
    _sym("atom",     &_pdp_sym_atom);
178
 
    _sym("null",     &_pdp_sym_null);
179
 
    _sym("[",        &_pdp_sym_quote_start);
180
 
    _sym("]",        &_pdp_sym_quote_end);
181
 
    _sym("ret",      &_pdp_sym_return);
182
 
    _sym("nret",     &_pdp_sym_nreturn);
183
 
    _sym(":",        &_pdp_sym_defstart);
184
 
    _sym(";",        &_pdp_sym_defend);
185
 
    _sym("if",       &_pdp_sym_if);
186
 
    _sym("then",     &_pdp_sym_then);
187
 
    _sym("local",    &_pdp_sym_local);
188
 
    _sym("forth",    &_pdp_sym_forth);
189
 
    _sym("call",     &_pdp_sym_call);
190
 
    _sym("push",     &_pdp_sym_push);
191
 
    _sym("pop",      &_pdp_sym_pop);
192
 
 
193
 
}
194
 
 
195