runtime.h remove extern "C" and distribute normally instead of extra. (#15558)
[mono-project.git] / support / sys-mman.c
blobabf5015c749369eb7d1049a30b82878106856243
1 /*
2 * <sys/mman.h> wrapper functions.
4 * Authors:
5 * Jonathan Pryor (jonpryor@vt.edu)
7 * Copyright (C) 2004-2006 Jonathan Pryor
8 */
10 #include <config.h>
12 #ifndef __OpenBSD__
13 #define _XOPEN_SOURCE 600
14 #endif
16 #ifdef HOST_DARWIN
17 /* For mincore () */
18 #define _DARWIN_C_SOURCE
19 #endif
20 #if defined(__FreeBSD__) || defined(__OpenBSD__)
21 /* For mincore () */
22 #define __BSD_VISIBLE 1
23 #endif
25 #ifdef __NetBSD__
26 /* For mincore () */
27 #define _NETBSD_SOURCE
28 #endif
30 #include <sys/types.h>
31 #include <sys/mman.h>
32 #include <errno.h>
34 #include "mono/utils/mono-compiler.h"
35 #include "map.h"
36 #include "mph.h"
38 G_BEGIN_DECLS
40 void*
41 Mono_Posix_Syscall_mmap (void *start, mph_size_t length, int prot, int flags,
42 int fd, mph_off_t offset)
44 int _prot, _flags;
46 mph_return_val_if_size_t_overflow (length, MAP_FAILED);
47 mph_return_val_if_off_t_overflow (offset, MAP_FAILED);
49 if (Mono_Posix_FromMmapProts (prot, &_prot) == -1)
50 return MAP_FAILED;
51 if (Mono_Posix_FromMmapFlags (flags, &_flags) == -1)
52 return MAP_FAILED;
54 return mmap (start, (size_t) length, _prot, _flags, fd, (off_t) offset);
57 int
58 Mono_Posix_Syscall_munmap (void *start, mph_size_t length)
60 mph_return_if_size_t_overflow (length);
62 return munmap (start, (size_t) length);
65 int
66 Mono_Posix_Syscall_mprotect (void *start, mph_size_t len, int prot)
68 int _prot;
69 mph_return_if_size_t_overflow (len);
71 if (Mono_Posix_FromMmapProts (prot, &_prot) == -1)
72 return -1;
74 return mprotect (start, (size_t) len, _prot);
77 int
78 Mono_Posix_Syscall_msync (void *start, mph_size_t len, int flags)
80 int _flags;
81 mph_return_if_size_t_overflow (len);
83 if (Mono_Posix_FromMsyncFlags (flags, &_flags) == -1)
84 return -1;
86 return msync (start, (size_t) len, _flags);
89 int
90 Mono_Posix_Syscall_mlock (void *start, mph_size_t len)
92 #if !defined (HAVE_MLOCK)
93 return ENOSYS;
94 #else
95 mph_return_if_size_t_overflow (len);
97 return mlock (start, (size_t) len);
98 #endif
102 Mono_Posix_Syscall_munlock (void *start, mph_size_t len)
104 #if !defined (HAVE_MUNLOCK)
105 return ENOSYS;
106 #else
107 mph_return_if_size_t_overflow (len);
109 return munlock (start, (size_t) len);
110 #endif
113 #ifdef HAVE_MREMAP
114 void*
115 Mono_Posix_Syscall_mremap (void *old_address, mph_size_t old_size,
116 mph_size_t new_size, guint64 flags)
118 guint64 _flags;
120 mph_return_val_if_size_t_overflow (old_size, MAP_FAILED);
121 mph_return_val_if_size_t_overflow (new_size, MAP_FAILED);
123 if (Mono_Posix_FromMremapFlags (flags, &_flags) == -1)
124 return MAP_FAILED;
126 #if defined(linux)
127 return mremap (old_address, (size_t) old_size, (size_t) new_size,
128 (unsigned long) _flags);
129 #elif defined(__NetBSD__)
130 return mremap (old_address, (size_t) old_size, old_address,
131 (size_t) new_size, (unsigned long) _flags);
132 #else
133 #error Port me
134 #endif
136 #endif /* def HAVE_MREMAP */
139 Mono_Posix_Syscall_mincore (void *start, mph_size_t length, unsigned char *vec)
141 #if !defined (HAVE_MINCORE)
142 return ENOSYS;
143 #else
144 mph_return_if_size_t_overflow (length);
146 #if defined (__linux__) || defined (HOST_WASM)
147 typedef unsigned char T;
148 #else
149 typedef char T;
150 #endif
151 return mincore (start, (size_t) length, (T*)vec);
152 #endif
155 #ifdef HAVE_POSIX_MADVISE
156 gint32
157 Mono_Posix_Syscall_posix_madvise (void *addr, mph_size_t len, gint32 advice)
159 mph_return_if_size_t_overflow (len);
161 if (Mono_Posix_FromPosixMadviseAdvice (advice, &advice) == -1)
162 return -1;
164 return posix_madvise (addr, (size_t) len, advice);
166 #endif /* def HAVE_POSIX_MADVISE */
168 #ifdef HAVE_REMAP_FILE_PAGES
170 Mono_Posix_Syscall_remap_file_pages (void *start, mph_size_t size,
171 int prot, mph_ssize_t pgoff, int flags)
173 int _prot, _flags;
175 mph_return_if_size_t_overflow (size);
176 mph_return_if_ssize_t_overflow (pgoff);
178 if (Mono_Posix_FromMmapProts (prot, &_prot) == -1)
179 return -1;
180 if (Mono_Posix_FromMmapFlags (flags, &_flags) == -1)
181 return -1;
183 return remap_file_pages (start, (size_t) size, _prot, (ssize_t) pgoff, _flags);
185 #endif /* def HAVE_REMAP_FILE_PAGES */
187 G_END_DECLS
190 * vim: noexpandtab