[sgen] Remove some redundancy with nursery section
[mono-project.git] / support / sys-mman.c
blobafbe46624447d87a05a10d44e367427e0b45311d
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 PLATFORM_MACOSX
17 /* For mincore () */
18 #define _DARWIN_C_SOURCE
19 #endif
20 #ifdef __FreeBSD__
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 "map.h"
35 #include "mph.h"
37 G_BEGIN_DECLS
39 void*
40 Mono_Posix_Syscall_mmap (void *start, mph_size_t length, int prot, int flags,
41 int fd, mph_off_t offset)
43 int _prot, _flags;
45 mph_return_val_if_size_t_overflow (length, MAP_FAILED);
46 mph_return_val_if_off_t_overflow (offset, MAP_FAILED);
48 if (Mono_Posix_FromMmapProts (prot, &_prot) == -1)
49 return MAP_FAILED;
50 if (Mono_Posix_FromMmapFlags (flags, &_flags) == -1)
51 return MAP_FAILED;
53 return mmap (start, (size_t) length, _prot, _flags, fd, (off_t) offset);
56 int
57 Mono_Posix_Syscall_munmap (void *start, mph_size_t length)
59 mph_return_if_size_t_overflow (length);
61 return munmap (start, (size_t) length);
64 int
65 Mono_Posix_Syscall_mprotect (void *start, mph_size_t len, int prot)
67 int _prot;
68 mph_return_if_size_t_overflow (len);
70 if (Mono_Posix_FromMmapProts (prot, &_prot) == -1)
71 return -1;
73 return mprotect (start, (size_t) len, _prot);
76 int
77 Mono_Posix_Syscall_msync (void *start, mph_size_t len, int flags)
79 int _flags;
80 mph_return_if_size_t_overflow (len);
82 if (Mono_Posix_FromMsyncFlags (flags, &_flags) == -1)
83 return -1;
85 return msync (start, (size_t) len, _flags);
88 int
89 Mono_Posix_Syscall_mlock (void *start, mph_size_t len)
91 #if !defined(HAVE_MINCORE)
92 return ENOSYS;
93 #else
94 mph_return_if_size_t_overflow (len);
96 return mlock (start, (size_t) len);
97 #endif
101 Mono_Posix_Syscall_munlock (void *start, mph_size_t len)
103 #if defined(__HAIKU__)
104 return ENOSYS;
105 #else
106 mph_return_if_size_t_overflow (len);
108 return munlock (start, (size_t) len);
109 #endif
112 #ifdef HAVE_MREMAP
113 void*
114 Mono_Posix_Syscall_mremap (void *old_address, mph_size_t old_size,
115 mph_size_t new_size, guint64 flags)
117 guint64 _flags;
119 mph_return_val_if_size_t_overflow (old_size, MAP_FAILED);
120 mph_return_val_if_size_t_overflow (new_size, MAP_FAILED);
122 if (Mono_Posix_FromMremapFlags (flags, &_flags) == -1)
123 return MAP_FAILED;
125 #if defined(linux)
126 return mremap (old_address, (size_t) old_size, (size_t) new_size,
127 (unsigned long) _flags);
128 #elif defined(__NetBSD__)
129 return mremap (old_address, (size_t) old_size, old_address,
130 (size_t) new_size, (unsigned long) _flags);
131 #else
132 #error Port me
133 #endif
135 #endif /* def HAVE_MREMAP */
138 Mono_Posix_Syscall_mincore (void *start, mph_size_t length, unsigned char *vec)
140 #if defined(__HAIKU__)
141 return ENOSYS;
142 #else
143 mph_return_if_size_t_overflow (length);
145 return mincore (start, (size_t) length, (void*)vec);
146 #endif
149 #ifdef HAVE_POSIX_MADVISE
150 gint32
151 Mono_Posix_Syscall_posix_madvise (void *addr, mph_size_t len, gint32 advice)
153 mph_return_if_size_t_overflow (len);
155 if (Mono_Posix_FromPosixMadviseAdvice (advice, &advice) == -1)
156 return -1;
158 return posix_madvise (addr, (size_t) len, advice);
160 #endif /* def HAVE_POSIX_MADVISE */
162 #ifdef HAVE_REMAP_FILE_PAGES
164 Mono_Posix_Syscall_remap_file_pages (void *start, mph_size_t size,
165 int prot, mph_ssize_t pgoff, int flags)
167 int _prot, _flags;
169 mph_return_if_size_t_overflow (size);
170 mph_return_if_ssize_t_overflow (pgoff);
172 if (Mono_Posix_FromMmapProts (prot, &_prot) == -1)
173 return -1;
174 if (Mono_Posix_FromMmapFlags (flags, &_flags) == -1)
175 return -1;
177 return remap_file_pages (start, (size_t) size, _prot, (ssize_t) pgoff, _flags);
179 #endif /* def HAVE_REMAP_FILE_PAGES */
181 G_END_DECLS
184 * vim: noexpandtab