munmap slowness; IsBadReadPtr considered harmful

Corinna Vinschen
Tue Feb 24 16:57:00 GMT 2004

On Feb 23 18:04, Brian Ford wrote:
> Ok, no response.  That means either:
> 1.) No one knows an alternative way to do the check.
> 2.) No one cares that munmap has this overhead.
> 3.) No one believes me, or they can't reproduce the problem.
> 4.) No one likes me :( ...

5.) No one has enough spare time currently.

> Ok, now back on topic :).
> FWIW, the following replacement for IsBadReadPtr appears to have the
> same functionality without the associated page fault overhead.
> static inline bool
> is_addr_valid (void *addr, size_t len)
> {
>   void *end;
>   for (end = (char *) addr + len; addr < end;
>        addr = (char *) addr + mbuf.RegionSize)
>     {
>       if (!VirtualQuery (addr, &mbuf, sizeof mbuf))
>         {
>           syscall_printf ("VirtualQuery (addr %x) %E", addr);
>           return false;
>         }
>     }
>   return true;
> }
> So, any comments about the function above, where to put it, or what to
> name it?

IMHO, the name's ok.  It could perhaps get its place in

> I see in the archives that the IsBadXPtr vs. VirtualQuery debate has
> happened before in different contexts.  It seams to me that VirtualQuery
> wins here hands down, though.

Did you check on 9x and NT?  Is that really correctly implemented?
AFAIU VirtualQuery, it returns information about consecutive pages
which share the same attributes.  So the length returned must be >=
the len parameter or you must check the actual attributes returned
by VirtualQuery.  One possible attribute would be PAGE_NOACCESS. 
In that case you would return true, even though the pages are not
accessible, right?


Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Developer                      
Red Hat, Inc.

More information about the Cygwin-developers mailing list