// tiny_nextptr.h - Authoritative next-pointer offset/load/store for tiny boxes // // Finalized Phase E1-CORRECT spec (物理制約込み): // // HAKMEM_TINY_HEADER_CLASSIDX != 0 のとき: // // Class 0: // [1B header][7B payload] (total 8B) // → offset 1 に 8B ポインタは入らないため不可能 // → freelist中は header を潰して next を base+0 に格納 // → next_off = 0 // // Class 1〜6: // [1B header][payload >= 8B] // → headerは保持し、next は header直後 base+1 に格納 // → next_off = 1 // // Class 7: // 大きなクラス、互換性と実装方針により next は base+0 扱い // → next_off = 0 // // HAKMEM_TINY_HEADER_CLASSIDX == 0 のとき: // // 全クラス headerなし → next_off = 0 // // このヘッダは上記仕様を唯一の真実として提供する。 // すべての tiny freelist / TLS / fast-cache / refill / SLL で // tiny_next_off/tiny_next_load/tiny_next_store を経由すること。 // 直接の *(void**) アクセスやローカルな offset 分岐は使用禁止。 #ifndef TINY_NEXTPTR_H #define TINY_NEXTPTR_H #include #include #include "hakmem_build_flags.h" // Compute freelist next-pointer offset within a block for the given class. static inline __attribute__((always_inline)) size_t tiny_next_off(int class_idx) { #if HAKMEM_TINY_HEADER_CLASSIDX // Phase E1-CORRECT finalized rule: // Class 0,7 → offset 0 // Class 1-6 → offset 1 return (class_idx == 0 || class_idx == 7) ? 0u : 1u; #else (void)class_idx; return 0u; #endif } // Safe load of next pointer from a block base. static inline __attribute__((always_inline)) void* tiny_next_load(const void* base, int class_idx) { size_t off = tiny_next_off(class_idx); if (off == 0) { // Aligned access at base (header無し or C0/C7 freelist時) return *(void* const*)base; } // off != 0: use memcpy to avoid UB on architectures that forbid unaligned loads. void* next = NULL; const uint8_t* p = (const uint8_t*)base + off; memcpy(&next, p, sizeof(void*)); return next; } // Safe store of next pointer into a block base. static inline __attribute__((always_inline)) void tiny_next_store(void* base, int class_idx, void* next) { size_t off = tiny_next_off(class_idx); if (off == 0) { // Aligned access at base. *(void**)base = next; return; } // off != 0: use memcpy for portability / UB-avoidance. uint8_t* p = (uint8_t*)base + off; memcpy(p, &next, sizeof(void*)); } #endif // TINY_NEXTPTR_H