| ... | @@ -442,6 +442,7 @@ test "DirectAllocator" { | ... | @@ -442,6 +442,7 @@ test "DirectAllocator" { |
| 442 | | 442 | |
| 443 | const allocator = &direct_allocator.allocator; | 443 | const allocator = &direct_allocator.allocator; |
| 444 | try testAllocator(allocator); | 444 | try testAllocator(allocator); |
| | 445 | try testAllocatorAligned(allocator, 16); |
| 445 | try testAllocatorLargeAlignment(allocator); | 446 | try testAllocatorLargeAlignment(allocator); |
| 446 | } | 447 | } |
| 447 | | 448 | |
| ... | @@ -453,6 +454,7 @@ test "ArenaAllocator" { | ... | @@ -453,6 +454,7 @@ test "ArenaAllocator" { |
| 453 | defer arena_allocator.deinit(); | 454 | defer arena_allocator.deinit(); |
| 454 | | 455 | |
| 455 | try testAllocator(&arena_allocator.allocator); | 456 | try testAllocator(&arena_allocator.allocator); |
| | 457 | try testAllocatorAligned(&arena_allocator.allocator, 16); |
| 456 | try testAllocatorLargeAlignment(&arena_allocator.allocator); | 458 | try testAllocatorLargeAlignment(&arena_allocator.allocator); |
| 457 | } | 459 | } |
| 458 | | 460 | |
| ... | @@ -461,6 +463,7 @@ test "FixedBufferAllocator" { | ... | @@ -461,6 +463,7 @@ test "FixedBufferAllocator" { |
| 461 | var fixed_buffer_allocator = FixedBufferAllocator.init(test_fixed_buffer_allocator_memory[0..]); | 463 | var fixed_buffer_allocator = FixedBufferAllocator.init(test_fixed_buffer_allocator_memory[0..]); |
| 462 | | 464 | |
| 463 | try testAllocator(&fixed_buffer_allocator.allocator); | 465 | try testAllocator(&fixed_buffer_allocator.allocator); |
| | 466 | try testAllocatorAligned(&fixed_buffer_allocator.allocator, 16); |
| 464 | try testAllocatorLargeAlignment(&fixed_buffer_allocator.allocator); | 467 | try testAllocatorLargeAlignment(&fixed_buffer_allocator.allocator); |
| 465 | } | 468 | } |
| 466 | | 469 | |
| ... | @@ -468,12 +471,13 @@ test "ThreadSafeFixedBufferAllocator" { | ... | @@ -468,12 +471,13 @@ test "ThreadSafeFixedBufferAllocator" { |
| 468 | var fixed_buffer_allocator = ThreadSafeFixedBufferAllocator.init(test_fixed_buffer_allocator_memory[0..]); | 471 | var fixed_buffer_allocator = ThreadSafeFixedBufferAllocator.init(test_fixed_buffer_allocator_memory[0..]); |
| 469 | | 472 | |
| 470 | try testAllocator(&fixed_buffer_allocator.allocator); | 473 | try testAllocator(&fixed_buffer_allocator.allocator); |
| | 474 | try testAllocatorAligned(&fixed_buffer_allocator.allocator, 16); |
| 471 | try testAllocatorLargeAlignment(&fixed_buffer_allocator.allocator); | 475 | try testAllocatorLargeAlignment(&fixed_buffer_allocator.allocator); |
| 472 | } | 476 | } |
| 473 | | 477 | |
| 474 | fn testAllocator(allocator: *mem.Allocator) !void { | 478 | fn testAllocator(allocator: *mem.Allocator) !void { |
| 475 | var slice = try allocator.alloc(*i32, 100); | 479 | var slice = try allocator.alloc(*i32, 100); |
| 476 | | 480 | assert(slice.len == 100); |
| 477 | for (slice) |*item, i| { | 481 | for (slice) |*item, i| { |
| 478 | item.* = try allocator.create(@intCast(i32, i)); | 482 | item.* = try allocator.create(@intCast(i32, i)); |
| 479 | } | 483 | } |
| ... | @@ -483,13 +487,43 @@ fn testAllocator(allocator: *mem.Allocator) !void { | ... | @@ -483,13 +487,43 @@ fn testAllocator(allocator: *mem.Allocator) !void { |
| 483 | } | 487 | } |
| 484 | | 488 | |
| 485 | slice = try allocator.realloc(*i32, slice, 20000); | 489 | slice = try allocator.realloc(*i32, slice, 20000); |
| | 490 | assert(slice.len == 20000); |
| 486 | slice = try allocator.realloc(*i32, slice, 50); | 491 | slice = try allocator.realloc(*i32, slice, 50); |
| | 492 | assert(slice.len == 50); |
| 487 | slice = try allocator.realloc(*i32, slice, 25); | 493 | slice = try allocator.realloc(*i32, slice, 25); |
| | 494 | assert(slice.len == 25); |
| | 495 | slice = try allocator.realloc(*i32, slice, 0); |
| | 496 | assert(slice.len == 0); |
| 488 | slice = try allocator.realloc(*i32, slice, 10); | 497 | slice = try allocator.realloc(*i32, slice, 10); |
| | 498 | assert(slice.len == 10); |
| 489 | | 499 | |
| 490 | allocator.free(slice); | 500 | allocator.free(slice); |
| 491 | } | 501 | } |
| 492 | | 502 | |
| | 503 | fn testAllocatorAligned(allocator: *mem.Allocator, comptime alignment: u29) !void { |
| | 504 | // initial |
| | 505 | var slice = try allocator.alignedAlloc(u8, alignment, 10); |
| | 506 | assert(slice.len == 10); |
| | 507 | // grow |
| | 508 | slice = try allocator.alignedRealloc(u8, alignment, slice, 100); |
| | 509 | assert(slice.len == 100); |
| | 510 | // shrink |
| | 511 | slice = try allocator.alignedRealloc(u8, alignment, slice, 10); |
| | 512 | assert(slice.len == 10); |
| | 513 | // go to zero |
| | 514 | slice = try allocator.alignedRealloc(u8, alignment, slice, 0); |
| | 515 | assert(slice.len == 0); |
| | 516 | // realloc from zero |
| | 517 | slice = try allocator.alignedRealloc(u8, alignment, slice, 100); |
| | 518 | assert(slice.len == 100); |
| | 519 | // shrink with shrink |
| | 520 | slice = allocator.alignedShrink(u8, alignment, slice, 10); |
| | 521 | assert(slice.len == 10); |
| | 522 | // shrink to zero |
| | 523 | slice = allocator.alignedShrink(u8, alignment, slice, 0); |
| | 524 | assert(slice.len == 0); |
| | 525 | } |
| | 526 | |
| 493 | fn testAllocatorLargeAlignment(allocator: *mem.Allocator) mem.Allocator.Error!void { | 527 | fn testAllocatorLargeAlignment(allocator: *mem.Allocator) mem.Allocator.Error!void { |
| 494 | //Maybe a platform's page_size is actually the same as or | 528 | //Maybe a platform's page_size is actually the same as or |
| 495 | // very near usize? | 529 | // very near usize? |