/src/binutils-gdb/gas/hash.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* hash.c -- gas hash table code |
2 | | Copyright (C) 1987-2025 Free Software Foundation, Inc. |
3 | | |
4 | | This file is part of GAS, the GNU Assembler. |
5 | | |
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 3, or (at your option) |
9 | | any later version. |
10 | | |
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. |
15 | | |
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 the Free |
18 | | Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA |
19 | | 02110-1301, USA. */ |
20 | | |
21 | | #include "as.h" |
22 | | |
23 | | /* Hash function for a string_tuple. */ |
24 | | |
25 | | hashval_t |
26 | | hash_string_tuple (const void *e) |
27 | 768k | { |
28 | 768k | const string_tuple_t *tuple = e; |
29 | 768k | return htab_hash_string (tuple->key); |
30 | 768k | } |
31 | | |
32 | | /* Equality function for a string_tuple. */ |
33 | | |
34 | | int |
35 | | eq_string_tuple (const void *a, const void *b) |
36 | 868k | { |
37 | 868k | const string_tuple_t *ea = a; |
38 | 868k | const string_tuple_t *eb = b; |
39 | | |
40 | 868k | return strcmp (ea->key, eb->key) == 0; |
41 | 868k | } |
42 | | |
43 | | /* Insert ELEMENT into HTAB. If REPLACE is non-zero existing elements |
44 | | are overwritten. If ELEMENT already exists, a pointer to the slot |
45 | | is returned. Otherwise NULL is returned. */ |
46 | | |
47 | | void ** |
48 | | htab_insert (htab_t htab, void *element, int replace) |
49 | 126k | { |
50 | 126k | void **slot = htab_find_slot (htab, element, INSERT); |
51 | 126k | if (*slot != NULL) |
52 | 14.8k | { |
53 | 14.8k | if (replace) |
54 | 14.2k | { |
55 | 14.2k | if (htab->del_f) |
56 | 0 | (*htab->del_f) (*slot); |
57 | 14.2k | *slot = element; |
58 | 14.2k | } |
59 | 14.8k | return slot; |
60 | 14.8k | } |
61 | 111k | *slot = element; |
62 | 111k | return NULL; |
63 | 126k | } |
64 | | |
65 | | /* Print statistics about a hash table. */ |
66 | | |
67 | | void |
68 | | htab_print_statistics (FILE *f, const char *name, htab_t table) |
69 | 0 | { |
70 | 0 | fprintf (f, "%s hash statistics:\n", name); |
71 | 0 | fprintf (f, "\t%u searches\n", table->searches); |
72 | 0 | fprintf (f, "\t%u collisions\n", table->collisions); |
73 | 0 | fprintf (f, "\t%lu elements\n", (unsigned long) htab_elements (table)); |
74 | 0 | fprintf (f, "\t%lu table size\n", (unsigned long) htab_size (table)); |
75 | 0 | } |