标记/编码指针 [英] Tagging/Encoding Pointers

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

问题描述

我需要一种将指针标记为集合x或集合y的一部分的方法(即:标记只有2个状态"),我的意思是可以假定untagged = x和agged = y.

I need a way to tag a pointer as being either part of set x or part of set y (ie: the tag has only 2 'states'), I'm that means one can assume untagged = x and tagged = y.

目前,我正在考虑使用按位异或来实现此目的:

Currently I'm looking at using bitwise xor to do this:

ptr ^ magic = encoded_ptr
encoded_ptr ^ magic = ptr

但是我很困惑如何首先确定指针是否被标记. 我正在使用它来标记链接列表中的池节点来自何处,以便在取消链接时,它们可以返回到正确的perant.

but I'm stumped at how to determine if the pointer is tagged in the first place. I'm using this to mark what pools nodes in a linked list come from, so that when the are delinked, they can go back to the correct perants.

更新

只是为了向所有建议将标志存储在额外的数据成员中的人清楚,我仅限于sizeof(void*),所以我不能添加新成员,否则我会添加.而且这些池不是连续的,它们由许多页面组成,跟踪范围会增加过多的开销(我想使用一种 fast& 快速解决方案,如果可以的话)./p>

Just to make it clear to all those people suggesting to store the flag in extra data members, I'm limited to sizeof(void*), so I can't add new members, else I would have. Also the pools aren't contiguous, they consist of many pages, tracking the ranges would add too much overhead (I'm after a fast & simple solution, if one can call it that).

推荐答案

大多数解决方案都是特定于平台的.这里有一些:

Most solution will be platform specific. here a few of them:

1)由mallocnew返回的指针将对齐(4、8、16、32个字节,您可以为其命名).因此,在大多数体系结构上,地址的几个LSB位将始终为0.

1) A pointer returned by malloc or new will be aligned (4, 8, 16, 32 bytes, you name it). So, on most architectures, several LSB bits of the address will be always 0.

2)和Win32特定的方式:除非您的程序使用3GB开关,否则所有用户模式指针的值都小于0x80000000,因此可以将最高位用作标志.作为奖励,当取消标记的指针的引用而没有修复时,它也会崩溃.

2) And a Win32 specific way: unless your program uses 3GB switch, values of all usermode pointers are less than 0x80000000, so the highest bit can be used as flag. As bonus, it will also crash when the flagged pointer is dereferenced without being repaired.

这篇关于标记/编码指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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