Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 1 | /* Copyright (C) 2000-2010 Red Hat, Inc. |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 2 | This file is part of elfutils. |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 3 | Written by Ulrich Drepper <drepper@redhat.com>, 2000. |
| 4 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 5 | This file is free software; you can redistribute it and/or modify |
| 6 | it under the terms of either |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 7 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 8 | * the GNU Lesser General Public License as published by the Free |
| 9 | Software Foundation; either version 3 of the License, or (at |
| 10 | your option) any later version |
| 11 | |
| 12 | or |
| 13 | |
| 14 | * the GNU General Public License as published by the Free |
| 15 | Software Foundation; either version 2 of the License, or (at |
| 16 | your option) any later version |
| 17 | |
| 18 | or both in parallel, as here. |
| 19 | |
| 20 | elfutils is distributed in the hope that it will be useful, but |
Ulrich Drepper | 361df7d | 2006-04-04 21:38:57 +0000 | [diff] [blame] | 21 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 23 | General Public License for more details. |
| 24 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 25 | You should have received copies of the GNU General Public License and |
| 26 | the GNU Lesser General Public License along with this program. If |
| 27 | not, see <http://www.gnu.org/licenses/>. */ |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 28 | |
| 29 | #include <stddef.h> |
| 30 | |
| 31 | /* Before including this file the following macros must be defined: |
| 32 | |
| 33 | NAME name of the hash table structure. |
| 34 | TYPE data type of the hash table entries |
| 35 | |
| 36 | The following macros if present select features: |
| 37 | |
| 38 | ITERATE iterating over the table entries is possible |
Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 39 | HASHTYPE integer type for hash values, default unsigned long int |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 40 | */ |
| 41 | |
| 42 | |
| 43 | /* Optionally include an entry pointing to the first used entry. */ |
| 44 | #ifdef ITERATE |
| 45 | # define FIRST(name) name##_ent *first; |
| 46 | # define NEXT(name) struct name##_ent *next; |
| 47 | #else |
| 48 | # define FIRST(name) |
| 49 | # define NEXT(name) |
| 50 | #endif |
| 51 | |
Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 52 | #ifndef HASHTYPE |
| 53 | # define HASHTYPE unsigned long int |
| 54 | #endif |
| 55 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 56 | |
| 57 | /* Defined separately. */ |
| 58 | extern size_t next_prime (size_t seed); |
| 59 | |
| 60 | |
| 61 | /* Table entry type. */ |
| 62 | #define _DYNHASHENTTYPE(name) \ |
| 63 | typedef struct name##_ent \ |
| 64 | { \ |
Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 65 | HASHTYPE hashval; \ |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 66 | TYPE data; \ |
| 67 | NEXT (name) \ |
| 68 | } name##_ent |
| 69 | #define DYNHASHENTTYPE(name) _DYNHASHENTTYPE (name) |
| 70 | DYNHASHENTTYPE (NAME); |
| 71 | |
| 72 | |
| 73 | /* Type of the dynamic hash table data structure. */ |
| 74 | #define _DYNHASHTYPE(name) \ |
| 75 | typedef struct \ |
| 76 | { \ |
Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 77 | size_t size; \ |
| 78 | size_t filled; \ |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 79 | name##_ent *table; \ |
| 80 | FIRST (name) \ |
| 81 | } name |
| 82 | #define DYNHASHTYPE(name) _DYNHASHTYPE (name) |
| 83 | DYNHASHTYPE (NAME); |
| 84 | |
| 85 | |
| 86 | |
| 87 | #define _FUNCTIONS(name) \ |
| 88 | /* Initialize the hash table. */ \ |
Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 89 | extern int name##_init (name *htab, size_t init_size); \ |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 90 | \ |
| 91 | /* Free resources allocated for hash table. */ \ |
| 92 | extern int name##_free (name *htab); \ |
| 93 | \ |
| 94 | /* Insert new entry. */ \ |
Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 95 | extern int name##_insert (name *htab, HASHTYPE hval, TYPE data); \ |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 96 | \ |
| 97 | /* Insert new entry, possibly overwrite old entry. */ \ |
Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 98 | extern int name##_overwrite (name *htab, HASHTYPE hval, TYPE data); \ |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 99 | \ |
| 100 | /* Find entry in hash table. */ \ |
Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 101 | extern TYPE name##_find (name *htab, HASHTYPE hval, TYPE val); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 102 | #define FUNCTIONS(name) _FUNCTIONS (name) |
| 103 | FUNCTIONS (NAME) |
| 104 | |
| 105 | |
| 106 | #ifdef ITERATE |
| 107 | # define _XFUNCTIONS(name) \ |
| 108 | /* Get next element in table. */ \ |
| 109 | extern TYPE name##_iterate (name *htab, void **ptr); |
| 110 | # define XFUNCTIONS(name) _XFUNCTIONS (name) |
| 111 | XFUNCTIONS (NAME) |
| 112 | #endif |
| 113 | |
| 114 | #ifndef NO_UNDEF |
| 115 | # undef DYNHASHENTTYPE |
| 116 | # undef DYNHASHTYPE |
| 117 | # undef FUNCTIONS |
| 118 | # undef _FUNCTIONS |
| 119 | # undef XFUNCTIONS |
| 120 | # undef _XFUNCTIONS |
| 121 | # undef NAME |
| 122 | # undef TYPE |
| 123 | # undef ITERATE |
| 124 | # undef COMPARE |
| 125 | # undef FIRST |
| 126 | # undef NEXT |
| 127 | #endif |