Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <stdio.h>
#include <mach/mach.h>
#include <mach/mach_vm.h>
static const size_t LENGTH = 10;
static const size_t SIZE = sizeof(int) * LENGTH;
void fillBuffer(int* buffer, size_t length)
{
for (size_t i = 0; i < length; ++i)
buffer[i] = i;
}
#define PRINT_BUFFER(buffer, length) \
printf(#buffer " = { "); \
printBuffer(buffer, length); \
printf(" }\n");
void printBuffer(int* buffer, size_t length)
{
if (length) {
printf("%d", buffer[0]);
for (size_t i = 1; i < length; ++i)
printf(", %d", buffer[i]);
}
}
int main()
{
int* a = 0;
mach_vm_allocate(mach_task_self(), (mach_vm_address_t*)&a, SIZE, VM_FLAGS_ANYWHERE);
fillBuffer(a, LENGTH);
mach_port_t handle = 0;
vm_size_t size = vm_page_size;
mach_make_memory_entry(mach_task_self(), &size, (mach_vm_address_t)a, VM_PROT_READ | VM_PROT_WRITE, &handle, 0);
int* b = 0;
mach_vm_map(mach_task_self(), (mach_vm_address_t*)&b, vm_page_size, 0, TRUE, handle, 0, TRUE, VM_PROT_READ | VM_PROT_WRITE, VM_PROT_READ | VM_PROT_WRITE, VM_INHERIT_DEFAULT);
fillBuffer(a + 1, LENGTH - 1);
printf("a = %p\n", a);
printf("b = %p\n", b);
PRINT_BUFFER(a, LENGTH);
PRINT_BUFFER(b, LENGTH);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.