aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/edje/src/bin/epp/cpphash.c
blob: e3b68e0c5c9f10b36175e92547583d23150692ae (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
/* Part of CPP library.  (Macro hash table support.)
 * Copyright (C) 1986, 87, 89, 92, 93, 94, 1995 Free Software Foundation, Inc.
 * Written by Per Bothner, 1994.
 * Based on CCCP program by by Paul Rubin, June 1986
 * Adapted to ANSI C, Richard Stallman, Jan 1987
 * Copyright (C) 2003-2011 Kim Woelders
 * 
 * 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, 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, 675 Mass Ave, Cambridge, MA 02139, USA.
 * 
 * In other words, you are welcome to use, share and improve this program.
 * You are forbidden to forbid anyone else to use, share and improve
 * what you give them.   Help stamp out software-hoarding!  */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <string.h>
#include <stdlib.h>

#include "cpplib.h"
#include "cpphash.h"

static HASHNODE    *hashtab[HASHSIZE];

#define IS_IDCHAR(ch) is_idchar[(unsigned char)(ch)]

/*
 * return hash function on name.  must be compatible with the one
 * computed a step at a time, elsewhere
 */
int
hashf(const char *name, int len, int hashsize)
{
   int                 r = 0;

   while (len--)
      r = HASHSTEP(r, *name++);

   return MAKE_POS(r) % hashsize;
}

/*
 * find the most recent hash node for name name (ending with first
 * non-identifier char) installed by install
 *
 * If LEN is >= 0, it is the length of the name.
 * Otherwise, compute the length by scanning the entire name.
 *
 * If HASH is >= 0, it is the precomputed hash code.
 * Otherwise, compute the hash code.
 */
HASHNODE           *
cpp_lookup(const char *name, int len, int hash)
{
   const char         *bp;
   HASHNODE           *bucket;

   if (len < 0)
     {
	for (bp = name; IS_IDCHAR(*bp); bp++)
	   ;
	len = bp - name;
     }
   if (hash < 0)
      hash = hashf(name, len, HASHSIZE);

   bucket = hashtab[hash];
   while (bucket)
     {
	if (bucket->length == len
	    && strncmp((const char *)bucket->name, name, len) == 0)
	   return bucket;
	bucket = bucket->next;
     }
   return (HASHNODE *) 0;
}

/*
 * Delete a hash node.  Some weirdness to free junk from macros.
 * More such weirdness will have to be added if you define more hash
 * types that need it.
 */

/* Note that the DEFINITION of a macro is removed from the hash table
 * but its storage is not freed.  This would be a storage leak
 * except that it is not reasonable to keep undefining and redefining
 * large numbers of macros many times.
 * In any case, this is necessary, because a macro can be #undef'd
 * in the middle of reading the arguments to a call to it.
 * If #undef freed the DEFINITION, that would crash.  */

void
delete_macro(HASHNODE * hp)
{

   if (hp->prev)
      hp->prev->next = hp->next;
   if (hp->next)
      hp->next->prev = hp->prev;

   /* make sure that the bucket chain header that
    * the deleted guy was on points to the right thing afterwards. */
   if (hp == *hp->bucket_hdr)
      *hp->bucket_hdr = hp->next;

   if (hp->type == T_MACRO)
     {
	DEFINITION         *d = hp->value.defn;
	struct reflist     *ap, *nextap;

	for (ap = d->pattern; ap; ap = nextap)
	  {
	     nextap = ap->next;
	     free(ap);
	  }
	if (d->nargs >= 0)
	   free(d->args.argnames);
	free(d);
     }
   free(hp);
}
/*
 * install a name in the main hash table, even if it is already there.
 *   name stops with first non alphanumeric, except leading '#'.
 * caller must check against redefinition if that is desired.
 * delete_macro () removes things installed by install () in fifo order.
 * this is important because of the `defined' special symbol used
 * in #if, and also if pushdef/popdef directives are ever implemented.
 *
 * If LEN is >= 0, it is the length of the name.
 * Otherwise, compute the length by scanning the entire name.
 *
 * If HASH is >= 0, it is the precomputed hash code.
 * Otherwise, compute the hash code.
 */
HASHNODE           *
install(const char *name, int len, enum node_type type, int ivalue, char *value,
	int hash)
{
   HASHNODE           *hp;
   int                 i, bucket;
   const char         *p;

   if (len < 0)
     {
	p = name;
	while (IS_IDCHAR(*p))
	   p++;
	len = p - name;
     }
   if (hash < 0)
      hash = hashf(name, len, HASHSIZE);

   i = sizeof(HASHNODE) + len + 1;
   hp = (HASHNODE *) xmalloc(i);
   bucket = hash;
   hp->bucket_hdr = &hashtab[bucket];
   hp->next = hashtab[bucket];
   hashtab[bucket] = hp;
   hp->prev = NULL;
   if (hp->next)
      hp->next->prev = hp;
   hp->type = type;
   hp->length = len;
   if (hp->type == T_CONST)
      hp->value.ival = ivalue;
   else
      hp->value.cpval = value;
   hp->name = ((char *)hp) + sizeof(HASHNODE);
   memcpy(hp->name, name, len);
   hp->name[len] = 0;
   return hp;
}

void
cpp_hash_cleanup(cpp_reader * pfile __UNUSED__)
{
   int                 i;

   for (i = HASHSIZE; --i >= 0;)
     {
	while (hashtab[i])
	   delete_macro(hashtab[i]);
     }
}