33 lines
987 B
C
33 lines
987 B
C
|
|
#ifndef HAKMEM_TINY_PTR_CONVERT_BOX_H
|
||
|
|
#define HAKMEM_TINY_PTR_CONVERT_BOX_H
|
||
|
|
|
||
|
|
#include <stddef.h>
|
||
|
|
|
||
|
|
// Purpose: Zero-overhead inline conversions between BASE and USER pointers
|
||
|
|
// Contract: Header is stored at offset -1 from USER pointer
|
||
|
|
//
|
||
|
|
// Example:
|
||
|
|
// - Allocation: base → user (user = base + 1)
|
||
|
|
// - Deallocation: user → base (base = user - 1)
|
||
|
|
//
|
||
|
|
// Offset definition (centralized for future extension)
|
||
|
|
#define TINY_HEADER_OFFSET 1
|
||
|
|
|
||
|
|
// Direct inline macros (zero cost, lvalue compatible in some contexts)
|
||
|
|
#define tiny_base_to_user_inline(base) \
|
||
|
|
((void*)((char*)(base) + TINY_HEADER_OFFSET))
|
||
|
|
|
||
|
|
#define tiny_user_to_base_inline(user) \
|
||
|
|
((void*)((char*)(user) - TINY_HEADER_OFFSET))
|
||
|
|
|
||
|
|
// Verbose variants (for clarity in critical paths)
|
||
|
|
static inline void* tiny_base_to_user(void* base) {
|
||
|
|
return tiny_base_to_user_inline(base);
|
||
|
|
}
|
||
|
|
|
||
|
|
static inline void* tiny_user_to_base(void* user) {
|
||
|
|
return tiny_user_to_base_inline(user);
|
||
|
|
}
|
||
|
|
|
||
|
|
#endif // HAKMEM_TINY_PTR_CONVERT_BOX_H
|