Skip to content

Instantly share code, notes, and snippets.

@s-u
Created October 22, 2014 15:21
Show Gist options
  • Save s-u/6712c97ca74181f5a1a5 to your computer and use it in GitHub Desktop.
Save s-u/6712c97ca74181f5a1a5 to your computer and use it in GitHub Desktop.
example of using custom mmap allocator in R
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <fcntl.h>
#include <Rinternals.h>
#include <R_ext/Rallocators.h>
static void *mmap_alloc(R_allocator_t *allocator, size_t length) {
int fd = open((char*) allocator->data, O_RDWR|O_CREAT, 0666);
if (fd < 0) return 0;
/* some OSes require the file to actually exist with the needed size - ensure that */
lseek(fd, length, SEEK_SET);
if (write(fd, "", 1)) {}
void *mem = mmap(0, length, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
close(fd);
Rprintf("mmap allocated '%s'\n", (char*) allocator->data);
allocator->data = mem ? (((char*)mem) + length) : 0;
return mem;
}
static void mmap_free(R_allocator_t *allocator, void *mem) {
Rprintf("mmap freed %p\n", mem);
munmap(mem, allocator->data ? (((char*)allocator->data) - ((char*) mem)) : 0);
}
SEXP mmap_vector(SEXP template, SEXP sFile, SEXP length) {
R_allocator_t allocator = { mmap_alloc, mmap_free, 0, 0 };
allocator.data = (void*) CHAR(STRING_ELT(sFile, 0));
return Rf_allocVector3(TYPEOF(template), asInteger(length), &allocator);
}
### v = .Call(1L, "test.bin", 1e4); v[1:10]=1:10; rm(v); gc()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment