• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #pragma once
30 
31 #include <sys/cdefs.h>
32 #include <sys/types.h>
33 #include <linux/memfd.h>
34 #include <linux/mman.h>
35 #include <linux/uio.h>
36 
37 __BEGIN_DECLS
38 
39 /** Alternative spelling of the `MAP_ANONYMOUS` flag for mmap(). */
40 #define MAP_ANON MAP_ANONYMOUS
41 
42 /** Return value for mmap(). */
43 #define MAP_FAILED __BIONIC_CAST(reinterpret_cast, void*, -1)
44 
45 /**
46  * [mmap(2)](http://man7.org/linux/man-pages/man2/mmap.2.html)
47  * creates a memory mapping for the given range.
48  *
49  * Returns the address of the mapping on success,
50  * and returns `MAP_FAILED` and sets `errno` on failure.
51  */
52 #if defined(__USE_FILE_OFFSET64)
53 void* mmap(void* __addr, size_t __size, int __prot, int __flags, int __fd, off_t __offset) __RENAME(mmap64);
54 #else
55 void* mmap(void* __addr, size_t __size, int __prot, int __flags, int __fd, off_t __offset);
56 #endif
57 
58 #if __ANDROID_API__ >= 21
59 /**
60  * mmap64() is a variant of mmap() that takes a 64-bit offset even on LP32.
61  *
62  * See https://android.googlesource.com/platform/bionic/+/master/docs/32-bit-abi.md
63  *
64  * mmap64 wasn't really around until L, but we added an inline for it since it
65  * allows a lot more code to compile with _FILE_OFFSET_BITS=64.
66  */
67 void* mmap64(void* __addr, size_t __size, int __prot, int __flags, int __fd, off64_t __offset) __INTRODUCED_IN(21);
68 #endif
69 
70 /**
71  * [munmap(2)](http://man7.org/linux/man-pages/man2/munmap.2.html)
72  * deletes a memory mapping for the given range.
73  *
74  * Returns 0 on success, and returns -1 and sets `errno` on failure.
75  */
76 int munmap(void* __addr, size_t __size);
77 
78 /**
79  * [msync(2)](http://man7.org/linux/man-pages/man2/msync.2.html)
80  * flushes changes to a memory-mapped file to disk.
81  *
82  * Returns 0 on success, and returns -1 and sets `errno` on failure.
83  */
84 int msync(void* __addr, size_t __size, int __flags);
85 
86 /**
87  * [mprotect(2)](http://man7.org/linux/man-pages/man2/mprotect.2.html)
88  * sets the protection on a memory region.
89  *
90  * Returns 0 on success, and returns -1 and sets `errno` on failure.
91  */
92 int mprotect(void* __addr, size_t __size, int __prot);
93 
94 /** Flag for mremap(). */
95 #define MREMAP_MAYMOVE  1
96 
97 /** Flag for mremap(). */
98 #define MREMAP_FIXED    2
99 
100 /**
101  * [mremap(2)](http://man7.org/linux/man-pages/man2/mremap.2.html)
102  * expands or shrinks an existing memory mapping.
103  *
104  * Returns the address of the mapping on success,
105  * and returns `MAP_FAILED` and sets `errno` on failure.
106  */
107 void* mremap(void* __old_addr, size_t __old_size, size_t __new_size, int __flags, ...);
108 
109 /**
110  * [mlockall(2)](http://man7.org/linux/man-pages/man2/mlockall.2.html)
111  * locks pages (preventing swapping).
112  *
113  * Returns 0 on success, and returns -1 and sets `errno` on failure.
114  */
115 int mlockall(int __flags) __INTRODUCED_IN(17);
116 
117 /**
118  * [munlockall(2)](http://man7.org/linux/man-pages/man2/munlockall.2.html)
119  * unlocks pages (allowing swapping).
120  *
121  * Returns 0 on success, and returns -1 and sets `errno` on failure.
122  */
123 int munlockall(void) __INTRODUCED_IN(17);
124 
125 /**
126  * [mlock(2)](http://man7.org/linux/man-pages/man2/mlock.2.html)
127  * locks pages (preventing swapping).
128  *
129  * Returns 0 on success, and returns -1 and sets `errno` on failure.
130  */
131 int mlock(const void* __addr, size_t __size);
132 
133 /**
134  * [mlock2(2)](http://man7.org/linux/man-pages/man2/mlock.2.html)
135  * locks pages (preventing swapping), with optional flags.
136  *
137  * Returns 0 on success, and returns -1 and sets `errno` on failure.
138  */
139 int mlock2(const void* __addr, size_t __size, int __flags) __INTRODUCED_IN(30);
140 
141 /**
142  * [munlock(2)](http://man7.org/linux/man-pages/man2/munlock.2.html)
143  * unlocks pages (allowing swapping).
144  *
145  * Returns 0 on success, and returns -1 and sets `errno` on failure.
146  */
147 int munlock(const void* __addr, size_t __size);
148 
149 /**
150  * [mincore(2)](http://man7.org/linux/man-pages/man2/mincore.2.html)
151  * tests whether pages are resident in memory.
152  *
153  * Returns 0 on success, and returns -1 and sets `errno` on failure.
154  */
155 int mincore(void* __addr, size_t __size, unsigned char* __vector);
156 
157 /**
158  * [madvise(2)](http://man7.org/linux/man-pages/man2/madvise.2.html)
159  * gives the kernel advice about future usage patterns.
160  *
161  * Returns 0 on success, and returns -1 and sets `errno` on failure.
162  */
163 int madvise(void* __addr, size_t __size, int __advice);
164 
165 /**
166  * [process_madvise(2)](http://man7.org/linux/man-pages/man2/process_madvise.2.html)
167  * works just like madvise(2) but applies to the process specified by the given
168  * PID file descriptor.
169  *
170  * Returns the number of bytes advised on success, and returns -1 and sets `errno` on failure.
171  */
172 ssize_t process_madvise(int __pid_fd, const struct iovec* __iov, size_t __count, int __advice, unsigned __flags);
173 
174 #if defined(__USE_GNU)
175 
176 /**
177  * [memfd_create(2)](http://man7.org/linux/man-pages/man2/memfd_create.2.html)
178  * creates an anonymous file.
179  *
180  * Returns an fd on success, and returns -1 and sets `errno` on failure.
181  */
182 int memfd_create(const char* __name, unsigned __flags) __INTRODUCED_IN(30);
183 
184 #endif
185 
186 #if __ANDROID_API__ >= 23
187 
188 /*
189  * Some third-party code uses the existence of POSIX_MADV_NORMAL to detect the
190  * availability of posix_madvise. This is not correct, since having up-to-date
191  * UAPI headers says nothing about the C library, but for the time being we
192  * don't want to harm adoption of the unified headers.
193  *
194  * https://github.com/android-ndk/ndk/issues/395
195  */
196 
197 /** Flag for posix_madvise(). */
198 #define POSIX_MADV_NORMAL     MADV_NORMAL
199 /** Flag for posix_madvise(). */
200 #define POSIX_MADV_RANDOM     MADV_RANDOM
201 /** Flag for posix_madvise(). */
202 #define POSIX_MADV_SEQUENTIAL MADV_SEQUENTIAL
203 /** Flag for posix_madvise(). */
204 #define POSIX_MADV_WILLNEED   MADV_WILLNEED
205 /** Flag for posix_madvise(). */
206 #define POSIX_MADV_DONTNEED   MADV_DONTNEED
207 
208 #endif
209 
210 /**
211  * [posix_madvise(3)](http://man7.org/linux/man-pages/man3/posix_madvise.3.html)
212  * gives the kernel advice about future usage patterns.
213  *
214  * Returns 0 on success, and returns a positive error number on failure.
215  *
216  * See also madvise() which has been available much longer.
217  */
218 int posix_madvise(void* __addr, size_t __size, int __advice) __INTRODUCED_IN(23);
219 
220 __END_DECLS
221 
222 #include <android/legacy_sys_mman_inlines.h>
223