mirror of
https://github.com/Ed94/Odin.git
synced 2026-07-30 19:30:06 +00:00
Clean up allocator stuff into allocators.odin
This commit is contained in:
@@ -0,0 +1,46 @@
|
|||||||
|
//+private
|
||||||
|
package os2
|
||||||
|
|
||||||
|
import "base:runtime"
|
||||||
|
|
||||||
|
file_allocator :: proc() -> runtime.Allocator {
|
||||||
|
return heap_allocator()
|
||||||
|
}
|
||||||
|
|
||||||
|
temp_allocator_proc :: runtime.arena_allocator_proc
|
||||||
|
|
||||||
|
@(private="file", thread_local)
|
||||||
|
global_default_temp_allocator_arena: runtime.Arena
|
||||||
|
|
||||||
|
temp_allocator :: proc() -> runtime.Allocator {
|
||||||
|
return runtime.Allocator{
|
||||||
|
procedure = temp_allocator_proc,
|
||||||
|
data = &global_default_temp_allocator_arena,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@(require_results)
|
||||||
|
temp_allocator_temp_begin :: proc(loc := #caller_location) -> (temp: runtime.Arena_Temp) {
|
||||||
|
temp = runtime.arena_temp_begin(&global_default_temp_allocator_arena, loc)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
temp_allocator_temp_end :: proc(temp: runtime.Arena_Temp, loc := #caller_location) {
|
||||||
|
runtime.arena_temp_end(temp, loc)
|
||||||
|
}
|
||||||
|
|
||||||
|
@(fini, private)
|
||||||
|
temp_allocator_fini :: proc() {
|
||||||
|
runtime.arena_destroy(&global_default_temp_allocator_arena)
|
||||||
|
global_default_temp_allocator_arena = {}
|
||||||
|
}
|
||||||
|
|
||||||
|
@(deferred_out=temp_allocator_temp_end)
|
||||||
|
TEMP_ALLOCATOR_GUARD :: #force_inline proc(ignore := false, loc := #caller_location) -> (runtime.Arena_Temp, runtime.Source_Code_Location) {
|
||||||
|
if ignore {
|
||||||
|
return {}, loc
|
||||||
|
} else {
|
||||||
|
return temp_allocator_temp_begin(loc), loc
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -19,9 +19,9 @@ _lookup_env :: proc(key: string, allocator: runtime.Allocator) -> (value: string
|
|||||||
return "", true
|
return "", true
|
||||||
}
|
}
|
||||||
|
|
||||||
_TEMP_ALLOCATOR_GUARD()
|
TEMP_ALLOCATOR_GUARD()
|
||||||
|
|
||||||
b := make([]u16, n+1, _temp_allocator())
|
b := make([]u16, n+1, temp_allocator())
|
||||||
|
|
||||||
n = win32.GetEnvironmentVariableW(wkey, raw_data(b), u32(len(b)))
|
n = win32.GetEnvironmentVariableW(wkey, raw_data(b), u32(len(b)))
|
||||||
if n == 0 {
|
if n == 0 {
|
||||||
@@ -50,8 +50,8 @@ _unset_env :: proc(key: string) -> bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_clear_env :: proc() {
|
_clear_env :: proc() {
|
||||||
_TEMP_ALLOCATOR_GUARD()
|
TEMP_ALLOCATOR_GUARD()
|
||||||
envs := environ(_temp_allocator())
|
envs := environ(temp_allocator())
|
||||||
for env in envs {
|
for env in envs {
|
||||||
for j in 1..<len(env) {
|
for j in 1..<len(env) {
|
||||||
if env[j] == '=' {
|
if env[j] == '=' {
|
||||||
|
|||||||
@@ -218,8 +218,8 @@ copy_file :: proc(dst_path, src_path: string) -> Error {
|
|||||||
src := open(src_path) or_return
|
src := open(src_path) or_return
|
||||||
defer close(src)
|
defer close(src)
|
||||||
|
|
||||||
info := fstat(src, _file_allocator()) or_return
|
info := fstat(src, file_allocator()) or_return
|
||||||
defer file_info_delete(info, _file_allocator())
|
defer file_info_delete(info, file_allocator())
|
||||||
if info.is_directory {
|
if info.is_directory {
|
||||||
return .Invalid_File
|
return .Invalid_File
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,10 +35,6 @@ _File :: struct {
|
|||||||
allocator: runtime.Allocator,
|
allocator: runtime.Allocator,
|
||||||
}
|
}
|
||||||
|
|
||||||
_file_allocator :: proc() -> runtime.Allocator {
|
|
||||||
return heap_allocator()
|
|
||||||
}
|
|
||||||
|
|
||||||
_open :: proc(name: string, flags: File_Flags, perm: File_Mode) -> (^File, Error) {
|
_open :: proc(name: string, flags: File_Flags, perm: File_Mode) -> (^File, Error) {
|
||||||
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
||||||
name_cstr := strings.clone_to_cstring(name, context.temp_allocator)
|
name_cstr := strings.clone_to_cstring(name, context.temp_allocator)
|
||||||
@@ -69,9 +65,9 @@ _open :: proc(name: string, flags: File_Flags, perm: File_Mode) -> (^File, Error
|
|||||||
}
|
}
|
||||||
|
|
||||||
_new_file :: proc(fd: uintptr, _: string) -> ^File {
|
_new_file :: proc(fd: uintptr, _: string) -> ^File {
|
||||||
file := new(File, _file_allocator())
|
file := new(File, file_allocator())
|
||||||
file.impl.fd = int(fd)
|
file.impl.fd = int(fd)
|
||||||
file.impl.allocator = _file_allocator()
|
file.impl.allocator = file_allocator()
|
||||||
file.impl.name = _get_full_path(file.impl.fd, file.impl.allocator)
|
file.impl.name = _get_full_path(file.impl.fd, file.impl.allocator)
|
||||||
file.stream = {
|
file.stream = {
|
||||||
data = file,
|
data = file,
|
||||||
|
|||||||
@@ -17,49 +17,6 @@ S_IWRITE :: 0o200
|
|||||||
_ERROR_BAD_NETPATH :: 53
|
_ERROR_BAD_NETPATH :: 53
|
||||||
MAX_RW :: 1<<30
|
MAX_RW :: 1<<30
|
||||||
|
|
||||||
_file_allocator :: proc() -> runtime.Allocator {
|
|
||||||
return heap_allocator()
|
|
||||||
}
|
|
||||||
|
|
||||||
_temp_allocator_proc :: runtime.arena_allocator_proc
|
|
||||||
|
|
||||||
@(private="file", thread_local)
|
|
||||||
_global_default_temp_allocator_arena: runtime.Arena
|
|
||||||
|
|
||||||
_temp_allocator :: proc() -> runtime.Allocator {
|
|
||||||
return runtime.Allocator{
|
|
||||||
procedure = _temp_allocator_proc,
|
|
||||||
data = &_global_default_temp_allocator_arena,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@(require_results)
|
|
||||||
_temp_allocator_temp_begin :: proc(loc := #caller_location) -> (temp: runtime.Arena_Temp) {
|
|
||||||
temp = runtime.arena_temp_begin(&_global_default_temp_allocator_arena, loc)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
_temp_allocator_temp_end :: proc(temp: runtime.Arena_Temp, loc := #caller_location) {
|
|
||||||
runtime.arena_temp_end(temp, loc)
|
|
||||||
}
|
|
||||||
|
|
||||||
@(fini, private)
|
|
||||||
_destroy_temp_allocator_fini :: proc() {
|
|
||||||
runtime.arena_destroy(&_global_default_temp_allocator_arena)
|
|
||||||
_global_default_temp_allocator_arena = {}
|
|
||||||
}
|
|
||||||
|
|
||||||
@(deferred_out=_temp_allocator_temp_end)
|
|
||||||
_TEMP_ALLOCATOR_GUARD :: #force_inline proc(ignore := false, loc := #caller_location) -> (runtime.Arena_Temp, runtime.Source_Code_Location) {
|
|
||||||
if ignore {
|
|
||||||
return {}, loc
|
|
||||||
} else {
|
|
||||||
return _temp_allocator_temp_begin(loc), loc
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
_File_Kind :: enum u8 {
|
_File_Kind :: enum u8 {
|
||||||
File,
|
File,
|
||||||
@@ -162,9 +119,9 @@ _new_file :: proc(handle: uintptr, name: string) -> ^File {
|
|||||||
if handle == INVALID_HANDLE {
|
if handle == INVALID_HANDLE {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
f := new(File, _file_allocator())
|
f := new(File, file_allocator())
|
||||||
|
|
||||||
f.impl.allocator = _file_allocator()
|
f.impl.allocator = file_allocator()
|
||||||
f.impl.fd = rawptr(handle)
|
f.impl.fd = rawptr(handle)
|
||||||
f.impl.name = strings.clone(name, f.impl.allocator)
|
f.impl.name = strings.clone(name, f.impl.allocator)
|
||||||
f.impl.wname = win32.utf8_to_wstring(name, f.impl.allocator)
|
f.impl.wname = win32.utf8_to_wstring(name, f.impl.allocator)
|
||||||
@@ -583,9 +540,9 @@ _normalize_link_path :: proc(p: []u16, allocator: runtime.Allocator) -> (str: st
|
|||||||
return "", _get_platform_error()
|
return "", _get_platform_error()
|
||||||
}
|
}
|
||||||
|
|
||||||
_TEMP_ALLOCATOR_GUARD()
|
TEMP_ALLOCATOR_GUARD()
|
||||||
|
|
||||||
buf := make([]u16, n+1, _temp_allocator())
|
buf := make([]u16, n+1, temp_allocator())
|
||||||
n = win32.GetFinalPathNameByHandleW(handle, raw_data(buf), u32(len(buf)), win32.VOLUME_NAME_DOS)
|
n = win32.GetFinalPathNameByHandleW(handle, raw_data(buf), u32(len(buf)), win32.VOLUME_NAME_DOS)
|
||||||
if n == 0 {
|
if n == 0 {
|
||||||
return "", _get_platform_error()
|
return "", _get_platform_error()
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ _mkdir_all :: proc(path: string, perm: File_Mode) -> Error {
|
|||||||
fix_root_directory :: proc(p: string) -> (s: string, allocated: bool, err: runtime.Allocator_Error) {
|
fix_root_directory :: proc(p: string) -> (s: string, allocated: bool, err: runtime.Allocator_Error) {
|
||||||
if len(p) == len(`\\?\c:`) {
|
if len(p) == len(`\\?\c:`) {
|
||||||
if is_path_separator(p[0]) && is_path_separator(p[1]) && p[2] == '?' && is_path_separator(p[3]) && p[5] == ':' {
|
if is_path_separator(p[0]) && is_path_separator(p[1]) && p[2] == '?' && is_path_separator(p[3]) && p[5] == ':' {
|
||||||
s = strings.concatenate({p, `\`}, _file_allocator()) or_return
|
s = strings.concatenate({p, `\`}, file_allocator()) or_return
|
||||||
allocated = true
|
allocated = true
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -31,9 +31,9 @@ _mkdir_all :: proc(path: string, perm: File_Mode) -> Error {
|
|||||||
return p, false, nil
|
return p, false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
_TEMP_ALLOCATOR_GUARD()
|
TEMP_ALLOCATOR_GUARD()
|
||||||
|
|
||||||
dir, err := stat(path, _temp_allocator())
|
dir, err := stat(path, temp_allocator())
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if dir.is_directory {
|
if dir.is_directory {
|
||||||
return nil
|
return nil
|
||||||
@@ -54,14 +54,14 @@ _mkdir_all :: proc(path: string, perm: File_Mode) -> Error {
|
|||||||
if j > 1 {
|
if j > 1 {
|
||||||
new_path, allocated := fix_root_directory(path[:j-1]) or_return
|
new_path, allocated := fix_root_directory(path[:j-1]) or_return
|
||||||
defer if allocated {
|
defer if allocated {
|
||||||
delete(new_path, _file_allocator())
|
delete(new_path, file_allocator())
|
||||||
}
|
}
|
||||||
mkdir_all(new_path, perm) or_return
|
mkdir_all(new_path, perm) or_return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = mkdir(path, perm)
|
err = mkdir(path, perm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
dir1, err1 := lstat(path, _temp_allocator())
|
dir1, err1 := lstat(path, temp_allocator())
|
||||||
if err1 == nil && dir1.is_directory {
|
if err1 == nil && dir1.is_directory {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -127,10 +127,10 @@ _fix_long_path_internal :: proc(path: string) -> string {
|
|||||||
return path
|
return path
|
||||||
}
|
}
|
||||||
|
|
||||||
_TEMP_ALLOCATOR_GUARD()
|
TEMP_ALLOCATOR_GUARD()
|
||||||
|
|
||||||
PREFIX :: `\\?`
|
PREFIX :: `\\?`
|
||||||
path_buf := make([]byte, len(PREFIX)+len(path)+1, _temp_allocator())
|
path_buf := make([]byte, len(PREFIX)+len(path)+1, temp_allocator())
|
||||||
copy(path_buf, PREFIX)
|
copy(path_buf, PREFIX)
|
||||||
n := len(path)
|
n := len(path)
|
||||||
r, w := 0, len(PREFIX)
|
r, w := 0, len(PREFIX)
|
||||||
|
|||||||
@@ -46,15 +46,15 @@ full_path_from_name :: proc(name: string, allocator: runtime.Allocator) -> (path
|
|||||||
if name == "" {
|
if name == "" {
|
||||||
name = "."
|
name = "."
|
||||||
}
|
}
|
||||||
_TEMP_ALLOCATOR_GUARD()
|
TEMP_ALLOCATOR_GUARD()
|
||||||
|
|
||||||
p := win32.utf8_to_utf16(name, _temp_allocator())
|
p := win32.utf8_to_utf16(name, temp_allocator())
|
||||||
|
|
||||||
n := win32.GetFullPathNameW(raw_data(p), 0, nil, nil)
|
n := win32.GetFullPathNameW(raw_data(p), 0, nil, nil)
|
||||||
if n == 0 {
|
if n == 0 {
|
||||||
return "", _get_platform_error()
|
return "", _get_platform_error()
|
||||||
}
|
}
|
||||||
buf := make([]u16, n+1, _temp_allocator())
|
buf := make([]u16, n+1, temp_allocator())
|
||||||
n = win32.GetFullPathNameW(raw_data(p), u32(len(buf)), raw_data(buf), nil)
|
n = win32.GetFullPathNameW(raw_data(p), u32(len(buf)), raw_data(buf), nil)
|
||||||
if n == 0 {
|
if n == 0 {
|
||||||
return "", _get_platform_error()
|
return "", _get_platform_error()
|
||||||
@@ -131,8 +131,8 @@ _cleanpath_from_handle :: proc(f: ^File, allocator: runtime.Allocator) -> (strin
|
|||||||
if n == 0 {
|
if n == 0 {
|
||||||
return "", _get_platform_error()
|
return "", _get_platform_error()
|
||||||
}
|
}
|
||||||
_TEMP_ALLOCATOR_GUARD()
|
TEMP_ALLOCATOR_GUARD()
|
||||||
buf := make([]u16, max(n, 260)+1, _temp_allocator())
|
buf := make([]u16, max(n, 260)+1, temp_allocator())
|
||||||
n = win32.GetFinalPathNameByHandleW(h, raw_data(buf), u32(len(buf)), 0)
|
n = win32.GetFinalPathNameByHandleW(h, raw_data(buf), u32(len(buf)), 0)
|
||||||
return _cleanpath_from_buf(buf[:n], allocator)
|
return _cleanpath_from_buf(buf[:n], allocator)
|
||||||
}
|
}
|
||||||
@@ -147,8 +147,8 @@ _cleanpath_from_handle_u16 :: proc(f: ^File) -> ([]u16, Error) {
|
|||||||
if n == 0 {
|
if n == 0 {
|
||||||
return nil, _get_platform_error()
|
return nil, _get_platform_error()
|
||||||
}
|
}
|
||||||
_TEMP_ALLOCATOR_GUARD()
|
TEMP_ALLOCATOR_GUARD()
|
||||||
buf := make([]u16, max(n, 260)+1, _temp_allocator())
|
buf := make([]u16, max(n, 260)+1, temp_allocator())
|
||||||
n = win32.GetFinalPathNameByHandleW(h, raw_data(buf), u32(len(buf)), 0)
|
n = win32.GetFinalPathNameByHandleW(h, raw_data(buf), u32(len(buf)), 0)
|
||||||
return _cleanpath_strip_prefix(buf[:n]), nil
|
return _cleanpath_strip_prefix(buf[:n]), nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,9 +17,9 @@ _temp_dir :: proc(allocator: runtime.Allocator) -> (string, runtime.Allocator_Er
|
|||||||
if n == 0 {
|
if n == 0 {
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
_TEMP_ALLOCATOR_GUARD()
|
TEMP_ALLOCATOR_GUARD()
|
||||||
|
|
||||||
b := make([]u16, max(win32.MAX_PATH, n), _temp_allocator())
|
b := make([]u16, max(win32.MAX_PATH, n), temp_allocator())
|
||||||
n = win32.GetTempPathW(u32(len(b)), raw_data(b))
|
n = win32.GetTempPathW(u32(len(b)), raw_data(b))
|
||||||
|
|
||||||
if n == 3 && b[1] == ':' && b[2] == '\\' {
|
if n == 3 && b[1] == ':' && b[2] == '\\' {
|
||||||
|
|||||||
Reference in New Issue
Block a user