3 * $Id: sym.c,v 1.7 1999/06/01 09:49:08 mdw Exp $
5 * Symbol table management
7 * (c) 1998 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
14 * mLib is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * mLib is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.7 1999/06/01 09:49:08 mdw
34 * Allow things to be looked up by just their caller-supplied hashes. This
35 * actually needs to be thought through better.
37 * Revision 1.6 1999/05/26 21:08:31 mdw
38 * Rename symbols in line with newer conventions.
40 * Revision 1.5 1999/05/13 22:48:37 mdw
41 * Twiddle the extension threshold. Change `-ise' to `-ize' throughout.
43 * Revision 1.4 1999/05/06 19:51:35 mdw
44 * Reformatted the LGPL notice a little bit.
46 * Revision 1.3 1999/05/05 18:50:31 mdw
47 * Change licensing conditions to LGPL.
49 * Revision 1.2 1998/11/26 19:27:33 mdw
50 * Move SYM_NAME into the header file. Fix bugs.
52 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
53 * Initial version of mLib
57 /*----- Header files ------------------------------------------------------*/
59 /* --- ANSI headers --- */
65 /* --- Local headers --- */
75 /*----- Tuning parameters -------------------------------------------------*/
77 /* --- Initial hash table size --- *
79 * This is the initial @mask@ value. It must be of the form %$2^n - 1$%,
80 * so that it can be used to mask of the bottom bits of a hash value.
83 #define SYM_INITSZ 255 /* Size of a new hash table */
85 /* --- Maximum load factor --- *
87 * This parameter controls how much the table has to be loaded before the
88 * table is extended. The number of elements %$n$%, the number of bins %$b$%
89 * and the limit %$l$% satisfy the relation %$n < bl$%; if a new item is
90 * added to the table and this relation is found to be false, the table is
94 #define SYM_LIMIT(n) ((n) * 4) /* Load factor for growing table */
96 /*----- Main code ---------------------------------------------------------*/
98 /* --- @sym_create@ --- *
100 * Arguments: @sym_table *t@ = symbol table to initialize
104 * Use: Initializes the given symbol table. Raises @EXC_NOMEM@ if
105 * there isn't enough memory.
108 void sym_create(sym_table
*t
)
112 TRACK_CTX("symbol table creation");
115 t
->mask
= SYM_INITSZ
;
116 t
->c
= SYM_LIMIT(SYM_INITSZ
);
117 t
->a
= xmalloc((t
->mask
+ 1) * sizeof(sym_base
*));
119 for (i
= 0; i
< SYM_INITSZ
+ 1; i
++)
125 /* --- @sym_destroy@ --- *
127 * Arguments: @sym_table *t@ = pointer to symbol table in question
131 * Use: Destroys a symbol table, freeing all the memory it used to
135 void sym_destroy(sym_table
*t
)
140 TRACK_CTX("symbol table deletion");
143 for (i
= 0; i
<= t
->mask
; i
++) {
147 if (p
->len
> SYM_BUFSZ
)
148 sub_free(p
->name
.p
, p
->len
);
158 /* --- @sym_find@ --- *
160 * Arguments: @sym_table *t@ = pointer to symbol table in question
161 * @const char *n@ = pointer to symbol table to look up
162 * @long l@ = length of the name string or negative to measure
163 * @size_t sz@ = size of desired symbol object, or zero
164 * @unsigned *f@ = pointer to a flag, or null.
166 * Returns: The address of a @sym_base@ structure, or null if not found
169 * Use: Looks up a symbol in a given symbol table. The name is
170 * passed by the address of its first character. The length
171 * may be given, in which case the name may contain arbitrary
172 * binary data, or it may be given as a negative number, in
173 * which case the length of the name is calculated as
174 * @strlen(n) + 1@. The name pointer @n@ may also be zero; in
175 * this case, @l@ is taken to be a raw hash, and any element
176 * with a matching hash is taken to be the one wanted.
178 * The return value is the address of a pointer to a @sym_base@
179 * block (which may have other things on the end, as above). If
180 * the symbol could be found, the return value points to the
181 * symbol block. If the symbol wasn't there, then if @sz@ is
182 * nonzero, a new symbol is created and its address is returned;
183 * otherwise a null pointer is returned. The exception
184 * @EXC_NOMEM@ is raised if the block can't be allocated.
186 * The value of @*f@ indicates whether a new symbol entry was
187 * created: a nonzero value indicates that an old value was
191 void *sym_find(sym_table
*t
, const char *n
, long l
, size_t sz
, unsigned *f
)
198 /* --- Find the correct bin --- */
201 len
= l
< 0 ?
strlen(n
) + 1 : l
;
202 CRC32(hash
, 0, n
, len
);
205 bin
= p
= (sym_base
*)(t
->a
+ (hash
& t
->mask
));
207 /* --- Search the bin list --- */
210 if (hash
== p
->next
->hash
&&
211 (n
== 0 || (len
== p
->next
->len
&&
212 !memcmp(n
, SYM_NAME(p
->next
), len
))))
214 /* --- Found a match --- *
216 * As a minor, and probably pointless, tweak, move the item to the
217 * front of its bin list.
225 /* --- Return the block --- */
234 /* --- Couldn't find the item there --- */
239 /* --- Create a new symbol block and initialize it --- */
242 TRACK_CTX("new symbol creation");
250 if (len
<= SYM_BUFSZ
)
251 memcpy(p
->name
.b
, n
, len
);
254 p
->name
.p
= sub_alloc(len
);
255 memcpy(p
->name
.p
, n
, len
);
269 /* --- Consider growing the array --- */
274 uint32 m
= t
->mask
+ 1;
278 TRACK_CTX("symbol table extension");
281 /* --- Update values in the anchor block --- */
284 t
->a
= xrealloc(t
->a
, (t
->mask
+ 1) * 2 * sizeof(sym_base
*));
285 } CATCH
switch (exc_type
) {
294 t
->c
= SYM_LIMIT(t
->mask
+ 1);
295 t
->mask
= (t
->mask
+ 1) * 2 - 1;
297 /* --- Now wander through the table rehashing things --- *
299 * This loop is very careful to avoid problems with aliasing. The items
300 * are dealt with from the end backwards to avoid overwriting bins before
301 * they've been processed.
304 lim
= (t
->mask
+ 1) >> 1;
305 for (i
= 0; i
< lim
; i
++) {
307 /* --- Some initialization --- */
310 p
= (sym_base
*)(t
->a
+ i
);
311 q
= (sym_base
*)(t
->a
+ i
+ lim
);
313 /* --- Now go through the @r@ list --- */
322 p
->next
= q
->next
= 0;
328 /* --- Finished that, so return the new symbol block --- */
333 /* --- @sym_remove@ --- *
335 * Arguments: @sym_table *i@ = pointer to a symbol table object
336 * @void *b@ = pointer to symbol table entry
340 * Use: Removes the object from the symbol table. The space occupied
341 * by the object and its name is freed; anything else attached
342 * to the entry should already be gone by this point.
345 void sym_remove(sym_table
*t
, void *b
)
347 /* --- A quick comment --- *
349 * Since the @sym_base@ block contains the hash, finding the element in the
350 * bin list is really quick -- it's not worth bothering with things like
351 * doubly linked lists.
355 sym_base
*bin
= (sym_base
*)(t
->a
+ (p
->hash
& t
->mask
));
357 /* --- Find the item in the bin list --- */
367 /* --- Now just remove the item from the list and free it --- *
369 * Oh, and bump the load counter.
373 if (p
->len
> SYM_BUFSZ
)
374 sub_free(p
->name
.p
, p
->len
);
379 /* --- @sym_mkiter@ --- *
381 * Arguments: @sym_iter *i@ = pointer to an iterator object
382 * @sym_table *t@ = pointer to a symbol table object
386 * Use: Creates a new symbol table iterator which may be used to
387 * iterate through a symbol table.
390 void sym_mkiter(sym_iter
*i
, sym_table
*t
)
397 /* --- @sym_next@ --- *
399 * Arguments: @sym_iter *i@ = pointer to iterator object
401 * Returns: Pointer to the next symbol found, or null when finished.
403 * Use: Returns the next symbol from the table. Symbols are not
404 * returned in any particular order.
407 void *sym_next(sym_iter
*i
)
411 /* --- Find the next item --- */
414 if (i
->i
> i
->t
->mask
)
416 i
->n
= i
->t
->a
[i
->i
++];
419 /* --- Update the iterator block --- */
429 /*----- Symbol table test code --------------------------------------------*/
436 typedef struct sym_word
{
442 /* --- What it does --- *
444 * Reads the file /usr/dict/words (change to some other file full of
445 * interesting and appropriate bits of text to taste) into a big buffer and
446 * picks apart into lines. Then picks lines at random and enters them into
452 char *buff
, *p
, *lim
;
461 /* --- Initialize for reading the file --- */
464 buff
= xmalloc(sz
+ 1);
468 if ((fp
= fopen("/usr/dict/words", "r")) == 0)
469 fprintf(stderr
, "buggered ;-( (%s)\n", strerror(errno
));
471 /* --- Read buffers of text --- *
473 * Read a buffer. If more to come, double the buffer size and try again.
474 * This is the method I recommended to comp.lang.c, so I may as well try
479 i
= fread(buff
+ done
, 1, sz
- done
, fp
);
484 buff
= xrealloc(buff
, sz
+ 1);
487 /* --- Count the lines --- */
492 for (p
= buff
; p
< lim
; p
++)
493 if (*p
== '\n') sz
++;
495 /* --- Build a table of line starts --- */
497 line
= xmalloc(sz
* sizeof(char *));
500 for (p
= buff
; p
< lim
; p
++)
501 if (*p
== '\n') *p
= 0, line
[i
++] = p
+ 1;
504 /* --- Build a table of lines --- *
506 * This reverses the mapping which the symbol table performs, so that its
507 * accuracy can be tested.
510 flag
= xmalloc(sz
* sizeof(sym_word
*));
511 for (i
= 0; i
< sz
; i
++)
518 i
= (unsigned)rand() % sz
;
525 printf("find `%s'\n", line
[i
]);
526 if ((rand() & 1023) == 0) {
527 putchar('.'); fflush(stdout
);
530 w
= sym_find(&tbl
, line
[i
], -1, 0, 0);
532 printf("*** error: find `%s' gave %p not %p\n",
533 line
[i
], (void *)w
, (void *)flag
[i
]);
534 else if (w
&& w
->i
!= i
)
535 printf("*** error: find sym for `%s' gives index %i not %i\n",
543 printf("create `%s'\n", line
[i
]);
544 if ((rand() & 1023) == 0) {
545 putchar('+'); fflush(stdout
);
548 w
= sym_find(&tbl
, line
[i
], -1, sizeof(sym_word
), &f
);
552 printf("*** error: create `%s' gave %p not %p\n",
553 line
[i
], (void *)w
, (void *)flag
[i
]);
554 else if (w
&& w
->i
!= i
)
555 printf("*** error: create sym for `%s' gives index %i not %i\n",
561 printf("*** error: create `%s' gave new block, should be %p\n",
562 line
[i
], (void *)flag
[i
]);
578 v
= (rand() % entries
) == 0;
581 printf("\niterated %i entries\n", entries
);
586 ntbl
= xmalloc(sz
* sizeof(sym_word
*));
587 memcpy(ntbl
, flag
, sz
* sizeof(sym_word
*));
588 sym_mkiter(&it
, &tbl
);
590 while ((w
= sym_next(&it
)) != 0) {
592 printf("*** error: iterate returned duff item %i\n", w
->i
);
597 for (i
= 0; i
< sz
; i
++)
598 if (ntbl
[i
]) printf("*** error: iterate didn't return item %i\n",
605 int v
= rand() & 255 ?
0 : 1;
610 for (i
= 0; i
<= tbl
.mask
; i
++) {
611 if (!tbl
.a
[i
]) continue;
612 if (v
) printf(" %i: ", i
);
615 if ((b
->hash
& tbl
.mask
) != i
)
616 printf("*** error: bad hash value found");
617 if (v
) printf("`%s'(%08lx:%lu) ",
618 line
[((sym_word
*)b
)->i
],
623 if (v
) putchar('\n');
629 printf("remove `%s'\n", SYM_NAME(&flag
[i
]->base
));
630 if ((rand() & 1023) == 0) {
631 putchar('-'); fflush(stdout
);
633 sym_remove(&tbl
, flag
[i
]);
647 /*----- That's all, folks -------------------------------------------------*/