hash.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /* hash.c -- gas hash table code
  2. Copyright (C) 1987-2015 Free Software Foundation, Inc.
  3. This file is part of GAS, the GNU Assembler.
  4. GAS is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. GAS is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GAS; see the file COPYING. If not, write to the Free
  14. Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
  15. 02110-1301, USA. */
  16. /* This version of the hash table code is a wholescale replacement of
  17. the old hash table code, which was fairly bad. This is based on
  18. the hash table code in BFD, but optimized slightly for the
  19. assembler. The assembler does not need to derive structures that
  20. are stored in the hash table. Instead, it always stores a pointer.
  21. The assembler uses the hash table mostly to store symbols, and we
  22. don't need to confuse the symbol structure with a hash table
  23. structure. */
  24. #include "as.h"
  25. #include "safe-ctype.h"
  26. #include "obstack.h"
  27. /* An entry in a hash table. */
  28. struct hash_entry {
  29. /* Next entry for this hash code. */
  30. struct hash_entry *next;
  31. /* String being hashed. */
  32. const char *string;
  33. /* Hash code. This is the full hash code, not the index into the
  34. table. */
  35. unsigned long hash;
  36. /* Pointer being stored in the hash table. */
  37. void *data;
  38. };
  39. /* A hash table. */
  40. struct hash_control {
  41. /* The hash array. */
  42. struct hash_entry **table;
  43. /* The number of slots in the hash table. */
  44. unsigned int size;
  45. /* An obstack for this hash table. */
  46. struct obstack memory;
  47. #ifdef HASH_STATISTICS
  48. /* Statistics. */
  49. unsigned long lookups;
  50. unsigned long hash_compares;
  51. unsigned long string_compares;
  52. unsigned long insertions;
  53. unsigned long replacements;
  54. unsigned long deletions;
  55. #endif /* HASH_STATISTICS */
  56. };
  57. /* The default number of entries to use when creating a hash table.
  58. Note this value can be reduced to 4051 by using the command line
  59. switch --reduce-memory-overheads, or set to other values by using
  60. the --hash-size=<NUMBER> switch. */
  61. static unsigned long gas_hash_table_size = 65537;
  62. void
  63. set_gas_hash_table_size (unsigned long size)
  64. {
  65. gas_hash_table_size = bfd_hash_set_default_size (size);
  66. }
  67. /* Create a hash table. This return a control block. */
  68. struct hash_control *
  69. hash_new_sized (unsigned long size)
  70. {
  71. unsigned long alloc;
  72. struct hash_control *ret;
  73. ret = (struct hash_control *) xmalloc (sizeof *ret);
  74. obstack_begin (&ret->memory, chunksize);
  75. alloc = size * sizeof (struct hash_entry *);
  76. ret->table = (struct hash_entry **) obstack_alloc (&ret->memory, alloc);
  77. memset (ret->table, 0, alloc);
  78. ret->size = size;
  79. #ifdef HASH_STATISTICS
  80. ret->lookups = 0;
  81. ret->hash_compares = 0;
  82. ret->string_compares = 0;
  83. ret->insertions = 0;
  84. ret->replacements = 0;
  85. ret->deletions = 0;
  86. #endif
  87. return ret;
  88. }
  89. struct hash_control *
  90. hash_new (void)
  91. {
  92. return hash_new_sized (gas_hash_table_size);
  93. }
  94. /* Delete a hash table, freeing all allocated memory. */
  95. void
  96. hash_die (struct hash_control *table)
  97. {
  98. obstack_free (&table->memory, 0);
  99. free (table);
  100. }
  101. /* Look up a string in a hash table. This returns a pointer to the
  102. hash_entry, or NULL if the string is not in the table. If PLIST is
  103. not NULL, this sets *PLIST to point to the start of the list which
  104. would hold this hash entry. If PHASH is not NULL, this sets *PHASH
  105. to the hash code for KEY.
  106. Each time we look up a string, we move it to the start of the list
  107. for its hash code, to take advantage of referential locality. */
  108. static struct hash_entry *
  109. hash_lookup (struct hash_control *table, const char *key, size_t len,
  110. struct hash_entry ***plist, unsigned long *phash)
  111. {
  112. unsigned long hash;
  113. size_t n;
  114. unsigned int c;
  115. unsigned int hindex;
  116. struct hash_entry **list;
  117. struct hash_entry *p;
  118. struct hash_entry *prev;
  119. #ifdef HASH_STATISTICS
  120. ++table->lookups;
  121. #endif
  122. hash = 0;
  123. for (n = 0; n < len; n++)
  124. {
  125. c = key[n];
  126. hash += c + (c << 17);
  127. hash ^= hash >> 2;
  128. }
  129. hash += len + (len << 17);
  130. hash ^= hash >> 2;
  131. if (phash != NULL)
  132. *phash = hash;
  133. hindex = hash % table->size;
  134. list = table->table + hindex;
  135. if (plist != NULL)
  136. *plist = list;
  137. prev = NULL;
  138. for (p = *list; p != NULL; p = p->next)
  139. {
  140. #ifdef HASH_STATISTICS
  141. ++table->hash_compares;
  142. #endif
  143. if (p->hash == hash)
  144. {
  145. #ifdef HASH_STATISTICS
  146. ++table->string_compares;
  147. #endif
  148. if (strncmp (p->string, key, len) == 0 && p->string[len] == '\0')
  149. {
  150. if (prev != NULL)
  151. {
  152. prev->next = p->next;
  153. p->next = *list;
  154. *list = p;
  155. }
  156. return p;
  157. }
  158. }
  159. prev = p;
  160. }
  161. return NULL;
  162. }
  163. /* Insert an entry into a hash table. This returns NULL on success.
  164. On error, it returns a printable string indicating the error. It
  165. is considered to be an error if the entry already exists in the
  166. hash table. */
  167. const char *
  168. hash_insert (struct hash_control *table, const char *key, void *val)
  169. {
  170. struct hash_entry *p;
  171. struct hash_entry **list;
  172. unsigned long hash;
  173. p = hash_lookup (table, key, strlen (key), &list, &hash);
  174. if (p != NULL)
  175. return "exists";
  176. #ifdef HASH_STATISTICS
  177. ++table->insertions;
  178. #endif
  179. p = (struct hash_entry *) obstack_alloc (&table->memory, sizeof (*p));
  180. p->string = key;
  181. p->hash = hash;
  182. p->data = val;
  183. p->next = *list;
  184. *list = p;
  185. return NULL;
  186. }
  187. /* Insert or replace an entry in a hash table. This returns NULL on
  188. success. On error, it returns a printable string indicating the
  189. error. If an entry already exists, its value is replaced. */
  190. const char *
  191. hash_jam (struct hash_control *table, const char *key, void *val)
  192. {
  193. struct hash_entry *p;
  194. struct hash_entry **list;
  195. unsigned long hash;
  196. p = hash_lookup (table, key, strlen (key), &list, &hash);
  197. if (p != NULL)
  198. {
  199. #ifdef HASH_STATISTICS
  200. ++table->replacements;
  201. #endif
  202. p->data = val;
  203. }
  204. else
  205. {
  206. #ifdef HASH_STATISTICS
  207. ++table->insertions;
  208. #endif
  209. p = (struct hash_entry *) obstack_alloc (&table->memory, sizeof (*p));
  210. p->string = key;
  211. p->hash = hash;
  212. p->data = val;
  213. p->next = *list;
  214. *list = p;
  215. }
  216. return NULL;
  217. }
  218. /* Replace an existing entry in a hash table. This returns the old
  219. value stored for the entry. If the entry is not found in the hash
  220. table, this does nothing and returns NULL. */
  221. void *
  222. hash_replace (struct hash_control *table, const char *key, void *value)
  223. {
  224. struct hash_entry *p;
  225. void *ret;
  226. p = hash_lookup (table, key, strlen (key), NULL, NULL);
  227. if (p == NULL)
  228. return NULL;
  229. #ifdef HASH_STATISTICS
  230. ++table->replacements;
  231. #endif
  232. ret = p->data;
  233. p->data = value;
  234. return ret;
  235. }
  236. /* Find an entry in a hash table, returning its value. Returns NULL
  237. if the entry is not found. */
  238. void *
  239. hash_find (struct hash_control *table, const char *key)
  240. {
  241. struct hash_entry *p;
  242. p = hash_lookup (table, key, strlen (key), NULL, NULL);
  243. if (p == NULL)
  244. return NULL;
  245. return p->data;
  246. }
  247. /* As hash_find, but KEY is of length LEN and is not guaranteed to be
  248. NUL-terminated. */
  249. void *
  250. hash_find_n (struct hash_control *table, const char *key, size_t len)
  251. {
  252. struct hash_entry *p;
  253. p = hash_lookup (table, key, len, NULL, NULL);
  254. if (p == NULL)
  255. return NULL;
  256. return p->data;
  257. }
  258. /* Delete an entry from a hash table. This returns the value stored
  259. for that entry, or NULL if there is no such entry. */
  260. void *
  261. hash_delete (struct hash_control *table, const char *key, int freeme)
  262. {
  263. struct hash_entry *p;
  264. struct hash_entry **list;
  265. p = hash_lookup (table, key, strlen (key), &list, NULL);
  266. if (p == NULL)
  267. return NULL;
  268. if (p != *list)
  269. abort ();
  270. #ifdef HASH_STATISTICS
  271. ++table->deletions;
  272. #endif
  273. *list = p->next;
  274. if (freeme)
  275. obstack_free (&table->memory, p);
  276. return p->data;
  277. }
  278. /* Traverse a hash table. Call the function on every entry in the
  279. hash table. */
  280. void
  281. hash_traverse (struct hash_control *table,
  282. void (*pfn) (const char *key, void *value))
  283. {
  284. unsigned int i;
  285. for (i = 0; i < table->size; ++i)
  286. {
  287. struct hash_entry *p;
  288. for (p = table->table[i]; p != NULL; p = p->next)
  289. (*pfn) (p->string, p->data);
  290. }
  291. }
  292. /* Print hash table statistics on the specified file. NAME is the
  293. name of the hash table, used for printing a header. */
  294. void
  295. hash_print_statistics (FILE *f ATTRIBUTE_UNUSED,
  296. const char *name ATTRIBUTE_UNUSED,
  297. struct hash_control *table ATTRIBUTE_UNUSED)
  298. {
  299. #ifdef HASH_STATISTICS
  300. unsigned int i;
  301. unsigned long total;
  302. unsigned long empty;
  303. fprintf (f, "%s hash statistics:\n", name);
  304. fprintf (f, "\t%lu lookups\n", table->lookups);
  305. fprintf (f, "\t%lu hash comparisons\n", table->hash_compares);
  306. fprintf (f, "\t%lu string comparisons\n", table->string_compares);
  307. fprintf (f, "\t%lu insertions\n", table->insertions);
  308. fprintf (f, "\t%lu replacements\n", table->replacements);
  309. fprintf (f, "\t%lu deletions\n", table->deletions);
  310. total = 0;
  311. empty = 0;
  312. for (i = 0; i < table->size; ++i)
  313. {
  314. struct hash_entry *p;
  315. if (table->table[i] == NULL)
  316. ++empty;
  317. else
  318. {
  319. for (p = table->table[i]; p != NULL; p = p->next)
  320. ++total;
  321. }
  322. }
  323. fprintf (f, "\t%g average chain length\n", (double) total / table->size);
  324. fprintf (f, "\t%lu empty slots\n", empty);
  325. #endif
  326. }
  327. #ifdef TEST
  328. /* This test program is left over from the old hash table code. */
  329. /* Number of hash tables to maintain (at once) in any testing. */
  330. #define TABLES (6)
  331. /* We can have 12 statistics. */
  332. #define STATBUFSIZE (12)
  333. /* Display statistics here. */
  334. int statbuf[STATBUFSIZE];
  335. /* Human farts here. */
  336. char answer[100];
  337. /* We test many hash tables at once. */
  338. char *hashtable[TABLES];
  339. /* Points to current hash_control. */
  340. char *h;
  341. char **pp;
  342. char *p;
  343. char *name;
  344. char *value;
  345. int size;
  346. int used;
  347. char command;
  348. /* Number 0:TABLES-1 of current hashed symbol table. */
  349. int number;
  350. int
  351. main ()
  352. {
  353. void applicatee ();
  354. void destroy ();
  355. char *what ();
  356. int *ip;
  357. number = 0;
  358. h = 0;
  359. printf ("type h <RETURN> for help\n");
  360. for (;;)
  361. {
  362. printf ("hash_test command: ");
  363. gets (answer);
  364. command = answer[0];
  365. command = TOLOWER (command); /* Ecch! */
  366. switch (command)
  367. {
  368. case '#':
  369. printf ("old hash table #=%d.\n", number);
  370. whattable ();
  371. break;
  372. case '?':
  373. for (pp = hashtable; pp < hashtable + TABLES; pp++)
  374. {
  375. printf ("address of hash table #%d control block is %xx\n",
  376. pp - hashtable, *pp);
  377. }
  378. break;
  379. case 'a':
  380. hash_traverse (h, applicatee);
  381. break;
  382. case 'd':
  383. hash_traverse (h, destroy);
  384. hash_die (h);
  385. break;
  386. case 'f':
  387. p = hash_find (h, name = what ("symbol"));
  388. printf ("value of \"%s\" is \"%s\"\n", name, p ? p : "NOT-PRESENT");
  389. break;
  390. case 'h':
  391. printf ("# show old, select new default hash table number\n");
  392. printf ("? display all hashtable control block addresses\n");
  393. printf ("a apply a simple display-er to each symbol in table\n");
  394. printf ("d die: destroy hashtable\n");
  395. printf ("f find value of nominated symbol\n");
  396. printf ("h this help\n");
  397. printf ("i insert value into symbol\n");
  398. printf ("j jam value into symbol\n");
  399. printf ("n new hashtable\n");
  400. printf ("r replace a value with another\n");
  401. printf ("s say what %% of table is used\n");
  402. printf ("q exit this program\n");
  403. printf ("x delete a symbol from table, report its value\n");
  404. break;
  405. case 'i':
  406. p = hash_insert (h, name = what ("symbol"), value = what ("value"));
  407. if (p)
  408. {
  409. printf ("symbol=\"%s\" value=\"%s\" error=%s\n", name, value,
  410. p);
  411. }
  412. break;
  413. case 'j':
  414. p = hash_jam (h, name = what ("symbol"), value = what ("value"));
  415. if (p)
  416. {
  417. printf ("symbol=\"%s\" value=\"%s\" error=%s\n", name, value, p);
  418. }
  419. break;
  420. case 'n':
  421. h = hashtable[number] = (char *) hash_new ();
  422. break;
  423. case 'q':
  424. exit (EXIT_SUCCESS);
  425. case 'r':
  426. p = hash_replace (h, name = what ("symbol"), value = what ("value"));
  427. printf ("old value was \"%s\"\n", p ? p : "{}");
  428. break;
  429. case 's':
  430. hash_say (h, statbuf, STATBUFSIZE);
  431. for (ip = statbuf; ip < statbuf + STATBUFSIZE; ip++)
  432. {
  433. printf ("%d ", *ip);
  434. }
  435. printf ("\n");
  436. break;
  437. case 'x':
  438. p = hash_delete (h, name = what ("symbol"));
  439. printf ("old value was \"%s\"\n", p ? p : "{}");
  440. break;
  441. default:
  442. printf ("I can't understand command \"%c\"\n", command);
  443. break;
  444. }
  445. }
  446. }
  447. char *
  448. what (description)
  449. char *description;
  450. {
  451. printf (" %s : ", description);
  452. gets (answer);
  453. return xstrdup (answer);
  454. }
  455. void
  456. destroy (string, value)
  457. char *string;
  458. char *value;
  459. {
  460. free (string);
  461. free (value);
  462. }
  463. void
  464. applicatee (string, value)
  465. char *string;
  466. char *value;
  467. {
  468. printf ("%.20s-%.20s\n", string, value);
  469. }
  470. /* Determine number: what hash table to use.
  471. Also determine h: points to hash_control. */
  472. void
  473. whattable ()
  474. {
  475. for (;;)
  476. {
  477. printf (" what hash table (%d:%d) ? ", 0, TABLES - 1);
  478. gets (answer);
  479. sscanf (answer, "%d", &number);
  480. if (number >= 0 && number < TABLES)
  481. {
  482. h = hashtable[number];
  483. if (!h)
  484. {
  485. printf ("warning: current hash-table-#%d. has no hash-control\n", number);
  486. }
  487. return;
  488. }
  489. else
  490. {
  491. printf ("invalid hash table number: %d\n", number);
  492. }
  493. }
  494. }
  495. #endif /* TEST */