From 616bb9fca16e3313f17c6be640464c043d7643f6 Mon Sep 17 00:00:00 2001
From: Fred Wright <fw@fwright.net>
Date: Thu, 14 Nov 2024 16:46:03 -0800
Subject: [PATCH] sysconf: Cache dlsym() result.

This avoids repeating the symbol lookup on every call.

TESTED:
Passes tests on all platforms.
---
 src/sysconf.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/src/sysconf.c b/src/sysconf.c
index 399ca9c..8c196ab 100644
--- a/src/sysconf.c
+++ b/src/sysconf.c
@@ -33,7 +33,7 @@
  */
 
 long sysconf(int name) {
-    long (*real_sysconf)(int);
+    static long (*os_sysconf)(int);
 
 #if __MPLS_LIB_SUPPORT_SYSCONF_NPROCESSORS__
     if ( name == _SC_NPROCESSORS_ONLN ) {
@@ -85,11 +85,14 @@ long sysconf(int name) {
 #endif /* __MPLS_LIB_SUPPORT_SYSCONF_PHYS_PAGES__ */
 
     /* for any other values of "name", call the real sysconf() */
-    real_sysconf = dlsym(RTLD_NEXT, "sysconf");
-    if (real_sysconf == NULL) {
-        exit(EXIT_FAILURE);
+    if (!os_sysconf) {
+        os_sysconf = dlsym(RTLD_NEXT, "sysconf");
+        /* Something's badly broken if this fails */
+        if (!os_sysconf) {
+            abort();
+        }
     }
-    return real_sysconf(name);
+    return (*os_sysconf)(name);
 }
 
 /* compatibility function so code does not have to be recompiled */
-- 
GitLab