Skip to content

Commit c96a5d9

Browse files
authored
misc: hash_ops related cleanups (systemd#37117)
2 parents 6858c1f + 4100e0f commit c96a5d9

File tree

7 files changed

+47
-38
lines changed

7 files changed

+47
-38
lines changed

src/analyze/analyze-verify-util.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ static void log_syntax_callback(const char *unit, int level, void *userdata) {
3131

3232
r = set_put_strdup(s, unit);
3333
if (r < 0) {
34-
set_free_free(*s);
34+
set_free(*s);
3535
*s = POINTER_MAX;
3636
}
3737
}
@@ -265,7 +265,7 @@ static int verify_unit(Unit *u, bool check_man, const char *root) {
265265
static void set_destroy_ignore_pointer_max(Set **s) {
266266
if (*s == POINTER_MAX)
267267
return;
268-
set_free_free(*s);
268+
set_free(*s);
269269
}
270270

271271
int verify_units(

src/coredump/coredump-vacuum.c

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,10 @@ static VacuumCandidate* vacuum_candidate_free(VacuumCandidate *c) {
3838
}
3939
DEFINE_TRIVIAL_CLEANUP_FUNC(VacuumCandidate*, vacuum_candidate_free);
4040

41-
static Hashmap* vacuum_candidate_hashmap_free(Hashmap *h) {
42-
return hashmap_free_with_destructor(h, vacuum_candidate_free);
43-
}
44-
45-
DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap*, vacuum_candidate_hashmap_free);
41+
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
42+
vacuum_candidate_hash_ops,
43+
void, trivial_hash_func, trivial_compare_func,
44+
VacuumCandidate, vacuum_candidate_free);
4645

4746
static int uid_from_file_name(const char *filename, uid_t *uid) {
4847
const char *p, *e, *u;
@@ -141,7 +140,7 @@ int coredump_vacuum(int exclude_fd, uint64_t keep_free, uint64_t max_use) {
141140
}
142141

143142
for (;;) {
144-
_cleanup_(vacuum_candidate_hashmap_freep) Hashmap *h = NULL;
143+
_cleanup_hashmap_free_ Hashmap *h = NULL;
145144
VacuumCandidate *worst = NULL;
146145
uint64_t sum = 0;
147146

@@ -171,10 +170,6 @@ int coredump_vacuum(int exclude_fd, uint64_t keep_free, uint64_t max_use) {
171170
if (exclude_fd >= 0 && stat_inode_same(&exclude_st, &st))
172171
continue;
173172

174-
r = hashmap_ensure_allocated(&h, NULL);
175-
if (r < 0)
176-
return log_oom();
177-
178173
t = timespec_load(&st.st_mtim);
179174

180175
c = hashmap_get(h, UID_TO_PTR(uid));
@@ -197,7 +192,7 @@ int coredump_vacuum(int exclude_fd, uint64_t keep_free, uint64_t max_use) {
197192
return r;
198193
n->oldest_mtime = t;
199194

200-
r = hashmap_put(h, UID_TO_PTR(uid), n);
195+
r = hashmap_ensure_put(&h, &vacuum_candidate_hash_ops, UID_TO_PTR(uid), n);
201196
if (r < 0)
202197
return log_oom();
203198

src/libsystemd/sd-device/device-enumerator.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ static void device_unref_many(sd_device **devices, size_t n) {
9191
static void device_enumerator_unref_devices(sd_device_enumerator *enumerator) {
9292
assert(enumerator);
9393

94-
hashmap_clear_with_destructor(enumerator->devices_by_syspath, sd_device_unref);
94+
hashmap_clear(enumerator->devices_by_syspath);
9595
device_unref_many(enumerator->devices, enumerator->n_devices);
9696
enumerator->devices = mfree(enumerator->devices);
9797
enumerator->n_devices = 0;
@@ -471,6 +471,11 @@ static int enumerator_sort_devices(sd_device_enumerator *enumerator) {
471471
return r;
472472
}
473473

474+
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
475+
device_hash_ops_by_syspath,
476+
char, path_hash_func, path_compare,
477+
sd_device, sd_device_unref);
478+
474479
int device_enumerator_add_device(sd_device_enumerator *enumerator, sd_device *device) {
475480
const char *syspath;
476481
int r;
@@ -482,7 +487,7 @@ int device_enumerator_add_device(sd_device_enumerator *enumerator, sd_device *de
482487
if (r < 0)
483488
return r;
484489

485-
r = hashmap_ensure_put(&enumerator->devices_by_syspath, &string_hash_ops, syspath, device);
490+
r = hashmap_ensure_put(&enumerator->devices_by_syspath, &device_hash_ops_by_syspath, syspath, device);
486491
if (IN_SET(r, -EEXIST, 0))
487492
return 0;
488493
if (r < 0)

src/libsystemd/sd-device/device-private.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -692,16 +692,16 @@ int device_clone_with_db(sd_device *device, sd_device **ret) {
692692
void device_cleanup_tags(sd_device *device) {
693693
assert(device);
694694

695-
device->all_tags = set_free_free(device->all_tags);
696-
device->current_tags = set_free_free(device->current_tags);
695+
device->all_tags = set_free(device->all_tags);
696+
device->current_tags = set_free(device->current_tags);
697697
device->property_tags_outdated = true;
698698
device->tags_generation++;
699699
}
700700

701701
void device_cleanup_devlinks(sd_device *device) {
702702
assert(device);
703703

704-
set_free_free(device->devlinks);
704+
set_free(device->devlinks);
705705
device->devlinks = NULL;
706706
device->property_devlinks_outdated = true;
707707
device->devlinks_generation++;

src/libsystemd/sd-netlink/netlink-genl.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,21 @@ static GenericNetlinkFamily *genl_family_free(GenericNetlinkFamily *f) {
4646

4747
DEFINE_TRIVIAL_CLEANUP_FUNC(GenericNetlinkFamily*, genl_family_free);
4848

49+
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
50+
genl_family_hash_ops_by_name,
51+
char, string_hash_func, string_compare_func,
52+
GenericNetlinkFamily, genl_family_free);
53+
54+
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
55+
genl_family_hash_ops_by_id,
56+
void, trivial_hash_func, trivial_compare_func,
57+
GenericNetlinkFamily, genl_family_free);
58+
4959
void genl_clear_family(sd_netlink *nl) {
5060
assert(nl);
5161

52-
nl->genl_family_by_name = hashmap_free_with_destructor(nl->genl_family_by_name, genl_family_free);
53-
nl->genl_family_by_id = hashmap_free_with_destructor(nl->genl_family_by_id, genl_family_free);
62+
nl->genl_family_by_name = hashmap_free(nl->genl_family_by_name);
63+
nl->genl_family_by_id = hashmap_free(nl->genl_family_by_id);
5464
}
5565

5666
static int genl_family_new_unsupported(
@@ -80,7 +90,7 @@ static int genl_family_new_unsupported(
8090
if (!f->name)
8191
return -ENOMEM;
8292

83-
r = hashmap_ensure_put(&nl->genl_family_by_name, &string_hash_ops, f->name, f);
93+
r = hashmap_ensure_put(&nl->genl_family_by_name, &genl_family_hash_ops_by_name, f->name, f);
8494
if (r < 0)
8595
return r;
8696

@@ -190,11 +200,11 @@ static int genl_family_new(
190200
return r;
191201
}
192202

193-
r = hashmap_ensure_put(&nl->genl_family_by_id, NULL, UINT_TO_PTR(f->id), f);
203+
r = hashmap_ensure_put(&nl->genl_family_by_id, &genl_family_hash_ops_by_id, UINT_TO_PTR(f->id), f);
194204
if (r < 0)
195205
return r;
196206

197-
r = hashmap_ensure_put(&nl->genl_family_by_name, &string_hash_ops, f->name, f);
207+
r = hashmap_ensure_put(&nl->genl_family_by_name, &genl_family_hash_ops_by_name, f->name, f);
198208
if (r < 0) {
199209
hashmap_remove(nl->genl_family_by_id, UINT_TO_PTR(f->id));
200210
return r;

src/sysusers/sysusers.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ static void context_done(Context *c) {
143143
hashmap_free(c->database_by_gid);
144144
hashmap_free(c->database_by_groupname);
145145

146-
set_free_free(c->names);
146+
set_free(c->names);
147147
uid_range_free(c->uid_range);
148148
}
149149

@@ -231,9 +231,8 @@ static int load_user_database(Context *c) {
231231
if (!n)
232232
return -ENOMEM;
233233

234-
/* Note that we use NULL hash_ops (i.e. trivial_hash_ops) here, so identical strings can
235-
* exist in the set. */
236-
r = set_ensure_consume(&c->names, /* hash_ops= */ NULL, n);
234+
/* Note that we use trivial_hash_ops_free here, so identical strings can exist in the set. */
235+
r = set_ensure_consume(&c->names, &trivial_hash_ops_free, n);
237236
if (r < 0)
238237
return r;
239238
assert(r > 0); /* The set uses pointer comparisons, so n must not be in the set. */
@@ -274,9 +273,8 @@ static int load_group_database(Context *c) {
274273
if (!n)
275274
return -ENOMEM;
276275

277-
/* Note that we use NULL hash_ops (i.e. trivial_hash_ops) here, so identical strings can
278-
* exist in the set. */
279-
r = set_ensure_consume(&c->names, /* hash_ops= */ NULL, n);
276+
/* Note that we use trivial_hash_ops_free here, so identical strings can exist in the set. */
277+
r = set_ensure_consume(&c->names, &trivial_hash_ops_free, n);
280278
if (r < 0)
281279
return r;
282280
assert(r > 0); /* The set uses pointer comparisons, so n must not be in the set. */

src/sysv-generator/sysv-generator.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ typedef struct SysvStub {
6464
bool loaded;
6565
} SysvStub;
6666

67-
static SysvStub* free_sysvstub(SysvStub *s) {
67+
static SysvStub* sysvstub_free(SysvStub *s) {
6868
if (!s)
6969
return NULL;
7070

@@ -78,11 +78,12 @@ static SysvStub* free_sysvstub(SysvStub *s) {
7878
strv_free(s->wanted_by);
7979
return mfree(s);
8080
}
81-
DEFINE_TRIVIAL_CLEANUP_FUNC(SysvStub*, free_sysvstub);
81+
DEFINE_TRIVIAL_CLEANUP_FUNC(SysvStub*, sysvstub_free);
8282

83-
static void free_sysvstub_hashmapp(Hashmap **h) {
84-
hashmap_free_with_destructor(*h, free_sysvstub);
85-
}
83+
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
84+
sysvstub_hash_ops,
85+
char, string_hash_func, string_compare_func,
86+
SysvStub, sysvstub_free);
8687

8788
static int add_alias(const char *service, const char *alias) {
8889
_cleanup_free_ char *link = NULL;
@@ -728,7 +729,7 @@ static int enumerate_sysv(const LookupPaths *lp, Hashmap *all_services) {
728729

729730
FOREACH_DIRENT(de, d, log_error_errno(errno, "Failed to enumerate directory %s, ignoring: %m", *path)) {
730731
_cleanup_free_ char *fpath = NULL, *name = NULL;
731-
_cleanup_(free_sysvstubp) SysvStub *service = NULL;
732+
_cleanup_(sysvstub_freep) SysvStub *service = NULL;
732733
struct stat st;
733734

734735
if (fstatat(dirfd(d), de->d_name, &st, 0) < 0) {
@@ -894,7 +895,7 @@ static int set_dependencies_from_rcnd(const LookupPaths *lp, Hashmap *all_servic
894895
}
895896

896897
static int run(const char *dest, const char *dest_early, const char *dest_late) {
897-
_cleanup_(free_sysvstub_hashmapp) Hashmap *all_services = NULL;
898+
_cleanup_hashmap_free_ Hashmap *all_services = NULL;
898899
_cleanup_(lookup_paths_done) LookupPaths lp = {};
899900
SysvStub *service;
900901
int r;
@@ -910,7 +911,7 @@ static int run(const char *dest, const char *dest_early, const char *dest_late)
910911
if (r < 0)
911912
return r;
912913

913-
all_services = hashmap_new(&string_hash_ops);
914+
all_services = hashmap_new(&sysvstub_hash_ops);
914915
if (!all_services)
915916
return log_oom();
916917

0 commit comments

Comments
 (0)