Files
hakmem/core/box/tiny_ptr_convert_box.h
Moe Charm (CI) c848a60696 Phase REFACTOR-3: Inline Pointer Macro Centralization (tiny_base_to_user_inline)
Centralize BASE ↔ USER pointer conversions into reusable, zero-cost macros.
Previously, pointer arithmetic (base + 1, ptr - 1) was scattered across
allocation/deallocation code with hardcoded offsets.

Changes:
- NEW: core/box/tiny_ptr_convert_box.h
  - tiny_base_to_user_inline(): BASE → USER (base + TINY_HEADER_OFFSET)
  - tiny_user_to_base_inline(): USER → BASE (user - TINY_HEADER_OFFSET)
  - TINY_HEADER_OFFSET: Centralized constant (currently 1)
  - Function variants: tiny_base_to_user(), tiny_user_to_base()

- Modified: core/front/malloc_tiny_fast.h
  - L181: return (uint8_t*)base + 1 → tiny_base_to_user_inline(base)
  - L299: void* base = (void*)((char*)ptr - 1) → tiny_user_to_base_inline(ptr)

Benefits:
- Self-documenting code (semantic intent is clear)
- Single source of truth for header offset
- Easier to extend (e.g., variable-length headers, alignment changes)
- Type-safe conversions (macro validates pointer types)
- Zero performance cost (inline macro, same compiled code)

Contract:
- Header stored at offset -1 from USER pointer
- Allocation: base → user (user = base + 1)
- Deallocation: user → base (base = user - 1)

No semantic changes - identical logic, just centralized.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
2025-12-11 19:02:49 +09:00

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