1 /* Copyright (C) 2000-2019 Red Hat, Inc.
2    This file is part of elfutils.
3    Written by Srdan Milakovic <sm108@rice.edu>, 2019.
4    Derived from Ulrich Drepper <drepper@redhat.com>, 2000.
5 
6    This file is free software; you can redistribute it and/or modify
7    it under the terms of either
8 
9      * the GNU Lesser General Public License as published by the Free
10        Software Foundation; either version 3 of the License, or (at
11        your option) any later version
12 
13    or
14 
15      * the GNU General Public License as published by the Free
16        Software Foundation; either version 2 of the License, or (at
17        your option) any later version
18 
19    or both in parallel, as here.
20 
21    elfutils is distributed in the hope that it will be useful, but
22    WITHOUT ANY WARRANTY; without even the implied warranty of
23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24    General Public License for more details.
25 
26    You should have received copies of the GNU General Public License and
27    the GNU Lesser General Public License along with this program.  If
28    not, see <http://www.gnu.org/licenses/>.  */
29 
30 #include <stddef.h>
31 #include <pthread.h>
32 #include "atomics.h"
33 /* Before including this file the following macros must be defined:
34 
35    NAME      name of the hash table structure.
36    TYPE      data type of the hash table entries
37 
38    The following macros if present select features:
39 
40    ITERATE   iterating over the table entries is possible
41    HASHTYPE  integer type for hash values, default unsigned long int
42  */
43 
44 
45 
46 #ifndef HASHTYPE
47 # define HASHTYPE unsigned long int
48 #endif
49 
50 #ifndef RESIZE_BLOCK_SIZE
51 # define RESIZE_BLOCK_SIZE 256
52 #endif
53 
54 /* Defined separately.  */
55 extern size_t next_prime (size_t seed);
56 
57 
58 /* Table entry type.  */
59 #define _DYNHASHCONENTTYPE(name)       \
60   typedef struct name##_ent         \
61   {                                 \
62     _Atomic(HASHTYPE) hashval;      \
63     atomic_uintptr_t val_ptr;       \
64   } name##_ent
65 #define DYNHASHENTTYPE(name) _DYNHASHCONENTTYPE (name)
66 DYNHASHENTTYPE (NAME);
67 
68 /* Type of the dynamic hash table data structure.  */
69 #define _DYNHASHCONTYPE(name) \
70 typedef struct                                     \
71 {                                                  \
72   size_t size;                                     \
73   size_t old_size;                                 \
74   atomic_size_t filled;                            \
75   name##_ent *table;                               \
76   name##_ent *old_table;                           \
77   atomic_size_t resizing_state;                    \
78   atomic_size_t next_init_block;                   \
79   atomic_size_t num_initialized_blocks;            \
80   atomic_size_t next_move_block;                   \
81   atomic_size_t num_moved_blocks;                  \
82   pthread_rwlock_t resize_rwl;                     \
83 } name
84 #define DYNHASHTYPE(name) _DYNHASHCONTYPE (name)
85 DYNHASHTYPE (NAME);
86 
87 
88 
89 #define _FUNCTIONS(name)                                            \
90 /* Initialize the hash table.  */                                   \
91 extern int name##_init (name *htab, size_t init_size);              \
92                                                                     \
93 /* Free resources allocated for hash table.  */                     \
94 extern int name##_free (name *htab);                                \
95                                                                     \
96 /* Insert new entry.  */                                            \
97 extern int name##_insert (name *htab, HASHTYPE hval, TYPE data);    \
98                                                                     \
99 /* Find entry in hash table.  */                                    \
100 extern TYPE name##_find (name *htab, HASHTYPE hval);
101 #define FUNCTIONS(name) _FUNCTIONS (name)
102 FUNCTIONS (NAME)
103 
104 
105 #ifndef NO_UNDEF
106 # undef DYNHASHENTTYPE
107 # undef DYNHASHTYPE
108 # undef FUNCTIONS
109 # undef _FUNCTIONS
110 # undef XFUNCTIONS
111 # undef _XFUNCTIONS
112 # undef NAME
113 # undef TYPE
114 # undef ITERATE
115 # undef COMPARE
116 # undef FIRST
117 # undef NEXT
118 #endif
119