Skip to content

Instantly share code, notes, and snippets.

@methodmissing
Created January 19, 2010 21:18
Show Gist options
  • Select an option

  • Save methodmissing/281307 to your computer and use it in GitHub Desktop.

Select an option

Save methodmissing/281307 to your computer and use it in GitHub Desktop.
static void *
alloc_ruby_heap_with_file(size_t size)
{
FileHeapAllocatorMetaData meta;
meta.fd = open("/dev/zero", O_RDONLY);
meta.size = size;
if (meta.fd == -1) {
return NULL;
} else {
void *memory = mmap(NULL, size + sizeof(meta), PROT_READ | PROT_WRITE,
MAP_PRIVATE, meta.fd, 0);
if (memory == NULL) {
return NULL;
} else {
memcpy(memory, &meta, sizeof(meta));
return memory + sizeof(meta);
}
}
}
static void *
alloc_ruby_heap(size_t size)
{
if (debug_options.alloc_heap_with_file) {
return alloc_ruby_heap_with_file(size);
} else {
return malloc(size);
}
}
static void
free_ruby_heap_with_file(void *heap)
{
FileHeapAllocatorMetaData *meta = (FileHeapAllocatorMetaData *)
(heap - sizeof(FileHeapAllocatorMetaData));
close(meta->fd);
munmap(heap, meta->size + sizeof(FileHeapAllocatorMetaData));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment