]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
coredump: replace custom cleanup function with specific hash_ops with destructor
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 12 Apr 2025 02:16:26 +0000 (11:16 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 13 Apr 2025 01:00:57 +0000 (10:00 +0900)
src/coredump/coredump-vacuum.c

index 8e2febd5810bffe06927d3f2b492ec2f25553530..3f99d04cf00d850049c68467f6b00fe3abd72b2f 100644 (file)
@@ -38,11 +38,10 @@ static VacuumCandidate* vacuum_candidate_free(VacuumCandidate *c) {
 }
 DEFINE_TRIVIAL_CLEANUP_FUNC(VacuumCandidate*, vacuum_candidate_free);
 
-static Hashmap* vacuum_candidate_hashmap_free(Hashmap *h) {
-        return hashmap_free_with_destructor(h, vacuum_candidate_free);
-}
-
-DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap*, vacuum_candidate_hashmap_free);
+DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
+                vacuum_candidate_hash_ops,
+                void, trivial_hash_func, trivial_compare_func,
+                VacuumCandidate, vacuum_candidate_free);
 
 static int uid_from_file_name(const char *filename, uid_t *uid) {
         const char *p, *e, *u;
@@ -141,7 +140,7 @@ int coredump_vacuum(int exclude_fd, uint64_t keep_free, uint64_t max_use) {
         }
 
         for (;;) {
-                _cleanup_(vacuum_candidate_hashmap_freep) Hashmap *h = NULL;
+                _cleanup_hashmap_free_ Hashmap *h = NULL;
                 VacuumCandidate *worst = NULL;
                 uint64_t sum = 0;
 
@@ -171,10 +170,6 @@ int coredump_vacuum(int exclude_fd, uint64_t keep_free, uint64_t max_use) {
                         if (exclude_fd >= 0 && stat_inode_same(&exclude_st, &st))
                                 continue;
 
-                        r = hashmap_ensure_allocated(&h, NULL);
-                        if (r < 0)
-                                return log_oom();
-
                         t = timespec_load(&st.st_mtim);
 
                         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) {
                                         return r;
                                 n->oldest_mtime = t;
 
-                                r = hashmap_put(h, UID_TO_PTR(uid), n);
+                                r = hashmap_ensure_put(&h, &vacuum_candidate_hash_ops, UID_TO_PTR(uid), n);
                                 if (r < 0)
                                         return log_oom();