[libc-commits] [libc] 1ac525b - [libc] add sysconf with pagesize

Michael Jones via libc-commits libc-commits at lists.llvm.org
Mon Oct 10 14:51:53 PDT 2022


Author: Michael Jones
Date: 2022-10-10T14:51:45-07:00
New Revision: 1ac525b28b3b0e24a762471104eee6368e954c44

URL: https://github.com/llvm/llvm-project/commit/1ac525b28b3b0e24a762471104eee6368e954c44
DIFF: https://github.com/llvm/llvm-project/commit/1ac525b28b3b0e24a762471104eee6368e954c44.diff

LOG: [libc] add sysconf with pagesize

The sysconf function has many options, this patch adds the basic funtion
and the pagesize option. More options will be added in future patches.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D135409

Added: 
    libc/src/unistd/linux/sysconf.cpp
    libc/src/unistd/sysconf.h
    libc/test/src/unistd/sysconf_test.cpp

Modified: 
    libc/config/linux/x86_64/entrypoints.txt
    libc/include/llvm-libc-macros/linux/unistd-macros.h
    libc/spec/posix.td
    libc/src/unistd/CMakeLists.txt
    libc/src/unistd/linux/CMakeLists.txt
    libc/test/src/unistd/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 7fd11a389dea3..32ceb8f1f3434 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -167,6 +167,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.rmdir
     libc.src.unistd.symlink
     libc.src.unistd.symlinkat
+    libc.src.unistd.sysconf
     libc.src.unistd.truncate
     libc.src.unistd.unlink
     libc.src.unistd.unlinkat

diff  --git a/libc/include/llvm-libc-macros/linux/unistd-macros.h b/libc/include/llvm-libc-macros/linux/unistd-macros.h
index 9fbb9ae28d4af..cfdfb9a93ee9b 100644
--- a/libc/include/llvm-libc-macros/linux/unistd-macros.h
+++ b/libc/include/llvm-libc-macros/linux/unistd-macros.h
@@ -15,6 +15,9 @@
 #define W_OK 2
 #define R_OK 4
 
+#define _SC_PAGESIZE 1
+#define _SC_PAGE_SIZE _SC_PAGESIZE
+
 // Macro to set up the call to the __llvm_libc_syscall function
 // This is to prevent the call from having fewer than 6 arguments, since six
 // arguments are always passed to the syscall. Unnecessary arguments are

diff  --git a/libc/spec/posix.td b/libc/spec/posix.td
index fc7ac25b10c6f..61e7e4d425555 100644
--- a/libc/spec/posix.td
+++ b/libc/spec/posix.td
@@ -511,6 +511,11 @@ def POSIX : StandardSpec<"POSIX"> {
           RetValSpec<IntType>,
           [ArgSpec<IntType>, ArgSpec<ConstCharPtr>, ArgSpec<IntType>, ArgSpec<ConstCharPtr>]
         >,
+        FunctionSpec<
+          "sysconf",
+          RetValSpec<IntType>,
+          [ArgSpec<IntType>]
+        >,
         FunctionSpec<
           "__llvm_libc_syscall",
           RetValSpec<LongType>,

diff  --git a/libc/src/unistd/CMakeLists.txt b/libc/src/unistd/CMakeLists.txt
index a9d88ee9441c8..ae53763df8893 100644
--- a/libc/src/unistd/CMakeLists.txt
+++ b/libc/src/unistd/CMakeLists.txt
@@ -198,6 +198,13 @@ add_entrypoint_object(
     .${LIBC_TARGET_OS}.__llvm_libc_syscall
 )
 
+add_entrypoint_object(
+  sysconf
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.sysconf
+)
+
 add_entrypoint_object(
   truncate
   ALIAS

diff  --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index c81b9219de8d6..2a6a0e66910df 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -369,6 +369,17 @@ add_entrypoint_object(
     libc.src.errno.errno
 )
 
+add_entrypoint_object(
+  sysconf
+  SRCS
+    sysconf.cpp
+  HDRS
+    ../sysconf.h
+  DEPENDS
+    libc.include.unistd
+    libc.src.errno.errno
+)
+
 add_entrypoint_object(
   truncate
   SRCS

diff  --git a/libc/src/unistd/linux/sysconf.cpp b/libc/src/unistd/linux/sysconf.cpp
new file mode 100644
index 0000000000000..b4a9cd151e719
--- /dev/null
+++ b/libc/src/unistd/linux/sysconf.cpp
@@ -0,0 +1,33 @@
+//===-- 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/unistd/sysconf.h"
+
+#include "src/__support/common.h"
+
+#include <errno.h>
+#include <linux/param.h> // For EXEC_PAGESIZE.
+#include <unistd.h>
+
+namespace __llvm_libc {
+
+LLVM_LIBC_FUNCTION(long, sysconf, (int name)) {
+  long ret = 0;
+  if (name == _SC_PAGESIZE) {
+    // TODO: get this information from the auxvector.
+    return EXEC_PAGESIZE;
+  }
+  // TODO: Complete the rest of the sysconf options.
+  if (ret < 0) {
+    errno = EINVAL;
+    return -1;
+  }
+  return ret;
+}
+
+} // namespace __llvm_libc

diff  --git a/libc/src/unistd/sysconf.h b/libc/src/unistd/sysconf.h
new file mode 100644
index 0000000000000..b704edb4ada1f
--- /dev/null
+++ b/libc/src/unistd/sysconf.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for sysconf -----------------------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_UNISTD_SYSCONF_H
+#define LLVM_LIBC_SRC_UNISTD_SYSCONF_H
+
+#include <unistd.h>
+
+namespace __llvm_libc {
+
+long sysconf(int name);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_UNISTD_SYSCONF_H

diff  --git a/libc/test/src/unistd/CMakeLists.txt b/libc/test/src/unistd/CMakeLists.txt
index 448116e140b23..3b99acaddd87b 100644
--- a/libc/test/src/unistd/CMakeLists.txt
+++ b/libc/test/src/unistd/CMakeLists.txt
@@ -376,3 +376,15 @@ add_libc_unittest(
     libc.include.sys_syscall
     libc.test.errno_setter_matcher
 )
+
+
+add_libc_unittest(
+  sysconf_test
+  SUITE
+    libc_unistd_unittests
+  SRCS
+    sysconf_test.cpp
+  DEPENDS
+    libc.include.unistd
+    libc.src.unistd.sysconf
+)

diff  --git a/libc/test/src/unistd/sysconf_test.cpp b/libc/test/src/unistd/sysconf_test.cpp
new file mode 100644
index 0000000000000..9f93746e03590
--- /dev/null
+++ b/libc/test/src/unistd/sysconf_test.cpp
@@ -0,0 +1,17 @@
+//===-- 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/unistd/sysconf.h"
+#include "utils/UnitTest/Test.h"
+
+#include <unistd.h>
+
+TEST(LlvmLibcSysconfTest, PagesizeTest) {
+  long pagesize = __llvm_libc::sysconf(_SC_PAGESIZE);
+  ASSERT_GT(pagesize, 0l);
+}


        


More information about the libc-commits mailing list