Web lists-archives.com

Atomic mmap replacement

Some code in emacs wants to reserve a chunk of address space with a big PROT_NONE anonymous mapping, and then carve it up into separate mappings associated to segments of a file. This fails on Cygwin. Here's a test case that illustrates the problem:

$ truncate -s 64k foo

$ cat mmap_test.c
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/mman.h>

const size_t page_size = 64 * 1024;

main ()
  void *mem = mmap (NULL, 2 * page_size,
                    PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  if (mem == MAP_FAILED)
      perror ("mmap");
      exit (1);
  int fd = open ("foo", O_RDONLY);
  void *res = mmap (mem, page_size, PROT_READ | PROT_WRITE,
                    MAP_PRIVATE | MAP_FIXED, fd, 0);
  if (res == MAP_FAILED)
      perror ("mmap");
      exit (2);

$ gcc mmap_test.c

$ ./a
mmap: Invalid argument

$ echo $?

Is this a bug, or is it simply a limitation of Cygwin's mmap? If the latter, is there a simple workaround?


P.S. For context, you can start reading here, but it might not make a lot of sense:


Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple