1 /* hash.c - hash table lookup strings -
2 Copyright (C) 1987, 1990, 1991, 1992 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 * BUGS, GRIPES, APOLOGIA etc.
23 * A typical user doesn't need ALL this: I intend to make a library out
24 * of it one day - Dean Elsner.
25 * Also, I want to change the definition of a symbol to (address,length)
26 * so I can put arbitrary binary in the names stored. [see hsh.c for that]
28 * This slime is common coupled inside the module. Com-coupling (and other
29 * vandalism) was done to speed running time. The interfaces at the
30 * module's edges are adequately clean.
32 * There is no way to (a) run a test script through this heap and (b)
33 * compare results with previous scripts, to see if we have broken any
34 * code. Use GNU (f)utilities to do this. A few commands assist test.
35 * The testing is awkward: it tries to be both batch & interactive.
36 * For now, interactive rules!
40 * The idea is to implement a symbol table. A test jig is here.
41 * Symbols are arbitrary strings; they can't contain '\0'.
42 * [See hsh.c for a more general symbol flavour.]
43 * Each symbol is associated with a char*, which can point to anything
44 * you want, allowing an arbitrary property list for each symbol.
46 * The basic operations are:
48 * new creates symbol table, returns handle
49 * find (symbol) returns char*
50 * insert (symbol,char*) error if symbol already in table
51 * delete (symbol) returns char* if symbol was in table
52 * apply so you can delete all symbols before die()
53 * die destroy symbol table (free up memory)
55 * Supplementary functions include:
57 * say how big? what % full?
58 * replace (symbol,newval) report previous value
59 * jam (symbol,value) assert symbol:=value
61 * You, the caller, have control over errors: this just reports them.
63 * This package requires malloc(), free().
64 * Malloc(size) returns NULL or address of char[size].
65 * Free(address) frees same.
69 * The code and its structures are re-enterent.
70 * Before you do anything else, you must call hash_new() which will
71 * return the address of a hash-table-control-block (or NULL if there
72 * is not enough memory). You then use this address as a handle of the
73 * symbol table by passing it to all the other hash_...() functions.
74 * The only approved way to recover the memory used by the symbol table
75 * is to call hash_die() with the handle of the symbol table.
77 * Before you call hash_die() you normally delete anything pointed to
78 * by individual symbols. After hash_die() you can't use that symbol
81 * The char* you associate with a symbol may not be NULL (0) because
82 * NULL is returned whenever a symbol is not in the table. Any other
83 * value is OK, except DELETED, #defined below.
85 * When you supply a symbol string for insertion, YOU MUST PRESERVE THE
86 * STRING until that symbol is deleted from the table. The reason is that
87 * only the address you supply, NOT the symbol string itself, is stored
88 * in the symbol table.
90 * You may delete and add symbols arbitrarily.
91 * Any or all symbols may have the same 'value' (char *). In fact, these
92 * routines don't do anything with your symbol values.
94 * You have no right to know where the symbol:char* mapping is stored,
95 * because it moves around in memory; also because we may change how it
96 * works and we don't want to break your code do we? However the handle
97 * (address of struct hash_control) is never changed in
98 * the life of the symbol table.
100 * What you CAN find out about a symbol table is:
101 * how many slots are in the hash table?
102 * how many slots are filled with symbols?
103 * (total hashes,collisions) for (reads,writes) (*)
104 * All of the above values vary in time.
105 * (*) some of these numbers will not be meaningful if we change the
112 * Hash table is an array of hash_entries; each entry is a pointer to a
113 * a string and a user-supplied value 1 char* wide.
115 * The array always has 2 ** n elements, n>0, n integer.
116 * There is also a 'wall' entry after the array, which is always empty
117 * and acts as a sentinel to stop running off the end of the array.
118 * When the array gets too full, we create a new array twice as large
119 * and re-hash the symbols into the new array, then forget the old array.
120 * (Of course, we copy the values into the new array before we junk the
126 static char rcsid[] = "$Id$";
133 #define TRUE (!FALSE)
134 #endif /* no FALSE yet */
137 #define min(a, b) ((a) < (b) ? (a) : (b))
141 #define error as_fatal
143 #define DELETED ((char *)1) /* guarenteed invalid address */
144 #define START_POWER (11) /* power of two: size of new hash table *//* JF was 6 */
145 /* JF These next two aren't used any more. */
146 /* #define START_SIZE (64) / * 2 ** START_POWER */
147 /* #define START_FULL (32) / * number of entries before table expands */
148 #define islive(ptr) (ptr->hash_string && ptr->hash_string != DELETED)
149 /* above TRUE if a symbol is in entry @ ptr */
151 #define STAT_SIZE (0) /* number of slots in hash table */
152 /* the wall does not count here */
153 /* we expect this is always a power of 2 */
154 #define STAT_ACCESS (1) /* number of hash_ask()s */
155 #define STAT__READ (0) /* reading */
156 #define STAT__WRITE (1) /* writing */
157 #define STAT_COLLIDE (3) /* number of collisions (total) */
158 /* this may exceed STAT_ACCESS if we have */
159 /* lots of collisions/access */
160 #define STAT_USED (5) /* slots used right now */
161 #define STATLENGTH (6) /* size of statistics block */
162 #if STATLENGTH != HASH_STATLENGTH
163 Panic! Please make #include "stat.h" agree with previous definitions!
166 /* #define SUSPECT to do runtime checks */
167 /* #define TEST to be a test jig for hash...() */
169 #ifdef TEST /* TEST: use smaller hash table */
171 #define START_POWER (3)
173 #define START_SIZE (8)
175 #define START_FULL (4)
178 /*------------------ plan ---------------------------------- i = internal
180 struct hash_control * c;
181 struct hash_entry * e; i
182 int b[z]; buffer for statistics
184 char * s; symbol string (address) [ key ]
185 char * v; value string (address) [datum]
186 boolean f; TRUE if we found s in hash table i
187 char * t; error string; "" means OK
188 int a; access type [0...n) i
190 c=hash_new () create new hash_control
192 hash_die (c) destroy hash_control (and hash table)
193 table should be empty.
194 doesn't check if table is empty.
195 c has no meaning after this.
197 hash_say (c,b,z) report statistics of hash_control.
198 also report number of available statistics.
200 v=hash_delete (c,s) delete symbol, return old value if any.
201 ask() NULL means no old value.
204 v=hash_replace (c,s,v) replace old value of s with v.
205 ask() NULL means no old value: no table change.
208 t=hash_insert (c,s,v) insert (s,v) in c.
209 ask() return error string.
210 f it is an error to insert if s is already
212 if any error, c is unchanged.
214 t=hash_jam (c,s,v) assert that new value of s will be v. i
215 ask() it may decide to GROW the table. i
218 t=hash_grow (c) grow the hash table. i
219 jam() will invoke JAM. i
221 ?=hash_apply (c,y) apply y() to every symbol in c.
222 y evtries visited in 'unspecified' order.
224 v=hash_find (c,s) return value of s, or NULL if s not in c.
228 f,e=hash_ask() (c,s,a) return slot where s SHOULD live. i
229 code() maintain collision stats in c. i
231 .=hash_code (c,s) compute hash-code for s, i
232 from parameters of c. i
236 static char hash_found; /* returned by hash_ask() to stop extra */
237 /* testing. hash_ask() wants to return both */
238 /* a slot and a status. This is the status. */
239 /* TRUE: found symbol */
240 /* FALSE: absent: empty or deleted slot */
241 /* Also returned by hash_jam(). */
242 /* TRUE: we replaced a value */
243 /* FALSE: we inserted a value */
245 static struct hash_entry * hash_ask();
246 static int hash_code ();
247 static char * hash_grow();
250 * h a s h _ n e w ( )
253 struct hash_control *
254 hash_new() /* create a new hash table */
255 /* return NULL if failed */
256 /* return handle (address of struct hash) */
258 register struct hash_control * retval;
259 register struct hash_entry * room; /* points to hash table */
260 register struct hash_entry * wall;
261 register struct hash_entry * entry;
262 register int * ip; /* scan stats block of struct hash_control */
263 register int * nd; /* limit of stats block */
265 if (( room = (struct hash_entry *) malloc( sizeof(struct
266 hash_entry)*((1<<START_POWER) + 1) ) ) != NULL)
267 /* +1 for the wall entry */
269 if (( retval = (struct hash_control *) malloc(sizeof(struct
270 hash_control)) ) != NULL)
272 nd = retval->hash_stat + STATLENGTH;
273 for (ip=retval->hash_stat; ip<nd; ip++)
278 retval->hash_stat[STAT_SIZE] = 1<<START_POWER;
279 retval->hash_mask = (1<<START_POWER) - 1;
280 retval->hash_sizelog = START_POWER;
281 /* works for 1's compl ok */
282 retval->hash_where = room;
284 wall = room + (1<<START_POWER);
285 retval->hash_full = (1<<START_POWER)/2;
286 for (entry=room; entry <= wall; entry++)
288 entry->hash_string = NULL;
294 retval = NULL; /* no room for table: fake a failure */
296 return(retval); /* return NULL or set-up structs */
300 * h a s h _ d i e ( )
302 * Table should be empty, but this is not checked.
303 * To empty the table, try hash_apply()ing a symbol deleter.
304 * Return to free memory both the hash table and it's control
306 * 'handle' has no meaning after this function.
307 * No errors are recoverable.
311 struct hash_control * handle;
313 free((char *)handle->hash_where);
314 free((char *)handle);
318 * h a s h _ s a y ( )
320 * Return the size of the statistics table, and as many statistics as
321 * we can until either (a) we have run out of statistics or (b) caller
322 * has run out of buffer.
323 * NOTE: hash_say treats all statistics alike.
324 * These numbers may change with time, due to insertions, deletions
325 * and expansions of the table.
326 * The first "statistic" returned is the length of hash_stat[].
327 * Then contents of hash_stat[] are read out (in ascending order)
328 * until your buffer or hash_stat[] is exausted.
331 hash_say(handle,buffer,bufsiz)
332 register struct hash_control * handle;
333 register int buffer[/*bufsiz*/];
336 register int * nd; /* limit of statistics block */
337 register int * ip; /* scan statistics */
339 ip = handle->hash_stat;
340 nd = ip + min(bufsiz-1,STATLENGTH);
341 if (bufsiz>0) /* trust nothing! bufsiz <= 0 is dangerous */
343 *buffer++ = STATLENGTH;
344 for (; ip<nd; ip++,buffer++)
352 * h a s h _ d e l e t e ( )
354 * Try to delete a symbol from the table.
355 * If it was there, return its value (and adjust STAT_USED).
356 * Otherwise, return NULL.
357 * Anyway, the symbol is not present after this function.
360 char * /* NULL if string not in table, else */
361 /* returns value of deleted symbol */
362 hash_delete(handle,string)
363 register struct hash_control * handle;
364 register char * string;
366 register char * retval; /* NULL if string not in table */
367 register struct hash_entry * entry; /* NULL or entry of this symbol */
369 entry = hash_ask(handle,string,STAT__WRITE);
372 retval = entry->hash_value;
373 entry->hash_string = DELETED; /* mark as deleted */
374 handle->hash_stat[STAT_USED] -= 1; /* slots-in-use count */
376 if (handle->hash_stat[STAT_USED]<0)
378 error("hash_delete");
380 #endif /* def SUSPECT */
390 * h a s h _ r e p l a c e ( )
392 * Try to replace the old value of a symbol with a new value.
393 * Normally return the old value.
394 * Return NULL and don't change the table if the symbol is not already
398 hash_replace(handle,string,value)
399 register struct hash_control * handle;
400 register char * string;
401 register char * value;
403 register struct hash_entry * entry;
404 register char * retval;
406 entry = hash_ask(handle,string,STAT__WRITE);
409 retval = entry->hash_value;
410 entry->hash_value = value;
421 * h a s h _ i n s e r t ( )
423 * Insert a (symbol-string, value) into the hash table.
424 * Return an error string, "" means OK.
425 * It is an 'error' to insert an existing symbol.
428 char * /* return error string */
429 hash_insert(handle,string,value)
430 register struct hash_control * handle;
431 register char * string;
432 register char * value;
434 register struct hash_entry * entry;
435 register char * retval;
438 if (handle->hash_stat[STAT_USED] > handle->hash_full)
440 retval = hash_grow(handle);
444 entry = hash_ask(handle,string,STAT__WRITE);
451 entry->hash_value = value;
452 entry->hash_string = string;
453 handle->hash_stat[STAT_USED] += 1;
460 * h a s h _ j a m ( )
462 * Regardless of what was in the symbol table before, after hash_jam()
463 * the named symbol has the given value. The symbol is either inserted or
464 * (its value is) relpaced.
465 * An error message string is returned, "" means OK.
467 * WARNING: this may decide to grow the hashed symbol table.
468 * To do this, we call hash_grow(), WHICH WILL recursively CALL US.
470 * We report status internally: hash_found is TRUE if we replaced, but
471 * false if we inserted.
474 hash_jam(handle,string,value)
475 register struct hash_control * handle;
476 register char * string;
477 register char * value;
479 register char * retval;
480 register struct hash_entry * entry;
483 if (handle->hash_stat[STAT_USED] > handle->hash_full)
485 retval = hash_grow(handle);
489 entry = hash_ask(handle,string,STAT__WRITE);
492 entry->hash_string = string;
493 handle->hash_stat[STAT_USED] += 1;
495 entry->hash_value = value;
501 * h a s h _ g r o w ( )
503 * Grow a new (bigger) hash table from the old one.
504 * We choose to double the hash table's size.
505 * Return a human-scrutible error string: "" if OK.
506 * Warning! This uses hash_jam(), which had better not recurse
507 * back here! Hash_jam() conditionally calls us, but we ALWAYS
512 hash_grow(handle) /* make a hash table grow */
513 struct hash_control * handle;
515 register struct hash_entry * newwall;
516 register struct hash_entry * newwhere;
517 struct hash_entry * newtrack;
518 register struct hash_entry * oldtrack;
519 register struct hash_entry * oldwhere;
520 register struct hash_entry * oldwall;
530 * capture info about old hash table
532 oldwhere = handle->hash_where;
533 oldwall = handle->hash_wall;
535 oldused = handle->hash_stat[STAT_USED];
538 * attempt to get enough room for a hash table twice as big
540 temp = handle->hash_stat[STAT_SIZE];
541 if (( newwhere = (struct hash_entry *)
542 xmalloc((long)((temp+temp+1)*sizeof(struct hash_entry)))) != NULL)
543 /* +1 for wall slot */
545 retval = ""; /* assume success until proven otherwise */
547 * have enough room: now we do all the work.
548 * double the size of everything in handle,
549 * note: hash_mask frob works for 1's & for 2's complement machines
551 handle->hash_mask = handle->hash_mask + handle->hash_mask + 1;
552 handle->hash_stat[STAT_SIZE] <<= 1;
553 newsize = handle->hash_stat[STAT_SIZE];
554 handle->hash_where = newwhere;
555 handle->hash_full <<= 1;
556 handle->hash_sizelog += 1;
557 handle->hash_stat[STAT_USED] = 0;
559 newwall = newwhere + newsize;
561 * set all those pesky new slots to vacant.
563 for (newtrack=newwhere; newtrack <= newwall; newtrack++)
565 newtrack->hash_string = NULL;
568 * we will do a scan of the old table, the hard way, using the
569 * new control block to re-insert the data into new hash table.
571 handle->hash_stat[STAT_USED] = 0; /* inserts will bump it up to correct */
572 for (oldtrack=oldwhere; oldtrack < oldwall; oldtrack++)
574 if (((string = oldtrack->hash_string) != NULL) && string != DELETED)
576 if ( * (retval = hash_jam(handle,string,oldtrack->hash_value) ) )
583 if ( !*retval && handle->hash_stat[STAT_USED] != oldused)
585 retval = "hash_used";
591 * we have a completely faked up control block.
592 * return the old hash table.
594 free((char *)oldwhere);
596 * Here with success. retval is already "".
608 * h a s h _ a p p l y ( )
610 * Use this to scan each entry in symbol table.
611 * For each symbol, this calls (applys) a nominated function supplying the
612 * symbol's value (and the symbol's name).
613 * The idea is you use this to destroy whatever is associted with
614 * any values in the table BEFORE you destroy the table with hash_die.
615 * Of course, you can use it for other jobs; whenever you need to
616 * visit all extant symbols in the table.
618 * We choose to have a call-you-back idea for two reasons:
619 * asthetic: it is a neater idea to use apply than an explicit loop
620 * sensible: if we ever had to grow the symbol table (due to insertions)
621 * then we would lose our place in the table when we re-hashed
622 * symbols into the new table in a different order.
624 * The order symbols are visited depends entirely on the hashing function.
625 * Whenever you insert a (symbol, value) you risk expanding the table. If
626 * you do expand the table, then the hashing function WILL change, so you
627 * MIGHT get a different order of symbols visited. In other words, if you
628 * want the same order of visiting symbols as the last time you used
629 * hash_apply() then you better not have done any hash_insert()s or
630 * hash_jam()s since the last time you used hash_apply().
632 * In future we may use the value returned by your nominated function.
633 * One idea is to abort the scan if, after applying the function to a
634 * certain node, the function returns a certain code.
635 * To be safe, please make your functions of type char *. If you always
636 * return NULL, then the scan will complete, visiting every symbol in
637 * the table exactly once. ALL OTHER RETURNED VALUES have no meaning yet!
640 * The function you supply should be of the form:
641 * char * myfunct(string,value)
642 * char * string; |* the symbol's name *|
643 * char * value; |* the symbol's value *|
649 * The returned value of hash_apply() is (char*)NULL. In future it may return
650 * other values. NULL means "completed scan OK". Other values have no meaning
651 * yet. (The function has no graceful failures.)
654 hash_apply(handle,function)
655 struct hash_control * handle;
658 register struct hash_entry * entry;
659 register struct hash_entry * wall;
661 wall = handle->hash_wall;
662 for (entry = handle->hash_where; entry < wall; entry++)
664 if (islive(entry)) /* silly code: tests entry->string twice! */
666 (*function)(entry->hash_string,entry->hash_value);
673 * h a s h _ f i n d ( )
675 * Given symbol string, find value (if any).
676 * Return found value or NULL.
679 hash_find(handle,string) /* return char* or NULL */
680 struct hash_control * handle;
683 register struct hash_entry * entry;
684 register char * retval;
686 entry = hash_ask(handle,string,STAT__READ);
689 retval = entry->hash_value;
699 * h a s h _ a s k ( )
701 * Searches for given symbol string.
702 * Return the slot where it OUGHT to live. It may be there.
703 * Return hash_found: TRUE only if symbol is in that slot.
704 * Access argument is to help keep statistics in control block.
707 static struct hash_entry * /* string slot, may be empty or deleted */
708 hash_ask(handle,string,access)
709 struct hash_control * handle;
711 int access; /* access type */
713 register char *string1; /* JF avoid strcmp calls */
716 register struct hash_entry * slot;
717 register int collision; /* count collisions */
719 slot = handle->hash_where + hash_code(handle,string); /* start looking here */
720 handle->hash_stat[STAT_ACCESS+access] += 1;
723 while (((s = slot->hash_string) != NULL) && s != DELETED)
725 for (string1=string;;) {
726 if ((c= *s++) == 0) {
741 * in use: we found string slot
743 * at wall: we fell off: wrap round ????
744 * in table: dig here slot
745 * at DELETED: dig here slot
747 if (slot == handle->hash_wall)
749 slot = handle->hash_where; /* now look again */
750 while (((s = slot->hash_string) != NULL) && s != DELETED)
752 for (string1=string;*s;string1++,s++) {
756 if (*s == *string1) {
765 * in use: we found it slot
766 * empty: wall: ERROR IMPOSSIBLE !!!!
767 * in table: dig here slot
768 * DELETED:dig here slot
771 /* fprintf(stderr,"hash_ask(%s)->%d(%d)\n",string,hash_code(handle,string),collision); */
772 handle->hash_stat[STAT_COLLIDE+access] += collision;
773 return(slot); /* also return hash_found */
779 * Does hashing of symbol string to hash number.
783 hash_code(handle,string)
784 struct hash_control * handle;
785 register char * string;
787 register long h; /* hash code built here */
788 register long c; /* each character lands here */
789 register int n; /* Amount to shift h by */
791 n = (handle->hash_sizelog - 3);
793 while ((c = *string++) != 0)
796 h = (h<<3) + (h>>n) + c;
798 return (h & handle->hash_mask);
802 * Here is a test program to exercise above.
806 #define TABLES (6) /* number of hash tables to maintain */
807 /* (at once) in any testing */
808 #define STATBUFSIZE (12) /* we can have 12 statistics */
810 int statbuf[STATBUFSIZE]; /* display statistics here */
811 char answer[100]; /* human farts here */
812 char * hashtable[TABLES]; /* we test many hash tables at once */
813 char * h; /* points to curent hash_control */
821 int number; /* number 0:TABLES-1 of current hashed */
826 char (*applicatee());
830 struct hash_control * hash_new();
831 char * hash_replace();
836 printf("type h <RETURN> for help\n");
839 printf("hash_test command: ");
840 fgets(answer, 100, stdin);
842 if (isupper(command)) command = tolower(command); /* ecch! */
846 printf("old hash table #=%d.\n",number);
850 for (pp=hashtable; pp<hashtable+TABLES; pp++)
852 printf("address of hash table #%d control block is %xx\n"
857 hash_apply(h,applicatee);
860 hash_apply(h,destroy);
864 p = hash_find(h,name=what("symbol"));
865 printf("value of \"%s\" is \"%s\"\n",name,p?p:"NOT-PRESENT");
868 printf("# show old, select new default hash table number\n");
869 printf("? display all hashtable control block addresses\n");
870 printf("a apply a simple display-er to each symbol in table\n");
871 printf("d die: destroy hashtable\n");
872 printf("f find value of nominated symbol\n");
873 printf("h this help\n");
874 printf("i insert value into symbol\n");
875 printf("j jam value into symbol\n");
876 printf("n new hashtable\n");
877 printf("r replace a value with another\n");
878 printf("s say what %% of table is used\n");
879 printf("q exit this program\n");
880 printf("x delete a symbol from table, report its value\n");
883 p = hash_insert(h,name=what("symbol"),value=what("value"));
886 printf("symbol=\"%s\" value=\"%s\" error=%s\n",name,value,p);
890 p = hash_jam(h,name=what("symbol"),value=what("value"));
893 printf("symbol=\"%s\" value=\"%s\" error=%s\n",name,value,p);
897 h = hashtable[number] = (char *) hash_new();
902 p = hash_replace(h,name=what("symbol"),value=what("value"));
903 printf("old value was \"%s\"\n",p?p:"{}");
906 hash_say(h,statbuf,STATBUFSIZE);
907 for (ip=statbuf; ip<statbuf+STATBUFSIZE; ip++)
914 p = hash_delete(h,name=what("symbol"));
915 printf("old value was \"%s\"\n",p?p:"{}");
918 printf("I can't understand command \"%c\"\n",command);
931 printf(" %s : ",description);
932 fgets(answer, 100, stdin);
933 /* will one day clean up answer here */
934 retval = malloc(strlen(answer)+1);
939 (void)strcpy(retval,answer);
944 destroy(string,value)
955 applicatee(string,value)
959 printf("%.20s-%.20s\n",string,value);
963 whattable() /* determine number: what hash table to use */
964 /* also determine h: points to hash_control */
969 printf(" what hash table (%d:%d) ? ",0,TABLES-1);
970 fgets(answer, 100, stdin);
971 sscanf(answer,"%d",&number);
972 if (number >= 0 && number<TABLES)
974 h = hashtable[number];
977 printf("warning: current hash-table-#%d. has no hash-control\n",number);
983 printf("invalid hash table number: %d\n",number);
990 #endif /* #ifdef TEST */