public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* how to get mmap page size?
@ 2009-01-16 15:11 Jay Foad
  2009-01-16 15:25 ` Corinna Vinschen
  2009-01-16 15:34 ` Phil Betts
  0 siblings, 2 replies; 7+ messages in thread
From: Jay Foad @ 2009-01-16 15:11 UTC (permalink / raw)
  To: cygwin

I have an application that wants to use mmap() to read a file, but
only if it can guarantee that this will leave one or more zero bytes
after the end of the contents of the file in memory:

  if ((filesize & (pagesize - 1) != 0)
    use_mmap();
  else
    use_read();

How do I get the appropriate page size on cygwin?

If I call getpagesize() it returns 64k, the "allocation granularity".
Internally cygwin has a getsystempagesize() (introduced here:
http://cygwin.com/ml/cygwin-cvs/2005-q4/msg00099.html) that returns
the appropriate page size, 4k, but that doesn't seem to be exposed to
applications.

I know that this has been discussed before, e.g. here:

http://www.cygwin.com/ml/cygwin/2002-01/msg00747.html

but I haven't found an answer to this particular question.

Thanks,
Jay.

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

^ permalink raw reply	[flat|nested] 7+ messages in thread
* Re: how to get mmap page size?
@ 2009-01-16 16:11 Jay Foad
  2009-01-16 17:21 ` Corinna Vinschen
  0 siblings, 1 reply; 7+ messages in thread
From: Jay Foad @ 2009-01-16 16:11 UTC (permalink / raw)
  To: cygwin

Corinna Vinschen wrote:

> mmap always allocates in 64K chunks.

That doesn't seem to be true in practice. In the test below I mmap a
1080K file (this is a multiple of 4K but not a multiple of 64K). The
first byte after the end of the file isn't readable.

$ cat mmaptest.c
#include <fcntl.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/stat.h>
int main(int argc, char *argv[])
{
        int fd;
        struct stat st;
        void *p;
        if (argc != 2) return 1;
        if ((fd = open(argv[1], O_RDONLY, 0)) < 0) return 1;
        if (fstat(fd, &st) != 0) return 1;
        if ((p = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd,
0)) == MAP_FAILED) return 1;
        printf("last byte 0x%02X\n", ((unsigned char *)p)[st.st_size - 1]);
        fflush(stdout);
        printf("next byte 0x%02X\n", ((unsigned char *)p)[st.st_size]);
        return 0;
}

$ gcc -o mmaptest mmaptest.c

$ dd bs=1080K count=1 if=/dev/random of=foo
1+0 records in
1+0 records out
1105920 bytes (1.1 MB) copied, 0.077 s, 14.4 MB/s

$ ./mmaptest foo
last byte 0x5F
      9 [main] mmaptest 3040 _cygtls::handle_exceptions: Error while dumping sta
te (probably corrupted stack)
Segmentation fault (core dumped)

This is a recent cygwin installation on XP Pro SP2.

Thanks,
Jay.

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

^ permalink raw reply	[flat|nested] 7+ messages in thread
* Re: how to get mmap page size?
@ 2009-01-16 17:28 Jay Foad
  2009-01-18  1:04 ` Corinna Vinschen
  0 siblings, 1 reply; 7+ messages in thread
From: Jay Foad @ 2009-01-16 17:28 UTC (permalink / raw)
  To: cygwin

Corinna Vinschen wrote:

> Right.  The reason is that mmap started to map memory top down at one
> point to fix a problem with Windows Vista.  The mechanism to align
> the map always to 64K is still in Cygwin, but it currently doesn't
> work for files.  I'll have a look into fixing that for Cygwin 1.7.

OK, thanks. In the mean time I guess I'll just use a hard coded value
of 4K for the page size in my application.

Thanks,
Jay.

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2009-01-17 17:23 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-01-16 15:11 how to get mmap page size? Jay Foad
2009-01-16 15:25 ` Corinna Vinschen
2009-01-16 15:34 ` Phil Betts
2009-01-16 16:11 Jay Foad
2009-01-16 17:21 ` Corinna Vinschen
2009-01-16 17:28 Jay Foad
2009-01-18  1:04 ` Corinna Vinschen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).