#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <inttypes.h>

int main(void) {
  int64_t i, j, size=0, memsize=0;
  FILE *o;
  int fd;
  void *mem = NULL, *mmem = NULL;
  o = fopen("test.dat", "w+");
  fd = fileno(o);
  for (i=0; i<1e6; i++) {
    size += 100000; //Allocates up to 100GB for mmap()ed file
    memsize += 3000; //Allocates up to 3GB for in-memory usage
    //ftruncate(fd, size);  //<--- this would be the correct usage
    mmem = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
    mem = realloc(mem, memsize);
    memset(mem, i%256, memsize);
    memset(mmem+size-100000, i%256, 100000);
    munmap(mmem, size);
  }
  return 0;
}
