Skip to content

Commit 172dcd7

Browse files
committed
changed munmap to return an ErrorOr
1 parent 30af3b2 commit 172dcd7

File tree

6 files changed

+20
-14
lines changed

6 files changed

+20
-14
lines changed

libc/src/__support/OSUtil/linux/munmap.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
1212
#include "src/__support/common.h"
1313

14-
#include "src/__support/libc_errno.h"
14+
#include "src/__support/error_or.h"
1515
#include "src/__support/macros/config.h"
1616
#include <sys/syscall.h> // For syscall numbers.
1717

@@ -20,9 +20,14 @@ namespace internal {
2020

2121
// This function is currently linux only. It has to be refactored suitably if
2222
// munmap is to be supported on non-linux operating systems also.
23-
int munmap(void *addr, size_t size) {
24-
return LIBC_NAMESPACE::syscall_impl<int>(SYS_munmap,
25-
reinterpret_cast<long>(addr), size);
23+
ErrorOr<int> munmap(void *addr, size_t size) {
24+
int ret = LIBC_NAMESPACE::syscall_impl<int>(
25+
SYS_munmap, reinterpret_cast<long>(addr), size);
26+
27+
if (ret < 0)
28+
return Error(-ret);
29+
30+
return 0;
2631
}
2732

2833
} // namespace internal

libc/src/__support/OSUtil/munmap.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_MUNMAP_H
1010
#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_MUNMAP_H
1111

12+
#include "src/__support/error_or.h"
1213
#include "src/__support/macros/config.h"
1314
#include <sys/mman.h> // For size_t and off_t
1415

1516
namespace LIBC_NAMESPACE_DECL {
1617
namespace internal {
1718

18-
int munmap(void *addr, size_t size);
19+
ErrorOr<int> munmap(void *addr, size_t size);
1920

2021
} // namespace internal
2122
} // namespace LIBC_NAMESPACE_DECL

libc/src/__support/OSUtil/prctl.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@
99
#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_PRCTL_H
1010
#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_PRCTL_H
1111

12-
#include "src/__support/macros/config.h"
1312
#include "src/__support/error_or.h"
14-
#include <sys/prctl.h>
1513
#include "src/__support/libc_errno.h"
14+
#include "src/__support/macros/config.h"
15+
#include <sys/prctl.h>
1616

1717
namespace LIBC_NAMESPACE_DECL {
1818
namespace internal {
1919

2020
ErrorOr<int> prctl(int option, unsigned long arg2, unsigned long arg3,
21-
unsigned long arg4, unsigned long arg5);
21+
unsigned long arg4, unsigned long arg5);
2222

2323
} // namespace internal
2424
} // namespace LIBC_NAMESPACE_DECL

libc/src/sys/auxv/linux/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ add_entrypoint_object(
1515
libc.src.unistd.read
1616
libc.src.unistd.close
1717
libc.include.sys_auxv
18-
)
18+
)

libc/src/sys/mman/linux/munmap.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@
1515
namespace LIBC_NAMESPACE_DECL {
1616

1717
LLVM_LIBC_FUNCTION(int, munmap, (void *addr, size_t size)) {
18-
int ret = internal::munmap(addr, size);
18+
auto ret = internal::munmap(addr, size);
1919

2020
// A negative return value indicates an error with the magnitude of the
2121
// value being the error code.
22-
if (ret < 0) {
23-
libc_errno = -ret;
22+
if (!ret.has_value()) {
23+
libc_errno = ret.error();
2424
return -1;
2525
}
2626

27-
return 0;
27+
return ret.value();
2828
}
2929

3030
} // namespace LIBC_NAMESPACE_DECL

libc/src/sys/prctl/linux/prctl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#include "src/sys/prctl/prctl.h"
1010

1111
#include "src/__support/OSUtil/prctl.h"
12-
#include "src/__support/libc_errno.h"
1312
#include "src/__support/common.h"
13+
#include "src/__support/libc_errno.h"
1414

1515
namespace LIBC_NAMESPACE_DECL {
1616

0 commit comments

Comments
 (0)