Start using arena for allocations
This commit is contained in:
+14
-21
@@ -8,6 +8,7 @@ package base
|
|||||||
|
|
||||||
import "base:runtime"
|
import "base:runtime"
|
||||||
import "core:crypto/hash"
|
import "core:crypto/hash"
|
||||||
|
import "core:crypto/sha2"
|
||||||
import "core:fmt"
|
import "core:fmt"
|
||||||
import "core:mem"
|
import "core:mem"
|
||||||
import "core:os"
|
import "core:os"
|
||||||
@@ -21,6 +22,9 @@ MAGIC_STRING :: 0x72646E73 // "rdns" -> "sndr" in little endian
|
|||||||
HOT_RELOAD_VERSION :: 1
|
HOT_RELOAD_VERSION :: 1
|
||||||
APP_VERSION_SIZE :: 4 + time.MIN_YYYY_DATE_LEN + 8 + 6 // example: `dev-2026-09-23-a4084b3-debug`
|
APP_VERSION_SIZE :: 4 + time.MIN_YYYY_DATE_LEN + 8 + 6 // example: `dev-2026-09-23-a4084b3-debug`
|
||||||
|
|
||||||
|
HASH_ALGORITHM :: hash.Algorithm.SHA256
|
||||||
|
DIGEST_SIZE :: sha2.DIGEST_SIZE_256
|
||||||
|
|
||||||
// -----------------------------------------
|
// -----------------------------------------
|
||||||
// Types
|
// Types
|
||||||
|
|
||||||
@@ -42,7 +46,7 @@ Region_Header :: struct #packed #all_or_none {
|
|||||||
Memory_Region :: struct {
|
Memory_Region :: struct {
|
||||||
ptr_location: rawptr,
|
ptr_location: rawptr,
|
||||||
// TODO: How do we do hash compare without having to hash current memory every frame?
|
// TODO: How do we do hash compare without having to hash current memory every frame?
|
||||||
digest: []u8, // hash of this region in last snapshot
|
digest: [DIGEST_SIZE]u8, // hash of this region in last snapshot
|
||||||
}
|
}
|
||||||
|
|
||||||
@(private)
|
@(private)
|
||||||
@@ -110,9 +114,6 @@ hot_reload_destroy :: proc() {
|
|||||||
delete(hr.snapshot_path, hr.allocator)
|
delete(hr.snapshot_path, hr.allocator)
|
||||||
|
|
||||||
for key, region in hr.region_headers do delete(key, hr.allocator)
|
for key, region in hr.region_headers do delete(key, hr.allocator)
|
||||||
for key, region in hr.regions {
|
|
||||||
if region.digest != nil do delete(region.digest, hr.allocator)
|
|
||||||
}
|
|
||||||
|
|
||||||
delete(hr.region_headers)
|
delete(hr.region_headers)
|
||||||
delete(hr.regions)
|
delete(hr.regions)
|
||||||
@@ -140,6 +141,7 @@ register_region :: proc(
|
|||||||
}
|
}
|
||||||
hr.regions[hash_cpy] = Memory_Region {
|
hr.regions[hash_cpy] = Memory_Region {
|
||||||
ptr_location = ptr_location,
|
ptr_location = ptr_location,
|
||||||
|
digest = {},
|
||||||
}
|
}
|
||||||
|
|
||||||
hr.snapshot_header.region_count += 1
|
hr.snapshot_header.region_count += 1
|
||||||
@@ -209,13 +211,8 @@ read_snapshot :: proc() -> (fresh: bool, err: os.Error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create digest and store in registered regions
|
// Create digest and store in registered regions
|
||||||
digest := hash.hash_bytes(
|
|
||||||
hash.Algorithm.SHA256,
|
|
||||||
data_bytes,
|
|
||||||
hr.allocator,
|
|
||||||
)
|
|
||||||
region := &hr.regions[key]
|
region := &hr.regions[key]
|
||||||
region.digest = digest
|
hash.hash_bytes_to_buffer(HASH_ALGORITHM, data_bytes, region.digest[:])
|
||||||
|
|
||||||
// Copy snapshot memory to the memory region
|
// Copy snapshot memory to the memory region
|
||||||
dest := mem.byte_slice(
|
dest := mem.byte_slice(
|
||||||
@@ -276,24 +273,20 @@ write_snapshot :: proc() -> os.Error {
|
|||||||
|
|
||||||
// Write region data (if bytes differ)
|
// Write region data (if bytes differ)
|
||||||
data_bytes := mem.byte_slice(region.ptr_location, header.data_size)
|
data_bytes := mem.byte_slice(region.ptr_location, header.data_size)
|
||||||
digest := hash.hash_bytes(
|
@(static) new_digest: [DIGEST_SIZE]u8
|
||||||
hash.Algorithm.SHA256,
|
digest := hash.hash_bytes_to_buffer(
|
||||||
|
HASH_ALGORITHM,
|
||||||
data_bytes,
|
data_bytes,
|
||||||
hr.allocator,
|
new_digest[:],
|
||||||
)
|
)
|
||||||
if hr.write_full_snapshot {
|
if hr.write_full_snapshot {
|
||||||
delete(region.digest, hr.allocator)
|
region.digest = new_digest
|
||||||
region.digest = digest
|
|
||||||
os.write_at(snapshot, data_bytes, offset) or_return
|
os.write_at(snapshot, data_bytes, offset) or_return
|
||||||
}
|
}
|
||||||
else if mem.compare(digest, region.digest) != 0 {
|
else if mem.compare(digest, region.digest[:]) != 0 {
|
||||||
delete(region.digest, hr.allocator)
|
region.digest = new_digest
|
||||||
region.digest = digest
|
|
||||||
write_diff_at(snapshot, data_bytes, offset) or_return
|
write_diff_at(snapshot, data_bytes, offset) or_return
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
delete(digest, hr.allocator)
|
|
||||||
}
|
|
||||||
offset += cast(i64)header.data_size
|
offset += cast(i64)header.data_size
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+17
-6
@@ -17,12 +17,22 @@ import "sindri:platform"
|
|||||||
VERSION :: #config(VERSION, "dev")
|
VERSION :: #config(VERSION, "dev")
|
||||||
|
|
||||||
// -----------------------------------------
|
// -----------------------------------------
|
||||||
|
// Variables
|
||||||
|
|
||||||
|
arena: base.Arena
|
||||||
|
|
||||||
|
// -----------------------------------------
|
||||||
|
// Public functions
|
||||||
|
|
||||||
main :: proc() {
|
main :: proc() {
|
||||||
fmt.println("hello world!", VERSION)
|
fmt.println("hello world!", VERSION)
|
||||||
|
|
||||||
|
arena = base.arena_init_static()
|
||||||
|
defer base.arena_destroy(&arena)
|
||||||
|
allocator := base.arena_allocator(&arena)
|
||||||
|
|
||||||
// Hot reload development functionality
|
// Hot reload development functionality
|
||||||
base.hot_reload_init(VERSION)
|
base.hot_reload_init(VERSION, allocator)
|
||||||
defer base.hot_reload_destroy()
|
defer base.hot_reload_destroy()
|
||||||
|
|
||||||
// Load game API & settings
|
// Load game API & settings
|
||||||
@@ -31,7 +41,7 @@ main :: proc() {
|
|||||||
settings := api.settings()
|
settings := api.settings()
|
||||||
|
|
||||||
// Event systems
|
// Event systems
|
||||||
base.event_bus_init()
|
base.event_bus_init(allocator)
|
||||||
defer base.event_bus_destroy()
|
defer base.event_bus_destroy()
|
||||||
|
|
||||||
base.event_subscribe(base.Key_Press_Event, test)
|
base.event_subscribe(base.Key_Press_Event, test)
|
||||||
@@ -46,14 +56,15 @@ main :: proc() {
|
|||||||
defer platform.os_destroy()
|
defer platform.os_destroy()
|
||||||
|
|
||||||
// Initialize GPU resources
|
// Initialize GPU resources
|
||||||
platform.instance_init(settings)
|
platform.instance_init(settings, allocator)
|
||||||
defer platform.instance_destroy()
|
defer platform.instance_destroy()
|
||||||
load_test_model()
|
load_test_model()
|
||||||
|
|
||||||
things := core.Things{}
|
things := new(core.Things, allocator)
|
||||||
camera := core.add(&things)
|
defer free(things, allocator)
|
||||||
|
camera := core.add(things)
|
||||||
|
|
||||||
base.register_region("things", &things, size_of(core.Things))
|
base.register_region("things", things, size_of(core.Things))
|
||||||
|
|
||||||
api.register_memory()
|
api.register_memory()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user