39 |
|
#endif |
40 |
|
|
41 |
|
#ifdef HAVE_MMAP_VM |
42 |
+ |
static char * next_address = 0; |
43 |
|
#ifdef HAVE_MMAP_ANON |
44 |
|
#define map_flags (MAP_PRIVATE | MAP_ANON) |
45 |
|
#define zero_fd -1 |
96 |
|
return VM_MAP_FAILED; |
97 |
|
#else |
98 |
|
#ifdef HAVE_MMAP_VM |
99 |
< |
if ((addr = mmap(0, size, VM_PAGE_DEFAULT, map_flags, zero_fd, 0)) == MAP_FAILED) |
99 |
> |
if ((addr = mmap(next_address, size, VM_PAGE_DEFAULT, map_flags, zero_fd, 0)) == MAP_FAILED) |
100 |
|
return VM_MAP_FAILED; |
101 |
|
|
102 |
+ |
next_address = (char *)addr + size; |
103 |
+ |
|
104 |
|
// Since I don't know the standard behavior of mmap(), zero-fill here |
105 |
|
if (memset(addr, 0, size) != addr) |
106 |
|
return VM_MAP_FAILED; |
157 |
|
|
158 |
|
int vm_release(void * addr, size_t size) |
159 |
|
{ |
160 |
+ |
// Safety check: don't try to release memory that was not allocated |
161 |
+ |
if (addr == VM_MAP_FAILED) |
162 |
+ |
return 0; |
163 |
+ |
|
164 |
|
#ifdef HAVE_MACH_VM |
165 |
|
int ret_code = vm_deallocate(mach_task_self(), (vm_address_t)addr, size); |
166 |
|
return ret_code == KERN_SUCCESS ? 0 : -1; |