The various function depend on UINTPTR_MAX
having its 2 LSBits set. Re-coded to eliminate that dependency.
Some simplifications.
Performed comparison for bool
return.
Be sure these functions, when called, were properly declared or prototyped. Else the return value is assumed to be int
and could explain the 0x100
return.
The problem may still lie elsewhere. Are you expecting calls like setFlag1(p)
to set p
's bit (which code does not do) or return a pointer with the bit set (which code does)?
static inline struct node* getAddress(struct node* p) {
return (struct node*)((uintptr_t) p & ~((uintptr_t) 3));
}
static inline bool isFlag1(struct node* p) {
return ((uintptr_t) p & 1) != 0;
}
static inline bool isFlag2(struct node* p) {
return ((uintptr_t) p & 2) != 0;
}
static inline struct node* setFlag1(struct node* p) {
return (struct node*) ((uintptr_t) p | 1);
}
static inline struct node* unsetFlag1(struct node* p) {
return (struct node*) ((uintptr_t) p & ~((uintptr_t) 1));
}
static inline struct node* setFlag2(struct node* p)
return (struct node*) ((uintptr_t) p | 2);
}
static inline struct node* unsetFlag2(struct node* p) {
return (struct node*) ((uintptr_t) p & ~((uintptr_t) 2));
}
[Edit]
In review, it is certain that UINTPTR_MAX
2 LSBits must be 1. See @Kaz comment above.