|
| 1 | +sb = MallocSlabBuffer(;slab_size=16_384, slabs_max_length=1, custom_slabs_max_length=0) |
| 2 | + |
| 3 | +@no_escape sb begin |
| 4 | + p = sb.current |
| 5 | + e = sb.slab_end |
| 6 | + # Allocate something that takes up almost all the room on the slab |
| 7 | + x1 = @alloc(Int8, 16_384÷2 - 1) |
| 8 | + x2 = @alloc(Int8, 16_384÷2 - 1) |
| 9 | + @test pointer(x1) == p |
| 10 | + @test pointer(x2) == pointer(x1) + 16_384÷2 - 1 |
| 11 | + @test sb.slabs_max_length == 1 |
| 12 | + @test sb.slabs_length == 1 |
| 13 | + for i ∈ 1:5 |
| 14 | + @no_escape sb begin |
| 15 | + @test sb.current == p + 16_384 - 2 |
| 16 | + @test p <= sb.current <= e |
| 17 | + # Allocate a new vector that won't fit on the old slab |
| 18 | + y = @alloc(Int, 10) |
| 19 | + # A new slab was allocated automatically |
| 20 | + @test !(p <= sb.current <= e) |
| 21 | + @test sb.slabs_length == 2 |
| 22 | + |
| 23 | + @test sb.slabs_max_length == 65 |
| 24 | + end |
| 25 | + end |
| 26 | + @test sb.custom_slabs_max_length == 0 |
| 27 | + # Allocate something too big to fit on any slab |
| 28 | + z = @alloc(Int, 100_000) |
| 29 | + @test sb.custom_slabs_max_length == 0 |
| 30 | + |
| 31 | + # This doesn't effect sb.current |
| 32 | + @test sb.current == p + 16_384 - 2 |
| 33 | + @test sb.current == p + 16_384 - 2 |
| 34 | + @test sb.slab_end == e |
| 35 | + @test !(p <= pointer(z) <= e) |
| 36 | + # The pointer for z is tracked in the custom_slabs field instead |
| 37 | + @test pointer(z) == unsafe_load(sb.custom_slabs, sb.custom_slabs_length) |
| 38 | +end |
0 commit comments