dictionary improvements (#13052)
* fix typo in foreach write; added unit tests to traverse empty dictionaries * rename variable dfe in macro to be uniform with name variable
Costa Tsaousis committed
Jun 2, 2022 at 08:55 UTC
04b1c9e0dadbea871ca51342be8e71265c40034d
2 files changed
+7
-5
libnetdata/dictionary/dictionary.c
+2
@@ -1253,6 +1253,8 @@ int dictionary_unittest(size_t entries) {
1253
dict = dictionary_create(DICTIONARY_FLAG_WITH_STATISTICS|DICTIONARY_FLAG_NAME_LINK_DONT_CLONE|DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE|DICTIONARY_FLAG_DONT_OVERWRITE_VALUE);
1254
dictionary_unittest_run_and_measure_time(dict, "adding entries", names, values, entries, &errors, dictionary_unittest_set_nonclone);
1255
dictionary_unittest_run_and_measure_time(dict, "foreach write delete this", names, values, entries, &errors, dictionary_unittest_foreach_delete_this);
1256
+ dictionary_unittest_run_and_measure_time(dict, "traverse foreach read loop empty", names, values, 0, &errors, dictionary_unittest_foreach);
1257
+ dictionary_unittest_run_and_measure_time(dict, "walkthrough read callback empty", names, values, 0, &errors, dictionary_unittest_walkthrough);
1258
dictionary_unittest_run_and_measure_time(dict, "destroying empty dictionary", names, values, entries, &errors, dictionary_unittest_destroy);
1259
1260
dictionary_unittest_free_char_pp(names, entries);
libnetdata/dictionary/dictionary.h
+5
-5
@@ -154,17 +154,17 @@ typedef DICTFE_CONST struct dictionary_foreach {
154
} DICTFE;
155
156
#define dfe_start_read(dict, value) dfe_start_rw(dict, value, 'r')
157
-#define dfe_start_write(dict, value) dfe_start_rw(dict, value, 'r')
157
+#define dfe_start_write(dict, value) dfe_start_rw(dict, value, 'w')
158
#define dfe_start_rw(dict, value, mode) \
159
do { \
160
- DICTFE dfe_ ## value = {}; \
160
+ DICTFE value ## _dfe = {}; \
161
const char *value ## _name; (void)(value ## _name); \
162
- for((value) = dictionary_foreach_start_rw(&dfe_ ## value, (dict), (mode)), ( value ## _name ) = dfe_ ## value.name; \
162
+ for((value) = dictionary_foreach_start_rw(&value ## _dfe, (dict), (mode)), ( value ## _name ) = value ## _dfe.name; \
163
(value) ;\
164
- (value) = dictionary_foreach_next(&dfe_ ## value), ( value ## _name ) = dfe_ ## value.name)
164
+ (value) = dictionary_foreach_next(&value ## _dfe), ( value ## _name ) = value ## _dfe.name)
165
166
#define dfe_done(value) \
167
- dictionary_foreach_done(&dfe_ ## value); \
167
+ dictionary_foreach_done(&value ## _dfe); \
168
} while(0)
169
170
extern void * dictionary_foreach_start_rw(DICTFE *dfe, DICTIONARY *dict, char rw);