[libc-commits] [libc] [libc] Clean up sysconf implementation and tests (PR #204130)

Jeff Bailey via libc-commits libc-commits at lists.llvm.org
Wed Jun 17 05:33:59 PDT 2026


https://github.com/kaladron updated https://github.com/llvm/llvm-project/pull/204130

>From 364d39de3a4c2609fdc33ccd03499c2f3252b944 Mon Sep 17 00:00:00 2001
From: Jeff Bailey <jbailey at raspberryginger.com>
Date: Tue, 16 Jun 2026 12:40:56 +0100
Subject: [PATCH] [libc] Clean up sysconf implementation and tests

Refactored sysconf.cpp to use a switch-case block instead of an if-else chain.
Delegated the logic for existing options to helper functions (get_page_size,
get_nprocessors_conf, get_nprocessors_onln) in an anonymous namespace to
reduce cognitive complexity.

Updated file headers in sysconf.cpp and sysconf_test.cpp to the standard
three-section format.

Modernized suffix literals in sysconf_test.cpp.

Updated RLIM_INFINITY definition in sys-resource-macros.h to be 64-bit
safe (~0ULL) to prevent comparison failures on 32-bit platforms where
rlim_t is 64-bit but RLIM_INFINITY was 32-bit.

Assisted-by: Automated tooling, human reviewed.
---
 .../linux/sys-resource-macros.h               |  2 +-
 libc/src/unistd/linux/sysconf.cpp             | 58 ++++++++++++-------
 libc/test/src/unistd/sysconf_test.cpp         | 13 +++--
 3 files changed, 48 insertions(+), 25 deletions(-)

diff --git a/libc/include/llvm-libc-macros/linux/sys-resource-macros.h b/libc/include/llvm-libc-macros/linux/sys-resource-macros.h
index c9d93c30c35a4..ff7eb42a5ba6c 100644
--- a/libc/include/llvm-libc-macros/linux/sys-resource-macros.h
+++ b/libc/include/llvm-libc-macros/linux/sys-resource-macros.h
@@ -26,6 +26,6 @@
 #define RLIMIT_RTPRIO 14
 #define RLIMIT_RTTIME 15
 
-#define RLIM_INFINITY (~0UL)
+#define RLIM_INFINITY (~0ULL)
 
 #endif // LLVM_LIBC_MACROS_LINUX_SYS_RESOURCE_MACROS_H
diff --git a/libc/src/unistd/linux/sysconf.cpp b/libc/src/unistd/linux/sysconf.cpp
index dff64cb8140d3..6b808150ab83d 100644
--- a/libc/src/unistd/linux/sysconf.cpp
+++ b/libc/src/unistd/linux/sysconf.cpp
@@ -1,10 +1,15 @@
-//===-- Linux implementation of sysconf -----------------------------------===//
+//===----------------------------------------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
+///
+/// \file
+/// Linux implementation of sysconf
+///
+//===----------------------------------------------------------------------===//
 
 #include "src/unistd/sysconf.h"
 
@@ -19,29 +24,42 @@
 
 namespace LIBC_NAMESPACE_DECL {
 
-LLVM_LIBC_FUNCTION(long, sysconf, (int name)) {
-  if (name == _SC_PAGESIZE) {
-    cpp::optional<unsigned long> page_size = auxv::get(AT_PAGESZ);
-    if (page_size)
-      return static_cast<long>(*page_size);
-    libc_errno = EINVAL;
-    return -1;
-  }
+namespace { // Anonymous namespace for internal helpers
+
+long get_page_size() {
+  cpp::optional<unsigned long> page_size = auxv::get(AT_PAGESZ);
+  if (page_size)
+    return static_cast<long>(*page_size);
+  libc_errno = EINVAL;
+  return -1;
+}
 
-  if (name == _SC_NPROCESSORS_CONF)
-    return static_cast<long>(
-        sysinfo::parse_nproc_with_fallback_from(sysinfo::POSSIBLE_NPROC_PATH));
+long get_nprocessors_conf() {
+  return static_cast<long>(
+      sysinfo::parse_nproc_with_fallback_from(sysinfo::POSSIBLE_NPROC_PATH));
+}
 
-  if (name == _SC_NPROCESSORS_ONLN)
-    return static_cast<long>(
-        sysinfo::parse_nproc_with_fallback_from(sysinfo::ONLINE_NPROC_PATH));
+long get_nprocessors_onln() {
+  return static_cast<long>(
+      sysinfo::parse_nproc_with_fallback_from(sysinfo::ONLINE_NPROC_PATH));
+}
 
-  if (name == _SC_THREADS)
-    return _POSIX_THREADS;
+} // anonymous namespace
 
-  // TODO: Complete the rest of the sysconf options.
-  libc_errno = EINVAL;
-  return -1;
+LLVM_LIBC_FUNCTION(long, sysconf, (int name)) {
+  switch (name) {
+  case _SC_PAGESIZE:
+    return get_page_size();
+  case _SC_NPROCESSORS_CONF:
+    return get_nprocessors_conf();
+  case _SC_NPROCESSORS_ONLN:
+    return get_nprocessors_onln();
+  case _SC_THREADS:
+    return _POSIX_THREADS;
+  default:
+    libc_errno = EINVAL;
+    return -1;
+  }
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/unistd/sysconf_test.cpp b/libc/test/src/unistd/sysconf_test.cpp
index 0c37eef13b283..16882714cdf1c 100644
--- a/libc/test/src/unistd/sysconf_test.cpp
+++ b/libc/test/src/unistd/sysconf_test.cpp
@@ -1,10 +1,15 @@
-//===-- Unittests for sysconf ---------------------------------------------===//
+//===----------------------------------------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for sysconf
+///
+//===----------------------------------------------------------------------===//
 
 #include "src/unistd/sysconf.h"
 #include "test/UnitTest/Test.h"
@@ -13,17 +18,17 @@
 
 TEST(LlvmLibcSysconfTest, PagesizeTest) {
   long pagesize = LIBC_NAMESPACE::sysconf(_SC_PAGESIZE);
-  ASSERT_GT(pagesize, 0l);
+  ASSERT_GT(pagesize, 0L);
 }
 
 TEST(LlvmLibcSysconfTest, NprocessorsConfTest) {
   long sysconf_count = LIBC_NAMESPACE::sysconf(_SC_NPROCESSORS_CONF);
-  ASSERT_GT(sysconf_count, 0l);
+  ASSERT_GT(sysconf_count, 0L);
 }
 
 TEST(LlvmLibcSysconfTest, NprocessorsOnlnTest) {
   long sysconf_count = LIBC_NAMESPACE::sysconf(_SC_NPROCESSORS_ONLN);
-  ASSERT_GT(sysconf_count, 0l);
+  ASSERT_GT(sysconf_count, 0L);
 }
 
 TEST(LlvmLibcSysconfTest, ThreadsTest) {



More information about the libc-commits mailing list