aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDanielGavin <danielgavin5@hotmail.com>2025-06-21 00:55:49 +0200
committerDanielGavin <danielgavin5@hotmail.com>2025-06-21 00:55:49 +0200
commit02658e0ea297f5f906acd2b887def53deac3698e (patch)
treed346d61dc6fafb1cbba0a02c130df85900732efb /src
parent1edbffe4a4f4094374ecb58e8bf686f998dd1451 (diff)
Move to new allocator for documents.
Diffstat (limited to 'src')
-rw-r--r--src/common/allocator.odin143
-rw-r--r--src/common/uri.odin26
-rw-r--r--src/server/caches.odin3
-rw-r--r--src/server/documents.odin23
-rw-r--r--src/testing/testing.odin10
5 files changed, 26 insertions, 179 deletions
diff --git a/src/common/allocator.odin b/src/common/allocator.odin
deleted file mode 100644
index a8e23c2..0000000
--- a/src/common/allocator.odin
+++ /dev/null
@@ -1,143 +0,0 @@
-package common
-
-import "base:runtime"
-
-import "core:mem"
-
-Scratch_Allocator :: struct {
- data: []byte,
- curr_offset: int,
- prev_allocation: rawptr,
- backup_allocator: mem.Allocator,
- leaked_allocations: [dynamic][]byte,
-}
-
-scratch_allocator_init :: proc(s: ^Scratch_Allocator, size: int, backup_allocator := context.allocator) {
- s.data, _ = mem.make_aligned([]byte, size, 2 * align_of(rawptr), backup_allocator)
- s.curr_offset = 0
- s.prev_allocation = nil
- s.backup_allocator = backup_allocator
- s.leaked_allocations.allocator = backup_allocator
-}
-
-scratch_allocator_destroy :: proc(s: ^Scratch_Allocator) {
- if s == nil {
- return
- }
- for ptr in s.leaked_allocations {
- mem.free_bytes(ptr, s.backup_allocator)
- }
- delete(s.leaked_allocations)
- delete(s.data, s.backup_allocator)
- s^ = {}
-}
-
-scratch_allocator_proc :: proc(
- allocator_data: rawptr,
- mode: mem.Allocator_Mode,
- size, alignment: int,
- old_memory: rawptr,
- old_size: int,
- loc := #caller_location,
-) -> (
- []byte,
- mem.Allocator_Error,
-) {
-
- s := (^Scratch_Allocator)(allocator_data)
-
- if s.data == nil {
- DEFAULT_BACKING_SIZE :: 1 << 22
- if !(context.allocator.procedure != scratch_allocator_proc && context.allocator.data != allocator_data) {
- panic("cyclic initialization of the scratch allocator with itself")
- }
- scratch_allocator_init(s, DEFAULT_BACKING_SIZE)
- }
-
- size := size
-
- switch mode {
- case .Alloc, .Alloc_Non_Zeroed:
- size = mem.align_forward_int(size, alignment)
-
- switch {
- case s.curr_offset + size <= len(s.data):
- start := uintptr(raw_data(s.data))
- ptr := start + uintptr(s.curr_offset)
- ptr = mem.align_forward_uintptr(ptr, uintptr(alignment))
- mem.zero(rawptr(ptr), size)
-
- s.prev_allocation = rawptr(ptr)
- offset := int(ptr - start)
- s.curr_offset = offset + size
- return mem.byte_slice(rawptr(ptr), size), nil
- }
-
- a := s.backup_allocator
- if a.procedure == nil {
- a = context.allocator
- s.backup_allocator = a
- }
-
- ptr, err := mem.alloc_bytes(size, alignment, a, loc)
- if err != nil {
- return ptr, err
- }
- if s.leaked_allocations == nil {
- s.leaked_allocations = make([dynamic][]byte, a)
- }
- append(&s.leaked_allocations, ptr)
-
- if logger := context.logger; logger.lowest_level <= .Warning {
- if logger.procedure != nil {
- logger.procedure(
- logger.data,
- .Warning,
- "mem.Scratch_Allocator resorted to backup_allocator",
- logger.options,
- loc,
- )
- }
- }
-
- return ptr, err
-
- case .Free:
- case .Free_All:
- s.curr_offset = 0
- s.prev_allocation = nil
- for ptr in s.leaked_allocations {
- mem.free_bytes(ptr, s.backup_allocator)
- }
- clear(&s.leaked_allocations)
-
- case .Resize, .Resize_Non_Zeroed:
- begin := uintptr(raw_data(s.data))
- end := begin + uintptr(len(s.data))
- old_ptr := uintptr(old_memory)
-
- data, err := scratch_allocator_proc(allocator_data, .Alloc, size, alignment, old_memory, old_size, loc)
- if err != nil {
- return data, err
- }
-
- runtime.copy(data, mem.byte_slice(old_memory, old_size))
- _, err = scratch_allocator_proc(allocator_data, .Free, 0, alignment, old_memory, old_size, loc)
- return data, err
-
- case .Query_Features:
- set := (^mem.Allocator_Mode_Set)(old_memory)
- if set != nil {
- set^ = {.Alloc, .Free, .Free_All, .Resize, .Query_Features}
- }
- return nil, nil
- case .Query_Info:
- return nil, nil
- }
-
- return nil, nil
-}
-
-scratch_allocator :: proc(allocator: ^Scratch_Allocator) -> mem.Allocator {
- return mem.Allocator{procedure = scratch_allocator_proc, data = allocator}
-}
diff --git a/src/common/uri.odin b/src/common/uri.odin
index 0e2c705..32c32e0 100644
--- a/src/common/uri.odin
+++ b/src/common/uri.odin
@@ -1,11 +1,11 @@
package common
+import "core:fmt"
import "core:mem"
-import "core:strings"
+import "core:path/filepath"
import "core:strconv"
-import "core:fmt"
+import "core:strings"
import "core:unicode/utf8"
-import "core:path/filepath"
Uri :: struct {
uri: string,
@@ -50,16 +50,13 @@ create_uri :: proc(path: string, allocator: mem.Allocator) -> Uri {
builder := strings.builder_make(allocator)
//bad
- when ODIN_OS == .Windows {
+ when ODIN_OS == .Windows && !ODIN_TEST {
strings.write_string(&builder, "file:///")
} else {
strings.write_string(&builder, "file://")
}
- strings.write_string(
- &builder,
- encode_percent(path_forward, context.temp_allocator),
- )
+ strings.write_string(&builder, encode_percent(path_forward, context.temp_allocator))
uri: Uri
@@ -93,10 +90,7 @@ encode_percent :: proc(value: string, allocator: mem.Allocator) -> string {
for i := 0; i < w; i += 1 {
strings.write_string(
&builder,
- strings.concatenate(
- {"%", fmt.tprintf("%X", data[index + i])},
- context.temp_allocator,
- ),
+ strings.concatenate({"%", fmt.tprintf("%X", data[index + i])}, context.temp_allocator),
)
}
} else {
@@ -125,13 +119,7 @@ starts_with :: proc(value: string, starts_with: string) -> bool {
}
@(private)
-decode_percent :: proc(
- value: string,
- allocator: mem.Allocator,
-) -> (
- string,
- bool,
-) {
+decode_percent :: proc(value: string, allocator: mem.Allocator) -> (string, bool) {
builder := strings.builder_make(allocator)
for i := 0; i < len(value); i += 1 {
diff --git a/src/server/caches.odin b/src/server/caches.odin
index 73a7613..f8ca0d0 100644
--- a/src/server/caches.odin
+++ b/src/server/caches.odin
@@ -3,6 +3,7 @@ package server
import "src:common"
import "core:time"
+import "core:mem/virtual"
//Used in semantic tokens and inlay hints to handle the entire file being resolved.
@@ -20,7 +21,7 @@ file_resolve_cache: FileResolveCache
resolve_entire_file_cached :: proc(document: ^Document) -> map[uintptr]SymbolAndNode {
if document.uri.uri not_in file_resolve_cache.files {
file_resolve_cache.files[document.uri.uri] = FileResolve {
- symbols = resolve_entire_file(document, .None, common.scratch_allocator(document.allocator)),
+ symbols = resolve_entire_file(document, .None, virtual.arena_allocator(document.allocator)),
}
}
diff --git a/src/server/documents.odin b/src/server/documents.odin
index eca2938..dee90d5 100644
--- a/src/server/documents.odin
+++ b/src/server/documents.odin
@@ -5,6 +5,7 @@ import "base:intrinsics"
import "core:fmt"
import "core:log"
import "core:mem"
+import "core:mem/virtual"
import "core:odin/ast"
import "core:odin/parser"
import "core:odin/tokenizer"
@@ -40,7 +41,7 @@ Document :: struct {
ast: ast.File,
imports: []Package,
package_name: string,
- allocator: ^common.Scratch_Allocator, //because parser does not support freeing I use arena allocators for each document
+ allocator: ^virtual.Arena, //because parser does not support freeing I use arena allocators for each document
operating_on: int, //atomic
version: Maybe(int),
}
@@ -48,20 +49,20 @@ Document :: struct {
DocumentStorage :: struct {
documents: map[string]Document,
- free_allocators: [dynamic]^common.Scratch_Allocator,
+ free_allocators: [dynamic]^virtual.Arena,
}
document_storage: DocumentStorage
document_storage_shutdown :: proc() {
for k, v in document_storage.documents {
- common.scratch_allocator_destroy(v.allocator)
+ virtual.arena_destroy(v.allocator)
free(v.allocator)
delete(k)
}
for alloc in document_storage.free_allocators {
- common.scratch_allocator_destroy(alloc)
+ virtual.arena_destroy(alloc)
free(alloc)
}
@@ -69,18 +70,18 @@ document_storage_shutdown :: proc() {
delete(document_storage.documents)
}
-document_get_allocator :: proc() -> ^common.Scratch_Allocator {
+document_get_allocator :: proc() -> ^virtual.Arena {
if len(document_storage.free_allocators) > 0 {
return pop(&document_storage.free_allocators)
} else {
- allocator := new(common.Scratch_Allocator)
- common.scratch_allocator_init(allocator, mem.Megabyte * 3)
+ allocator := new(virtual.Arena)
+ _ = virtual.arena_init_growing(allocator)
return allocator
}
}
-document_free_allocator :: proc(allocator: ^common.Scratch_Allocator) {
- free_all(common.scratch_allocator(allocator))
+document_free_allocator :: proc(allocator: ^virtual.Arena) {
+ free_all(virtual.arena_allocator(allocator))
append(&document_storage.free_allocators, allocator)
}
@@ -394,9 +395,9 @@ parse_document :: proc(document: ^Document, config: ^common.Config) -> ([]Parser
delete_key(&file_resolve_cache.files, document.uri.uri)
}
- free_all(common.scratch_allocator(document.allocator))
+ free_all(virtual.arena_allocator(document.allocator))
- context.allocator = common.scratch_allocator(document.allocator)
+ context.allocator = virtual.arena_allocator(document.allocator)
pkg := new(ast.Package)
pkg.kind = .Normal
diff --git a/src/testing/testing.odin b/src/testing/testing.odin
index 0fff9d6..473eaf9 100644
--- a/src/testing/testing.odin
+++ b/src/testing/testing.odin
@@ -3,6 +3,7 @@ package ols_testing
import "core:fmt"
import "core:log"
import "core:mem"
+import "core:mem/virtual"
import "core:odin/ast"
import "core:odin/parser"
import "core:path/filepath"
@@ -34,10 +35,10 @@ setup :: proc(src: ^Source) {
src.document.client_owned = true
src.document.text = transmute([]u8)src.main
src.document.used_text = len(src.document.text)
- src.document.allocator = new(common.Scratch_Allocator, context.temp_allocator)
+ src.document.allocator = new(virtual.Arena, context.temp_allocator)
src.document.package_name = "test"
- common.scratch_allocator_init(src.document.allocator, mem.Kilobyte * 2000, context.temp_allocator)
+ _ = virtual.arena_init_growing(src.document.allocator)
//no unicode in tests currently
current, last: u8
@@ -73,7 +74,7 @@ setup :: proc(src: ^Source) {
server.document_refresh(src.document, &src.config, nil)
for src_pkg in src.packages {
- context.allocator = common.scratch_allocator(src.document.allocator)
+ context.allocator = virtual.arena_allocator(src.document.allocator)
uri := common.create_uri(fmt.aprintf("test/%v/package.odin", src_pkg.pkg), context.temp_allocator)
@@ -119,8 +120,7 @@ setup :: proc(src: ^Source) {
teardown :: proc(src: ^Source) {
server.free_index()
server.indexer.index = {}
-
- common.scratch_allocator_destroy(src.document.allocator)
+ virtual.arena_destroy(src.document.allocator)
}
expect_signature_labels :: proc(t: ^testing.T, src: ^Source, expect_labels: []string) {