@@ -80,72 +80,92 @@ typedef unsigned long(lh_hash_fn)(const void *k);
80
80
typedef int (lh_equal_fn )(const void * k1 , const void * k2 );
81
81
82
82
/**
83
- * An entry in the hash table
83
+ * An entry in the hash table. Outside of linkhash.c, treat this as opaque.
84
84
*/
85
85
struct lh_entry
86
86
{
87
87
/**
88
- * The key. Use lh_entry_k() instead of accessing this directly.
88
+ * The key.
89
+ * @deprecated Use lh_entry_k() instead of accessing this directly.
89
90
*/
90
91
const void * k ;
91
92
/**
92
93
* A flag for users of linkhash to know whether or not they
93
94
* need to free k.
95
+ * @deprecated use lh_entry_k_is_constant() instead.
94
96
*/
95
97
int k_is_constant ;
96
98
/**
97
- * The value. Use lh_entry_v() instead of accessing this directly.
99
+ * The value.
100
+ * @deprecated Use lh_entry_v() instead of accessing this directly.
98
101
*/
99
102
const void * v ;
100
103
/**
101
- * The next entry
104
+ * The next entry.
105
+ * @deprecated Use lh_entry_next() instead of accessing this directly.
102
106
*/
103
107
struct lh_entry * next ;
104
108
/**
105
109
* The previous entry.
110
+ * @deprecated Use lh_entry_prev() instead of accessing this directly.
106
111
*/
107
112
struct lh_entry * prev ;
108
113
};
109
114
110
115
/**
111
- * The hash table structure.
116
+ * The hash table structure. Outside of linkhash.c, treat this as opaque.
112
117
*/
113
118
struct lh_table
114
119
{
115
120
/**
116
121
* Size of our hash.
122
+ * @deprecated do not use outside of linkhash.c
117
123
*/
118
124
int size ;
119
125
/**
120
126
* Numbers of entries.
127
+ * @deprecated Use lh_table_length() instead.
121
128
*/
122
129
int count ;
123
130
124
131
/**
125
132
* The first entry.
133
+ * @deprecated Use lh_table_head() instead.
126
134
*/
127
135
struct lh_entry * head ;
128
136
129
137
/**
130
138
* The last entry.
139
+ * @deprecated Do not use, may be removed in a future release.
131
140
*/
132
141
struct lh_entry * tail ;
133
142
143
+ /**
144
+ * Internal storage of the actual table of entries.
145
+ * @deprecated do not use outside of linkhash.c
146
+ */
134
147
struct lh_entry * table ;
135
148
136
149
/**
137
- * A pointer onto the function responsible for freeing an entry.
150
+ * A pointer to the function responsible for freeing an entry.
151
+ * @deprecated do not use outside of linkhash.c
138
152
*/
139
153
lh_entry_free_fn * free_fn ;
154
+ /**
155
+ * @deprecated do not use outside of linkhash.c
156
+ */
140
157
lh_hash_fn * hash_fn ;
158
+ /**
159
+ * @deprecated do not use outside of linkhash.c
160
+ */
141
161
lh_equal_fn * equal_fn ;
142
162
};
143
163
typedef struct lh_table lh_table ;
144
164
145
165
/**
146
166
* Convenience list iterator.
147
167
*/
148
- #define lh_foreach (table , entry ) for (entry = table->head ; entry; entry = entry->next )
168
+ #define lh_foreach (table , entry ) for (entry = lh_table_head( table) ; entry; entry = lh_entry_next( entry) )
149
169
150
170
/**
151
171
* lh_foreach_safe allows calling of deletion routine while iterating.
@@ -155,7 +175,7 @@ typedef struct lh_table lh_table;
155
175
* @param tmp a struct lh_entry * variable to hold a temporary pointer to the next element
156
176
*/
157
177
#define lh_foreach_safe (table , entry , tmp ) \
158
- for (entry = table->head ; entry && ((tmp = entry->next ) || 1); entry = tmp)
178
+ for (entry = lh_table_head( table) ; entry && ((tmp = lh_entry_next( entry) ) || 1); entry = tmp)
159
179
160
180
/**
161
181
* Create a new linkhash table.
@@ -295,6 +315,9 @@ extern int lh_table_delete_entry(struct lh_table *t, struct lh_entry *e);
295
315
*/
296
316
extern int lh_table_delete (struct lh_table * t , const void * k );
297
317
318
+ /**
319
+ * Return the number of entries in the table.
320
+ */
298
321
extern int lh_table_length (struct lh_table * t );
299
322
300
323
/**
@@ -318,6 +341,15 @@ int lh_table_resize(struct lh_table *t, int new_size);
318
341
#define _LH_INLINE inline
319
342
#endif
320
343
344
+ /**
345
+ * Return the first entry in the lh_table.
346
+ * @see lh_entry_next()
347
+ */
348
+ static _LH_INLINE struct lh_entry * lh_table_head (const lh_table * t )
349
+ {
350
+ return t -> head ;
351
+ }
352
+
321
353
/**
322
354
* Calculate the hash of a key for a given table.
323
355
*
@@ -334,7 +366,6 @@ static _LH_INLINE unsigned long lh_get_hash(const struct lh_table *t, const void
334
366
return t -> hash_fn (k );
335
367
}
336
368
337
- #undef _LH_INLINE
338
369
339
370
/**
340
371
* @deprecated Don't use this outside of linkhash.h:
@@ -350,17 +381,64 @@ static _LH_INLINE unsigned long lh_get_hash(const struct lh_table *t, const void
350
381
*
351
382
* lh_entry.k is const to indicate and help ensure that linkhash itself doesn't modify
352
383
* it, but callers are allowed to do what they want with it.
353
- * See also lh_entry.k_is_constant
384
+ * @see lh_entry_k_is_constant()
385
+ */
386
+ static _LH_INLINE void * lh_entry_k (const struct lh_entry * e )
387
+ {
388
+ return _LH_UNCONST (e -> k );
389
+ }
390
+
391
+ /**
392
+ * Returns 1 if the key for the given entry is constant, and thus
393
+ * does not need to be freed when the lh_entry is freed.
394
+ * @see lh_table_insert_w_hash()
354
395
*/
355
- #define lh_entry_k (entry ) _LH_UNCONST((entry)->k)
396
+ static _LH_INLINE int lh_entry_k_is_constant (const struct lh_entry * e )
397
+ {
398
+ return e -> k_is_constant ;
399
+ }
356
400
357
401
/**
358
402
* Return a non-const version of lh_entry.v.
359
403
*
360
404
* v is const to indicate and help ensure that linkhash itself doesn't modify
361
405
* it, but callers are allowed to do what they want with it.
362
406
*/
363
- #define lh_entry_v (entry ) _LH_UNCONST((entry)->v)
407
+ static _LH_INLINE void * lh_entry_v (const struct lh_entry * e )
408
+ {
409
+ return _LH_UNCONST (e -> v );
410
+ }
411
+
412
+ /**
413
+ * Change the value for an entry. The caller is responsible for freeing
414
+ * the previous value.
415
+ */
416
+ static _LH_INLINE void lh_entry_set_val (struct lh_entry * e , void * newval )
417
+ {
418
+ e -> v = newval ;
419
+ }
420
+
421
+ /**
422
+ * Return the next element, or NULL if there is no next element.
423
+ * @see lh_table_head()
424
+ * @see lh_entry_prev()
425
+ */
426
+ static _LH_INLINE struct lh_entry * lh_entry_next (const struct lh_entry * e )
427
+ {
428
+ return e -> next ;
429
+ }
430
+
431
+ /**
432
+ * Return the previous element, or NULL if there is no previous element.
433
+ * @see lh_table_head()
434
+ * @see lh_entry_next()
435
+ */
436
+ static _LH_INLINE struct lh_entry * lh_entry_prev (const struct lh_entry * e )
437
+ {
438
+ return e -> prev ;
439
+ }
440
+
441
+ #undef _LH_INLINE
364
442
365
443
#ifdef __cplusplus
366
444
}
0 commit comments