Xenomai  3.0-rc3
hash.h
1 /*
2  * Copyright (C) 2008 Philippe Gerum <rpm@xenomai.org>.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13 
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18 
19 #ifndef _BOILERPLATE_HASH_H
20 #define _BOILERPLATE_HASH_H
21 
22 #include <pthread.h>
23 #include <boilerplate/list.h>
24 
25 #define HASHSLOTS (1<<8)
26 
27 struct hashobj {
28  const void *key;
29  size_t len;
30  struct holder link;
31 };
32 
33 struct hash_bucket {
34  struct list obj_list;
35 };
36 
37 struct hash_table {
38  struct hash_bucket table[HASHSLOTS];
39  int (*compare)(const struct hashobj *l,
40  const struct hashobj *r);
41  pthread_mutex_t lock;
42 };
43 
44 #ifdef CONFIG_XENO_PSHARED
45 /* Private version - not shareable between processes. */
46 struct pvhashobj {
47  const void *key;
48  size_t len;
49  struct pvholder link;
50 };
51 
52 struct pvhash_bucket {
53  struct pvlist obj_list;
54 };
55 
56 struct pvhash_table {
57  struct pvhash_bucket table[HASHSLOTS];
58  int (*compare)(const struct pvhashobj *l,
59  const struct pvhashobj *r);
60  pthread_mutex_t lock;
61 };
62 #else /* !CONFIG_XENO_PSHARED */
63 #define pvhashobj hashobj
64 #define pvhash_bucket hash_bucket
65 #define pvhash_table hash_table
66 #endif /* !CONFIG_XENO_PSHARED */
67 
68 #ifdef __cplusplus
69 extern "C" {
70 #endif
71 
72 unsigned int __hash_key(const void *key,
73  size_t length, unsigned int c);
74 
75 void __hash_init(void *heap, struct hash_table *t,
76  int (*compare)(const struct hashobj *l,
77  const struct hashobj *r));
78 
79 int __hash_enter(struct hash_table *t,
80  const void *key, size_t len,
81  struct hashobj *newobj, int nodup);
82 
83 static inline void hash_init(struct hash_table *t,
84  int (*compare)(const struct hashobj *l,
85  const struct hashobj *r))
86 {
87  __hash_init(__main_heap, t, compare);
88 }
89 
90 void hash_destroy(struct hash_table *t);
91 
92 static inline int hash_enter(struct hash_table *t,
93  const void *key, size_t len,
94  struct hashobj *newobj)
95 {
96  return __hash_enter(t, key, len, newobj, 1);
97 }
98 
99 static inline int hash_enter_dup(struct hash_table *t,
100  const void *key, size_t len,
101  struct hashobj *newobj)
102 {
103  return __hash_enter(t, key, len, newobj, 0);
104 }
105 
106 int hash_remove(struct hash_table *t, struct hashobj *delobj);
107 
108 struct hashobj *hash_search(struct hash_table *t,
109  const void *key, size_t len);
110 
111 int hash_walk(struct hash_table *t,
112  int (*walk)(struct hash_table *t, struct hashobj *obj));
113 
114 int hash_compare_strings(const struct hashobj *l,
115  const struct hashobj *r);
116 
117 #ifdef CONFIG_XENO_PSHARED
118 
119 int __hash_enter_probe(struct hash_table *t,
120  const void *key, size_t len,
121  struct hashobj *newobj,
122  int (*probefn)(struct hashobj *oldobj),
123  int nodup);
124 
125 int __pvhash_enter(struct pvhash_table *t,
126  const void *key, size_t len,
127  struct pvhashobj *newobj, int nodup);
128 
129 static inline
130 int hash_enter_probe(struct hash_table *t,
131  const void *key, size_t len,
132  struct hashobj *newobj,
133  int (*probefn)(struct hashobj *oldobj))
134 {
135  return __hash_enter_probe(t, key, len, newobj, probefn, 1);
136 }
137 
138 static inline
139 int hash_enter_probe_dup(struct hash_table *t,
140  const void *key, size_t len,
141  struct hashobj *newobj,
142  int (*probefn)(struct hashobj *oldobj))
143 {
144  return __hash_enter_probe(t, key, len, newobj, probefn, 0);
145 }
146 
147 struct hashobj *hash_search_probe(struct hash_table *t,
148  const void *key, size_t len,
149  int (*probefn)(struct hashobj *obj));
150 
151 void pvhash_init(struct pvhash_table *t,
152  int (*compare)(const struct pvhashobj *l,
153  const struct pvhashobj *r));
154 
155 static inline
156 int pvhash_enter(struct pvhash_table *t,
157  const void *key, size_t len,
158  struct pvhashobj *newobj)
159 {
160  return __pvhash_enter(t, key, len, newobj, 1);
161 }
162 
163 static inline
164 int pvhash_enter_dup(struct pvhash_table *t,
165  const void *key, size_t len,
166  struct pvhashobj *newobj)
167 {
168  return __pvhash_enter(t, key, len, newobj, 0);
169 }
170 
171 int pvhash_remove(struct pvhash_table *t, struct pvhashobj *delobj);
172 
173 struct pvhashobj *pvhash_search(struct pvhash_table *t,
174  const void *key, size_t len);
175 
176 int pvhash_walk(struct pvhash_table *t,
177  int (*walk)(struct pvhash_table *t, struct pvhashobj *obj));
178 
179 int pvhash_compare_strings(const struct pvhashobj *l,
180  const struct pvhashobj *r);
181 
182 #else /* !CONFIG_XENO_PSHARED */
183 #define pvhash_init hash_init
184 #define pvhash_enter hash_enter
185 #define pvhash_enter_dup hash_enter_dup
186 #define pvhash_remove hash_remove
187 #define pvhash_search hash_search
188 #define pvhash_walk hash_walk
189 #define pvhash_compare_strings hash_compare_strings
190 #endif /* !CONFIG_XENO_PSHARED */
191 
192 #ifdef __cplusplus
193 }
194 #endif
195 
196 #endif /* _BOILERPLATE_HASH_H */