| ... | ... | @@ -319,6 +319,11 @@ fn pushFreeList(arena: *ArenaAllocator, first: *Node, last: *Node) void { |
| 319 | 319 | } |
| 320 | 320 | } |
| 321 | 321 | |
| 322 | fn sliceContainsSlice(container: []u8, slice: []u8) bool { |
| 323 | return @intFromPtr(slice.ptr) >= @intFromPtr(container.ptr) and |
| 324 | @intFromPtr(slice.ptr + slice.len) <= @intFromPtr(container.ptr + container.len); |
| 325 | } |
| 326 | |
| 322 | 327 | fn alignedIndex(buf_ptr: [*]u8, end_index: usize, alignment: Alignment) usize { |
| 323 | 328 | // Wrapping arithmetic to avoid overflows since `end_index` isn't bounded by |
| 324 | 329 | // `size`. This is always ok since the max alignment in byte units is also |
| ... | ... | @@ -543,12 +548,17 @@ fn resize(ctx: *anyopaque, memory: []u8, alignment: Alignment, new_len: usize, r |
| 543 | 548 | assert(new_len > 0); |
| 544 | 549 | |
| 545 | 550 | const node = arena.loadFirstNode().?; |
| 546 | | const buf_ptr = @as([*]u8, @ptrCast(node)) + @sizeOf(Node); |
| 551 | const buf = node.loadBuf(); |
| 552 | |
| 553 | if (!sliceContainsSlice(buf, memory)) { |
| 554 | // Not within current node. |
| 555 | return new_len <= memory.len; |
| 556 | } |
| 547 | 557 | |
| 548 | 558 | const cur_end_index = @atomicLoad(usize, &node.end_index, .monotonic); |
| 549 | | if (buf_ptr + cur_end_index != memory.ptr + memory.len) { |
| 550 | | // It's not the most recent allocation, so it cannot be expanded, |
| 551 | | // but it's fine if they want to make it smaller. |
| 559 | |
| 560 | if (buf.ptr + cur_end_index != memory.ptr + memory.len) { |
| 561 | // It's not the most recent allocation, so it cannot be expanded. |
| 552 | 562 | return new_len <= memory.len; |
| 553 | 563 | } |
| 554 | 564 | |
| ... | ... | @@ -556,15 +566,12 @@ fn resize(ctx: *anyopaque, memory: []u8, alignment: Alignment, new_len: usize, r |
| 556 | 566 | if (memory.len >= new_len) { |
| 557 | 567 | break :new_end_index cur_end_index - (memory.len - new_len); |
| 558 | 568 | } |
| 559 | | const cur_buf_len: usize = node.loadBuf().len; |
| 560 | | // Saturating arithmetic because `end_index` and `size` are not |
| 561 | | // guaranteed to be in sync. |
| 562 | | if (cur_buf_len -| cur_end_index >= new_len - memory.len) { |
| 569 | if (buf.len - cur_end_index >= new_len - memory.len) { |
| 563 | 570 | break :new_end_index cur_end_index + (new_len - memory.len); |
| 564 | 571 | } |
| 565 | 572 | return false; |
| 566 | 573 | }; |
| 567 | | assert(buf_ptr + new_end_index == memory.ptr + new_len); |
| 574 | assert(buf.ptr + new_end_index == memory.ptr + new_len); |
| 568 | 575 | |
| 569 | 576 | return null == @cmpxchgStrong( |
| 570 | 577 | usize, |
| ... | ... | @@ -589,16 +596,22 @@ fn free(ctx: *anyopaque, memory: []u8, alignment: Alignment, ret_addr: usize) vo |
| 589 | 596 | assert(memory.len > 0); |
| 590 | 597 | |
| 591 | 598 | const node = arena.loadFirstNode().?; |
| 592 | | const buf_ptr = @as([*]u8, @ptrCast(node)) + @sizeOf(Node); |
| 599 | const buf = node.loadBuf(); |
| 600 | |
| 601 | if (!sliceContainsSlice(buf, memory)) { |
| 602 | // Not within current node; we cannot free it. |
| 603 | return; |
| 604 | } |
| 593 | 605 | |
| 594 | 606 | const cur_end_index = @atomicLoad(usize, &node.end_index, .monotonic); |
| 595 | | if (buf_ptr + cur_end_index != memory.ptr + memory.len) { |
| 607 | |
| 608 | if (buf.ptr + cur_end_index != memory.ptr + memory.len) { |
| 596 | 609 | // Not the most recent allocation; we cannot free it. |
| 597 | 610 | return; |
| 598 | 611 | } |
| 599 | 612 | |
| 600 | 613 | const new_end_index = cur_end_index - memory.len; |
| 601 | | assert(buf_ptr + new_end_index == memory.ptr); |
| 614 | assert(buf.ptr + new_end_index == memory.ptr); |
| 602 | 615 | |
| 603 | 616 | _ = @cmpxchgStrong( |
| 604 | 617 | usize, |