Des*_*ost 1 c pointers complement
我正在查看"search.h"C库中"tfind"函数的源代码,我偶然发现了这一行:
#define DEREFNODEPTR(NP) (node)((uintptr_t)(*(NP)) & ~((uintptr_t) 0x1))
Run Code Online (Sandbox Code Playgroud)
这就是它的用法:
/* Find datum in search tree.
KEY is the key to be located, ROOTP is the address of tree root,
COMPAR the ordering function. */
void *
__tfind (const void *key, void *const *vrootp, __compar_fn_t compar)
{
node root;
node *rootp = (node *) vrootp;
if (rootp == NULL)
return NULL;
root = DEREFNODEPTR(rootp);
CHECK_TREE (root);
while (DEREFNODEPTR(rootp) != NULL)
{
root = DEREFNODEPTR(rootp);
int r;
r = (*compar) (key, root->key);
if (r == 0)
return root;
rootp = r < 0 ? LEFTPTR(root) : RIGHTPTR(root);
}
return NULL;
}
Run Code Online (Sandbox Code Playgroud)
那么,为什么在这种情况下需要补充一个?
感谢您的时间和考虑.
可能的情况是,实现假设节点指针是始终至少2字节对齐的节点.这意味着有效节点指针的最低有效位始终为0.这允许该位被"用于"存储状态(例如它之前是否被访问过,或者它是红色还是黑色节点,还是其他一些东西......).
在将值作为指针访问之前,宏清除LSB.没有人的补充要求.
| 归档时间: |
|
| 查看次数: |
83 次 |
| 最近记录: |