Reputation: 958
While I was reading this code of an ART implementation, I did not understand the reason behind calling SET_LEAF to do bitwise or with pointer address
#define SET_LEAF(x) ((void*)((uintptr_t)x | 1))
*ref = (art_node*)SET_LEAF(make_leaf(key, key_len, value));
I also saw
#define IS_LEAF(x) (((uintptr_t)x & 1))
which looks at the least significant bit to check if it was a leaf node. Now the whole argument would work only if the pointer address is an even number (0x100504080).
2 | 1 // 3
3 | 1 // 3
Is it guaranteed that the pointer address would always be an even number across all the architecture?
Edit: Possible duplicate How come local types always get an even address
Question on top: Can i safely assume that the pointer would always be starting from event number no matter what kind of data I have?
Upvotes: 1
Views: 137
Reputation: 35560
It is technically undefined behavior, but considering the fact that they use _mm_movemask_epi8
, they probably don't care about portability. In x86 with standard glibc malloc, the returned address should always be aligned to a word (8 bytes in 64 bit), which means that it's always even.
Upvotes: 1