1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//! Memory layout calculation, alignment enforcement, and allocation.
//!
//! Handles `SoA` (Structure of Arrays) block allocation with 64-byte alignment and
//! power-of-two capacity invariants. All allocation routines are `#![no_std]` compatible
//! and rely on stable `alloc::alloc` functions.
//!
//! # Memory Layout
//! ```text
//! [ Meta(u8) | Padding... | Keys(K) | Padding... | Values(V) | Padding... ]
//! ^ 64B aligned
//! ```
//! Each section is padded to 64-byte boundaries to guarantee SIMD-friendly stride
//! and cache-line alignment for auto-vectorized probing loops.
//!
//! # Custom Allocator Support
//! This module uses stable `alloc::alloc` functions. For per-instance custom allocators,
//! enable the nightly `allocator_api` feature and replace direct `alloc`/`dealloc` calls
//! with `Allocator::allocate`/`deallocate`.
use ;
use NonNull;
const ALIGN: usize = 64;
/// Rounds `n` up to the nearest multiple of `ALIGN`.
const
/// Computes the total memory layout and section offsets for a `SoA` block.
///
/// # Arguments
/// * `capacity` - Power-of-two slot count. Must be `> 0`.
/// * `key_size` - `core::mem::size_of::<K>()`.
/// * `val_size` - `core::mem::size_of::<V>()`.
///
/// # Returns
/// A tuple `(layout, keys_offset, vals_offset)` representing the full block
/// and byte offsets for the `Keys` and `Values` arrays.
///
/// # Panics
/// Panics if capacity or sizes cause arithmetic overflow, or if alignment
/// requirements cannot be met. These are internal invariants enforced by caller.
///
/// # Safety
/// Caller must ensure `capacity` matches the actual table capacity.
/// `key_size` and `val_size` must exactly match the target `K` and `V` types.
///
/// # See Also
/// - `allocate_block`
/// - `get_array_ptrs`
/// Allocates a zero-initialized `SoA` memory block for the hash table.
///
/// # Arguments
/// * `capacity` - Power-of-two slot count.
/// * `key_size` - `size_of::<K>()`.
/// * `val_size` - `size_of::<V>()`.
///
/// # Returns
/// `NonNull<u8>` pointing to the start of the allocated block.
///
/// # Panics
/// Panics if allocation fails (OOM) via `handle_alloc_error`, or if layout
/// calculation overflows.
///
/// # Safety
/// Caller must ensure `capacity` is a valid power of two and `> 0`.
/// `key_size` and `val_size` must match the actual `K` and `V` types.
/// The metadata region (first `capacity` bytes) is zero-initialized, denoting
/// all slots as empty. Keys/Values regions are uninitialized and must be
/// properly initialized before use.
///
/// # See Also
/// - `deallocate_block`
pub unsafe
/// Deallocates a previously allocated `SoA` memory block.
///
/// # Arguments
/// * `ptr` - Pointer returned by `allocate_block`.
/// * `capacity`, `key_size`, `val_size` - Original allocation parameters.
///
/// # Safety
/// `ptr` must have been returned by `allocate_block` with identical parameters.
/// Must not be called on already deallocated, null, or invalid pointers.
/// All initialized keys/values must be dropped by the caller prior to this call.
///
/// # See Also
/// - `allocate_block`
pub unsafe
/// Returns typed raw pointers to the meta, keys, and values arrays.
///
/// # Arguments
/// * `base` - Base pointer to the allocated `SoA` block.
/// * `keys_offset`, `vals_offset` - Byte offsets from `calc_layout`.
///
/// # Safety
/// `base` must point to a valid, allocated `SoA` block. Offsets must match `calc_layout`.
/// Returned pointers are guaranteed to be aligned to `align_of::<K>()` and `align_of::<V>()`
/// due to 64-byte `SoA` padding. Caller must ensure proper initialization before dereferencing.
pub unsafe