比较ptrs / mmap [英] comparing ptrs/mmap

查看:70
本文介绍了比较ptrs / mmap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题涉及依赖于mmap的代码,因此

不是最大程度的可移植性。毫无疑问,很多人会抱怨我的问题不是主题...


我有两个指针,第一个是mmapped

到一页。我想确定页面上是否有第二个

。我想这样做:


#include" platform_appropriate_definition_of_PAGESIZE.h"

int compare1(const char * a,const char * b)

{

return(b> = a&& ba< PAGESIZE);

}


但这显然不合法,因为我正在比较

指针,这些指针可能无法比较。


我进一步受到诱惑类似于:


int compare2(const void * a,const void * b)

{

return((( int)b& PAGE_MASK)==(int)a);

}


我相信compare2可以在任何平台上运行

满足sizeof(int)== sizeof(void *)以及

可以提供合适的PAGE_MASK定义。


我想要知道是否有任何语言问题

涉及compare2()我应该知道。

另外,有没有办法写compare1()这是安全的?


最后,谢谢常见问题解答中的问题是:

何时可以安全地比较指针?我的理解

是一个<如果a和b指向同一个对象,b是安全的,但是我在任何地方都没有找到它。


-

Bill Pursell

解决方案



Bill Pursell写道:
< blockquote class =post_quotes>
这个问题涉及依赖mmap的代码,因此

不是最大的可移植性。毫无疑问,很多人会抱怨我的问题不是主题...


我有两个指针,第一个是mmapped

到一页。我想确定页面上是否有第二个

。我想这样做:


#include" platform_appropriate_definition_of_PAGESIZE.h"

int compare1(const char * a,const char * b)

{

return(b> = a&& ba< PAGESIZE);

}


但这显然不合法,因为我正在比较

指针,这些指针可能无法比较。


我进一步受到诱惑类似于:


int compare2(const void * a,const void * b)

{

return((( int)b& PAGE_MASK)==(int)a);

}


我相信compare2可以在任何平台上运行

满足sizeof(int)== sizeof(void *)以及

可以提供合适的PAGE_MASK定义。


我想要知道是否有任何语言问题

涉及compare2()我应该知道。

另外,有没有办法写compare1()这是安全的?


最后,我在哪里常见问题是一个问题:

何时可以安全地比较指针?我的理解

是一个<如果a和b指向同一个对象,那么b是安全的,但是我在任何地方的FAQ中都找不到它。



虽然OT mmap可能会引入关于

指针性质的保证,但肯定有平台没有实现指针

的方式允许compare1()或compare2()产生你想要的

结果。也许最熟悉的是x86分段

寻址,在32位操作系统上不常见,但在较旧的16位操作系统上是通用的。

这样的指针由段ID和偏移量组成指定的

段。段和页面之间的实际关系最多为操作系统,并且可能非常复杂。因此1234:0123,2345:0123和

3456:9876可能指的是相同的位置,

同一页面上的不同位置,或完全不同的页面(假设系统有

页面。


从根本上说,投射指向int的指针的结果是

特别是不可移植的。


BTW,compare2()需要掩盖

比较右侧的值。


On Thu,2006年11月2日06:57:18 UTC,Bill Pursell

< bi ********** @ gmail。 comwrote:


这个问题涉及依赖于mmap的代码,因此

不是最大的可移植性。毫无疑问,很多人会抱怨我的问题不是主题...


我有两个指针,第一个是mmapped

到一页。我想确定页面上是否有第二个

。我想这样做:


#include" platform_appropriate_definition_of_PAGESIZE.h"

int compare1(const char * a,const char * b)

{

return(b> = a&& ba< PAGESIZE);

}


,但这显然不合法,因为我正在比较

指针可能无法比较。



这显然是绝对合法的。至少假设

整个过程确实是两个指针都引用了

相同的对象。


我很想做类似的事情:


int compare2(const void * a,const void * b)

{

return(((int)b& PAGE_MASK)==(int)a);

}



And这绝对是非法的,因为没有任何东西可以保证指针可以转换为int。 compare()将在每个系统上运行
,compare2以未定义的行为结束。


我相信compare2可以在任何平台上运行/>
满足sizeof(int)== sizeof(void *)以及

可以提供合适的PAGE_MASK定义。



Nowhay。没有什么可以保证size_t匹配sizof

void *甚至sizeof int。你在这里深处

未定义的行为。


我想知道是否有语言问题
涉及compare2()我应该知道。

另外,有没有办法写compare1()这是安全的?



您向我们展示的方式在任何系统上都有明确定义

指针正按照系统的定义进入。

如果没有,你绝对没办法用任何指示

两个指针。


最后,常见问题解答中的问题是:

何时可以安全地比较指针?我的理解

是一个< b如果a和b指向同一个对象,则是安全的,

但我在FAQ中找不到任何地方。



这是由标准的solong b点保证,不在对象的最后一个成员的最后一个成员之外,而且是在第一个和

最后+ 1的范围之内。


-

Tschau / Bye

Herbert


访问 http://www.ecomstation.de 德国eComStation的主页

eComStation 1.2 Deutsch ist da !


Herbert Rosenau写道:


On Thu,2006年11月2日06:57:18 UTC," ; Bill Pursell"

< bi ********** @ gmail.comwrote:



我有两个指针,第一个是mmapped

到一个页面。我想确定页面上是否有第二个

。我想这样做:


#include" platform_appropriate_definition_of_PAGESIZE.h"

int compare1(const char * a,const char * b)

{

return(b> = a&& ba< PAGESIZE);

}


,但这显然不合法,因为我正在比较

指针可能无法比较。



这显然是绝对合法的。至少当假设

整个过程确实是两个指针都引用了

相同的对象时。



我不知道指针是否引用同一个对象,并且

这就是我正在尝试的应付。我可以从线程中删除问题

的mmap并重新表达这个问题

方式:假设我有一个对象列表和一个指针:


struct obj_list {

struct obj * data;

struct obj_list * next;

} list;


void * b;


我知道b位于其中一个对象中,但我不知道

哪一个。我怎么弄清楚它是哪一个?

显而易见的是将b与每个数据的数据进行比较

指针在列表中(b(void *)data&& b<(void *) (数据+ 1)),

但这意味着大多数比较

都是在不在同一个对象上的指针上完成的。

-

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.

--
Bill Pursell

解决方案


Bill Pursell wrote:

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.

While the OT mmap may introduce guarantees about the nature of
pointers, there are certainly platforms that do not implement pointers
in a way that would allow compare1() or compare2() to produce the
result you want. Perhaps the most familiar is x86 segmented
addressing, uncommon on 32 bit OS, but universal on older 16 bit OSs.
Such a pointer consists of a segment ID and offset within the specified
segment. The actual relationship between segments and pages is up to
the OS, and may be quite complex. Thus 1234:0123, 2345:0123 and
3456:9876 might refer to the same location, different locations on the
same page, or different pages entirely (assuming that the system has
pages).

And fundamentally, the result of casting a pointer to an int is
specifically not portable.

BTW, compare2() needs to mask the value on the right side of the
comparison too.


On Thu, 2 Nov 2006 06:57:18 UTC, "Bill Pursell"
<bi**********@gmail.comwrote:

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.

That is clearly absolutely legal. At least when the assumption the
whole procedure does is true that both pointers are referencing to the
same object.

I''m further tempted to something like:

int compare2(const void *a, const void *b)
{
return ( ((int)b & PAGE_MASK) == (int)a);
}

And that is absolutely illegal because there is nothing that can
guarantee that pointer can ever casted to int. compare() will work on
each system, compare2 ends in undefined behavior.

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.

Nowhay. There is nothing that guaratees that size_t matches sizof
void* or even sizeof int. You''re here deep inside the lands of
undefined behavior.

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?

The way you showed us is well defined on any system solong both
pointers are coming in as they should by the definition of the system.
When not there is absolutely no way you can work with any realtion of
both pointers.

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.

That is guaranteed by the standard solong b points not outside the
last member of the object and a is inside the range of first and
last+1.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!


Herbert Rosenau wrote:

On Thu, 2 Nov 2006 06:57:18 UTC, "Bill Pursell"
<bi**********@gmail.comwrote:


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.


That is clearly absolutely legal. At least when the assumption the
whole procedure does is true that both pointers are referencing to the
same object.

I don''t know if the pointers reference the same object, and
that''s what I''m trying to cope with. I can remove the issue
of mmap from the thread and re-phrase the question this
way: suppose I have a list of objects and a pointer:

struct obj_list {
struct obj *data;
struct obj_list *next;
} list;

void *b;

I know that b lies in one of the objects, but I don''t know
which one. How do I figure out which one it''s in? The
obvious thing is to compare b with data for each data
pointer in the list ( b (void *)data && b < (void *)(data + 1)),
but that means that most of the comparisons
are being done on pointers that aren''t in the same object.
--
Bill Pursell


这篇关于比较ptrs / mmap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆