-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathcode_heap.zig
More file actions
508 lines (438 loc) · 19.1 KB
/
Copy pathcode_heap.zig
File metadata and controls
508 lines (438 loc) · 19.1 KB
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
// code_heap.zig - Code heap management
// Manages the JIT-compiled code heap: allocation, block tracking,
// remembered sets for GC, scan flags, and mark bits.
const std = @import("std");
const builtin = @import("builtin");
const code_blocks_mod = @import("code_blocks.zig");
const free_list = @import("free_list.zig");
const layouts = @import("layouts.zig");
const mark_bits = @import("mark_bits.zig");
const segments = @import("segments.zig");
const write_barrier = @import("write_barrier.zig");
const Cell = layouts.Cell;
const CodeBlock = code_blocks_mod.CodeBlock;
pub const CodeHeap = struct {
seg: ?*segments.Segment,
free_list: ?*free_list.FreeListAllocator = null, // typed allocator for JIT
safepoint_page: Cell,
// Code heap address range (for callstack walking)
code_start: Cell = 0,
code_size: Cell = 0,
// Source of truth for live code block addresses (kept sorted).
// New inserts go to pending_blocks (O(1) append) and are merged
// into the sorted list lazily via flushPending().
all_blocks_sorted: std.ArrayList(Cell) = .empty,
pending_blocks: std.ArrayListUnmanaged(Cell) = .empty,
// Memory allocator used for code heap metadata (mark bits, hash maps, etc.)
allocator: ?std.mem.Allocator = null,
// Remembered sets for GC - track code blocks that may reference young objects
remembered_sets: write_barrier.CodeHeapRememberedSets,
// Scan flags for code heap GC (skip blocks without literals/code pointers)
scan_literals: ?std.DynamicBitSet = null,
scan_code_ptrs: ?std.DynamicBitSet = null,
// Uninitialized blocks: code blocks allocated but not yet relocated.
// Maps block address -> literals Cell for deferred initialization.
uninitialized_blocks: std.AutoArrayHashMapUnmanaged(Cell, Cell) = .{},
// Scratch map reused during compaction to avoid repeated allocations.
uninitialized_blocks_scratch: std.AutoArrayHashMapUnmanaged(Cell, Cell) = .{},
marks: ?*mark_bits.MarkBits = null,
const Self = @This();
pub fn allocate(self: *Self, size: Cell) ?*CodeBlock {
const alloc = self.free_list orelse return null;
const aligned_size = layouts.alignCell(size, layouts.data_alignment);
if (alloc.allocate(aligned_size)) |addr| {
const block: *CodeBlock = @ptrFromInt(addr);
// Add to all_blocks for codeBlockForAddress to find
self.addToAllBlocks(addr);
return block;
}
return null;
}
fn addToAllBlocks(self: *Self, block_addr: Cell) void {
const al = self.allocator orelse return;
self.pending_blocks.append(al, block_addr) catch @panic("OOM");
}
// Merge pending inserts into the sorted list. Call before bulk iteration
// or binary-search-dependent operations.
pub fn flushPending(self: *Self) void {
if (self.pending_blocks.items.len == 0) return;
self.flushPendingSlow();
}
fn flushPendingSlow(self: *Self) void {
const pending = self.pending_blocks.items;
const al = self.allocator orelse return;
// Sort pending, then merge into sorted list
std.mem.sortUnstable(Cell, pending, {}, std.sort.asc(Cell));
const old_len = self.all_blocks_sorted.items.len;
const new_total = old_len + pending.len;
self.all_blocks_sorted.ensureTotalCapacity(al, new_total) catch @panic("OOM");
// Merge: append pending, then do a single merge pass in-place.
// Since both halves are sorted, we merge from the end backwards.
self.all_blocks_sorted.items.len = new_total;
const items = self.all_blocks_sorted.items;
var dst = new_total;
var a = old_len; // end of sorted portion
var b = pending.len; // end of pending portion
while (b > 0) {
if (a > 0 and items[a - 1] >= pending[b - 1]) {
dst -= 1;
a -= 1;
items[dst] = items[a];
} else {
dst -= 1;
b -= 1;
items[dst] = pending[b];
}
}
// Remaining items[0..a] are already in place at items[0..dst] since dst == a.
// Deduplicate (pending may contain addresses already in sorted list)
if (items.len > 1) {
var write: usize = 1;
for (items[1..]) |v| {
if (v != items[write - 1]) {
items[write] = v;
write += 1;
}
}
self.all_blocks_sorted.items.len = write;
}
self.pending_blocks.clearRetainingCapacity();
}
pub fn occupiedSpace(self: *const Self) Cell {
if (self.free_list) |alloc| {
return alloc.size - alloc.free_space;
}
return self.code_size; // If no allocator, assume all space is occupied
}
/// Returns the byte extent from code_start to the end of the last occupied
/// block. Unlike occupiedSpace() (which sums non-free bytes), this accounts
/// for free blocks interleaved among occupied ones. Needed by save-image
/// because the Zig VM does not compact the code heap.
pub fn codeHeapExtent(self: *const Self) Cell {
const end = self.code_start + self.code_size;
var current = self.code_start;
var last_occupied_end: Cell = self.code_start;
while (current < end) {
const block: *const CodeBlock = @ptrFromInt(current);
const block_size = block.size();
if (block_size == 0) break;
if (!block.isFree()) {
last_occupied_end = current + block_size;
}
current += block_size;
}
return last_occupied_end - self.code_start;
}
pub fn writeBarrier(self: *Self, compiled: *CodeBlock) !void {
try self.remembered_sets.ensureInitialized(self.code_start, self.code_size);
try self.remembered_sets.writeBarrier(compiled);
}
pub fn ensureScanFlags(self: *Self, al: std.mem.Allocator) !void {
if (self.scan_literals != null and self.scan_code_ptrs != null) return;
const bit_count: usize = @intCast(self.code_size / layouts.data_alignment);
self.scan_literals = try std.DynamicBitSet.initEmpty(al, bit_count);
self.scan_code_ptrs = try std.DynamicBitSet.initEmpty(al, bit_count);
}
fn blockIndex(self: *const Self, block: *CodeBlock) usize {
return @intCast((@intFromPtr(block) - self.code_start) / layouts.data_alignment);
}
fn blockIndexFromAddress(self: *const Self, block_addr: Cell) usize {
return @intCast((block_addr - self.code_start) / layouts.data_alignment);
}
pub fn updateScanFlags(self: *Self, al: std.mem.Allocator, block: *CodeBlock) void {
// Graceful degradation: without scan flags, all blocks are scanned (slower but correct)
self.ensureScanFlags(al) catch return;
const flags = code_blocks_mod.scanRelocationFlags(block);
const idx = self.blockIndex(block);
if (self.scan_literals) |*set| {
if (flags.has_literals) set.set(idx) else set.unset(idx);
}
if (self.scan_code_ptrs) |*set| {
if (flags.has_code_ptrs) set.set(idx) else set.unset(idx);
}
}
pub fn putUninitializedBlock(self: *Self, al: std.mem.Allocator, block_addr: Cell, literals_cell: Cell) !void {
try self.uninitialized_blocks.put(al, block_addr, literals_cell);
}
pub fn removeUninitializedBlock(self: *Self, block_addr: Cell) bool {
return self.uninitialized_blocks.swapRemove(block_addr);
}
pub fn clearUninitializedBlocks(self: *Self) void {
self.uninitialized_blocks.clearRetainingCapacity();
}
pub fn isBlockUninitialized(self: *const Self, block: *const CodeBlock) bool {
return self.isUninitializedAddress(@intFromPtr(block));
}
pub fn isUninitializedAddress(self: *const Self, block_addr: Cell) bool {
return self.uninitialized_blocks.contains(block_addr);
}
pub fn removeScanFlags(self: *Self, block: *CodeBlock) void {
if (self.scan_literals == null or self.scan_code_ptrs == null) return;
const idx = self.blockIndex(block);
if (self.scan_literals) |*set| set.unset(idx);
if (self.scan_code_ptrs) |*set| set.unset(idx);
}
pub fn removeScanFlagsByAddress(self: *Self, block_addr: Cell) void {
if (self.scan_literals == null or self.scan_code_ptrs == null) return;
const idx = self.blockIndexFromAddress(block_addr);
if (self.scan_literals) |*set| set.unset(idx);
if (self.scan_code_ptrs) |*set| set.unset(idx);
}
pub fn clearScanFlags(self: *Self) void {
if (self.scan_literals) |*set| set.unmanaged.unsetAll();
if (self.scan_code_ptrs) |*set| set.unmanaged.unsetAll();
}
pub fn blockHasLiterals(self: *const Self, block: *CodeBlock) bool {
if (self.scan_literals) |set| return set.isSet(self.blockIndex(block));
return true;
}
pub fn blockHasCodePointers(self: *const Self, block: *CodeBlock) bool {
if (self.scan_code_ptrs) |set| return set.isSet(self.blockIndex(block));
return true;
}
pub fn clearRememberedSets(self: *Self) void {
self.remembered_sets.clear();
}
pub fn free(self: *Self, block: *CodeBlock) void {
self.remembered_sets.removeCodeBlock(block);
self.removeScanFlags(block);
const block_addr = @intFromPtr(block);
_ = self.removeUninitializedBlock(block_addr);
self.removeFromAllBlocks(@intFromPtr(block));
const size = block.size();
block.markFree(size);
if (self.free_list) |alloc| {
alloc.free(@intFromPtr(block), size);
}
}
fn removeFromAllBlocks(self: *Self, block_addr: Cell) void {
// Check pending first (swap-remove is O(1))
for (self.pending_blocks.items, 0..) |addr, i| {
if (addr == block_addr) {
_ = self.pending_blocks.swapRemove(i);
return;
}
}
// Fall back to sorted list: O(log n) search + O(n) shift
const items = self.all_blocks_sorted.items;
const pos = std.sort.lowerBound(Cell, items, block_addr, layouts.orderCell);
if (pos < items.len and items[pos] == block_addr) {
_ = self.all_blocks_sorted.orderedRemove(pos);
}
}
// Batch remove addresses from all_blocks_sorted.
pub fn batchRemoveFromAllBlocks(self: *Self, removes: []const Cell) void {
if (removes.len == 0) return;
if (!isNonDecreasing(removes)) {
for (removes) |addr| {
self.removeFromAllBlocks(addr);
}
return;
}
const items = self.all_blocks_sorted.items;
var write_idx: usize = 0;
var remove_idx: usize = 0;
for (items) |addr| {
while (remove_idx < removes.len and removes[remove_idx] < addr) : (remove_idx += 1) {}
if (remove_idx < removes.len and removes[remove_idx] == addr) {
// Skip duplicate remove entries for the same address.
const removed_addr = addr;
while (remove_idx < removes.len and removes[remove_idx] == removed_addr) : (remove_idx += 1) {}
continue;
}
items[write_idx] = addr;
write_idx += 1;
}
self.all_blocks_sorted.items.len = write_idx;
}
// Free a code block without removing from all_blocks.
// Used for batch freeing where all_blocks is updated separately.
pub fn freeBlockOnly(self: *Self, block: *CodeBlock) void {
self.remembered_sets.removeCodeBlock(block);
self.removeScanFlags(block);
const block_addr = @intFromPtr(block);
_ = self.removeUninitializedBlock(block_addr);
const size = block.size();
block.markFree(size);
if (self.free_list) |alloc| {
alloc.free(@intFromPtr(block), size);
}
}
pub fn codeBlockForAddress(self: *Self, address: Cell) ?*CodeBlock {
// Check sorted list first (binary search)
const blocks = self.all_blocks_sorted.items;
if (blocks.len > 0) {
const ub = std.sort.upperBound(Cell, blocks, address, layouts.orderCell);
if (ub > 0) {
const block: *CodeBlock = @ptrFromInt(blocks[ub - 1]);
const block_end = blocks[ub - 1] + block.size();
if (address < block_end) return block;
}
}
// Check pending blocks (linear scan — typically small)
for (self.pending_blocks.items) |block_addr| {
if (address >= block_addr) {
const block: *CodeBlock = @ptrFromInt(block_addr);
const block_end = block_addr + block.size();
if (address < block_end) return block;
}
}
return null;
}
// Get the predecessor frame (caller's frame) given the current frame top
pub fn framePredecessor(self: *Self, frame_top: Cell) Cell {
if (builtin.cpu.arch == .aarch64) {
// ARM64: frame_top[0] contains the saved frame pointer (x29)
// which points directly to the previous frame
return @as(*const Cell, @ptrFromInt(frame_top)).*;
} else {
// x86-64: FRAME_RETURN_ADDRESS = 0, so return address is at frame_top
// We compute the next frame by adding the frame size
const FRAME_RETURN_ADDRESS: Cell = 0;
const addr = @as(*const Cell, @ptrFromInt(frame_top + FRAME_RETURN_ADDRESS)).*;
const block = self.codeBlockForAddress(addr) orelse {
// Can't find code block - return minimum frame size
return frame_top + CodeBlock.LEAF_FRAME_SIZE;
};
const frame_size = block.stackFrameSizeForAddress(addr);
return frame_top + frame_size;
}
}
// Verify that all live code blocks in the heap are present in all_blocks_sorted.
// Catches missed inserts/removes or stale entries.
pub fn verifyAllBlocksSet(self: *const Self) void {
if (comptime builtin.mode != .Debug) return;
if (self.code_start == 0 or self.code_size == 0) return;
const code_end = self.code_start + self.code_size;
var idx: usize = 0;
var current = self.code_start;
while (current < code_end) {
const block: *const CodeBlock = @ptrFromInt(current);
const block_size = codeBlockSize(current);
if (block_size == 0) break;
if (!block.isFree()) {
std.debug.assert(idx < self.all_blocks_sorted.items.len);
std.debug.assert(self.all_blocks_sorted.items[idx] == current);
idx += 1;
}
current += block_size;
}
std.debug.assert(idx == self.all_blocks_sorted.items.len);
if (self.all_blocks_sorted.items.len > 1) {
for (1..self.all_blocks_sorted.items.len) |i| {
std.debug.assert(self.all_blocks_sorted.items[i - 1] < self.all_blocks_sorted.items[i]);
}
}
}
// Initialize all_blocks_sorted by scanning the code heap.
// This must be called after image loading/fixup
pub fn initializeAllBlocksSet(self: *Self) !void {
if (self.code_start == 0 or self.code_size == 0) return;
const alloc = self.allocator orelse return;
// First pass: count non-free blocks
var count: usize = 0;
var current = self.code_start;
const code_end = self.code_start + self.code_size;
while (current < code_end) {
const block_size = codeBlockSize(current);
if (block_size == 0) break;
const block: *const CodeBlock = @ptrFromInt(current);
if (!block.isFree()) {
count += 1;
}
current += block_size;
}
// Rebuild sorted array (pending is stale after full scan).
self.pending_blocks.clearRetainingCapacity();
self.all_blocks_sorted.clearRetainingCapacity();
try self.all_blocks_sorted.ensureTotalCapacity(alloc, count);
// Second pass: populate. Iteration order is ascending address, so
// all_blocks_sorted is naturally sorted without a separate sort call.
current = self.code_start;
while (current < code_end) {
const block_size = codeBlockSize(current);
if (block_size == 0) break;
const block: *const CodeBlock = @ptrFromInt(current);
if (!block.isFree()) {
self.all_blocks_sorted.appendAssumeCapacity(current);
}
current += block_size;
}
self.verifyAllBlocksSet();
}
pub fn rebuildScanFlags(self: *Self, al: std.mem.Allocator) void {
self.flushPending();
self.ensureScanFlags(al) catch return;
self.clearScanFlags();
for (self.all_blocks_sorted.items) |block_addr| {
const block: *CodeBlock = @ptrFromInt(block_addr);
if (!block.isFree()) {
self.updateScanFlags(al, block);
}
}
}
pub fn ensureMarks(self: *Self, al: std.mem.Allocator) !void {
if (self.marks != null) return;
if (self.code_start == 0 or self.code_size == 0) return;
const marks = try al.create(mark_bits.MarkBits);
errdefer al.destroy(marks);
marks.* = try mark_bits.MarkBits.init(al, self.code_start, self.code_size);
self.marks = marks;
self.allocator = al;
}
pub fn clearMarks(self: *Self) void {
if (self.marks) |marks| {
marks.clearMarks();
}
}
pub fn deinit(self: *Self) void {
const alloc = self.allocator orelse return;
self.pending_blocks.deinit(alloc);
self.all_blocks_sorted.deinit(alloc);
self.uninitialized_blocks.deinit(alloc);
self.uninitialized_blocks_scratch.deinit(alloc);
// Deinit remembered sets and scan flags
self.remembered_sets.deinit();
if (self.scan_literals) |*set| {
set.deinit();
self.scan_literals = null;
}
if (self.scan_code_ptrs) |*set| {
set.deinit();
self.scan_code_ptrs = null;
}
if (self.marks) |marks| {
marks.deinit();
if (self.allocator) |gc_alloc| {
gc_alloc.destroy(marks);
}
self.marks = null;
}
}
pub fn blockSizeAt(self: *const Self, addr: Cell) Cell {
_ = self;
return codeBlockSize(addr);
}
};
// Compute block size at address, handling both code-block and free-list layouts.
// FreeListAllocator encodes size in the header (header = size | 1).
fn codeBlockSize(addr: Cell) Cell {
const block: *const CodeBlock = @ptrFromInt(addr);
if (!block.isFree()) {
return block.size();
}
var size = block.size();
if (size == 0) {
// For free blocks, read size from header (FreeBlock encodes size in header)
const free_block: *const free_list.FreeBlock = @ptrFromInt(addr);
size = free_block.size();
}
return size;
}
fn isNonDecreasing(values: []const Cell) bool {
if (values.len < 2) return true;
for (1..values.len) |i| {
if (values[i] < values[i - 1]) return false;
}
return true;
}