32 |
|
#include <windows.h> |
33 |
|
#endif |
34 |
|
|
35 |
+ |
#include <errno.h> |
36 |
|
#include <stdio.h> |
37 |
|
#include <stdlib.h> |
38 |
|
#include <string.h> |
152 |
|
} |
153 |
|
#endif |
154 |
|
|
155 |
+ |
/* Translate Mach return codes to POSIX errno values. */ |
156 |
+ |
#ifdef HAVE_MACH_VM |
157 |
+ |
static int vm_error(kern_return_t ret_code) |
158 |
+ |
{ |
159 |
+ |
switch (ret_code) { |
160 |
+ |
case KERN_SUCCESS: |
161 |
+ |
return 0; |
162 |
+ |
case KERN_INVALID_ADDRESS: |
163 |
+ |
case KERN_NO_SPACE: |
164 |
+ |
return ENOMEM; |
165 |
+ |
case KERN_PROTECTION_FAILURE: |
166 |
+ |
return EACCES; |
167 |
+ |
default: |
168 |
+ |
return EINVAL; |
169 |
+ |
} |
170 |
+ |
} |
171 |
+ |
#endif |
172 |
+ |
|
173 |
|
/* Initialize the VM system. Returns 0 if successful, -1 for errors. */ |
174 |
|
|
175 |
|
int vm_init(void) |
205 |
|
void * vm_acquire(size_t size, int options) |
206 |
|
{ |
207 |
|
void * addr; |
208 |
+ |
|
209 |
+ |
errno = 0; |
210 |
|
|
211 |
|
// VM_MAP_FIXED are to be used with vm_acquire_fixed() only |
212 |
|
if (options & VM_MAP_FIXED) |
219 |
|
|
220 |
|
#ifdef HAVE_MACH_VM |
221 |
|
// vm_allocate() returns a zero-filled memory region |
222 |
< |
if (vm_allocate(mach_task_self(), (vm_address_t *)&addr, size, TRUE) != KERN_SUCCESS) |
222 |
> |
kern_return_t ret_code = vm_allocate(mach_task_self(), (vm_address_t *)&addr, size, TRUE); |
223 |
> |
if (ret_code != KERN_SUCCESS) { |
224 |
> |
errno = vm_error(ret_code); |
225 |
|
return VM_MAP_FAILED; |
226 |
+ |
} |
227 |
|
#else |
228 |
|
#ifdef HAVE_MMAP_VM |
229 |
|
int fd = zero_fd; |
268 |
|
|
269 |
|
int vm_acquire_fixed(void * addr, size_t size, int options) |
270 |
|
{ |
271 |
+ |
errno = 0; |
272 |
+ |
|
273 |
|
// Fixed mappings are required to be private |
274 |
|
if (options & VM_MAP_SHARED) |
275 |
|
return -1; |
281 |
|
|
282 |
|
#ifdef HAVE_MACH_VM |
283 |
|
// vm_allocate() returns a zero-filled memory region |
284 |
< |
if (vm_allocate(mach_task_self(), (vm_address_t *)&addr, size, 0) != KERN_SUCCESS) |
284 |
> |
kern_return_t ret_code = vm_allocate(mach_task_self(), (vm_address_t *)&addr, size, 0); |
285 |
> |
if (ret_code != KERN_SUCCESS) { |
286 |
> |
errno = vm_error(ret_code); |
287 |
|
return -1; |
288 |
+ |
} |
289 |
|
#else |
290 |
|
#ifdef HAVE_MMAP_VM |
291 |
|
int fd = zero_fd; |