• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 14872145375

06 May 2025 09:07PM UTC coverage: 72.232% (+0.02%) from 72.214%
14872145375

push

github

DaanDeMeyer
string-table: annotate _to_string and _from_string with _const_ and _pure_, respectively

Follow-up for c94f6ab1b

297286 of 411572 relevant lines covered (72.23%)

695615.99 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.31
/src/basic/hashmap.h
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2
#pragma once
3

4
#include <limits.h>
5
#include <stdbool.h>
6
#include <stddef.h>
7

8
#include "hash-funcs.h"
9
#include "macro.h"
10

11
/*
12
 * A hash table implementation. As a minor optimization a NULL hashmap object
13
 * will be treated as empty hashmap for all read operations. That way it is not
14
 * necessary to instantiate an object for each Hashmap use.
15
 *
16
 * If ENABLE_DEBUG_HASHMAP is defined (by configuring with -Ddebug-extra=hashmap),
17
 * the implementation will:
18
 * - store extra data for debugging and statistics (see tools/gdb-sd_dump_hashmaps.py)
19
 * - perform extra checks for invalid use of iterators
20
 */
21

22
#define HASH_KEY_SIZE 16
23

24
typedef void* (*hashmap_destroy_t)(void *p);
25

26
/* The base type for all hashmap and set types. Many functions in the implementation take (HashmapBase*)
27
 * parameters and are run-time polymorphic, though the API is not meant to be polymorphic (do not call
28
 * underscore-prefixed functions directly). */
29
typedef struct HashmapBase HashmapBase;
30

31
/* Specific hashmap/set types */
32
typedef struct Hashmap Hashmap;               /* Maps keys to values */
33
typedef struct OrderedHashmap OrderedHashmap; /* Like Hashmap, but also remembers entry insertion order */
34
typedef struct Set Set;                       /* Stores just keys */
35

36
typedef struct IteratedCache IteratedCache;   /* Caches the iterated order of one of the above */
37

38
/* Ideally the Iterator would be an opaque struct, but it is instantiated
39
 * by hashmap users, so the definition has to be here. Do not use its fields
40
 * directly. */
41
typedef struct {
42
        const void *next_key; /* expected value of that entry's key pointer */
43
        unsigned idx;         /* index of an entry to be iterated next */
44
#if ENABLE_DEBUG_HASHMAP
45
        unsigned put_count;   /* hashmap's put_count recorded at start of iteration */
46
        unsigned rem_count;   /* hashmap's rem_count in previous iteration */
47
        unsigned prev_idx;    /* idx in previous iteration */
48
#endif
49
} Iterator;
50

51
#define _IDX_ITERATOR_FIRST (UINT_MAX - 1)
52
#define ITERATOR_FIRST ((Iterator) { .idx = _IDX_ITERATOR_FIRST, .next_key = NULL })
53
#define ITERATOR_IS_FIRST(i) ((i).idx == _IDX_ITERATOR_FIRST)
54

55
/* Macros for type checking */
56
#define PTR_COMPATIBLE_WITH_HASHMAP_BASE(h) \
57
        (__builtin_types_compatible_p(typeof(h), HashmapBase*) || \
58
         __builtin_types_compatible_p(typeof(h), Hashmap*) || \
59
         __builtin_types_compatible_p(typeof(h), OrderedHashmap*) || \
60
         __builtin_types_compatible_p(typeof(h), Set*))
61

62
#define PTR_COMPATIBLE_WITH_PLAIN_HASHMAP(h) \
63
        (__builtin_types_compatible_p(typeof(h), Hashmap*) || \
64
         __builtin_types_compatible_p(typeof(h), OrderedHashmap*)) \
65

66
#define HASHMAP_BASE(h) \
67
        __builtin_choose_expr(PTR_COMPATIBLE_WITH_HASHMAP_BASE(h), \
68
                (HashmapBase*)(h), \
69
                (void)0)
70

71
#define PLAIN_HASHMAP(h) \
72
        __builtin_choose_expr(PTR_COMPATIBLE_WITH_PLAIN_HASHMAP(h), \
73
                (Hashmap*)(h), \
74
                (void)0)
75

76
Hashmap* hashmap_new(const struct hash_ops *hash_ops);
77
OrderedHashmap* ordered_hashmap_new(const struct hash_ops *hash_ops);
78

79
#define hashmap_free_and_replace(a, b)                          \
80
        free_and_replace_full(a, b, hashmap_free)
81
#define ordered_hashmap_free_and_replace(a, b)                  \
82
        free_and_replace_full(a, b, ordered_hashmap_free)
83

84
HashmapBase* _hashmap_free(HashmapBase *h);
85
static inline Hashmap* hashmap_free(Hashmap *h) {
4,119,923✔
86
        return (void*) _hashmap_free(HASHMAP_BASE(h));
3,540,278✔
87
}
88
static inline OrderedHashmap* ordered_hashmap_free(OrderedHashmap *h) {
4,298,396✔
89
        return (void*) _hashmap_free(HASHMAP_BASE(h));
3,086,461✔
90
}
91

92
IteratedCache* iterated_cache_free(IteratedCache *cache);
93
int iterated_cache_get(IteratedCache *cache, const void ***res_keys, const void ***res_values, unsigned *res_n_entries);
94

95
HashmapBase* _hashmap_copy(HashmapBase *h);
96
static inline Hashmap* hashmap_copy(Hashmap *h) {
1✔
97
        return (Hashmap*) _hashmap_copy(HASHMAP_BASE(h));
1✔
98
}
99
static inline OrderedHashmap* ordered_hashmap_copy(OrderedHashmap *h) {
1✔
100
        return (OrderedHashmap*) _hashmap_copy(HASHMAP_BASE(h));
1✔
101
}
102

103
int hashmap_ensure_allocated(Hashmap **h, const struct hash_ops *hash_ops);
104
int hashmap_ensure_put(Hashmap **h, const struct hash_ops *hash_ops, const void *key, void *value);
105
int ordered_hashmap_ensure_allocated(OrderedHashmap **h, const struct hash_ops *hash_ops);
106
int hashmap_ensure_replace(Hashmap **h, const struct hash_ops *hash_ops, const void *key, void *value);
107

108
int ordered_hashmap_ensure_put(OrderedHashmap **h, const struct hash_ops *hash_ops, const void *key, void *value);
109
int ordered_hashmap_ensure_replace(OrderedHashmap **h, const struct hash_ops *hash_ops, const void *key, void *value);
110

111
IteratedCache* _hashmap_iterated_cache_new(HashmapBase *h);
112
static inline IteratedCache* hashmap_iterated_cache_new(Hashmap *h) {
1✔
113
        return (IteratedCache*) _hashmap_iterated_cache_new(HASHMAP_BASE(h));
1✔
114
}
115
static inline IteratedCache* ordered_hashmap_iterated_cache_new(OrderedHashmap *h) {
6,290✔
116
        return (IteratedCache*) _hashmap_iterated_cache_new(HASHMAP_BASE(h));
6,290✔
117
}
118

119
int hashmap_put(Hashmap *h, const void *key, void *value);
120
static inline int ordered_hashmap_put(OrderedHashmap *h, const void *key, void *value) {
7,961,248✔
121
        return hashmap_put(PLAIN_HASHMAP(h), key, value);
7,961,223✔
122
}
123

124
int hashmap_put_strdup_full(Hashmap **h, const struct hash_ops *hash_ops, const char *k, const char *v);
125
static inline int hashmap_put_strdup(Hashmap **h, const char *k, const char *v) {
17✔
126
        return hashmap_put_strdup_full(h, &string_hash_ops_free_free, k, v);
17✔
127
}
128

129
int hashmap_update(Hashmap *h, const void *key, void *value);
130
static inline int ordered_hashmap_update(OrderedHashmap *h, const void *key, void *value) {
4✔
131
        return hashmap_update(PLAIN_HASHMAP(h), key, value);
4✔
132
}
133

134
int hashmap_replace(Hashmap *h, const void *key, void *value);
135
static inline int ordered_hashmap_replace(OrderedHashmap *h, const void *key, void *value) {
12,248,845✔
136
        return hashmap_replace(PLAIN_HASHMAP(h), key, value);
12,248,845✔
137
}
138

139
void* _hashmap_get(HashmapBase *h, const void *key);
140
static inline void *hashmap_get(Hashmap *h, const void *key) {
28,359,860✔
141
        return _hashmap_get(HASHMAP_BASE(h), key);
27,779,254✔
142
}
143
static inline void *ordered_hashmap_get(OrderedHashmap *h, const void *key) {
29,595,451✔
144
        return _hashmap_get(HASHMAP_BASE(h), key);
29,595,451✔
145
}
146

147
void* hashmap_get2(Hashmap *h, const void *key, void **rkey);
148
static inline void *ordered_hashmap_get2(OrderedHashmap *h, const void *key, void **rkey) {
12,240,976✔
149
        return hashmap_get2(PLAIN_HASHMAP(h), key, rkey);
12,240,976✔
150
}
151

152
bool _hashmap_contains(HashmapBase *h, const void *key);
153
static inline bool hashmap_contains(Hashmap *h, const void *key) {
1,248,812✔
154
        return _hashmap_contains(HASHMAP_BASE(h), key);
1,248,812✔
155
}
156
static inline bool ordered_hashmap_contains(OrderedHashmap *h, const void *key) {
18,609✔
157
        return _hashmap_contains(HASHMAP_BASE(h), key);
18,609✔
158
}
159

160
void* _hashmap_remove(HashmapBase *h, const void *key);
161
static inline void *hashmap_remove(Hashmap *h, const void *key) {
6,859,826✔
162
        return _hashmap_remove(HASHMAP_BASE(h), key);
6,856,082✔
163
}
164
static inline void *ordered_hashmap_remove(OrderedHashmap *h, const void *key) {
64,458✔
165
        return _hashmap_remove(HASHMAP_BASE(h), key);
64,458✔
166
}
167

168
void* hashmap_remove2(Hashmap *h, const void *key, void **rkey);
169
static inline void *ordered_hashmap_remove2(OrderedHashmap *h, const void *key, void **rkey) {
566,879✔
170
        return hashmap_remove2(PLAIN_HASHMAP(h), key, rkey);
566,879✔
171
}
172

173
void* _hashmap_remove_value(HashmapBase *h, const void *key, void *value);
174
static inline void *hashmap_remove_value(Hashmap *h, const void *key, void *value) {
356,183✔
175
        return _hashmap_remove_value(HASHMAP_BASE(h), key, value);
356,093✔
176
}
177

178
static inline void* ordered_hashmap_remove_value(OrderedHashmap *h, const void *key, void *value) {
89,071✔
179
        return hashmap_remove_value(PLAIN_HASHMAP(h), key, value);
89,071✔
180
}
181

182
int hashmap_remove_and_put(Hashmap *h, const void *old_key, const void *new_key, void *value);
183
static inline int ordered_hashmap_remove_and_put(OrderedHashmap *h, const void *old_key, const void *new_key, void *value) {
4✔
184
        return hashmap_remove_and_put(PLAIN_HASHMAP(h), old_key, new_key, value);
4✔
185
}
186

187
int hashmap_remove_and_replace(Hashmap *h, const void *old_key, const void *new_key, void *value);
188
static inline int ordered_hashmap_remove_and_replace(OrderedHashmap *h, const void *old_key, const void *new_key, void *value) {
24✔
189
        return hashmap_remove_and_replace(PLAIN_HASHMAP(h), old_key, new_key, value);
24✔
190
}
191

192
/* Since merging data from an OrderedHashmap into a Hashmap or vice-versa
193
 * should just work, allow this by having looser type-checking here. */
194
int _hashmap_merge(Hashmap *h, Hashmap *other);
195
#define hashmap_merge(h, other) _hashmap_merge(PLAIN_HASHMAP(h), PLAIN_HASHMAP(other))
196
#define ordered_hashmap_merge(h, other) hashmap_merge(h, other)
197

198
int _hashmap_reserve(HashmapBase *h, unsigned entries_add);
199
static inline int hashmap_reserve(Hashmap *h, unsigned entries_add) {
16✔
200
        return _hashmap_reserve(HASHMAP_BASE(h), entries_add);
16✔
201
}
202
static inline int ordered_hashmap_reserve(OrderedHashmap *h, unsigned entries_add) {
1,482✔
203
        return _hashmap_reserve(HASHMAP_BASE(h), entries_add);
1,482✔
204
}
205

206
int _hashmap_move(HashmapBase *h, HashmapBase *other);
207
/* Unlike hashmap_merge, hashmap_move does not allow mixing the types. */
208
static inline int hashmap_move(Hashmap *h, Hashmap *other) {
2,485✔
209
        return _hashmap_move(HASHMAP_BASE(h), HASHMAP_BASE(other));
2,485✔
210
}
211
static inline int ordered_hashmap_move(OrderedHashmap *h, OrderedHashmap *other) {
2✔
212
        return _hashmap_move(HASHMAP_BASE(h), HASHMAP_BASE(other));
2✔
213
}
214

215
int _hashmap_move_one(HashmapBase *h, HashmapBase *other, const void *key);
216
static inline int hashmap_move_one(Hashmap *h, Hashmap *other, const void *key) {
5✔
217
        return _hashmap_move_one(HASHMAP_BASE(h), HASHMAP_BASE(other), key);
5✔
218
}
219
static inline int ordered_hashmap_move_one(OrderedHashmap *h, OrderedHashmap *other, const void *key) {
564✔
220
        return _hashmap_move_one(HASHMAP_BASE(h), HASHMAP_BASE(other), key);
564✔
221
}
222

223
unsigned _hashmap_size(HashmapBase *h) _pure_;
224
static inline unsigned hashmap_size(Hashmap *h) {
1,401,169✔
225
        return _hashmap_size(HASHMAP_BASE(h));
1,108,640✔
226
}
227
static inline unsigned ordered_hashmap_size(OrderedHashmap *h) {
9,113,135✔
228
        return _hashmap_size(HASHMAP_BASE(h));
9,020,164✔
229
}
230

231
static inline bool hashmap_isempty(Hashmap *h) {
927,505✔
232
        return hashmap_size(h) == 0;
861,772✔
233
}
234
static inline bool ordered_hashmap_isempty(OrderedHashmap *h) {
1,287,240✔
235
        return ordered_hashmap_size(h) == 0;
1,287,188✔
236
}
237

238
unsigned _hashmap_buckets(HashmapBase *h) _pure_;
239
static inline unsigned hashmap_buckets(Hashmap *h) {
2,521✔
240
        return _hashmap_buckets(HASHMAP_BASE(h));
2,521✔
241
}
242
static inline unsigned ordered_hashmap_buckets(OrderedHashmap *h) {
8✔
243
        return _hashmap_buckets(HASHMAP_BASE(h));
8✔
244
}
245

246
bool _hashmap_iterate(HashmapBase *h, Iterator *i, void **value, const void **key);
247
static inline bool hashmap_iterate(Hashmap *h, Iterator *i, void **value, const void **key) {
23,753,847✔
248
        return _hashmap_iterate(HASHMAP_BASE(h), i, value, key);
23,753,847✔
249
}
250
static inline bool ordered_hashmap_iterate(OrderedHashmap *h, Iterator *i, void **value, const void **key) {
8,865,404✔
251
        return _hashmap_iterate(HASHMAP_BASE(h), i, value, key);
8,865,404✔
252
}
253

254
void _hashmap_clear(HashmapBase *h);
255
static inline void hashmap_clear(Hashmap *h) {
112,257✔
256
        _hashmap_clear(HASHMAP_BASE(h));
112,257✔
257
}
92,932✔
258
static inline void ordered_hashmap_clear(OrderedHashmap *h) {
17,416✔
259
        _hashmap_clear(HASHMAP_BASE(h));
17,416✔
260
}
24✔
261

262
/*
263
 * Note about all *_first*() functions
264
 *
265
 * For plain Hashmaps and Sets the order of entries is undefined.
266
 * The functions find whatever entry is first in the implementation
267
 * internal order.
268
 *
269
 * Only for OrderedHashmaps the order is well defined and finding
270
 * the first entry is O(1).
271
 */
272

273
void *_hashmap_first_key_and_value(HashmapBase *h, bool remove, void **ret_key);
274
static inline void *hashmap_steal_first_key_and_value(Hashmap *h, void **ret) {
43,384✔
275
        return _hashmap_first_key_and_value(HASHMAP_BASE(h), true, ret);
43,384✔
276
}
277
static inline void *ordered_hashmap_steal_first_key_and_value(OrderedHashmap *h, void **ret) {
×
278
        return _hashmap_first_key_and_value(HASHMAP_BASE(h), true, ret);
×
279
}
280
static inline void *hashmap_first_key_and_value(Hashmap *h, void **ret) {
281
        return _hashmap_first_key_and_value(HASHMAP_BASE(h), false, ret);
282
}
283
static inline void *ordered_hashmap_first_key_and_value(OrderedHashmap *h, void **ret) {
284
        return _hashmap_first_key_and_value(HASHMAP_BASE(h), false, ret);
285
}
286

287
static inline void *hashmap_steal_first(Hashmap *h) {
391,747✔
288
        return _hashmap_first_key_and_value(HASHMAP_BASE(h), true, NULL);
391,747✔
289
}
290
static inline void *ordered_hashmap_steal_first(OrderedHashmap *h) {
7,608,738✔
291
        return _hashmap_first_key_and_value(HASHMAP_BASE(h), true, NULL);
7,608,738✔
292
}
293
static inline void *hashmap_first(Hashmap *h) {
51,720✔
294
        return _hashmap_first_key_and_value(HASHMAP_BASE(h), false, NULL);
51,720✔
295
}
296
static inline void *ordered_hashmap_first(OrderedHashmap *h) {
238,031✔
297
        return _hashmap_first_key_and_value(HASHMAP_BASE(h), false, NULL);
238,031✔
298
}
299

300
static inline void *_hashmap_first_key(HashmapBase *h, bool remove) {
910,658✔
301
        void *key = NULL;
910,658✔
302

303
        (void) _hashmap_first_key_and_value(HASHMAP_BASE(h), remove, &key);
910,658✔
304
        return key;
910,658✔
305
}
306
static inline void *hashmap_steal_first_key(Hashmap *h) {
897,740✔
307
        return _hashmap_first_key(HASHMAP_BASE(h), true);
897,740✔
308
}
309
static inline void *ordered_hashmap_steal_first_key(OrderedHashmap *h) {
2✔
310
        return _hashmap_first_key(HASHMAP_BASE(h), true);
2✔
311
}
312
static inline void *hashmap_first_key(Hashmap *h) {
12,532✔
313
        return _hashmap_first_key(HASHMAP_BASE(h), false);
12,532✔
314
}
315
static inline void *ordered_hashmap_first_key(OrderedHashmap *h) {
384✔
316
        return _hashmap_first_key(HASHMAP_BASE(h), false);
384✔
317
}
318

319
/* no hashmap_next */
320
void* ordered_hashmap_next(OrderedHashmap *h, const void *key);
321

322
char** _hashmap_get_strv(HashmapBase *h);
323
static inline char** hashmap_get_strv(Hashmap *h) {
1✔
324
        return _hashmap_get_strv(HASHMAP_BASE(h));
1✔
325
}
326
static inline char** ordered_hashmap_get_strv(OrderedHashmap *h) {
1✔
327
        return _hashmap_get_strv(HASHMAP_BASE(h));
1✔
328
}
329

330
int _hashmap_dump_sorted(HashmapBase *h, void ***ret, size_t *ret_n);
331
static inline int hashmap_dump_sorted(Hashmap *h, void ***ret, size_t *ret_n) {
107,694✔
332
        return _hashmap_dump_sorted(HASHMAP_BASE(h), ret, ret_n);
107,694✔
333
}
334
static inline int ordered_hashmap_dump_sorted(OrderedHashmap *h, void ***ret, size_t *ret_n) {
4✔
335
        return _hashmap_dump_sorted(HASHMAP_BASE(h), ret, ret_n);
4✔
336
}
337
static inline int set_dump_sorted(Set *h, void ***ret, size_t *ret_n) {
445✔
338
        return _hashmap_dump_sorted(HASHMAP_BASE(h), ret, ret_n);
445✔
339
}
340

341
int _hashmap_dump_keys_sorted(HashmapBase *h, void ***ret, size_t *ret_n);
342
static inline int hashmap_dump_keys_sorted(Hashmap *h, void ***ret, size_t *ret_n) {
87✔
343
        return _hashmap_dump_keys_sorted(HASHMAP_BASE(h), ret, ret_n);
87✔
344
}
345
static inline int ordered_hashmap_dump_keys_sorted(OrderedHashmap *h, void ***ret, size_t *ret_n) {
2✔
346
        return _hashmap_dump_keys_sorted(HASHMAP_BASE(h), ret, ret_n);
2✔
347
}
348

349
/*
350
 * Hashmaps are iterated in unpredictable order.
351
 * OrderedHashmaps are an exception to this. They are iterated in the order
352
 * the entries were inserted.
353
 * It is safe to remove the current entry.
354
 */
355
#define _HASHMAP_BASE_FOREACH(e, h, i) \
356
        for (Iterator i = ITERATOR_FIRST; _hashmap_iterate((h), &i, (void**)&(e), NULL); )
357
#define HASHMAP_BASE_FOREACH(e, h) \
358
        _HASHMAP_BASE_FOREACH(e, h, UNIQ_T(i, UNIQ))
359

360
#define _HASHMAP_FOREACH(e, h, i) \
361
        for (Iterator i = ITERATOR_FIRST; hashmap_iterate((h), &i, (void**)&(e), NULL); )
362
#define HASHMAP_FOREACH(e, h) \
363
        _HASHMAP_FOREACH(e, h, UNIQ_T(i, UNIQ))
364

365
#define _ORDERED_HASHMAP_FOREACH(e, h, i) \
366
        for (Iterator i = ITERATOR_FIRST; ordered_hashmap_iterate((h), &i, (void**)&(e), NULL); )
367
#define ORDERED_HASHMAP_FOREACH(e, h) \
368
        _ORDERED_HASHMAP_FOREACH(e, h, UNIQ_T(i, UNIQ))
369

370
#define _HASHMAP_BASE_FOREACH_KEY(e, k, h, i) \
371
        for (Iterator i = ITERATOR_FIRST; _hashmap_iterate((h), &i, (void**)&(e), (const void**) &(k)); )
372
#define HASHMAP_BASE_FOREACH_KEY(e, k, h) \
373
        _HASHMAP_BASE_FOREACH_KEY(e, k, h, UNIQ_T(i, UNIQ))
374

375
#define _HASHMAP_FOREACH_KEY(e, k, h, i) \
376
        for (Iterator i = ITERATOR_FIRST; hashmap_iterate((h), &i, (void**)&(e), (const void**) &(k)); )
377
#define HASHMAP_FOREACH_KEY(e, k, h) \
378
        _HASHMAP_FOREACH_KEY(e, k, h, UNIQ_T(i, UNIQ))
379

380
#define _ORDERED_HASHMAP_FOREACH_KEY(e, k, h, i) \
381
        for (Iterator i = ITERATOR_FIRST; ordered_hashmap_iterate((h), &i, (void**)&(e), (const void**) &(k)); )
382
#define ORDERED_HASHMAP_FOREACH_KEY(e, k, h) \
383
        _ORDERED_HASHMAP_FOREACH_KEY(e, k, h, UNIQ_T(i, UNIQ))
384

385
DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap*, hashmap_free);
744,329✔
386
DEFINE_TRIVIAL_CLEANUP_FUNC(OrderedHashmap*, ordered_hashmap_free);
57,939✔
387

388
#define _cleanup_hashmap_free_ _cleanup_(hashmap_freep)
389
#define _cleanup_ordered_hashmap_free_ _cleanup_(ordered_hashmap_freep)
390

391
DEFINE_TRIVIAL_CLEANUP_FUNC(IteratedCache*, iterated_cache_free);
392

393
#define _cleanup_iterated_cache_free_ _cleanup_(iterated_cache_freep)
394

395
void hashmap_trim_pools(void);
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc