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

via libc-commits libc-commits at lists.llvm.org
Wed Jun 17 07:09:35 PDT 2026


Author: Jeff Bailey
Date: 2026-06-17T15:09:29+01:00
New Revision: 4081d00456b62adfffbc6d489ee489f5a69ad891

URL: https://github.com/llvm/llvm-project/commit/4081d00456b62adfffbc6d489ee489f5a69ad891
DIFF: https://github.com/llvm/llvm-project/commit/4081d00456b62adfffbc6d489ee489f5a69ad891.diff

LOG: [libc] Clean up sysconf implementation and tests (#204130)

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.

Fix RLIM_INFINITY to work on both 32- and 64-bit systems.

Modernized suffix literals in sysconf_test.cpp.

Assisted-by: Automated tooling, human reviewed.

Added: 
    

Modified: 
    libc/include/llvm-libc-macros/linux/sys-resource-macros.h
    libc/src/unistd/linux/sysconf.cpp
    libc/test/src/unistd/sysconf_test.cpp

Removed: 
    


################################################################################
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..419761e01c453 100644
--- a/libc/include/llvm-libc-macros/linux/sys-resource-macros.h
+++ b/libc/include/llvm-libc-macros/linux/sys-resource-macros.h
@@ -9,6 +9,8 @@
 #ifndef LLVM_LIBC_MACROS_LINUX_SYS_RESOURCE_MACROS_H
 #define LLVM_LIBC_MACROS_LINUX_SYS_RESOURCE_MACROS_H
 
+#include "../../llvm-libc-types/rlim_t.h"
+
 #define RLIMIT_CPU 0
 #define RLIMIT_FSIZE 1
 #define RLIMIT_DATA 2
@@ -26,6 +28,6 @@
 #define RLIMIT_RTPRIO 14
 #define RLIMIT_RTTIME 15
 
-#define RLIM_INFINITY (~0UL)
+#define RLIM_INFINITY ((rlim_t) - 1)
 
 #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..c8bcf66dcccbc 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,43 @@
 
 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:
+    // TODO: Complete the rest of the sysconf options.
+    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