可以存储在指针可靠地工作的最低显著位无关的数据? [英] Can storing unrelated data in the least-significant-bit of a pointer work reliably?

查看:217
本文介绍了可以存储在指针可靠地工作的最低显著位无关的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想说前面那个什么我知道,就是我要提出一个弥天大罪,并且我会在地狱编程可能燃烧甚至考虑它。

Let me just say up front that what I'm aware that what I'm about to propose is a mortal sin, and that I will probably burn in Programming Hell for even considering it.

不过,我还是想知道,如果有任何理由,这是行不通的。

That said, I'm still interested in knowing if there's any reason why this wouldn't work.

的情况是:我有我到处使用一个引用计数智能指针类。目前,它看起来是这样的(注意:不完整/简化伪code):

The situation is: I have a reference-counting smart-pointer class that I use everywhere. It currently looks something like this (note: incomplete/simplified psuedocode):

class IRefCountable
{
public:
    IRefCountable() : _refCount(0) {}
    virtual ~IRefCountable() {}

    void Ref() {_refCount++;}
    bool Unref() {return (--_refCount==0);}

private:
    unsigned int _refCount;
};

class Ref
{
public:
   Ref(IRefCountable * ptr, bool isObjectOnHeap) : _ptr(ptr), _isObjectOnHeap(isObjectOnHeap) 
   { 
      _ptr->Ref();
   }

   ~Ref() 
   {
      if ((_ptr->Unref())&&(_isObjectOnHeap)) delete _ptr;
   }

private:
   IRefCountable * _ptr;
   bool _isObjectOnHeap;
};

今天,我发现的sizeof(REF)= 16。但是,如果我删除布尔成员变量_isObjectOnHeap,sizeof的(REF)被降低到8这意味着,在我的程序每一个参考,也有RAM 7.875浪费字节......还有很多很多参考文献在我的计划

Today I noticed that sizeof(Ref)=16. However, if I remove the boolean member variable _isObjectOnHeap, sizeof(Ref) is reduced to 8. That means that for every Ref in my program, there are 7.875 wasted bytes of RAM... and there are many, many Refs in my program.

好吧,这似乎像一些RAM的浪费。但我真的需要的信息,额外位(好吧,我的幽默和承担,我真的做了讨论的缘故)。而且我注意到,由于IRefCountable是一种非POD类,它会(presumably)始终就一个字对齐的内存地址分配。因此,(_Ptr)的至少显著位应始终为零。

Well, that seems like a waste of some RAM. But I really need that extra bit of information (okay, humor me and assume for the sake of the discussion that I really do). And I notice that since IRefCountable is a non-POD class, it will (presumably) always be allocated on a word-aligned memory address. Therefore, the least significant bit of (_ptr) should always be zero.

这使我不知道......没有任何理由,为什么我不能或者我的布尔数据位为指针的最低位显著,从而在不牺牲任何功能减少一半的sizeof(REF)?我不得不取消引用指针,当然,这将使指针引用效率较低之前要小心和出位,但可能的事实,裁判现在是更小的,因此更多的人来弥补可以放入处理器的高速缓存一次,依此类推。

Which makes me wonder... is there any reason why I can't OR my one bit of boolean data into the least-significant bit of the pointer, and thus reduce sizeof(Ref) by half without sacrificing any functionality? I'd have to be careful to AND out that bit before dereferencing the pointer, of course, which would make pointer dereferences less efficient, but that might be made up for by the fact that the Refs are now smaller, and thus more of them can fit into the processor's cache at once, and so on.

这是一个合理的事是什么?还是我自己设置为受伤的世界?如果是后者,究竟会疼会在我访问? (注意,这是code需要在所有合理现代台式环境中正常运行,但它并不需要在嵌入式机器或超级计算机或任何外来像运行)

Is this a reasonable thing to do? Or am I setting myself up for a world of hurt? And if the latter, how exactly would that hurt be visited upon me? (Note that this is code that needs to run correctly in all reasonably modern desktop environments, but it doesn't need to run in embedded machines or supercomputers or anything exotic like that)

推荐答案

任何的原因是什么?除非事情的标准已经改变最近,一个指针的值再presentation是实现定义的。这当然是可能的,一些地方的实施可能拉动同样的伎俩,定义了自己的目的,这些否则,未使用的低位。它更可能是一些实现可能使用文字三分球,而不是字节的指针,所以不是两个相邻的词处于地址0x8640和0x8642,他们将在地址0x4320和0x4321。

Any reason? Unless things have changed in the standard lately, the value representation of a pointer is implementation-defined. It is certainly possible that some implementation somewhere may pull the same trick, defining these otherwise-unused low bits for its own purposes. It's even more possible that some implementation might use word-pointers rather than byte-pointers, so instead of two adjacent words being at "addresses" 0x8640 and 0x8642, they would be at "addresses" 0x4320 and 0x4321.

一个棘手的解决该问题的方法是使参考一个(事实上)抽象类,和所有实例实际上是RefOnHeap和RefNotOnHeap的实例。如果有许多参考文献各处,在具有每价为一半大小用于存储code和元数据三个类而不是一个将由空间节省进行了额外的空间。(韩元'T工作也很好,编译器可以省略虚函数表的指针,如果没有虚方法,引入的虚方法将新增4或8个字节回类)。

One tricky way around the problem would be to make Ref a (de facto) abstract class, and all instances would actually be instances of RefOnHeap and RefNotOnHeap. If there are that many Refs around, the extra space used to store the code and metadata for three classes rather than one would be made up by the space savings in having each Ref being half the size. (Won't work too well, the compiler can omit the vtable pointer if there are no virtual methods and introducing virtual methods will add the 4-or-8 bytes back to the class).

这篇关于可以存储在指针可靠地工作的最低显著位无关的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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