A couple of things here…
I timed the attached test program of that bug report on my 10.10 system (the report was filed against OS X 10.7) and the gap between vm and mmap calls is now much smaller.
And more importantly, the way they're using the allocations, at least in the test program, is per 256KB block, and the test measures 51200 allocations and deallocations, which take a grand total of ~50ms all together.
In the case of HandmadeHero, the app makes about 3 calls to reserve memory from the OS and all at app startup time, so the sub-microsecond time difference between these two calls is irrelevant.
As for vm_allocate, I tried the following little test program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 | #include <mach/mach_init.h>
#include <mach/vm_map.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
// vm_address_t and vm_offset_t are just uint64_t on LP64 machines
vm_address_t address = 2ul * 1024 * 1024 * 1024 * 1024; // 2TB
vm_offset_t byteSize = 1ul * 1024 * 1024 * 1024; // 1GB
kern_return_t result = vm_allocate((vm_map_t)mach_task_self(),
&address,
byteSize,
VM_FLAGS_FIXED);
if (result == KERN_SUCCESS) {
printf("got mem at %lu\n", address);
}
else {
printf("failed to get memory, error: %d\n", result);
}
}
|
And that works fine.
Jeff, you posted as I was writing this up, are there other reasons to use mmap over vm_allocate?
--
Update: when you start reading/writing to the memory allocated by vm_allocate, the pages are zero filled, which is a requirement for the HH app.