Tagging/Encoding Pointers

前端 未结 5 691
青春惊慌失措
青春惊慌失措 2021-01-05 19:11

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.

5条回答
  •  渐次进展
    2021-01-05 19:39

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

    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) 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.

提交回复
热议问题