B
Bill Pursell
This question involves code relying on mmap, and thus
is not maximally portable. Undoubtedly, many will
complain that my question is not topical...
I have two pointers, the first of which is mmapped
to a single page. I want to determine if the second
is on the page. I'd like to do:
#include "platform_appropriate_definition_of_PAGESIZE.h"
int compare1(const char *a, const char *b)
{
return (b>=a && b-a < PAGESIZE);
}
but this is clearly not legal, since I'm comparing
pointers that may not be comparable.
I'm further tempted to something like:
int compare2(const void *a, const void *b)
{
return ( ((int)b & PAGE_MASK) == (int)a);
}
I believe compare2 will work on any platform which
satisfies sizeof (int) == sizeof (void *) and on which
one can provide a suitable definition of PAGE_MASK.
I want to know if there are any language issues
involved with compare2() of which I should be aware.
Also, is there a way to write compare1() that is safe?
Finally, where in the FAQ is the question:
when is it safe to compare pointers? My understanding
is that a < b is safe if a and b point into the same object,
but I don't find that in the FAQ anywhere.
is not maximally portable. Undoubtedly, many will
complain that my question is not topical...
I have two pointers, the first of which is mmapped
to a single page. I want to determine if the second
is on the page. I'd like to do:
#include "platform_appropriate_definition_of_PAGESIZE.h"
int compare1(const char *a, const char *b)
{
return (b>=a && b-a < PAGESIZE);
}
but this is clearly not legal, since I'm comparing
pointers that may not be comparable.
I'm further tempted to something like:
int compare2(const void *a, const void *b)
{
return ( ((int)b & PAGE_MASK) == (int)a);
}
I believe compare2 will work on any platform which
satisfies sizeof (int) == sizeof (void *) and on which
one can provide a suitable definition of PAGE_MASK.
I want to know if there are any language issues
involved with compare2() of which I should be aware.
Also, is there a way to write compare1() that is safe?
Finally, where in the FAQ is the question:
when is it safe to compare pointers? My understanding
is that a < b is safe if a and b point into the same object,
but I don't find that in the FAQ anywhere.