xref: /llvm-project/libc/src/sys/mman/linux/munmap.cpp (revision a0b65a7bcd6065688189b3d678c42ed6af9603db)
1 //===---------- Linux implementation of the POSIX munmap function ---------===//
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/sys/mman/munmap.h"
10 
11 #include "config/linux/syscall.h" // For internal syscall function.
12 #include "include/sys/syscall.h"  // For syscall numbers.
13 #include "src/__support/common.h"
14 #include "src/errno/llvmlibc_errno.h"
15 
16 namespace __llvm_libc {
17 
18 // This function is currently linux only. It has to be refactored suitably if
19 // mmap is to be supported on non-linux operating systems also.
20 LLVM_LIBC_FUNCTION(int, munmap, (void *addr, size_t size)) {
21   long ret_val =
22       __llvm_libc::syscall(SYS_munmap, reinterpret_cast<long>(addr), size);
23 
24   // A negative return value indicates an error with the magnitude of the
25   // value being the error code.
26   if (ret_val < 0) {
27     llvmlibc_errno = -ret_val;
28     return -1;
29   }
30 
31   return 0;
32 }
33 
34 } // namespace __llvm_libc
35