mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-03 22:28:46 +00:00
Bug Fixes: some assertions; variable inits;
Remove some dead code
This commit is contained in:
@@ -27,6 +27,24 @@ align_forward :: proc(ptr: rawptr, align: int) -> rawptr {
|
||||
}
|
||||
|
||||
|
||||
AllocationHeader :: struct {
|
||||
size: int
|
||||
}
|
||||
allocation_header_fill :: proc(header: ^AllocationHeader, data: rawptr, size: int) {
|
||||
header.size = size
|
||||
ptr := ptr_offset(header, 1) as ^int
|
||||
|
||||
for i := 0; ptr as rawptr < data; i++ {
|
||||
ptr_offset(ptr, i)^ = -1
|
||||
}
|
||||
}
|
||||
allocation_header :: proc(data: rawptr) -> ^AllocationHeader {
|
||||
p := data as ^int
|
||||
for ptr_offset(p, -1)^ == -1 {
|
||||
p = ptr_offset(p, -1)
|
||||
}
|
||||
return ptr_offset(p as ^AllocationHeader, -1)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -103,3 +103,15 @@ read_entire_file :: proc(name: string) -> (string, bool) {
|
||||
|
||||
return data as string, true
|
||||
}
|
||||
|
||||
|
||||
|
||||
heap_alloc :: proc(size: int) -> rawptr {
|
||||
return win32.HeapAlloc(win32.GetProcessHeap(), win32.HEAP_ZERO_MEMORY, size)
|
||||
}
|
||||
heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr {
|
||||
return win32.HeapReAlloc(win32.GetProcessHeap(), win32.HEAP_ZERO_MEMORY, ptr, new_size)
|
||||
}
|
||||
heap_free :: proc(ptr: rawptr) {
|
||||
win32.HeapFree(win32.GetProcessHeap(), 0, ptr)
|
||||
}
|
||||
|
||||
+17
-14
@@ -2,6 +2,7 @@
|
||||
|
||||
#import "os.odin"
|
||||
#import "fmt.odin"
|
||||
#import "mem.odin"
|
||||
|
||||
// IMPORTANT NOTE(bill): Do not change the order of any of this data
|
||||
// The compiler relies upon this _exact_ order
|
||||
@@ -97,16 +98,6 @@ byte_swap64 :: proc(b: u64) -> u64 #foreign "llvm.bswap.i64"
|
||||
fmuladd32 :: proc(a, b, c: f32) -> f32 #foreign "llvm.fmuladd.f32"
|
||||
fmuladd64 :: proc(a, b, c: f64) -> f64 #foreign "llvm.fmuladd.f64"
|
||||
|
||||
heap_alloc :: proc(len: int) -> rawptr {
|
||||
c_malloc :: proc(len: int) -> rawptr #foreign "malloc"
|
||||
return c_malloc(len)
|
||||
}
|
||||
|
||||
heap_free :: proc(ptr: rawptr) {
|
||||
c_free :: proc(ptr: rawptr) #foreign "free"
|
||||
c_free(ptr)
|
||||
}
|
||||
|
||||
current_thread_id :: proc() -> int {
|
||||
GetCurrentThreadId :: proc() -> u32 #foreign #dll_import
|
||||
return GetCurrentThreadId() as int
|
||||
@@ -251,14 +242,26 @@ __default_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator.Mode,
|
||||
using Allocator.Mode
|
||||
match mode {
|
||||
case ALLOC:
|
||||
return heap_alloc(size)
|
||||
case RESIZE:
|
||||
return default_resize_align(old_memory, old_size, size, alignment)
|
||||
total_size := size + alignment + size_of(mem.AllocationHeader)
|
||||
ptr := os.heap_alloc(total_size)
|
||||
header := ptr as ^mem.AllocationHeader
|
||||
ptr = mem.align_forward(ptr_offset(header, 1), alignment)
|
||||
mem.allocation_header_fill(header, ptr, size)
|
||||
memory_zero(ptr, size)
|
||||
return ptr
|
||||
case FREE:
|
||||
heap_free(old_memory)
|
||||
os.heap_free(mem.allocation_header(old_memory))
|
||||
return null
|
||||
case FREE_ALL:
|
||||
// NOTE(bill): Does nothing
|
||||
case RESIZE:
|
||||
total_size := size + alignment + size_of(mem.AllocationHeader)
|
||||
ptr := os.heap_resize(mem.allocation_header(old_memory), total_size)
|
||||
header := ptr as ^mem.AllocationHeader
|
||||
ptr = mem.align_forward(ptr_offset(header, 1), alignment)
|
||||
mem.allocation_header_fill(header, ptr, size)
|
||||
memory_zero(ptr, size)
|
||||
return ptr
|
||||
}
|
||||
|
||||
return null
|
||||
|
||||
+3
-2
@@ -162,8 +162,9 @@ OPEN_ALWAYS :: 4
|
||||
TRUNCATE_EXISTING :: 5
|
||||
|
||||
|
||||
HeapAlloc :: proc(h: HANDLE, flags: u32, bytes: int) -> rawptr #foreign #dll_import
|
||||
HeapFree :: proc(h: HANDLE, flags: u32, memory: rawptr) -> BOOL #foreign #dll_import
|
||||
HeapAlloc :: proc(h: HANDLE, flags: u32, bytes: int) -> rawptr #foreign #dll_import
|
||||
HeapReAlloc :: proc(h: HANDLE, flags: u32, memory: rawptr, bytes: int) -> rawptr #foreign #dll_import
|
||||
HeapFree :: proc(h: HANDLE, flags: u32, memory: rawptr) -> BOOL #foreign #dll_import
|
||||
GetProcessHeap :: proc() -> HANDLE #foreign #dll_import
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user