aboutsummaryrefslogtreecommitdiff
path: root/core/strings/intern.odin
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2022-08-08 12:23:19 +0100
committergingerBill <bill@gingerbill.org>2022-08-08 12:23:19 +0100
commitc97a8418dc9e99a0f39c74512cbc9d011f84426c (patch)
treed953d6d4b8ea36cb55a8619ce7ec66a4adc32c5b /core/strings/intern.odin
parent4aca9372a6292729c042242728800e0b619ecd96 (diff)
Clean-up and unification for the allocation procedures
Diffstat (limited to 'core/strings/intern.odin')
-rw-r--r--core/strings/intern.odin22
1 files changed, 11 insertions, 11 deletions
diff --git a/core/strings/intern.odin b/core/strings/intern.odin
index 1e9577e61..5e9193a0d 100644
--- a/core/strings/intern.odin
+++ b/core/strings/intern.odin
@@ -31,31 +31,31 @@ intern_destroy :: proc(m: ^Intern) {
// returns the `text` string from the intern map - gets set if it didnt exist yet
// the returned string lives as long as the map entry lives
-intern_get :: proc(m: ^Intern, text: string) -> string {
- entry := _intern_get_entry(m, text)
- #no_bounds_check return string(entry.str[:entry.len])
+intern_get :: proc(m: ^Intern, text: string) -> (str: string, err: runtime.Allocator_Error) {
+ entry := _intern_get_entry(m, text) or_return
+ #no_bounds_check return string(entry.str[:entry.len]), nil
}
// returns the `text` cstring from the intern map - gets set if it didnt exist yet
// the returned cstring lives as long as the map entry lives
-intern_get_cstring :: proc(m: ^Intern, text: string) -> cstring {
- entry := _intern_get_entry(m, text)
- return cstring(&entry.str[0])
+intern_get_cstring :: proc(m: ^Intern, text: string) -> (str: cstring, err: runtime.Allocator_Error) {
+ entry := _intern_get_entry(m, text) or_return
+ return cstring(&entry.str[0]), nil
}
// looks up wether the `text` string exists in the map, returns the entry
// sets & allocates the entry if it wasnt set yet
-_intern_get_entry :: proc(m: ^Intern, text: string) -> ^Intern_Entry #no_bounds_check {
+_intern_get_entry :: proc(m: ^Intern, text: string) -> (new_entry: ^Intern_Entry, err: runtime.Allocator_Error) #no_bounds_check {
if prev, ok := m.entries[text]; ok {
- return prev
+ return prev, nil
}
if m.allocator.procedure == nil {
m.allocator = context.allocator
}
entry_size := int(offset_of(Intern_Entry, str)) + len(text) + 1
- ptr, _ := runtime.mem_alloc(entry_size, align_of(Intern_Entry), m.allocator)
- new_entry := (^Intern_Entry)(ptr)
+ bytes := runtime.mem_alloc(entry_size, align_of(Intern_Entry), m.allocator) or_return
+ new_entry = (^Intern_Entry)(raw_data(bytes))
new_entry.len = len(text)
copy(new_entry.str[:new_entry.len], text)
@@ -63,5 +63,5 @@ _intern_get_entry :: proc(m: ^Intern, text: string) -> ^Intern_Entry #no_bounds_
key := string(new_entry.str[:new_entry.len])
m.entries[key] = new_entry
- return new_entry
+ return new_entry, nil
}