Skip to content

Commit a50a7ea

Browse files
[libc] Add support for getpagesize. (llvm#171713)
As in the description.
1 parent 65deac0 commit a50a7ea

File tree

13 files changed

+125
-0
lines changed

13 files changed

+125
-0
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ set(TARGET_LIBC_ENTRYPOINTS
335335
libc.src.unistd.getentropy
336336
libc.src.unistd.geteuid
337337
libc.src.unistd.gethostname
338+
libc.src.unistd.getpagesize
338339
libc.src.unistd.getpid
339340
libc.src.unistd.getppid
340341
libc.src.unistd.getsid

libc/config/linux/riscv/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ set(TARGET_LIBC_ENTRYPOINTS
338338
libc.src.unistd.getentropy
339339
libc.src.unistd.geteuid
340340
libc.src.unistd.gethostname
341+
libc.src.unistd.getpagesize
341342
libc.src.unistd.getpid
342343
libc.src.unistd.getppid
343344
libc.src.unistd.getsid

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ set(TARGET_LIBC_ENTRYPOINTS
347347
libc.src.unistd.getentropy
348348
libc.src.unistd.geteuid
349349
libc.src.unistd.gethostname
350+
libc.src.unistd.getpagesize
350351
libc.src.unistd.getpid
351352
libc.src.unistd.getppid
352353
libc.src.unistd.getsid

libc/include/unistd.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ functions:
174174
arguments:
175175
- type: char *
176176
- type: size_t
177+
- name: getpagesize
178+
standards:
179+
- BSDExtensions
180+
return_type: int
181+
arguments:
182+
- type: void
177183
- name: getopt
178184
standards:
179185
- POSIX

libc/src/unistd/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,13 @@ add_entrypoint_object(
139139
.${LIBC_TARGET_OS}.gethostname
140140
)
141141

142+
add_entrypoint_object(
143+
getpagesize
144+
ALIAS
145+
DEPENDS
146+
.${LIBC_TARGET_OS}.getpagesize
147+
)
148+
142149
add_entrypoint_object(
143150
getpid
144151
ALIAS

libc/src/unistd/getpagesize.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for getpagesize -------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_UNISTD_GETPAGESIZE_H
10+
#define LLVM_LIBC_SRC_UNISTD_GETPAGESIZE_H
11+
12+
#include "src/__support/common.h"
13+
14+
namespace LIBC_NAMESPACE_DECL {
15+
16+
int getpagesize();
17+
18+
} // namespace LIBC_NAMESPACE_DECL
19+
20+
#endif // LLVM_LIBC_SRC_UNISTD_GETPAGESIZE_H

libc/src/unistd/linux/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,16 @@ add_entrypoint_object(
249249
libc.src.errno.errno
250250
)
251251

252+
add_entrypoint_object(
253+
getpagesize
254+
SRCS
255+
getpagesize.cpp
256+
HDRS
257+
../getpagesize.h
258+
DEPENDS
259+
libc.src.__support.OSUtil.linux.auxv
260+
)
261+
252262
add_entrypoint_object(
253263
geteuid
254264
SRCS
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===-- Linux implementation of getpagesize -------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/unistd/getpagesize.h"
10+
11+
#include "src/__support/OSUtil/linux/auxv.h"
12+
#include "src/__support/common.h"
13+
#include "src/__support/macros/config.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
LLVM_LIBC_FUNCTION(int, getpagesize, ()) {
18+
cpp::optional<unsigned long> page_size =
19+
(LIBC_NAMESPACE::auxv::get(AT_PAGESZ));
20+
if (page_size)
21+
return static_cast<int>(*page_size);
22+
return -1;
23+
}
24+
25+
} // namespace LIBC_NAMESPACE_DECL

libc/test/src/unistd/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,17 @@ add_libc_unittest(
477477
libc.test.UnitTest.ErrnoCheckingTest
478478
)
479479

480+
add_libc_unittest(
481+
getpagesize_test
482+
SUITE
483+
libc_unistd_unittests
484+
SRCS
485+
getpagesize_test.cpp
486+
DEPENDS
487+
libc.src.unistd.getpagesize
488+
libc.test.UnitTest.ErrnoCheckingTest
489+
)
490+
480491
add_libc_unittest(
481492
getgid_test
482493
SUITE
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===-- Unittests for getpagesize -----------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/unistd/getpagesize.h"
10+
11+
#include "test/UnitTest/ErrnoCheckingTest.h"
12+
#include "test/UnitTest/Test.h"
13+
14+
using LlvmLibcGetPageSizeTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
15+
16+
TEST(LlvmLibcGetPageSizeTest, GetPageSize) {
17+
// getpagesize doesn't modify errno
18+
int ret = LIBC_NAMESPACE::getpagesize();
19+
ASSERT_NE(ret, -1);
20+
ASSERT_NE(ret, 0);
21+
// Correct page size depends on the hardware mode, but will be a modulus of
22+
// 4096
23+
ASSERT_EQ(ret % 4096, 0);
24+
}

0 commit comments

Comments
 (0)