Skip to content

Commit

Permalink
Add rewind/0 to Arena
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyalIcing committed Oct 30, 2023
1 parent b2a5ac5 commit 4dc73d4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
16 changes: 14 additions & 2 deletions test/examples/arena.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ defmodule Examples.Arena do

def alloc_impl(values_mod, byte_count) do
offset_global_name = values_mod.offset_global_name()
start_offset = values_mod.start_offset()
end_offset = values_mod.end_offset()

Orb.snippet Orb.S32, new_ptr: I32.UnsafePointer do
Expand All @@ -23,6 +22,15 @@ defmodule Examples.Arena do
end
end

def rewind_impl(values_mod) do
offset_global_name = values_mod.offset_global_name()
start_offset = values_mod.start_offset()

Orb.snippet Orb.S32 do
Instruction.global_set(Orb.I32, offset_global_name, start_offset * Orb.Memory.page_byte_size())
end
end

defmacro def(name, opts) do
quote do
require Orb.Memory
Expand All @@ -46,7 +54,7 @@ defmodule Examples.Arena do

global(
do: [
{offset_global_name, page_offset * 64 * 1024}
{offset_global_name, page_offset * Orb.Memory.page_byte_size()}
]
)

Expand All @@ -62,6 +70,10 @@ defmodule Examples.Arena do
defw alloc(byte_count: I32), I32.UnsafePointer, new_ptr: I32.UnsafePointer do
Examples.Arena.alloc_impl(Values, Instruction.local_get(I32, :byte_count))
end

defw rewind() do
Examples.Arena.rewind_impl(Values)
end
end
end

Expand Down
25 changes: 21 additions & 4 deletions test/examples/arena_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ defmodule Examples.ArenaTest do
Arena.def(First, pages: 2)
Arena.def(Second, pages: 3)

defw test(), {I32, I32, I32} do
defw test(), {I32, I32, I32, I32} do
First.alloc(16)
First.alloc(16)
Second.alloc(16)
First.rewind()
First.alloc(16)
end

defw just_enough(), I32, i: I32, final: I32 do
Expand All @@ -26,6 +28,7 @@ defmodule Examples.ArenaTest do
i = i + 1
AllocManyTimes.continue(if: i < 2 * 64 * 1024 / 16)
end

final
end

Expand All @@ -51,11 +54,25 @@ defmodule Examples.ArenaTest do
(func $Examples.ArenaTest.A.First.alloc (param $byte_count i32) (result i32)
"""

assert A.to_wat() =~ ~s"""
(func $Examples.ArenaTest.A.First.rewind
(i32.const #{0 * Orb.Memory.page_byte_size()})
(global.set $Examples.ArenaTest.A.First.bump_offset)
"""

assert A.to_wat() =~ ~s"""
(func $Examples.ArenaTest.A.Second.rewind
(i32.const #{2 * Orb.Memory.page_byte_size()})
(global.set $Examples.ArenaTest.A.Second.bump_offset)
"""

assert A.to_wat() =~ ~S"""
(func $test (export "test") (result i32 i32 i32)
(func $test (export "test") (result i32 i32 i32 i32)
(call $Examples.ArenaTest.A.First.alloc (i32.const 16))
(call $Examples.ArenaTest.A.First.alloc (i32.const 16))
(call $Examples.ArenaTest.A.Second.alloc (i32.const 16))
(call $Examples.ArenaTest.A.First.rewind)
(call $Examples.ArenaTest.A.First.alloc (i32.const 16))
)
"""
end
Expand All @@ -64,13 +81,13 @@ defmodule Examples.ArenaTest do
# IO.puts(A.to_wat())
i = Instance.run(A)
f = Instance.capture(i, :test, 0)
assert f.() === {0, 16, 131072}
assert f.() === {0, 16, 131_072, 0}
end

test "just enough allocations" do
i = Instance.run(A)
f = Instance.capture(i, :just_enough, 0)
assert 131056 = f.()
assert 131_056 = f.()
end

test "too many allocations" do
Expand Down

0 comments on commit 4dc73d4

Please sign in to comment.