doc.odin 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. A platform agnostic way to reserve/commit/decommit virtual memory.
  3. virtual.Arena usage
  4. Example:
  5. // Source: https://github.com/odin-lang/examples/blob/master/arena_allocator/arena_allocator.odin
  6. import "core:fmt"
  7. import "core:os"
  8. // virtual package implements a multi-purpose arena allocator. If you are on a
  9. // platform that does not support virtual memory, then there is also a similar
  10. // arena in `core:mem`.
  11. import vmem "core:mem/virtual"
  12. load_files :: proc() -> ([]string, vmem.Arena) {
  13. // This creates a growing virtual memory arena. It uses virtual memory and
  14. // can grow as things are added to it.
  15. arena: vmem.Arena
  16. arena_err := vmem.arena_init_growing(&arena)
  17. ensure(arena_err == nil)
  18. arena_alloc := vmem.arena_allocator(&arena)
  19. // See arena_init_static for an arena that uses virtual memory, but cannot grow.
  20. // See arena_init_buffer for an arena that does not use virtual memory,
  21. // instead it relies on you feeding it a buffer.
  22. f1, f1_ok := os.read_entire_file("file1.txt", arena_alloc)
  23. ensure(f1_ok)
  24. f2, f2_ok := os.read_entire_file("file2.txt", arena_alloc)
  25. ensure(f2_ok)
  26. f3, f3_ok := os.read_entire_file("file3.txt", arena_alloc)
  27. ensure(f3_ok)
  28. res := make([]string, 3, arena_alloc)
  29. res[0] = string(f1)
  30. res[1] = string(f2)
  31. res[2] = string(f3)
  32. return res, arena
  33. }
  34. main :: proc() {
  35. files, arena := load_files()
  36. for f in files {
  37. fmt.println(f)
  38. }
  39. // This deallocates everything that was allocated on the arena:
  40. // The loaded content of the files as well as the `files` slice.
  41. vmem.arena_destroy(&arena)
  42. }
  43. */
  44. package mem_virtual