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