]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
mmap-cache: merge mmap_try_harder() with make_room()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 28 Sep 2023 01:27:45 +0000 (10:27 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 4 Oct 2023 10:53:27 +0000 (19:53 +0900)
The function make_room() is short and only used by mmap_try_harder().
Let's merge them with short comments.

No functional change, just refactoring.

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

index 7592b1981413ebfc5891ec6b906064cf35fe8636..7a3057c88408ce0111d1ee9e2b5b0965761cc77f 100644 (file)
@@ -254,16 +254,6 @@ static MMapCache* mmap_cache_free(MMapCache *m) {
 
 DEFINE_TRIVIAL_REF_UNREF_FUNC(MMapCache, mmap_cache, mmap_cache_free);
 
-static int make_room(MMapCache *m) {
-        assert(m);
-
-        if (!m->last_unused)
-                return 0;
-
-        window_free(m->last_unused);
-        return 1;
-}
-
 static int try_context(
                 MMapFileDescriptor *f,
                 Context *c,
@@ -337,30 +327,29 @@ static int find_mmap(
         return 1;
 }
 
-static int mmap_try_harder(MMapFileDescriptor *f, void *addr, int flags, uint64_t offset, size_t size, void **res) {
-        void *ptr;
+static int mmap_try_harder(MMapFileDescriptor *f, void *addr, int flags, uint64_t offset, size_t size, void **ret) {
+        MMapCache *m = mmap_cache_fd_cache(f);
 
-        assert(f);
-        assert(res);
+        assert(ret);
 
         for (;;) {
-                int r;
+                void *ptr;
 
                 ptr = mmap(addr, size, f->prot, flags, f->fd, offset);
-                if (ptr != MAP_FAILED)
-                        break;
+                if (ptr != MAP_FAILED) {
+                        *ret = ptr;
+                        return 0;
+                }
                 if (errno != ENOMEM)
                         return negative_errno();
 
-                r = make_room(f->cache);
-                if (r < 0)
-                        return r;
-                if (r == 0)
-                        return -ENOMEM;
-        }
+                /* When failed with ENOMEM, try again after making a room by freeing an unused window. */
+
+                if (!m->last_unused)
+                        return -ENOMEM; /* no free window, propagate the original error. */
 
-        *res = ptr;
-        return 0;
+                window_free(m->last_unused);
+        }
 }
 
 static int add_mmap(