Skip to content

Commit

Permalink
Merge pull request #75 from karl-zylinski/arena-allocator
Browse files Browse the repository at this point in the history
Virtual growing arena allocator example
  • Loading branch information
karl-zylinski authored Jan 14, 2025
2 parents 11d71a2 + 6e2537b commit 4300d62
Show file tree
Hide file tree
Showing 17 changed files with 66 additions and 10 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
22 changes: 12 additions & 10 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,18 @@ jobs:
run: |
FLAGS="-vet -strict-style -vet-tabs -disallow-do -warnings-as-errors"
odin check basics/command_line_arguments $FLAGS
odin check basics/dir_info $FLAGS
odin check basics/dynamic_arrays $FLAGS
odin check basics/hellope $FLAGS
odin check basics/raw_console $FLAGS
odin check basics/raw_console -target:windows_amd64 $FLAGS
odin check basics/read_console_input $FLAGS
odin check basics/strings $FLAGS
odin check basics/variables $FLAGS
odin check basics/working_directory $FLAGS
odin check !basics/command_line_arguments $FLAGS
odin check !basics/dir_info $FLAGS
odin check !basics/dynamic_arrays $FLAGS
odin check !basics/hellope $FLAGS
odin check !basics/raw_console $FLAGS
odin check !basics/raw_console -target:windows_amd64 $FLAGS
odin check !basics/read_console_input $FLAGS
odin check !basics/strings $FLAGS
odin check !basics/variables $FLAGS
odin check !basics/working_directory $FLAGS
odin check arena_allocator $FLAGS
odin check directx/d3d12_triangle_sdl2 -target:windows_amd64 $FLAGS
Expand Down
51 changes: 51 additions & 0 deletions arena_allocator/arena_allocator.odin
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package arena_allocator

import "core:fmt"
import "core:os"

// virtual package implements a multi-purpose arena allocator. If you are on a
// platform that does not support virtual memory, then there is also a similar
// arena in `core:mem`.
import vmem "core:mem/virtual"

load_files :: proc() -> ([]string, vmem.Arena) {
// This creates a growing virtual memory arena. It uses virtual memory and
// can grow as things are added to it.
arena: vmem.Arena
arena_err := vmem.arena_init_growing(&arena)
ensure(arena_err == nil)
arena_alloc := vmem.arena_allocator(&arena)

// See arena_init_static for an arena that uses virtual memory, but cannot grow.

// See arena_init_buffer for an arena that does not use virtual memory,
// instead it relies on you feeding it a buffer.

f1, f1_ok := os.read_entire_file("file1.txt", arena_alloc)
ensure(f1_ok)

f2, f2_ok := os.read_entire_file("file2.txt", arena_alloc)
ensure(f2_ok)

f3, f3_ok := os.read_entire_file("file3.txt", arena_alloc)
ensure(f3_ok)

res := make([]string, 3, arena_alloc)
res[0] = string(f1)
res[1] = string(f2)
res[2] = string(f3)

return res, arena
}

main :: proc() {
files, arena := load_files()

for f in files {
fmt.println(f)
}

// This deallocates everything that was allocated on the arena:
// The loaded content of the files as well as the `files` slice.
vmem.arena_destroy(&arena)
}
1 change: 1 addition & 0 deletions arena_allocator/file1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is from file1.txt
1 change: 1 addition & 0 deletions arena_allocator/file2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is from file2.txt
1 change: 1 addition & 0 deletions arena_allocator/file3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is from file3.txt

0 comments on commit 4300d62

Please sign in to comment.