2007-01-22 Miguel de Icaza <miguel@novell.com>
[mono-project.git] / support / sys-mman.c
blob346c97d6df61c8f36b67428ab2907bdf8d57c2cc
1 /*
2 * <sys/mman.h> wrapper functions.
4 * Authors:
5 * Jonathan Pryor (jonpryor@vt.edu)
7 * Copyright (C) 2004-2006 Jonathan Pryor
8 */
10 #define _XOPEN_SOURCE 600
12 #include <sys/types.h>
13 #include <sys/mman.h>
14 #include <errno.h>
16 #include "map.h"
17 #include "mph.h"
19 G_BEGIN_DECLS
21 void*
22 Mono_Posix_Syscall_mmap (void *start, mph_size_t length, int prot, int flags,
23 int fd, mph_off_t offset)
25 int _prot, _flags;
27 mph_return_val_if_size_t_overflow (length, MAP_FAILED);
28 mph_return_val_if_off_t_overflow (offset, MAP_FAILED);
30 if (Mono_Posix_FromMmapProts (prot, &_prot) == -1)
31 return MAP_FAILED;
32 if (Mono_Posix_FromMmapFlags (flags, &_flags) == -1)
33 return MAP_FAILED;
35 return mmap (start, (size_t) length, _prot, _flags, fd, (off_t) offset);
38 int
39 Mono_Posix_Syscall_munmap (void *start, mph_size_t length)
41 mph_return_if_size_t_overflow (length);
43 return munmap (start, (size_t) length);
46 int
47 Mono_Posix_Syscall_mprotect (void *start, mph_size_t len, int prot)
49 int _prot;
50 mph_return_if_size_t_overflow (len);
52 if (Mono_Posix_FromMmapProts (prot, &_prot) == -1)
53 return -1;
55 return mprotect (start, (size_t) len, _prot);
58 int
59 Mono_Posix_Syscall_msync (void *start, mph_size_t len, int flags)
61 int _flags;
62 mph_return_if_size_t_overflow (len);
64 if (Mono_Posix_FromMsyncFlags (flags, &_flags) == -1)
65 return -1;
67 return msync (start, (size_t) len, _flags);
70 int
71 Mono_Posix_Syscall_mlock (void *start, mph_size_t len)
73 mph_return_if_size_t_overflow (len);
75 return mlock (start, (size_t) len);
78 int
79 Mono_Posix_Syscall_munlock (void *start, mph_size_t len)
81 mph_return_if_size_t_overflow (len);
83 return munlock (start, (size_t) len);
86 #ifdef HAVE_MREMAP
87 void*
88 Mono_Posix_Syscall_mremap (void *old_address, mph_size_t old_size,
89 mph_size_t new_size, guint64 flags)
91 guint64 _flags;
93 mph_return_val_if_size_t_overflow (old_size, MAP_FAILED);
94 mph_return_val_if_size_t_overflow (new_size, MAP_FAILED);
96 if (Mono_Posix_FromMremapFlags (flags, &_flags) == -1)
97 return MAP_FAILED;
99 return mremap (old_address, (size_t) old_size, (size_t) new_size,
100 (unsigned long) _flags);
102 #endif /* def HAVE_MREMAP */
105 Mono_Posix_Syscall_mincore (void *start, mph_size_t length, unsigned char *vec)
107 mph_return_if_size_t_overflow (length);
109 return mincore (start, (size_t) length, vec);
112 #ifdef HAVE_POSIX_MADVISE
113 gint32
114 Mono_Posix_Syscall_posix_madvise (void *addr, mph_size_t len, gint32 advice)
116 mph_return_if_size_t_overflow (len);
118 if (Mono_Posix_FromPosixMadviseAdvice (advice, &advice) == -1)
119 return -1;
121 return posix_madvise (addr, (size_t) len, advice);
123 #endif /* def HAVE_POSIX_MADVISE */
125 #ifdef HAVE_REMAP_FILE_PAGES
127 Mono_Posix_Syscall_remap_file_pages (void *start, mph_size_t size,
128 int prot, mph_ssize_t pgoff, int flags)
130 int _prot, _flags;
132 mph_return_if_size_t_overflow (size);
133 mph_return_if_ssize_t_overflow (pgoff);
135 if (Mono_Posix_FromMmapProts (prot, &_prot) == -1)
136 return -1;
137 if (Mono_Posix_FromMmapFlags (flags, &_flags) == -1)
138 return -1;
140 return remap_file_pages (start, (size_t) size, _prot, (ssize_t) pgoff, _flags);
142 #endif /* def HAVE_REMAP_FILE_PAGES */
144 G_END_DECLS
147 * vim: noexpandtab