| ... | @@ -1,13 +1,13 @@ | ... | @@ -1,13 +1,13 @@ |
| 1 | const std = @import("std.zig"); | 1 | const std = @import("std.zig"); |
| 2 | const builtin = @import("builtin"); | 2 | const builtin = @import("builtin"); |
| 3 | const root = @import("root"); | 3 | const root = @import("root"); |
| 4 | const debug = std.debug; | 4 | const assert = std.debug.assert; |
| 5 | const assert = debug.assert; | | |
| 6 | const testing = std.testing; | 5 | const testing = std.testing; |
| 7 | const mem = std.mem; | 6 | const mem = std.mem; |
| 8 | const os = std.os; | 7 | const os = std.os; |
| 9 | const c = std.c; | 8 | const c = std.c; |
| 10 | const maxInt = std.math.maxInt; | 9 | const maxInt = std.math.maxInt; |
| | 10 | const Allocator = std.mem.Allocator; |
| 11 | | 11 | |
| 12 | pub const LoggingAllocator = @import("heap/logging_allocator.zig").LoggingAllocator; | 12 | pub const LoggingAllocator = @import("heap/logging_allocator.zig").LoggingAllocator; |
| 13 | pub const loggingAllocator = @import("heap/logging_allocator.zig").loggingAllocator; | 13 | pub const loggingAllocator = @import("heap/logging_allocator.zig").loggingAllocator; |
| ... | @@ -16,8 +16,11 @@ pub const LogToWriterAllocator = @import("heap/log_to_writer_allocator.zig").Log | ... | @@ -16,8 +16,11 @@ pub const LogToWriterAllocator = @import("heap/log_to_writer_allocator.zig").Log |
| 16 | pub const logToWriterAllocator = @import("heap/log_to_writer_allocator.zig").logToWriterAllocator; | 16 | pub const logToWriterAllocator = @import("heap/log_to_writer_allocator.zig").logToWriterAllocator; |
| 17 | pub const ArenaAllocator = @import("heap/arena_allocator.zig").ArenaAllocator; | 17 | pub const ArenaAllocator = @import("heap/arena_allocator.zig").ArenaAllocator; |
| 18 | pub const GeneralPurposeAllocator = @import("heap/general_purpose_allocator.zig").GeneralPurposeAllocator; | 18 | pub const GeneralPurposeAllocator = @import("heap/general_purpose_allocator.zig").GeneralPurposeAllocator; |
| | 19 | pub const WasmPageAllocator = @import("heap/WasmPageAllocator.zig"); |
| | 20 | pub const PageAllocator = @import("heap/PageAllocator.zig"); |
| 19 | | 21 | |
| 20 | const Allocator = mem.Allocator; | 22 | /// TODO Utilize this on Windows. |
| | 23 | pub var next_mmap_addr_hint: ?[*]align(mem.page_size) u8 = null; |
| 21 | | 24 | |
| 22 | const CAllocator = struct { | 25 | const CAllocator = struct { |
| 23 | comptime { | 26 | comptime { |
| ... | @@ -227,303 +230,6 @@ pub fn alignPageAllocLen(full_len: usize, len: usize) usize { | ... | @@ -227,303 +230,6 @@ pub fn alignPageAllocLen(full_len: usize, len: usize) usize { |
| 227 | return aligned_len; | 230 | return aligned_len; |
| 228 | } | 231 | } |
| 229 | | 232 | |
| 230 | /// TODO Utilize this on Windows. | | |
| 231 | pub var next_mmap_addr_hint: ?[*]align(mem.page_size) u8 = null; | | |
| 232 | | | |
| 233 | const PageAllocator = struct { | | |
| 234 | const vtable = Allocator.VTable{ | | |
| 235 | .alloc = alloc, | | |
| 236 | .resize = resize, | | |
| 237 | .free = free, | | |
| 238 | }; | | |
| 239 | | | |
| 240 | fn alloc(_: *anyopaque, n: usize, log2_align: u8, ra: usize) ?[*]u8 { | | |
| 241 | _ = ra; | | |
| 242 | _ = log2_align; | | |
| 243 | assert(n > 0); | | |
| 244 | if (n > maxInt(usize) - (mem.page_size - 1)) return null; | | |
| 245 | const aligned_len = mem.alignForward(n, mem.page_size); | | |
| 246 | | | |
| 247 | if (builtin.os.tag == .windows) { | | |
| 248 | const w = os.windows; | | |
| 249 | const addr = w.VirtualAlloc( | | |
| 250 | null, | | |
| 251 | aligned_len, | | |
| 252 | w.MEM_COMMIT | w.MEM_RESERVE, | | |
| 253 | w.PAGE_READWRITE, | | |
| 254 | ) catch return null; | | |
| 255 | return @ptrCast([*]align(mem.page_size) u8, @alignCast(mem.page_size, addr)); | | |
| 256 | } | | |
| 257 | | | |
| 258 | const hint = @atomicLoad(@TypeOf(next_mmap_addr_hint), &next_mmap_addr_hint, .Unordered); | | |
| 259 | const slice = os.mmap( | | |
| 260 | hint, | | |
| 261 | aligned_len, | | |
| 262 | os.PROT.READ | os.PROT.WRITE, | | |
| 263 | os.MAP.PRIVATE | os.MAP.ANONYMOUS, | | |
| 264 | -1, | | |
| 265 | 0, | | |
| 266 | ) catch return null; | | |
| 267 | assert(mem.isAligned(@ptrToInt(slice.ptr), mem.page_size)); | | |
| 268 | const new_hint = @alignCast(mem.page_size, slice.ptr + aligned_len); | | |
| 269 | _ = @cmpxchgStrong(@TypeOf(next_mmap_addr_hint), &next_mmap_addr_hint, hint, new_hint, .Monotonic, .Monotonic); | | |
| 270 | return slice.ptr; | | |
| 271 | } | | |
| 272 | | | |
| 273 | fn resize( | | |
| 274 | _: *anyopaque, | | |
| 275 | buf_unaligned: []u8, | | |
| 276 | log2_buf_align: u8, | | |
| 277 | new_size: usize, | | |
| 278 | return_address: usize, | | |
| 279 | ) bool { | | |
| 280 | _ = log2_buf_align; | | |
| 281 | _ = return_address; | | |
| 282 | const new_size_aligned = mem.alignForward(new_size, mem.page_size); | | |
| 283 | | | |
| 284 | if (builtin.os.tag == .windows) { | | |
| 285 | const w = os.windows; | | |
| 286 | if (new_size <= buf_unaligned.len) { | | |
| 287 | const base_addr = @ptrToInt(buf_unaligned.ptr); | | |
| 288 | const old_addr_end = base_addr + buf_unaligned.len; | | |
| 289 | const new_addr_end = mem.alignForward(base_addr + new_size, mem.page_size); | | |
| 290 | if (old_addr_end > new_addr_end) { | | |
| 291 | // For shrinking that is not releasing, we will only | | |
| 292 | // decommit the pages not needed anymore. | | |
| 293 | w.VirtualFree( | | |
| 294 | @intToPtr(*anyopaque, new_addr_end), | | |
| 295 | old_addr_end - new_addr_end, | | |
| 296 | w.MEM_DECOMMIT, | | |
| 297 | ); | | |
| 298 | } | | |
| 299 | return true; | | |
| 300 | } | | |
| 301 | const old_size_aligned = mem.alignForward(buf_unaligned.len, mem.page_size); | | |
| 302 | if (new_size_aligned <= old_size_aligned) { | | |
| 303 | return true; | | |
| 304 | } | | |
| 305 | return false; | | |
| 306 | } | | |
| 307 | | | |
| 308 | const buf_aligned_len = mem.alignForward(buf_unaligned.len, mem.page_size); | | |
| 309 | if (new_size_aligned == buf_aligned_len) | | |
| 310 | return true; | | |
| 311 | | | |
| 312 | if (new_size_aligned < buf_aligned_len) { | | |
| 313 | const ptr = @alignCast(mem.page_size, buf_unaligned.ptr + new_size_aligned); | | |
| 314 | // TODO: if the next_mmap_addr_hint is within the unmapped range, update it | | |
| 315 | os.munmap(ptr[0 .. buf_aligned_len - new_size_aligned]); | | |
| 316 | return true; | | |
| 317 | } | | |
| 318 | | | |
| 319 | // TODO: call mremap | | |
| 320 | // TODO: if the next_mmap_addr_hint is within the remapped range, update it | | |
| 321 | return false; | | |
| 322 | } | | |
| 323 | | | |
| 324 | fn free(_: *anyopaque, slice: []u8, log2_buf_align: u8, return_address: usize) void { | | |
| 325 | _ = log2_buf_align; | | |
| 326 | _ = return_address; | | |
| 327 | | | |
| 328 | if (builtin.os.tag == .windows) { | | |
| 329 | os.windows.VirtualFree(slice.ptr, 0, os.windows.MEM_RELEASE); | | |
| 330 | } else { | | |
| 331 | const buf_aligned_len = mem.alignForward(slice.len, mem.page_size); | | |
| 332 | const ptr = @alignCast(mem.page_size, slice.ptr); | | |
| 333 | os.munmap(ptr[0..buf_aligned_len]); | | |
| 334 | } | | |
| 335 | } | | |
| 336 | }; | | |
| 337 | | | |
| 338 | const WasmPageAllocator = struct { | | |
| 339 | comptime { | | |
| 340 | if (!builtin.target.isWasm()) { | | |
| 341 | @compileError("WasmPageAllocator is only available for wasm32 arch"); | | |
| 342 | } | | |
| 343 | } | | |
| 344 | | | |
| 345 | const vtable = Allocator.VTable{ | | |
| 346 | .alloc = alloc, | | |
| 347 | .resize = resize, | | |
| 348 | .free = free, | | |
| 349 | }; | | |
| 350 | | | |
| 351 | const PageStatus = enum(u1) { | | |
| 352 | used = 0, | | |
| 353 | free = 1, | | |
| 354 | | | |
| 355 | pub const none_free: u8 = 0; | | |
| 356 | }; | | |
| 357 | | | |
| 358 | const FreeBlock = struct { | | |
| 359 | data: []u128, | | |
| 360 | | | |
| 361 | const Io = std.packed_int_array.PackedIntIo(u1, .Little); | | |
| 362 | | | |
| 363 | fn totalPages(self: FreeBlock) usize { | | |
| 364 | return self.data.len * 128; | | |
| 365 | } | | |
| 366 | | | |
| 367 | fn isInitialized(self: FreeBlock) bool { | | |
| 368 | return self.data.len > 0; | | |
| 369 | } | | |
| 370 | | | |
| 371 | fn getBit(self: FreeBlock, idx: usize) PageStatus { | | |
| 372 | const bit_offset = 0; | | |
| 373 | return @intToEnum(PageStatus, Io.get(mem.sliceAsBytes(self.data), idx, bit_offset)); | | |
| 374 | } | | |
| 375 | | | |
| 376 | fn setBits(self: FreeBlock, start_idx: usize, len: usize, val: PageStatus) void { | | |
| 377 | const bit_offset = 0; | | |
| 378 | var i: usize = 0; | | |
| 379 | while (i < len) : (i += 1) { | | |
| 380 | Io.set(mem.sliceAsBytes(self.data), start_idx + i, bit_offset, @enumToInt(val)); | | |
| 381 | } | | |
| 382 | } | | |
| 383 | | | |
| 384 | // Use '0xFFFFFFFF' as a _missing_ sentinel | | |
| 385 | // This saves ~50 bytes compared to returning a nullable | | |
| 386 | | | |
| 387 | // We can guarantee that conventional memory never gets this big, | | |
| 388 | // and wasm32 would not be able to address this memory (32 GB > usize). | | |
| 389 | | | |
| 390 | // Revisit if this is settled: https://github.com/ziglang/zig/issues/3806 | | |
| 391 | const not_found = std.math.maxInt(usize); | | |
| 392 | | | |
| 393 | fn useRecycled(self: FreeBlock, num_pages: usize, log2_align: u8) usize { | | |
| 394 | @setCold(true); | | |
| 395 | for (self.data) |segment, i| { | | |
| 396 | const spills_into_next = @bitCast(i128, segment) < 0; | | |
| 397 | const has_enough_bits = @popCount(segment) >= num_pages; | | |
| 398 | | | |
| 399 | if (!spills_into_next and !has_enough_bits) continue; | | |
| 400 | | | |
| 401 | var j: usize = i * 128; | | |
| 402 | while (j < (i + 1) * 128) : (j += 1) { | | |
| 403 | var count: usize = 0; | | |
| 404 | while (j + count < self.totalPages() and self.getBit(j + count) == .free) { | | |
| 405 | count += 1; | | |
| 406 | const addr = j * mem.page_size; | | |
| 407 | if (count >= num_pages and mem.isAlignedLog2(addr, log2_align)) { | | |
| 408 | self.setBits(j, num_pages, .used); | | |
| 409 | return j; | | |
| 410 | } | | |
| 411 | } | | |
| 412 | j += count; | | |
| 413 | } | | |
| 414 | } | | |
| 415 | return not_found; | | |
| 416 | } | | |
| 417 | | | |
| 418 | fn recycle(self: FreeBlock, start_idx: usize, len: usize) void { | | |
| 419 | self.setBits(start_idx, len, .free); | | |
| 420 | } | | |
| 421 | }; | | |
| 422 | | | |
| 423 | var _conventional_data = [_]u128{0} ** 16; | | |
| 424 | // Marking `conventional` as const saves ~40 bytes | | |
| 425 | const conventional = FreeBlock{ .data = &_conventional_data }; | | |
| 426 | var extended = FreeBlock{ .data = &[_]u128{} }; | | |
| 427 | | | |
| 428 | fn extendedOffset() usize { | | |
| 429 | return conventional.totalPages(); | | |
| 430 | } | | |
| 431 | | | |
| 432 | fn nPages(memsize: usize) usize { | | |
| 433 | return mem.alignForward(memsize, mem.page_size) / mem.page_size; | | |
| 434 | } | | |
| 435 | | | |
| 436 | fn alloc(_: *anyopaque, len: usize, log2_align: u8, ra: usize) ?[*]u8 { | | |
| 437 | _ = ra; | | |
| 438 | if (len > maxInt(usize) - (mem.page_size - 1)) return null; | | |
| 439 | const page_count = nPages(len); | | |
| 440 | const page_idx = allocPages(page_count, log2_align) catch return null; | | |
| 441 | return @intToPtr([*]u8, page_idx * mem.page_size); | | |
| 442 | } | | |
| 443 | | | |
| 444 | fn allocPages(page_count: usize, log2_align: u8) !usize { | | |
| 445 | { | | |
| 446 | const idx = conventional.useRecycled(page_count, log2_align); | | |
| 447 | if (idx != FreeBlock.not_found) { | | |
| 448 | return idx; | | |
| 449 | } | | |
| 450 | } | | |
| 451 | | | |
| 452 | const idx = extended.useRecycled(page_count, log2_align); | | |
| 453 | if (idx != FreeBlock.not_found) { | | |
| 454 | return idx + extendedOffset(); | | |
| 455 | } | | |
| 456 | | | |
| 457 | const next_page_idx = @wasmMemorySize(0); | | |
| 458 | const next_page_addr = next_page_idx * mem.page_size; | | |
| 459 | const aligned_addr = mem.alignForwardLog2(next_page_addr, log2_align); | | |
| 460 | const drop_page_count = @divExact(aligned_addr - next_page_addr, mem.page_size); | | |
| 461 | const result = @wasmMemoryGrow(0, @intCast(u32, drop_page_count + page_count)); | | |
| 462 | if (result <= 0) | | |
| 463 | return error.OutOfMemory; | | |
| 464 | assert(result == next_page_idx); | | |
| 465 | const aligned_page_idx = next_page_idx + drop_page_count; | | |
| 466 | if (drop_page_count > 0) { | | |
| 467 | freePages(next_page_idx, aligned_page_idx); | | |
| 468 | } | | |
| 469 | return @intCast(usize, aligned_page_idx); | | |
| 470 | } | | |
| 471 | | | |
| 472 | fn freePages(start: usize, end: usize) void { | | |
| 473 | if (start < extendedOffset()) { | | |
| 474 | conventional.recycle(start, @min(extendedOffset(), end) - start); | | |
| 475 | } | | |
| 476 | if (end > extendedOffset()) { | | |
| 477 | var new_end = end; | | |
| 478 | if (!extended.isInitialized()) { | | |
| 479 | // Steal the last page from the memory currently being recycled | | |
| 480 | // TODO: would it be better if we use the first page instead? | | |
| 481 | new_end -= 1; | | |
| 482 | | | |
| 483 | extended.data = @intToPtr([*]u128, new_end * mem.page_size)[0 .. mem.page_size / @sizeOf(u128)]; | | |
| 484 | // Since this is the first page being freed and we consume it, assume *nothing* is free. | | |
| 485 | mem.set(u128, extended.data, PageStatus.none_free); | | |
| 486 | } | | |
| 487 | const clamped_start = @max(extendedOffset(), start); | | |
| 488 | extended.recycle(clamped_start - extendedOffset(), new_end - clamped_start); | | |
| 489 | } | | |
| 490 | } | | |
| 491 | | | |
| 492 | fn resize( | | |
| 493 | _: *anyopaque, | | |
| 494 | buf: []u8, | | |
| 495 | log2_buf_align: u8, | | |
| 496 | new_len: usize, | | |
| 497 | return_address: usize, | | |
| 498 | ) bool { | | |
| 499 | _ = log2_buf_align; | | |
| 500 | _ = return_address; | | |
| 501 | const aligned_len = mem.alignForward(buf.len, mem.page_size); | | |
| 502 | if (new_len > aligned_len) return false; | | |
| 503 | const current_n = nPages(aligned_len); | | |
| 504 | const new_n = nPages(new_len); | | |
| 505 | if (new_n != current_n) { | | |
| 506 | const base = nPages(@ptrToInt(buf.ptr)); | | |
| 507 | freePages(base + new_n, base + current_n); | | |
| 508 | } | | |
| 509 | return true; | | |
| 510 | } | | |
| 511 | | | |
| 512 | fn free( | | |
| 513 | _: *anyopaque, | | |
| 514 | buf: []u8, | | |
| 515 | log2_buf_align: u8, | | |
| 516 | return_address: usize, | | |
| 517 | ) void { | | |
| 518 | _ = log2_buf_align; | | |
| 519 | _ = return_address; | | |
| 520 | const aligned_len = mem.alignForward(buf.len, mem.page_size); | | |
| 521 | const current_n = nPages(aligned_len); | | |
| 522 | const base = nPages(@ptrToInt(buf.ptr)); | | |
| 523 | freePages(base, base + current_n); | | |
| 524 | } | | |
| 525 | }; | | |
| 526 | | | |
| 527 | pub const HeapAllocator = switch (builtin.os.tag) { | 233 | pub const HeapAllocator = switch (builtin.os.tag) { |
| 528 | .windows => struct { | 234 | .windows => struct { |
| 529 | heap_handle: ?HeapHandle, | 235 | heap_handle: ?HeapHandle, |
| ... | @@ -1163,7 +869,10 @@ pub fn testAllocatorAlignedShrink(base_allocator: mem.Allocator) !void { | ... | @@ -1163,7 +869,10 @@ pub fn testAllocatorAlignedShrink(base_allocator: mem.Allocator) !void { |
| 1163 | try testing.expect(slice[60] == 0x34); | 869 | try testing.expect(slice[60] == 0x34); |
| 1164 | } | 870 | } |
| 1165 | | 871 | |
| 1166 | test "heap" { | 872 | test { |
| 1167 | _ = @import("heap/logging_allocator.zig"); | 873 | _ = LoggingAllocator; |
| 1168 | _ = @import("heap/log_to_writer_allocator.zig"); | 874 | _ = LogToWriterAllocator; |
| | 875 | _ = ScopedLoggingAllocator; |
| | 876 | _ = ArenaAllocator; |
| | 877 | _ = GeneralPurposeAllocator; |
| 1169 | } | 878 | } |