Krolyxon
Commit becf1cc

Branches

Refs →

nsxivbecf1cc

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.
Author
NRK
Date
2022-08-07 19:53 +0600
Committer
Gitea · 2022-08-11 03:58 +0200
Parent
fb9686c
Commit
becf1cc858d1590d75f00124b77f04578da7fae0

+6 −3

2 files changed

  1. M config.def.h +3 −0
  2. M image.c +3 −3
M config.def.h View
@@ -56,6 +56,9 @@ static const bool ALPHA_LAYER = false;
5656 * 3 means use 3% of total memory which is about 245MiB on 8GiB machine.
5757 * 0 or less means disable cache.
5858 * 100 means use all available memory (but not above CACHE_SIZE_LIMIT).
59+ *
60+ * NOTE: higher cache size means better image reloading performance, but also
61+ * higher memory usage.
5962 */
6063 static const int CACHE_SIZE_MEM_PERCENTAGE = 3; /* use 3% of total memory for cache */
6164 static const int CACHE_SIZE_LIMIT = 256 * 1024 * 1024; /* but not above 256MiB */
M image.c View
@@ -49,14 +49,14 @@ enum { DEF_WEBP_DELAY = 75 };
4949
5050 static int calc_cache_size(void)
5151 {
52− int cache;
53− long pages, page_size;
52+ long cache, pages = -1, page_size = -1;
5453
5554 if (CACHE_SIZE_MEM_PERCENTAGE <= 0)
5655 return 0;
57−
56+#ifdef _SC_PHYS_PAGES /* _SC_PHYS_PAGES isn't POSIX */
5857 pages = sysconf(_SC_PHYS_PAGES);
5958 page_size = sysconf(_SC_PAGE_SIZE);
59+#endif
6060 if (pages < 0 || page_size < 0)
6161 return CACHE_SIZE_FALLBACK;
6262 cache = (pages/100) * CACHE_SIZE_MEM_PERCENTAGE;