Skip to content

Commit 2d2382d

Browse files
committed
Add linkhash accessor functions (lh_table_head(), lh_entry_next(), etc...) to pave the way for making the lh_table and lh_entry structure opaque in the future.
Update the docs to mark all members of those structures deprecated, and suggest what to use instead.
1 parent 320548c commit 2d2382d

File tree

6 files changed

+107
-25
lines changed

6 files changed

+107
-25
lines changed

ChangeLog

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ Next Release 0.16
44

55
Significant changes and bug fixes
66
---------------------------------
7-
* Introduction of constant JSON_C_OBJECT_ADD_CONSTANT_KEY
7+
* Introduce constant JSON_C_OBJECT_ADD_CONSTANT_KEY
88
as a replacement of JSON_C_OBJECT_KEY_IS_CONSTANT,
99
JSON_C_OBJECT_KEY_IS_CONSTANT becoming legacy.
10+
* Direct access to lh_table and lh_entry structure members is deprecated.
11+
Use access functions instead, lh_table_head(), lh_entry_next(), etc...
1012
* Drop REFCOUNT_DEBUG code.
1113

1214
***

doc/Doxyfile.in

+4-2
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ EXTRACT_PACKAGE = NO
427427
# included in the documentation.
428428
# The default value is: NO.
429429

430-
EXTRACT_STATIC = NO
430+
EXTRACT_STATIC = YES
431431

432432
# If the EXTRACT_LOCAL_CLASSES tag is set to YES classes (and structs) defined
433433
# locally in source files will be included in the documentation. If set to NO
@@ -1984,7 +1984,9 @@ INCLUDE_FILE_PATTERNS =
19841984
# recursively expanded use the := operator instead of the = operator.
19851985
# This tag requires that the tag ENABLE_PREPROCESSING is set to YES.
19861986

1987-
PREDEFINED = THIS_FUNCTION_IS_DEPRECATED(f)=f
1987+
PREDEFINED = \
1988+
_LH_INLINE=inline \
1989+
JSON_C_CONST_FUNCTION(func)=func
19881990

19891991
# If the MACRO_EXPANSION and EXPAND_ONLY_PREDEF tags are set to YES then this
19901992
# tag can be used to specify a list of macro names that should be expanded. The

json_object.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ static int json_object_object_to_json_string(struct json_object *jso, struct pri
498498

499499
static void json_object_lh_entry_free(struct lh_entry *ent)
500500
{
501-
if (!ent->k_is_constant)
501+
if (!lh_entry_k_is_constant(ent))
502502
free(lh_entry_k(ent));
503503
json_object_put((struct json_object *)lh_entry_v(ent));
504504
}
@@ -569,7 +569,7 @@ int json_object_object_add_ex(struct json_object *jso, const char *const key,
569569
existing_value = (json_object *)lh_entry_v(existing_entry);
570570
if (existing_value)
571571
json_object_put(existing_value);
572-
existing_entry->v = val;
572+
lh_entry_set_val(existing_entry, val);
573573
return 0;
574574
}
575575

json_object.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -483,14 +483,14 @@ JSON_EXPORT void json_object_object_del(struct json_object *obj, const char *key
483483
#define json_object_object_foreach(obj, key, val) \
484484
char *key = NULL; \
485485
struct json_object *val __attribute__((__unused__)) = NULL; \
486-
for (struct lh_entry *entry##key = json_object_get_object(obj)->head, \
486+
for (struct lh_entry *entry##key = lh_table_head(json_object_get_object(obj)), \
487487
*entry_next##key = NULL; \
488488
({ \
489489
if (entry##key) \
490490
{ \
491491
key = (char *)lh_entry_k(entry##key); \
492492
val = (struct json_object *)lh_entry_v(entry##key); \
493-
entry_next##key = entry##key->next; \
493+
entry_next##key = lh_entry_next(entry##key); \
494494
}; \
495495
entry##key; \
496496
}); \
@@ -503,10 +503,10 @@ JSON_EXPORT void json_object_object_del(struct json_object *obj, const char *key
503503
struct json_object *val = NULL; \
504504
struct lh_entry *entry##key; \
505505
struct lh_entry *entry_next##key = NULL; \
506-
for (entry##key = json_object_get_object(obj)->head; \
506+
for (entry##key = lh_table_head(json_object_get_object(obj)); \
507507
(entry##key ? (key = (char *)lh_entry_k(entry##key), \
508508
val = (struct json_object *)lh_entry_v(entry##key), \
509-
entry_next##key = entry##key->next, entry##key) \
509+
entry_next##key = lh_entry_next(entry##key), entry##key) \
510510
: 0); \
511511
entry##key = entry_next##key)
512512

@@ -517,11 +517,11 @@ JSON_EXPORT void json_object_object_del(struct json_object *obj, const char *key
517517
* @param iter the object iterator, use type json_object_iter
518518
*/
519519
#define json_object_object_foreachC(obj, iter) \
520-
for (iter.entry = json_object_get_object(obj)->head; \
520+
for (iter.entry = lh_table_head(json_object_get_object(obj)); \
521521
(iter.entry ? (iter.key = (char *)lh_entry_k(iter.entry), \
522522
iter.val = (struct json_object *)lh_entry_v(iter.entry), iter.entry) \
523523
: 0); \
524-
iter.entry = iter.entry->next)
524+
iter.entry = lh_entry_next(iter.entry))
525525

526526
/* Array type methods */
527527

json_object_iterator.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ struct json_object_iterator json_object_iter_begin(struct json_object *obj)
7171

7272
/// @note For a pair-less Object, head is NULL, which matches our
7373
/// definition of the "end" iterator
74-
iter.opaque_ = pTable->head;
74+
iter.opaque_ = lh_table_head(pTable);
7575
return iter;
7676
}
7777

@@ -98,7 +98,7 @@ void json_object_iter_next(struct json_object_iterator *iter)
9898
JASSERT(NULL != iter);
9999
JASSERT(kObjectEndIterValue != iter->opaque_);
100100

101-
iter->opaque_ = ((const struct lh_entry *)iter->opaque_)->next;
101+
iter->opaque_ = lh_entry_next(((const struct lh_entry *)iter->opaque_));
102102
}
103103

104104
/**

linkhash.h

+90-12
Original file line numberDiff line numberDiff line change
@@ -80,72 +80,92 @@ typedef unsigned long(lh_hash_fn)(const void *k);
8080
typedef int(lh_equal_fn)(const void *k1, const void *k2);
8181

8282
/**
83-
* An entry in the hash table
83+
* An entry in the hash table. Outside of linkhash.c, treat this as opaque.
8484
*/
8585
struct lh_entry
8686
{
8787
/**
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.
8990
*/
9091
const void *k;
9192
/**
9293
* A flag for users of linkhash to know whether or not they
9394
* need to free k.
95+
* @deprecated use lh_entry_k_is_constant() instead.
9496
*/
9597
int k_is_constant;
9698
/**
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.
98101
*/
99102
const void *v;
100103
/**
101-
* The next entry
104+
* The next entry.
105+
* @deprecated Use lh_entry_next() instead of accessing this directly.
102106
*/
103107
struct lh_entry *next;
104108
/**
105109
* The previous entry.
110+
* @deprecated Use lh_entry_prev() instead of accessing this directly.
106111
*/
107112
struct lh_entry *prev;
108113
};
109114

110115
/**
111-
* The hash table structure.
116+
* The hash table structure. Outside of linkhash.c, treat this as opaque.
112117
*/
113118
struct lh_table
114119
{
115120
/**
116121
* Size of our hash.
122+
* @deprecated do not use outside of linkhash.c
117123
*/
118124
int size;
119125
/**
120126
* Numbers of entries.
127+
* @deprecated Use lh_table_length() instead.
121128
*/
122129
int count;
123130

124131
/**
125132
* The first entry.
133+
* @deprecated Use lh_table_head() instead.
126134
*/
127135
struct lh_entry *head;
128136

129137
/**
130138
* The last entry.
139+
* @deprecated Do not use, may be removed in a future release.
131140
*/
132141
struct lh_entry *tail;
133142

143+
/**
144+
* Internal storage of the actual table of entries.
145+
* @deprecated do not use outside of linkhash.c
146+
*/
134147
struct lh_entry *table;
135148

136149
/**
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
138152
*/
139153
lh_entry_free_fn *free_fn;
154+
/**
155+
* @deprecated do not use outside of linkhash.c
156+
*/
140157
lh_hash_fn *hash_fn;
158+
/**
159+
* @deprecated do not use outside of linkhash.c
160+
*/
141161
lh_equal_fn *equal_fn;
142162
};
143163
typedef struct lh_table lh_table;
144164

145165
/**
146166
* Convenience list iterator.
147167
*/
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))
149169

150170
/**
151171
* lh_foreach_safe allows calling of deletion routine while iterating.
@@ -155,7 +175,7 @@ typedef struct lh_table lh_table;
155175
* @param tmp a struct lh_entry * variable to hold a temporary pointer to the next element
156176
*/
157177
#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)
159179

160180
/**
161181
* Create a new linkhash table.
@@ -295,6 +315,9 @@ extern int lh_table_delete_entry(struct lh_table *t, struct lh_entry *e);
295315
*/
296316
extern int lh_table_delete(struct lh_table *t, const void *k);
297317

318+
/**
319+
* Return the number of entries in the table.
320+
*/
298321
extern int lh_table_length(struct lh_table *t);
299322

300323
/**
@@ -318,6 +341,15 @@ int lh_table_resize(struct lh_table *t, int new_size);
318341
#define _LH_INLINE inline
319342
#endif
320343

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+
321353
/**
322354
* Calculate the hash of a key for a given table.
323355
*
@@ -334,7 +366,6 @@ static _LH_INLINE unsigned long lh_get_hash(const struct lh_table *t, const void
334366
return t->hash_fn(k);
335367
}
336368

337-
#undef _LH_INLINE
338369

339370
/**
340371
* @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
350381
*
351382
* lh_entry.k is const to indicate and help ensure that linkhash itself doesn't modify
352383
* 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()
354395
*/
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+
}
356400

357401
/**
358402
* Return a non-const version of lh_entry.v.
359403
*
360404
* v is const to indicate and help ensure that linkhash itself doesn't modify
361405
* it, but callers are allowed to do what they want with it.
362406
*/
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
364442

365443
#ifdef __cplusplus
366444
}

0 commit comments

Comments
 (0)