mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 07:38:48 +00:00
Clean-up and unification for the allocation procedures
This commit is contained in:
+5
-36
@@ -61,55 +61,24 @@ DEFAULT_PAGE_SIZE ::
|
|||||||
4 * 1024
|
4 * 1024
|
||||||
|
|
||||||
alloc :: proc(size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> rawptr {
|
alloc :: proc(size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> rawptr {
|
||||||
if size == 0 {
|
data, _ := runtime.mem_alloc(size, alignment, allocator, loc)
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if allocator.procedure == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
data, err := allocator.procedure(allocator.data, .Alloc, size, alignment, nil, 0, loc)
|
|
||||||
_ = err
|
|
||||||
return raw_data(data)
|
return raw_data(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
alloc_bytes :: proc(size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> ([]byte, Allocator_Error) {
|
alloc_bytes :: proc(size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> ([]byte, Allocator_Error) {
|
||||||
if size == 0 {
|
return runtime.mem_alloc(size, alignment, allocator, loc)
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
if allocator.procedure == nil {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
return allocator.procedure(allocator.data, .Alloc, size, alignment, nil, 0, loc)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
free :: proc(ptr: rawptr, allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
|
free :: proc(ptr: rawptr, allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
|
||||||
if ptr == nil {
|
return runtime.mem_free(ptr, allocator, loc)
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if allocator.procedure == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
_, err := allocator.procedure(allocator.data, .Free, 0, 0, ptr, 0, loc)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
free_bytes :: proc(bytes: []byte, allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
|
free_bytes :: proc(bytes: []byte, allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
|
||||||
if bytes == nil {
|
return runtime.mem_free_bytes(bytes, allocator, loc)
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if allocator.procedure == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
_, err := allocator.procedure(allocator.data, .Free, 0, 0, raw_data(bytes), len(bytes), loc)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
free_all :: proc(allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
|
free_all :: proc(allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
|
||||||
if allocator.procedure != nil {
|
return runtime.mem_free_all(allocator, loc)
|
||||||
_, err := allocator.procedure(allocator.data, .Free_All, 0, 0, nil, 0, loc)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
resize :: proc(ptr: rawptr, old_size, new_size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> rawptr {
|
resize :: proc(ptr: rawptr, old_size, new_size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> rawptr {
|
||||||
|
|||||||
@@ -197,7 +197,7 @@ __slice_resize :: proc(array_: ^$T/[]$E, new_count: int, allocator: Allocator, l
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if new_data != nil || elem_size == 0 {
|
if new_data != nil || size_of(E) == 0 {
|
||||||
array.data = raw_data(new_data)
|
array.data = raw_data(new_data)
|
||||||
array.len = new_count
|
array.len = new_count
|
||||||
return true
|
return true
|
||||||
|
|||||||
+21
-23
@@ -103,7 +103,7 @@ mem_zero :: proc "contextless" (data: rawptr, len: int) -> rawptr {
|
|||||||
if data == nil {
|
if data == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if len < 0 {
|
if len <= 0 {
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
intrinsics.mem_zero(data, len)
|
intrinsics.mem_zero(data, len)
|
||||||
@@ -111,22 +111,18 @@ mem_zero :: proc "contextless" (data: rawptr, len: int) -> rawptr {
|
|||||||
}
|
}
|
||||||
|
|
||||||
mem_copy :: proc "contextless" (dst, src: rawptr, len: int) -> rawptr {
|
mem_copy :: proc "contextless" (dst, src: rawptr, len: int) -> rawptr {
|
||||||
if src == nil {
|
if src != nil && dst != src && len > 0 {
|
||||||
return dst
|
// NOTE(bill): This _must_ be implemented like C's memmove
|
||||||
|
intrinsics.mem_copy(dst, src, len)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE(bill): This _must_ be implemented like C's memmove
|
|
||||||
intrinsics.mem_copy(dst, src, len)
|
|
||||||
return dst
|
return dst
|
||||||
}
|
}
|
||||||
|
|
||||||
mem_copy_non_overlapping :: proc "contextless" (dst, src: rawptr, len: int) -> rawptr {
|
mem_copy_non_overlapping :: proc "contextless" (dst, src: rawptr, len: int) -> rawptr {
|
||||||
if src == nil {
|
if src != nil && dst != src && len > 0 {
|
||||||
return dst
|
// NOTE(bill): This _must_ be implemented like C's memcpy
|
||||||
|
intrinsics.mem_copy_non_overlapping(dst, src, len)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE(bill): This _must_ be implemented like C's memcpy
|
|
||||||
intrinsics.mem_copy_non_overlapping(dst, src, len)
|
|
||||||
return dst
|
return dst
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -142,28 +138,30 @@ mem_alloc_bytes :: #force_inline proc(size: int, alignment: int = DEFAULT_ALIGNM
|
|||||||
return allocator.procedure(allocator.data, .Alloc, size, alignment, nil, 0, loc)
|
return allocator.procedure(allocator.data, .Alloc, size, alignment, nil, 0, loc)
|
||||||
}
|
}
|
||||||
|
|
||||||
mem_alloc :: #force_inline proc(size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> (rawptr, Allocator_Error) {
|
mem_alloc :: #force_inline proc(size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> ([]byte, Allocator_Error) {
|
||||||
if size == 0 {
|
if size == 0 || allocator.procedure == nil {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
if allocator.procedure == nil {
|
return allocator.procedure(allocator.data, .Alloc, size, alignment, nil, 0, loc)
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
data, err := allocator.procedure(allocator.data, .Alloc, size, alignment, nil, 0, loc)
|
|
||||||
return raw_data(data), err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mem_free :: #force_inline proc(ptr: rawptr, allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
|
mem_free :: #force_inline proc(ptr: rawptr, allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
|
||||||
if ptr == nil {
|
if ptr == nil || allocator.procedure == nil {
|
||||||
return .None
|
return nil
|
||||||
}
|
|
||||||
if allocator.procedure == nil {
|
|
||||||
return .None
|
|
||||||
}
|
}
|
||||||
_, err := allocator.procedure(allocator.data, .Free, 0, 0, ptr, 0, loc)
|
_, err := allocator.procedure(allocator.data, .Free, 0, 0, ptr, 0, loc)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mem_free_bytes :: #force_inline proc(bytes: []byte, allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
|
||||||
|
if bytes == nil || allocator.procedure == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
_, err := allocator.procedure(allocator.data, .Free, 0, 0, raw_data(bytes), len(bytes), loc)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
mem_free_all :: #force_inline proc(allocator := context.allocator, loc := #caller_location) -> (err: Allocator_Error) {
|
mem_free_all :: #force_inline proc(allocator := context.allocator, loc := #caller_location) -> (err: Allocator_Error) {
|
||||||
if allocator.procedure != nil {
|
if allocator.procedure != nil {
|
||||||
_, err = allocator.procedure(allocator.data, .Free_All, 0, 0, nil, 0, loc)
|
_, err = allocator.procedure(allocator.data, .Free_All, 0, 0, nil, 0, loc)
|
||||||
|
|||||||
+11
-11
@@ -31,31 +31,31 @@ intern_destroy :: proc(m: ^Intern) {
|
|||||||
|
|
||||||
// returns the `text` string from the intern map - gets set if it didnt exist yet
|
// 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
|
// the returned string lives as long as the map entry lives
|
||||||
intern_get :: proc(m: ^Intern, text: string) -> string {
|
intern_get :: proc(m: ^Intern, text: string) -> (str: string, err: runtime.Allocator_Error) {
|
||||||
entry := _intern_get_entry(m, text)
|
entry := _intern_get_entry(m, text) or_return
|
||||||
#no_bounds_check return string(entry.str[:entry.len])
|
#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
|
// 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
|
// the returned cstring lives as long as the map entry lives
|
||||||
intern_get_cstring :: proc(m: ^Intern, text: string) -> cstring {
|
intern_get_cstring :: proc(m: ^Intern, text: string) -> (str: cstring, err: runtime.Allocator_Error) {
|
||||||
entry := _intern_get_entry(m, text)
|
entry := _intern_get_entry(m, text) or_return
|
||||||
return cstring(&entry.str[0])
|
return cstring(&entry.str[0]), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// looks up wether the `text` string exists in the map, returns the entry
|
// looks up wether the `text` string exists in the map, returns the entry
|
||||||
// sets & allocates the entry if it wasnt set yet
|
// 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 {
|
if prev, ok := m.entries[text]; ok {
|
||||||
return prev
|
return prev, nil
|
||||||
}
|
}
|
||||||
if m.allocator.procedure == nil {
|
if m.allocator.procedure == nil {
|
||||||
m.allocator = context.allocator
|
m.allocator = context.allocator
|
||||||
}
|
}
|
||||||
|
|
||||||
entry_size := int(offset_of(Intern_Entry, str)) + len(text) + 1
|
entry_size := int(offset_of(Intern_Entry, str)) + len(text) + 1
|
||||||
ptr, _ := runtime.mem_alloc(entry_size, align_of(Intern_Entry), m.allocator)
|
bytes := runtime.mem_alloc(entry_size, align_of(Intern_Entry), m.allocator) or_return
|
||||||
new_entry := (^Intern_Entry)(ptr)
|
new_entry = (^Intern_Entry)(raw_data(bytes))
|
||||||
|
|
||||||
new_entry.len = len(text)
|
new_entry.len = len(text)
|
||||||
copy(new_entry.str[:new_entry.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])
|
key := string(new_entry.str[:new_entry.len])
|
||||||
m.entries[key] = new_entry
|
m.entries[key] = new_entry
|
||||||
return new_entry
|
return new_entry, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user