-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsos_hlist.h
491 lines (426 loc) · 16 KB
/
sos_hlist.h
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/********************************************************************************
* _____ _ ______ _____ *
* / ___| | | | ___ \ __ \ *
* \ `--. _ __ ___ __ _ _ __| |_| |_/ / | \/ *
* `--. \ '_ ` _ \ / _` | '__| __| /| | __ *
* /\__/ / | | | | | (_| | | | |_| |\ \| |_\ \ *
* \____/|_| |_| |_|\__,_|_| \__\_| \_|\____/ Inc. *
* *
********************************************************************************
* *
* copyright 2017 by SmartRG, Inc. *
* Santa Barbara, CA *
* *
****************************************************************************//**
*
* @file sos_hlist.h
* @authors [email protected]
*
* @brief Defines a managed hash list
* @details
*
* @version 1.0
*
* @copyright 2017 by SmartRG, Inc.
*******************************************************************************/
#ifndef _SOS_HLIST_H_
#define _SOS_HLIST_H_
#ifdef __cplusplus
extern "C" {
#endif
/*******************************************************************************
* *
* INCLUDES *
* *
*******************************************************************************/
/*******************************************************************************
* *
* DEFINES *
* *
*******************************************************************************/
//! Enable this flag to enable debugging
//#define SOS_HLIST_DEBUG
#define SOS_HLIST_BITS (10) //!< Number of bits used to define the hash list size
#define SOS_HLIST_SIZE (1 << SOS_HLIST_BITS) //!< Hash list size in power of 2 for efficiency
//!< Poison for the @ref sos_hhead::pNext node pointer
#define SOS_HLIST_POISON1 ((sos_hhead_t *)((uintptr_t) 0xDEAD0100))
//!< Poison for the @ref sos_hhead::pPrev node pointer
#define SOS_HLIST_POISON2 ((sos_hhead_t *)((uintptr_t) 0xDEAD0200))
//!< Poison for the @ref sos_hhead::pHash pointer
#define SOS_HLIST_POISON3 ((sos_hash_t *)((uintptr_t) 0xDEAD0300))
/*******************************************************************************
* *
* TYPEDEFS *
* *
*******************************************************************************/
//! Node into a hashed list
typedef struct sos_hhead {
struct sos_hhead *pNext; //!< pointer to the next element in the list
struct sos_hhead *pPrev; //!< pointer to the previous element in the list
struct sos_hash *pHash; //!< pointer to our associated hash
} sos_hhead_t;
//! Hash structure
typedef struct sos_hash {
struct sos_hhead head; //!< head of the list
#ifdef SOS_HLIST_DEBUG
struct sos_hlist *pList; //!< Pointer to our associated hash list
uint32_t hash; //!< The hash identifier for this hash
uint32_t items; //!< Number of elements currently associated with this hash
uint32_t max; //!< Maximum number of elements seen at one time
#endif
} sos_hash_t;
//! Hash List implementation
typedef struct sos_hlist {
#ifdef SOS_HLIST_DEBUG
uint32_t items; //!< Number of elements currently associated with this hash list
uint32_t max; //!< Maximum number of elements seen at one time
#endif
struct sos_hash aHash[SOS_HLIST_SIZE]; //!< Array of hash
} sos_hlist_t;
/*******************************************************************************
* *
* GLOBAL PROTOTYPES *
* *
*******************************************************************************/
/*******************************************************************************
* *
* GLOBAL VARIABLES *
* *
*******************************************************************************/
/*******************************************************************************
* *
* MACROS *
* *
*******************************************************************************/
//! @{
//! @name MIN/MAX Fast Macros
//!
#define SOS_HLIST_MIN(_a, _b) ((_b) + (((_a) - (_b)) & -((_a) < (_b))))
#define SOS_HLIST_MAX(_a, _b) ((_a) - (((_a) - (_b)) & -((_a) < (_b))))
//!
//! @}
//! Macro to compute a hash and ensure the type of the key
#define __sos_hlist_hash(_pKey, _len, _bits) sos_hlist_hash(((const uint8_t *)(_pKey)), _len, _bits)
//! Convert a node to a given type
#define sos_hlist_entry(_ptr, _type, _member) container_of((_ptr), _type, _member)
/**
* @brief list_first_entry - get the first element from a list
*
* @param _ptr pointer to the list head to take the element from.
* @param _type the type of the struct this is embedded in.
* @param _member the name of the hash node member within the struct.
*
* Note, that list is expected to be not empty.
*/
#define sos_hlist_first_entry(_ptr, _type, _member) sos_hlist_entry((_ptr)->pNext, _type, _member)
/**
* @brief list_next_entry - get the next element in list
*
* @param _pos the type * to cursor
* @param _member the name of the list_struct within the struct.
*/
#define sos_hlist_next_entry(_pos, _member) sos_hlist_entry((_pos)->_member.pNext, typeof(*_pos), _member)
/**
* @brief list_for_each_entry - iterate over list of given type
*
* @param _pos the type * to use as a loop cursor.
* @param _head the head for your list.
* @param _member the name of the list_struct within the struct.
*/
#define sos_hlist_for_each_entry(_pos, _head, _member) \
for ((_pos) = sos_hlist_first_entry((_head), typeof(*_pos), _member); \
&(_pos)->_member != (_head); \
(_pos) = sos_hlist_next_entry((_pos), _member))
/**
* @brief list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
*
* @param _pos the type * to use as a loop cursor.
* @param _next another type * to use as temporary storage
* @param _head the head for your list.
* @param _member the name of the list_struct within the struct.
*/
#define sos_hlist_for_each_entry_safe(_pos, _next, _head, _member) \
for ((_pos) = sos_hlist_first_entry((_head), typeof(*_pos), _member), (_next) = sos_hlist_next_entry((_pos), _member); \
&(_pos)->_member != (_head); \
(_pos) = (_next), (_next) = sos_hlist_next_entry((_next), _member))
/*******************************************************************************
* *
* INLINES *
* *
*******************************************************************************/
/**
* @brief Computes a 32 bits one at the time hash of a given string
*
* @param pKey pointer to the data/key to hash
* @param len the length of the key to hash
* @param bits the number of bits to use for the hash value
*
* @return a 32 bits hash value
* @author [email protected]
*/
static __inline__ uint32_t sos_hlist_hash(const uint8_t *pKey, const size_t len, const uint8_t bits)
{
register uint32_t i = 0;
register uint32_t hash = 0;
for (hash = 0, i = 0; i < len; i++) {
hash += pKey[i];
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return ((uint32_t) (hash >> (32 - bits)));
}
/**
* @brief Initialize a hash node
*
* @param pNode pointer to the node to initialize
*
* @author [email protected]
*/
static __inline__ void __sos_hlist_node_init(sos_hhead_t *pNode)
{
pNode->pNext = pNode;
pNode->pPrev = pNode;
pNode->pHash = NULL;
}
/**
* @brief Initialize a given hash list
*
* @param pList pointer to the hash list to initialize
*
* @author [email protected]
*/
static __inline__ void __sos_hlist_init(sos_hlist_t *pList)
{
uint32_t hash = 0;
register size_t i = 0;
register sos_hash_t *pHash = NULL;
// Zero out our hasharray
memset(pList->aHash, 0, sizeof(pList->aHash));
// Initialize all of the hashes individually
for (i = SOS_HLIST_SIZE, pHash = pList->aHash, hash = 0; i > 0; i--, pHash++, hash++) {
// Initialize our list head
__sos_hlist_node_init(&pHash->head);
#ifdef SOS_HLIST_DEBUG
// Remember our hash identifier
pHash->hash = hash;
// Remember our list
pHash->pList = pList;
#endif
}
}
#ifdef SOS_HLIST_DEBUG
/**
* @brief Reset the hash list max counters
*
* @param pList pointer to the hash list to reset
*
* @author [email protected]
*/
static __inline__ void __sos_hlist_reset_max(sos_hlist_t *pList)
{
register size_t i = 0;
register sos_hash_t *pHash = NULL;
// Reset our hash list max
pList->max = pList->items;
// Reset the max of all of our individual hash
for (i = SOS_HLIST_SIZE, pHash = pList->aHash; i > 0; i--, pHash++) {
pHash->max = pHash->items;
}
}
#endif
/**
* @brief Check if a give node is part of a hashed list
*
* @param pNode pointer to the node to validate
*
* @return true if the node is not part of a hashed list otherwise false is returned.
* @author [email protected]
*/
static __inline__ bool sos_hlist_unhashed(const sos_hhead_t *pNode)
{
return ((pNode->pHash == NULL) || (pNode->pHash == SOS_HLIST_POISON3));
}
/**
* @brief Checks whether or not a given hash list head is empty
*
* @param pHash pointer to the hash to validate
*
* @return true if the list is empty otherwise false is returned
* @author [email protected]
*/
static __inline__ bool sos_hlist_empty(const sos_hash_t *pHash)
{
register const sos_hhead_t *pHead = &pHash->head;
return (pHead->pNext == pHead);
}
/**
* @brief Adds a hash node between two other nodes
*
* @param pNew pointer to the new node to add
* @param pPrev pointer to our previous node in the list
* @param pNext pointer to our next node in the list
*
* @author [email protected]
*/
static inline void __sos_hlist_add_node(sos_hhead_t *pNew, sos_hhead_t *pPrev, sos_hhead_t *pNext)
{
pNext->pPrev = pNew;
pNew->pNext = pNext;
pNew->pPrev = pPrev;
pPrev->pNext = pNew;
}
/**
* @brief Accounts for items added to a hash list under a given hash
*
* @param pHash pointer to the hash to do the accounting
*
* @author [email protected]
*/
#ifndef SOS_HLIST_DEBUG
#define __sos_hlist_add_account(_pHash) (void)(_pHash)
#else /* ! SOS_HLIST_DEBUG */
static __inline__ void __sos_hlist_add_account(sos_hash_t *pHash)
{
sos_hlist_t *pList = pHash->pList;
// Compute the hash items counters
pHash->items++;
pHash->max = SOS_HLIST_MAX(pHash->max, pHash->items);
// Compute the list items counters
pList->items++;
pList->max = SOS_HLIST_MAX(pList->max, pList->items);
}
#endif /* ! SOS_HLIST_DEBUG */
/**
* @brief Adds an item at the head of a hash from a hash list
*
* @param pNode pointer to the node to add to the hash
* @param pHash pointer to the hash to add to
*
* @author [email protected]
*/
static __inline__ void __sos_hlist_add(sos_hhead_t *pNode, sos_hash_t *pHash)
{
register sos_hhead_t *pHead = &pHash->head;
// Insert the node
__sos_hlist_add_node(pNode, pHead, pHead->pNext);
// Set our hash pointer
pNode->pHash = pHash;
// Compute the hash items counters
__sos_hlist_add_account(pHash);
}
/**
* @brief Adds an item at the tail of a hash from a hash list
*
* @param pNode pointer to the node to add to the hash
* @param pHash pointer to the hash to add to
*
* @author [email protected]
*/
static __inline__ void __sos_hlist_add_tail(sos_hhead_t *pNode, sos_hash_t *pHash)
{
register sos_hhead_t *pHead = &pHash->head;
// Insert the node
__sos_hlist_add_node(pNode, pHead->pPrev, pHead);
// Set our hash pointer
pNode->pHash = pHash;
// Compute the hash items counters
__sos_hlist_add_account(pHash);
}
/**
* @brief Removes an item between 2 node in a hash list
*
* @param pNode pointer to the node to remove from the hash
*
* @author [email protected]
*/
static __inline__ void __sos_hlist_del_node(sos_hhead_t *pPrev, sos_hhead_t *pNext)
{
pNext->pPrev = pPrev;
pPrev->pNext = pNext;
}
/**
* @brief Accounts for items removed from a hash list under a given hash
*
* @param pHash pointer to the hash to do the accounting
*
* @author [email protected]
*/
#ifndef SOS_HLIST_DEBUG
#define __sos_hlist_del_account(_pHash) (void)(_pHash)
#else /* ! SOS_HLIST_DEBUG */
static __inline__ void __sos_hlist_del_account(sos_hash_t *pHash)
{
register sos_hlist_t *pList = pHash->pList;
// Do our hash and list accounting
pHash->items--;
pList->items--;
}
#endif /* ! SOS_HLIST_DEBUG */
/**
* @brief Removes an item in a hash list and re-init the node
*
* @param pNode pointer to the node to remove from the hash
*
* @author [email protected]
*/
static __inline__ void __sos_hlist_del_reinit(sos_hhead_t *pNode)
{
register sos_hash_t *pHash = NULL;
// Make sure this node is in a hash list
if (!sos_hlist_unhashed(pNode)) {
// Get our hash
pHash = pNode->pHash;
// Remove the node from the hash list
__sos_hlist_del_node(pNode->pPrev, pNode->pNext);
// Re-initialize the node
__sos_hlist_node_init(pNode);
// Do our hash and list accounting
__sos_hlist_del_account(pHash);
}
}
/**
* @brief Removes an item in a hash list and poison the entry.
*
* @param pNode pointer to the node to remove from the hash
*
* @author [email protected]
*/
static __inline__ void __sos_hlist_del(sos_hhead_t *pNode)
{
register sos_hash_t *pHash = NULL;
// Make sure this node is in a hash list
if (!sos_hlist_unhashed(pNode)) {
// Get our hash
pHash = pNode->pHash;
// Remove the node from the hash list
__sos_hlist_del_node(pNode->pPrev, pNode->pNext);
// Poison the node
pNode->pNext = SOS_HLIST_POISON1;
pNode->pPrev = SOS_HLIST_POISON2;
pNode->pHash = SOS_HLIST_POISON3;
// Do our hash and list accounting
__sos_hlist_del_account(pHash);
}
}
/**
* @brief Retrieve a hash from a hash list based on the given key information
*
* @param pList pointer to the hash list to get the hash from
* @param pKey pointer to the data/key to hash
* @param len the length of the key to hash
* @param bits the number of bits to use for the hash value
*
* @return a pointer to the hash
* @author [email protected]
*/
static __inline__ sos_hash_t * __sos_hlist_get_hash(sos_hlist_t *pList, const uint8_t *pKey, const size_t len, const uint8_t bits)
{
return (&pList->aHash[sos_hlist_hash(pKey, len, bits)]);
}
#ifdef __cplusplus
}
#endif
#endif /* _SOS_HLIST_H_ */