diff options
author | NRK <nrk@disroot.org> | 2022-08-07 15:53:26 +0200 |
---|---|---|
committer | Gitea <gitea@fake.local> | 2022-08-11 03:58:26 +0200 |
commit | becf1cc858d1590d75f00124b77f04578da7fae0 (patch) | |
tree | 38109c75a5f81372eaf779f01904b119eeae5151 | |
parent | fb9686c6505475f342ad94fa5ed1351f554f97fe (diff) | |
download | nsxiv-becf1cc858d1590d75f00124b77f04578da7fae0.tar.zst |
fix: build failure when _SC_PHYS_PAGES is not defined
_SC_PHYS_PAGES isn't POSIX and might not be defined. in such case, just
return back `CACHE_SIZE_FALLBACK`.
NOTE: POSIX says the `names` in `sysconf()` are "symbolic constants" not
necessarily macros. So we might end up returning the fallback in some
cases where `_SC_PHYS_PAGES` *was* available, but not defined as a
macro. which is not ideal, but nothing fatal.
in practice, this shouldn't be an issue since most systems seems to
define them to be macros, i've checked Glibc, Musl, OpenBSD, FreeBSD and
Haiku.
also add a (useful) comment on `config.h` describing the effect higher
cache size has.
Closes: https://codeberg.org/nsxiv/nsxiv/issues/354
-rw-r--r-- | config.def.h | 3 | ||||
-rw-r--r-- | image.c | 6 |
2 files changed, 6 insertions, 3 deletions
diff --git a/config.def.h b/config.def.h index 8db8983..9ac3e9b 100644 --- a/config.def.h +++ b/config.def.h @@ -56,6 +56,9 @@ static const bool ALPHA_LAYER = false; * 3 means use 3% of total memory which is about 245MiB on 8GiB machine. * 0 or less means disable cache. * 100 means use all available memory (but not above CACHE_SIZE_LIMIT). + * + * NOTE: higher cache size means better image reloading performance, but also + * higher memory usage. */ static const int CACHE_SIZE_MEM_PERCENTAGE = 3; /* use 3% of total memory for cache */ static const int CACHE_SIZE_LIMIT = 256 * 1024 * 1024; /* but not above 256MiB */ @@ -49,14 +49,14 @@ enum { DEF_WEBP_DELAY = 75 }; static int calc_cache_size(void) { - int cache; - long pages, page_size; + long cache, pages = -1, page_size = -1; if (CACHE_SIZE_MEM_PERCENTAGE <= 0) return 0; - +#ifdef _SC_PHYS_PAGES /* _SC_PHYS_PAGES isn't POSIX */ pages = sysconf(_SC_PHYS_PAGES); page_size = sysconf(_SC_PAGE_SIZE); +#endif if (pages < 0 || page_size < 0) return CACHE_SIZE_FALLBACK; cache = (pages/100) * CACHE_SIZE_MEM_PERCENTAGE; |