2025-12-11 03:09:57 +09:00
|
|
|
|
// smallsegment_v5_box.h - SmallSegment v5(Phase v5-0)
|
|
|
|
|
|
//
|
|
|
|
|
|
// 2MiB Segment / 64KiB Page ベースの O(1) page_meta lookup
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef HAKMEM_SMALLSEGMENT_V5_BOX_H
|
|
|
|
|
|
#define HAKMEM_SMALLSEGMENT_V5_BOX_H
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
#include "smallobject_hotbox_v5_box.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define SMALL_SEGMENT_V5_SIZE (2 * 1024 * 1024) // 2 MiB
|
|
|
|
|
|
#define SMALL_SEGMENT_V5_PAGE_SIZE (64 * 1024) // 64 KiB
|
|
|
|
|
|
#define SMALL_SEGMENT_V5_NUM_PAGES (SMALL_SEGMENT_V5_SIZE / SMALL_SEGMENT_V5_PAGE_SIZE) // 32
|
|
|
|
|
|
#define SMALL_SEGMENT_V5_MAGIC 0xDEADBEEF
|
|
|
|
|
|
#define SMALL_SEGMENT_V5_PAGE_SHIFT 16 // log2(64KiB)
|
|
|
|
|
|
|
2025-12-11 03:19:18 +09:00
|
|
|
|
// ポインタマクロ - O(1) segment/page 計算
|
|
|
|
|
|
|
|
|
|
|
|
// SMALL_SEGMENT_V5_BASE_FROM_PTR(ptr) - ptr から segment base を mask で計算
|
|
|
|
|
|
#define SMALL_SEGMENT_V5_BASE_FROM_PTR(ptr) \
|
|
|
|
|
|
((SmallSegmentV5*)((uintptr_t)(ptr) & ~(SMALL_SEGMENT_V5_SIZE - 1)))
|
|
|
|
|
|
|
|
|
|
|
|
// SMALL_SEGMENT_V5_PAGE_IDX(seg, ptr) - segment 内の page_idx を shift で計算
|
|
|
|
|
|
#define SMALL_SEGMENT_V5_PAGE_IDX(seg, ptr) \
|
|
|
|
|
|
(((uintptr_t)(ptr) - (uintptr_t)(seg)->base) >> SMALL_SEGMENT_V5_PAGE_SHIFT)
|
|
|
|
|
|
|
|
|
|
|
|
// SMALL_SEGMENT_V5_PAGE_META(seg, ptr) - page_meta への O(1) access(bounds check付き)
|
|
|
|
|
|
#define SMALL_SEGMENT_V5_PAGE_META(seg, ptr) \
|
|
|
|
|
|
({ \
|
|
|
|
|
|
SmallSegmentV5* _seg = (seg); \
|
|
|
|
|
|
if (__builtin_expect(!_seg, 0)) { \
|
|
|
|
|
|
(SmallPageMetaV5*)NULL; \
|
|
|
|
|
|
} else { \
|
|
|
|
|
|
size_t _idx = SMALL_SEGMENT_V5_PAGE_IDX(_seg, (ptr)); \
|
|
|
|
|
|
(__builtin_expect(_idx < _seg->num_pages, 1)) ? \
|
|
|
|
|
|
&_seg->page_meta[_idx] : (SmallPageMetaV5*)NULL; \
|
|
|
|
|
|
} \
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
// SMALL_SEGMENT_V5_VALIDATE_MAGIC(seg) - magic 検証
|
|
|
|
|
|
#define SMALL_SEGMENT_V5_VALIDATE_MAGIC(seg) \
|
|
|
|
|
|
(__builtin_expect((seg) && (seg)->magic == SMALL_SEGMENT_V5_MAGIC, 1))
|
|
|
|
|
|
|
|
|
|
|
|
// SMALL_SEGMENT_V5_VALIDATE_PTR(ptr) - Fail-Fast validation pipeline
|
|
|
|
|
|
// ptr が valid な v5 segment 内にあるかを検証
|
|
|
|
|
|
#define SMALL_SEGMENT_V5_VALIDATE_PTR(ptr) \
|
|
|
|
|
|
({ \
|
|
|
|
|
|
int _valid = 0; \
|
|
|
|
|
|
if (__builtin_expect((ptr) != NULL, 1)) { \
|
|
|
|
|
|
SmallSegmentV5* _seg = SMALL_SEGMENT_V5_BASE_FROM_PTR(ptr); \
|
|
|
|
|
|
if (SMALL_SEGMENT_V5_VALIDATE_MAGIC(_seg)) { \
|
|
|
|
|
|
size_t _idx = SMALL_SEGMENT_V5_PAGE_IDX(_seg, (ptr)); \
|
|
|
|
|
|
_valid = (_idx < _seg->num_pages); \
|
|
|
|
|
|
} \
|
|
|
|
|
|
} \
|
|
|
|
|
|
_valid; \
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2025-12-11 03:09:57 +09:00
|
|
|
|
// SmallSegmentV5: セグメント構造体
|
|
|
|
|
|
typedef struct SmallSegmentV5 {
|
|
|
|
|
|
uintptr_t base; // セグメント先頭アドレス
|
|
|
|
|
|
uint32_t num_pages; // ページ数(通常は 32)
|
|
|
|
|
|
uint32_t owner_tid; // オーナースレッド ID
|
|
|
|
|
|
uint32_t magic; // 0xDEADBEEF for validation
|
|
|
|
|
|
SmallPageMetaV5 page_meta[SMALL_SEGMENT_V5_NUM_PAGES]; // page metadata array
|
|
|
|
|
|
} SmallSegmentV5;
|
|
|
|
|
|
|
|
|
|
|
|
// API(v5-0 では宣言のみ、実装は v5-2)
|
|
|
|
|
|
SmallSegmentV5* small_segment_v5_acquire(void);
|
|
|
|
|
|
void small_segment_v5_release(SmallSegmentV5* seg);
|
|
|
|
|
|
SmallPageMetaV5* small_segment_v5_page_meta_of(void* ptr);
|
|
|
|
|
|
|
|
|
|
|
|
#endif // HAKMEM_SMALLSEGMENT_V5_BOX_H
|