/** * @file tiny_layout_box.h * @brief Box: Tiny Allocator Layout Definitions * * MISSION: Single source of truth for class size and header layout * * Current Design (Phase E1-CORRECT): * - All classes (0-7) have 1-byte header * - User pointer = base + 1 for classes 0-6, base + 0 for class 7 * (Note: Class 7 is headerless in practice but marked for consistency) * - No external code should hardcode offsets */ #ifndef TINY_LAYOUT_BOX_H #define TINY_LAYOUT_BOX_H #include #include "../hakmem_tiny_config.h" // For g_tiny_class_sizes and TINY_NUM_CLASSES // Define all class-specific layout parameters // Current: Defined in g_tiny_class_sizes[8] in hakmem_tiny.c // This file makes them accessible via a unified Box API // Header size is 1 byte when enabled #define TINY_HEADER_SIZE 1 // Validation macros static inline int tiny_class_is_valid(int class_idx) { return class_idx >= 0 && class_idx < TINY_NUM_CLASSES; } static inline size_t tiny_class_stride(int class_idx) { // Use the extern global definition from hakmem_tiny_config.h // g_tiny_class_sizes is defined in core/hakmem_tiny_config_box.inc return tiny_class_is_valid(class_idx) ? g_tiny_class_sizes[class_idx] : 0; } // Calculate user pointer offset from base pointer // This logic centralizes the "User = Base + 1" vs "User = Base + 0" decision static inline size_t tiny_user_offset(int class_idx) { #if HAKMEM_TINY_HEADER_CLASSIDX // C0 (8B): offset 0 (8B stride too small for header + 8B pointer - would overflow) // C7 (2048B): offset 0 (overwrites header in freelist - largest class can tolerate) // C1-C6: offset 1 (header preserved - user data is not disturbed) // Optimized: Use bitmask lookup instead of branching // Bit pattern: C0=0, C1-C6=1, C7=0 → 0b01111110 = 0x7E return (0x7Eu >> class_idx) & 1u; #else (void)class_idx; return 0u; #endif } #endif // TINY_LAYOUT_BOX_H