Xenomai  3.1
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  dref_type(const void *) key;
29 #ifdef CONFIG_XENO_PSHARED
30  char static_key[16];
31 #endif
32  size_t len;
33  struct holder link;
34 };
35 
36 struct hash_bucket {
37  struct listobj obj_list;
38 };
39 
40 struct hash_table {
41  struct hash_bucket table[HASHSLOTS];
42  pthread_mutex_t lock;
43 };
44 
45 struct hash_operations {
46  int (*compare)(const void *l,
47  const void *r,
48  size_t len);
49 #ifdef CONFIG_XENO_PSHARED
50  int (*probe)(struct hashobj *oldobj);
51  void *(*alloc)(size_t len);
52  void (*free)(void *key);
53 #endif
54 };
55 
56 typedef int (*hash_walk_op)(struct hash_table *t,
57  struct hashobj *obj,
58  void *arg);
59 
60 #ifdef CONFIG_XENO_PSHARED
61 
62 /* Private version - h-table is not shareable between processes. */
63 
64 struct pvhashobj {
65  const void *key;
66  size_t len;
67  struct pvholder link;
68 };
69 
70 struct pvhash_bucket {
71  struct pvlistobj obj_list;
72 };
73 
74 struct pvhash_table {
75  struct pvhash_bucket table[HASHSLOTS];
76  pthread_mutex_t lock;
77 };
78 
79 struct pvhash_operations {
80  int (*compare)(const void *l,
81  const void *r,
82  size_t len);
83 };
84 
85 typedef int (*pvhash_walk_op)(struct pvhash_table *t,
86  struct pvhashobj *obj,
87  void *arg);
88 
89 #else /* !CONFIG_XENO_PSHARED */
90 #define pvhashobj hashobj
91 #define pvhash_bucket hash_bucket
92 #define pvhash_table hash_table
93 #define pvhash_walk_op hash_walk_op
94 #endif /* !CONFIG_XENO_PSHARED */
95 
96 #ifdef __cplusplus
97 extern "C" {
98 #endif
99 
100 unsigned int __hash_key(const void *key,
101  size_t length, unsigned int c);
102 
103 void __hash_init(void *heap, struct hash_table *t);
104 
105 int __hash_enter(struct hash_table *t,
106  const void *key, size_t len,
107  struct hashobj *newobj,
108  const struct hash_operations *hops,
109  int nodup);
110 
111 static inline void hash_init(struct hash_table *t)
112 {
113  __hash_init(__main_heap, t);
114 }
115 
116 void hash_destroy(struct hash_table *t);
117 
118 static inline int hash_enter(struct hash_table *t,
119  const void *key, size_t len,
120  struct hashobj *newobj,
121  const struct hash_operations *hops)
122 {
123  return __hash_enter(t, key, len, newobj, hops, 1);
124 }
125 
126 static inline int hash_enter_dup(struct hash_table *t,
127  const void *key, size_t len,
128  struct hashobj *newobj,
129  const struct hash_operations *hops)
130 {
131  return __hash_enter(t, key, len, newobj, hops, 0);
132 }
133 
134 int hash_remove(struct hash_table *t, struct hashobj *delobj,
135  const struct hash_operations *hops);
136 
137 struct hashobj *hash_search(struct hash_table *t,
138  const void *key, size_t len,
139  const struct hash_operations *hops);
140 
141 int hash_walk(struct hash_table *t,
142  hash_walk_op walk, void *arg);
143 
144 #ifdef CONFIG_XENO_PSHARED
145 
146 int __hash_enter_probe(struct hash_table *t,
147  const void *key, size_t len,
148  struct hashobj *newobj,
149  const struct hash_operations *hops,
150  int nodup);
151 
152 int __pvhash_enter(struct pvhash_table *t,
153  const void *key, size_t len,
154  struct pvhashobj *newobj,
155  const struct pvhash_operations *hops,
156  int nodup);
157 
158 static inline
159 int hash_enter_probe(struct hash_table *t,
160  const void *key, size_t len,
161  struct hashobj *newobj,
162  const struct hash_operations *hops)
163 {
164  return __hash_enter_probe(t, key, len, newobj, hops, 1);
165 }
166 
167 static inline
168 int hash_enter_probe_dup(struct hash_table *t,
169  const void *key, size_t len,
170  struct hashobj *newobj,
171  const struct hash_operations *hops)
172 {
173  return __hash_enter_probe(t, key, len, newobj, hops, 0);
174 }
175 
176 struct hashobj *hash_search_probe(struct hash_table *t,
177  const void *key, size_t len,
178  const struct hash_operations *hops);
179 
180 void pvhash_init(struct pvhash_table *t);
181 
182 static inline
183 int pvhash_enter(struct pvhash_table *t,
184  const void *key, size_t len,
185  struct pvhashobj *newobj,
186  const struct pvhash_operations *hops)
187 {
188  return __pvhash_enter(t, key, len, newobj, hops, 1);
189 }
190 
191 static inline
192 int pvhash_enter_dup(struct pvhash_table *t,
193  const void *key, size_t len,
194  struct pvhashobj *newobj,
195  const struct pvhash_operations *hops)
196 {
197  return __pvhash_enter(t, key, len, newobj, hops, 0);
198 }
199 
200 int pvhash_remove(struct pvhash_table *t, struct pvhashobj *delobj,
201  const struct pvhash_operations *hops);
202 
203 struct pvhashobj *pvhash_search(struct pvhash_table *t,
204  const void *key, size_t len,
205  const struct pvhash_operations *hops);
206 
207 int pvhash_walk(struct pvhash_table *t,
208  pvhash_walk_op walk, void *arg);
209 
210 #else /* !CONFIG_XENO_PSHARED */
211 #define pvhash_init hash_init
212 #define pvhash_enter hash_enter
213 #define pvhash_enter_dup hash_enter_dup
214 #define pvhash_remove hash_remove
215 #define pvhash_search hash_search
216 #define pvhash_walk hash_walk
217 #define pvhash_operations hash_operations
218 #endif /* !CONFIG_XENO_PSHARED */
219 
220 #ifdef __cplusplus
221 }
222 #endif
223 
224 #endif /* _BOILERPLATE_HASH_H */