[libc] Add support for getpagesize. (#171713)

As in the description.
This commit is contained in:
Sterling-Augustine
2025-12-11 14:27:50 -08:00
committed by GitHub
parent 65deac0872
commit a50a7ea2e2
13 changed files with 125 additions and 0 deletions

View File

@@ -335,6 +335,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.getentropy
libc.src.unistd.geteuid
libc.src.unistd.gethostname
libc.src.unistd.getpagesize
libc.src.unistd.getpid
libc.src.unistd.getppid
libc.src.unistd.getsid

View File

@@ -338,6 +338,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.getentropy
libc.src.unistd.geteuid
libc.src.unistd.gethostname
libc.src.unistd.getpagesize
libc.src.unistd.getpid
libc.src.unistd.getppid
libc.src.unistd.getsid

View File

@@ -347,6 +347,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.getentropy
libc.src.unistd.geteuid
libc.src.unistd.gethostname
libc.src.unistd.getpagesize
libc.src.unistd.getpid
libc.src.unistd.getppid
libc.src.unistd.getsid

View File

@@ -174,6 +174,12 @@ functions:
arguments:
- type: char *
- type: size_t
- name: getpagesize
standards:
- BSDExtensions
return_type: int
arguments:
- type: void
- name: getopt
standards:
- POSIX

View File

@@ -139,6 +139,13 @@ add_entrypoint_object(
.${LIBC_TARGET_OS}.gethostname
)
add_entrypoint_object(
getpagesize
ALIAS
DEPENDS
.${LIBC_TARGET_OS}.getpagesize
)
add_entrypoint_object(
getpid
ALIAS

View File

@@ -0,0 +1,20 @@
//===-- Implementation header for getpagesize -------------------*- 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_GETPAGESIZE_H
#define LLVM_LIBC_SRC_UNISTD_GETPAGESIZE_H
#include "src/__support/common.h"
namespace LIBC_NAMESPACE_DECL {
int getpagesize();
} // namespace LIBC_NAMESPACE_DECL
#endif // LLVM_LIBC_SRC_UNISTD_GETPAGESIZE_H

View File

@@ -249,6 +249,16 @@ add_entrypoint_object(
libc.src.errno.errno
)
add_entrypoint_object(
getpagesize
SRCS
getpagesize.cpp
HDRS
../getpagesize.h
DEPENDS
libc.src.__support.OSUtil.linux.auxv
)
add_entrypoint_object(
geteuid
SRCS

View File

@@ -0,0 +1,25 @@
//===-- Linux implementation of getpagesize -------------------------------===//
//
// 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/getpagesize.h"
#include "src/__support/OSUtil/linux/auxv.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(int, getpagesize, ()) {
cpp::optional<unsigned long> page_size =
(LIBC_NAMESPACE::auxv::get(AT_PAGESZ));
if (page_size)
return static_cast<int>(*page_size);
return -1;
}
} // namespace LIBC_NAMESPACE_DECL

View File

@@ -477,6 +477,17 @@ add_libc_unittest(
libc.test.UnitTest.ErrnoCheckingTest
)
add_libc_unittest(
getpagesize_test
SUITE
libc_unistd_unittests
SRCS
getpagesize_test.cpp
DEPENDS
libc.src.unistd.getpagesize
libc.test.UnitTest.ErrnoCheckingTest
)
add_libc_unittest(
getgid_test
SUITE

View File

@@ -0,0 +1,24 @@
//===-- Unittests for getpagesize -----------------------------------------===//
//
// 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/getpagesize.h"
#include "test/UnitTest/ErrnoCheckingTest.h"
#include "test/UnitTest/Test.h"
using LlvmLibcGetPageSizeTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
TEST(LlvmLibcGetPageSizeTest, GetPageSize) {
// getpagesize doesn't modify errno
int ret = LIBC_NAMESPACE::getpagesize();
ASSERT_NE(ret, -1);
ASSERT_NE(ret, 0);
// Correct page size depends on the hardware mode, but will be a modulus of
// 4096
ASSERT_EQ(ret % 4096, 0);
}

View File

@@ -577,6 +577,8 @@ functions:
in-latest-posix: ""
gethostname:
in-latest-posix: ""
getpagesize:
in-latest-posix: ""
getlogin:
in-latest-posix: ""
getlogin_r:

View File

@@ -6092,6 +6092,15 @@ libc_function(
],
)
libc_function(
name = "getpagesize",
srcs = ["src/unistd/linux/getpagesize.cpp"],
hdrs = ["src/unistd/getpagesize.h"],
deps = [
":__support_osutil_linux_auxv",
],
)
libc_function(
name = "getppid",
srcs = ["src/unistd/linux/getppid.cpp"],

View File

@@ -74,6 +74,14 @@ libc_test(
],
)
libc_test(
name = "getpagesize_test",
srcs = ["getpagesize_test.cpp"],
deps = [
"//libc:getpagesize",
],
)
libc_test(
name = "pread_pwrite_test",
srcs = ["pread_pwrite_test.cpp"],