mmap-cache: ref/unref MMapCache in fd add/free
authorVito Caputo <vcaputo@pengaru.com>
Thu, 25 Nov 2021 23:01:38 +0000 (15:01 -0800)
committerVito Caputo <vcaputo@pengaru.com>
Tue, 7 Dec 2021 20:42:19 +0000 (12:42 -0800)
Preparatory commit; callers manually ref/unref MMapCaches
alongside MMapFileDescriptor add/frees, when the latter should be
sufficient.

A subsequent commit will drop some of those manual MMapCache
reference hoop-jumping, leaving the lifecycle bound to
MMapFileDescriptors.

src/libsystemd/sd-journal/mmap-cache.c

index faf05c55073f51ca9ff29b44ba386f6ecdfbdd4d..4ecaaf23887e2e923dea32674222db1fcbb88ad2 100644 (file)
@@ -560,14 +560,14 @@ MMapFileDescriptor* mmap_cache_add_fd(MMapCache *m, int fd, int prot) {
         if (!f)
                 return NULL;
 
-        f->cache = m;
-        f->fd = fd;
-        f->prot = prot;
-
         r = hashmap_put(m->fds, FD_TO_PTR(fd), f);
         if (r < 0)
                 return mfree(f);
 
+        f->cache = mmap_cache_ref(m);
+        f->fd = fd;
+        f->prot = prot;
+
         return f;
 }
 
@@ -584,8 +584,10 @@ void mmap_cache_fd_free(MMapFileDescriptor *f) {
         while (f->windows)
                 window_free(f->windows);
 
-        if (f->cache)
+        if (f->cache) {
                 assert_se(hashmap_remove(f->cache->fds, FD_TO_PTR(f->fd)));
+                f->cache = mmap_cache_unref(f->cache);
+        }
 
         free(f);
 }