Tag / Encoding Pointers

I need a way to mark the pointer as part of set x or part of set y (i.e. the tag has only 2 'states'), I want to be able to accept unagged = x and tagged = y.

I am currently looking for using bitwise xor for this:

ptr ^ magic = encoded_ptr
encoded_ptr ^ magic = ptr

but I don’t understand how to determine if the pointer is marked first. I use this to mark what happens to the pool nodes in a linked list, so that when they are overloaded, they can return to the correct words.

Update

Just to make it clear to all those people who offer to store the flag in additional data members, I am limited sizeof(void*), so I can not add new members, otherwise I would do it. Also, pools do not touch, they consist of many pages, tracking ranges will add too much overhead (after a quick and easy decision, so to speak).

+3
source share
5 answers

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

1) The pointer returned mallocor newwill be aligned (4, 8, 16, 32 bytes, you name it). Thus, on most architectures, a few bits of the LSB address will always be 0.

2) Win32: 3GB-, usermode 0x80000000, . , - .

+14

. , , (, n ), , . , "" , .

- , .

+8

, , , - , ?

+2

, std:: set.

, 2 , . "" ...

0

You can have ptr1 ^ magic = ptr2 with ptr1 in the set X and ptr2 in the set Y (unless you prove otherwise). Since (I think) you have no control over the addresses of the pointers that are given to you, your technique seems inadequate.

An alternative to Vinay’s solution is to save the tags as bits of a pre-allocated buffer (especially simple if the list size is limited, since you do not need to increase or decrease the buffer). This is a very compact and efficient solution that does not require changing the structure of pointed data.

Greetings

-stan

0
source

Source: https://habr.com/ru/post/1783245/


All Articles