From: Yu Watanabe Date: Thu, 28 Sep 2023 01:27:45 +0000 (+0900) Subject: mmap-cache: merge mmap_try_harder() with make_room() X-Git-Tag: v255-rc1~327^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1ed867d309445b44be734b20d864b321bfb90792;p=thirdparty%2Fsystemd.git mmap-cache: merge mmap_try_harder() with make_room() 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. --- diff --git a/src/libsystemd/sd-journal/mmap-cache.c b/src/libsystemd/sd-journal/mmap-cache.c index 7592b198141..7a3057c8840 100644 --- a/src/libsystemd/sd-journal/mmap-cache.c +++ b/src/libsystemd/sd-journal/mmap-cache.c @@ -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(