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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
* Copyright (c) 2025-2026 Anton Kundenko <singaraiona@gmail.com>
* All rights reserved.
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/*
* heap.h -- Rayforce-style per-thread heap allocator (zero-prefix layout).
*
* Each thread owns one ray_heap_t. Blocks are allocated from self-aligned
* mmap'd pools via buddy splitting. ray_t IS the block — no prefix.
*
* Pool metadata (heap_id, pool_order) is stored in a pool header at
* offset 0 of each self-aligned pool (first min-block reserved).
* Pool base is derived in O(1): ptr & ~(pool_size - 1).
*
* Free-list prev/next overlay nullmap bytes 0-15 of ray_t (unused when free).
* rc == 0 indicates a free block (replaces the old ray_blk_t.used flag).
*
* Cross-thread free uses a foreign_blocks list (checked via pool heap_id).
*/
/* ===== Attribute Flags =====
*
* The `attrs` byte in ray_t is type-namespaced: the same bit positions carry
* different meanings depending on the object's type tag.
*
* Bits 0x01-0x03 RAY_SYM vectors: sym index width (RAY_SYM_W8/W16/W32/W64)
* Bits 0x01-0x10 function objects (RAY_UNARY/BINARY/VARY): RAY_FN_* flags
* Bit 0x02 -RAY_I64 atoms: RAY_ATTR_GRAPH (ray_rel_t* CSR handle in .i64)
* Bit 0x04 -RAY_I64 atoms: RAY_ATTR_HNSW (HNSW handle in .i64)
* Bit 0x08 vectors: RAY_ATTR_HAS_INDEX (index ray_t* in nullmap[0..7])
* Bit 0x10 vectors: RAY_ATTR_SLICE
* Bit 0x20 vectors: RAY_ATTR_NULLMAP_EXT
* Bit 0x20 -RAY_SYM: RAY_ATTR_NAME (variable reference)
* Bit 0x40 vectors: RAY_ATTR_HAS_NULLS
* Bit 0x80 all types: RAY_ATTR_ARENA (arena-allocated, no refcount)
*
* Overlapping bit values are safe because consumers always check the type tag
* before interpreting attrs.
*/
/* I64 atom carries an owning ray_rel_t* (CSR graph) in its .i64 slot.
* Checked by .graph.* builtins before dereferencing. User may call
* (.graph.free h) explicitly; the heap finalizer also frees the underlying
* ray_rel_t when the atom's rc drops to zero so rebindings/scope-exit
* never leak the graph. */
/* I64 atom carries an owning ray_hnsw_t* in its .i64 slot.
* Checked by HNSW builtins before dereferencing. User must (hnsw-free h). */
/* Vector is a linked column. The 8 bytes of the nullmap union at offset
* 8 (i.e. parent->_idx_pad / parent->slice_offset / parent->sym_dict /
* parent->str_pool slot, depending on which arm is in use) hold an int64
* sym ID naming the target table. Resolved against the global env at
* deref time. Restricted to RAY_I32 / RAY_I64 vectors — STR/SYM/SLICE
* already use bytes 8-15 for their own pointers/data so HAS_LINK on
* those types would alias.
*
* Coexists with HAS_INDEX: bytes 0-7 carry the index pointer (or saved
* nullmap), bytes 8-15 carry the link sym; both bits can be set on the
* same column. A linked vec with nulls is forced to RAY_ATTR_NULLMAP_EXT
* because the inline 128-bit bitmap would alias the link-target slot.
*
* Same numeric value as RAY_ATTR_HNSW (HNSW handles are -RAY_I64 atoms,
* the type tag disambiguates). */
/* Vector carries an attached accelerator index in nullmap[0..7] (a ray_t*
* of type RAY_INDEX). The original 16-byte nullmap union content (inline
* bitmap, ext_nullmap, str_ext_null/str_pool, sym_dict) is preserved inside
* the index ray_t and restored on detach.
*
* Attribute-bit invariant when HAS_INDEX is set:
* - HAS_NULLS is *preserved* (not cleared). Many call sites use it as a
* cheap "do I need null-aware logic?" gate; clearing it would silently
* break correctness for nullable columns. The bit is authoritative.
* - NULLMAP_EXT is *cleared*. The parent's ext_nullmap field is now the
* index pointer, not a U8 bitmap vec; readers that gate on NULLMAP_EXT
* and dereference ext_nullmap directly would otherwise read garbage.
* The displaced ext-nullmap pointer (if any) lives in
* ix->saved_nullmap[0..7]; ix->saved_attrs records the original
* NULLMAP_EXT bit for restoration on detach.
*
* Direct nullmap-byte readers (morsel iteration, ray_vec_is_null) MUST
* check HAS_INDEX first and route through ix->saved_nullmap / saved_attrs.
* See src/ops/idxop.h. */
/* == */
ray_t* ;
ray_t* ;
ray_t* ;
/* == */
ray_t* ;
/* == */
typedef struct ray_mem_stats_t;
/* == */
typedef struct ray_heap ray_heap_t;
typedef struct ray_sym_table ray_sym_table_t;
typedef struct ray_sym_map ray_sym_map_t;
typedef struct ray_task ray_task_t;
typedef struct ray_dispatch ray_dispatch_t;
/* == */
void ;
void ;
void ;
void ;
void ;
void ;
uint8_t ;
void ;
void ;
void ;
/* --------------------------------------------------------------------------
* Constants
* -------------------------------------------------------------------------- */
/* --------------------------------------------------------------------------
* Block size helper
* -------------------------------------------------------------------------- */
/* --------------------------------------------------------------------------
* Pool header: first min-block (64B) of each self-aligned pool.
*
* Overlaid on bytes 0-15 of the ray_t at pool offset 0.
* The ray_t at pool offset 0 has rc=1 (prevents coalescing) and
* order=RAY_ORDER_MIN (correct for buddy math).
* -------------------------------------------------------------------------- */
typedef struct ray_pool_hdr_t;
;
/* --------------------------------------------------------------------------
* Circular sentinel freelist (Rayforce-style)
*
* Each freelist[order] is a sentinel node with prev/next pointers at
* offsets 0/8 — same layout as ray_t.fl_prev/fl_next. This makes
* fl_remove() work without knowing which freelist the block belongs to,
* enabling safe cross-heap buddy coalescing.
*
* Empty list: sentinel.prev = sentinel.next = &sentinel.
* -------------------------------------------------------------------------- */
typedef struct RAY_ALIGN ray_fl_head_t;
static inline void
static inline bool
/* Unlink a block from whatever circular list it belongs to.
* Works across heaps — no head pointer needed. */
static inline void
/* --------------------------------------------------------------------------
* Pool tracking entry (in ray_heap_t)
*
* Pools are normally backed by anonymous mmap. When anon mmap fails (the
* OS refuses an N-byte allocation because RAM+swap can't satisfy it), the
* allocator falls back to a file-backed mmap pointed at a tempfile in the
* heap's swap directory — this lets fresh allocations exceed RAM, with
* dirty pages flushed to disk by the kernel.
*
* backed=0: anonymous mmap (the common case). swap_fd unused,
* swap_path NULL.
* backed=1: file-backed mmap. swap_fd holds the open fd and
* swap_path holds the absolute path; teardown closes the
* fd, unlinks the file, and ray_sys_frees the path string.
* -------------------------------------------------------------------------- */
typedef struct ray_pool_entry_t;
/* --------------------------------------------------------------------------
* Pool derivation helpers
*
* ray_pool_of: derive pool header from any block pointer.
*
* All pools are self-aligned (pool base = multiple of pool_size). Standard
* pools (32 MB) are derived in O(1) via a single AND mask. Oversized pools
* (> 32 MB) use a downward walk at 32 MB stride to find the pool header.
*
* Pool header validation: order == RAY_ORDER_MIN, mmod == 0, rc == 1.
* These conditions uniquely identify pool header blocks — cascade/split
* blocks always have order > RAY_ORDER_MIN.
* -------------------------------------------------------------------------- */
static inline ray_pool_hdr_t*
/* --------------------------------------------------------------------------
* Buddy derivation: uses self-aligned pool base
* -------------------------------------------------------------------------- */
static inline ray_t*
/* --------------------------------------------------------------------------
* Slab cache for small blocks (orders 6-10, i.e., 64B-1024B)
* -------------------------------------------------------------------------- */
typedef struct ray_slab_t;
/* --------------------------------------------------------------------------
* Per-thread heap
* -------------------------------------------------------------------------- */
typedef struct ray_heap ray_heap_t;
/* --------------------------------------------------------------------------
* Bitmap-based heap ID allocator (atomic CAS, reusable IDs)
* -------------------------------------------------------------------------- */
/* Global pending-merge queue head (lock-free LIFO) */
extern ray_heap_pending_merge;
/* --------------------------------------------------------------------------
* Pool-list scan: find which pool a block belongs to without reading the
* remote pool header (avoids cold cache line 32MB away on hot path).
* Returns pool index in h->pools[], or -1 if block is foreign.
* -------------------------------------------------------------------------- */
static inline int
/* --------------------------------------------------------------------------
* Thread-local state
* -------------------------------------------------------------------------- */
extern RAY_TLS ray_heap_t* ray_tl_heap;
/* --------------------------------------------------------------------------
* Global heap registry: look up any heap by ID so foreign blocks can be
* returned to their owning heap instead of accumulating on the freeing heap.
* -------------------------------------------------------------------------- */
extern ray_heap_t* ray_heap_registry;
/* --------------------------------------------------------------------------
* Scratch arena: bump-allocator backed by buddy-allocated pages.
* O(1) push (pointer bump), O(n_backing) reset (free all backing blocks).
* -------------------------------------------------------------------------- */
typedef struct ray_scratch_arena_t;
static inline void
/* Retain all child/owned refs inside a compound block (STR/LIST/TABLE/etc.).
* Used by ray_block_copy and ray_alloc_copy after shallow-copying a block.
*
* Returns true on success, false if a deep-clone of a uniquely-owned
* resource (e.g. an HNSW index) failed. On failure, any owned state that
* was memcpy'd into the copy has been neutralized (attr flags cleared,
* pointers zeroed) so the caller may safely ray_free(v) without leaks or
* double-frees. */
bool ;
void* ;
void ;
/* RAY_HEAP_H */