remove explicit init procs
This commit is contained in:
parent
7333b32418
commit
90590b964a
5 changed files with 41 additions and 25 deletions
|
|
@ -1,26 +1,30 @@
|
||||||
// @todo(judah): replace array_add
|
// @todo(judah): replace array_add
|
||||||
|
|
||||||
init :: inline (arr: *[..]$T, allocator: Allocator) {
|
|
||||||
arr.allocator = allocator;
|
|
||||||
}
|
|
||||||
|
|
||||||
append :: inline (arr: *[..]$T, value: T) -> *T {
|
append :: inline (arr: *[..]$T, value: T) -> *T {
|
||||||
|
mem.lazy_set_allocator(arr);
|
||||||
|
|
||||||
ptr := basic.array_add(arr,, allocator = arr.allocator);
|
ptr := basic.array_add(arr,, allocator = arr.allocator);
|
||||||
ptr.* = value;
|
ptr.* = value;
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
append :: inline (arr: *[..]$T, values: ..T) -> *T {
|
append :: inline (arr: *[..]$T, values: ..T) -> *T {
|
||||||
|
mem.lazy_set_allocator(arr);
|
||||||
|
|
||||||
count := arr.count;
|
count := arr.count;
|
||||||
basic.array_add(arr, ..values,, allocator = arr.allocator);
|
basic.array_add(arr, ..values,, allocator = arr.allocator);
|
||||||
return *arr.data[count];
|
return *arr.data[count];
|
||||||
}
|
}
|
||||||
|
|
||||||
append :: inline (arr: *[..]$T) -> *T {
|
append :: inline (arr: *[..]$T) -> *T {
|
||||||
|
mem.lazy_set_allocator(arr);
|
||||||
|
|
||||||
return basic.array_add(arr,, allocator = arr.allocator);
|
return basic.array_add(arr,, allocator = arr.allocator);
|
||||||
}
|
}
|
||||||
|
|
||||||
resize :: inline (arr: *[..]$T, new_count: int) {
|
resize :: inline (arr: *[..]$T, new_count: int) {
|
||||||
|
mem.lazy_set_allocator(arr);
|
||||||
|
|
||||||
if new_count <= arr.allocated return;
|
if new_count <= arr.allocated return;
|
||||||
basic.array_reserve(arr, new_count,, allocator = arr.allocator);
|
basic.array_reserve(arr, new_count,, allocator = arr.allocator);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,11 +10,6 @@ Stable_Array :: struct(T: Type, items_per_chunk := 32) {
|
||||||
Chunk :: Static_Array(items_per_chunk, T);
|
Chunk :: Static_Array(items_per_chunk, T);
|
||||||
}
|
}
|
||||||
|
|
||||||
init :: (a: *Stable_Array, allocator: Allocator) {
|
|
||||||
a.allocator = allocator;
|
|
||||||
a.chunks.allocator = allocator;
|
|
||||||
}
|
|
||||||
|
|
||||||
append :: (a: *Stable_Array) -> *a.T {
|
append :: (a: *Stable_Array) -> *a.T {
|
||||||
chunk := find_or_create_chunk(a, 1);
|
chunk := find_or_create_chunk(a, 1);
|
||||||
a.count += 1;
|
a.count += 1;
|
||||||
|
|
@ -119,19 +114,14 @@ find_or_create_chunk :: (a: *Stable_Array, amount: int) -> *a.Chunk {
|
||||||
}
|
}
|
||||||
|
|
||||||
create_chunk :: (a: *Stable_Array) -> *a.Chunk {
|
create_chunk :: (a: *Stable_Array) -> *a.Chunk {
|
||||||
inline try_lazy_init(a);
|
mem.lazy_set_allocator(a);
|
||||||
|
mem.lazy_set_allocator(*a.chunks);
|
||||||
|
|
||||||
chunk := mem.request_memory(a.Chunk,, allocator = a.allocator);
|
chunk := mem.request_memory(a.Chunk,, allocator = a.allocator);
|
||||||
append(*a.chunks, chunk);
|
append(*a.chunks, chunk);
|
||||||
return chunk;
|
return chunk;
|
||||||
}
|
}
|
||||||
|
|
||||||
try_lazy_init :: (a: *Stable_Array) {
|
|
||||||
if a.allocator.proc == null {
|
|
||||||
init(a, context.allocator);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// ----------------------------------------------------------
|
// ----------------------------------------------------------
|
||||||
// TESTS
|
// TESTS
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,8 @@ Buffer :: struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
append :: inline (buf: *Buffer, ptr: *void, count: int) {
|
append :: inline (buf: *Buffer, ptr: *void, count: int) {
|
||||||
|
inline meta.lazy_set_allocator(buf);
|
||||||
|
|
||||||
free_space := ensure_buffer_has_room(buf, count);
|
free_space := ensure_buffer_has_room(buf, count);
|
||||||
memcpy(free_space, ptr, count);
|
memcpy(free_space, ptr, count);
|
||||||
buf.count += count;
|
buf.count += count;
|
||||||
|
|
@ -26,6 +28,7 @@ reset :: inline (buf: *Buffer) {
|
||||||
#scope_file;
|
#scope_file;
|
||||||
|
|
||||||
array :: #import "jc/array";
|
array :: #import "jc/array";
|
||||||
|
meta :: #import "jc/meta";
|
||||||
|
|
||||||
ensure_buffer_has_room :: (buf: *Buffer, count: int) -> *u8 {
|
ensure_buffer_has_room :: (buf: *Buffer, count: int) -> *u8 {
|
||||||
ptr := buf.data.data + buf.count;
|
ptr := buf.data.data + buf.count;
|
||||||
|
|
|
||||||
|
|
@ -18,12 +18,6 @@ Kv :: struct(Key: Type, Value: Type) {
|
||||||
number_of_items_to_allocate_initially :: 16; // @note(judah): must be a power of two
|
number_of_items_to_allocate_initially :: 16; // @note(judah): must be a power of two
|
||||||
}
|
}
|
||||||
|
|
||||||
init :: (kv: *Kv, allocator: Allocator) {
|
|
||||||
kv.allocator = allocator;
|
|
||||||
kv.slots.allocator = allocator;
|
|
||||||
kv.free_slots.allocator = allocator;
|
|
||||||
}
|
|
||||||
|
|
||||||
get :: (kv: *Kv, key: kv.Key) -> kv.Value, bool {
|
get :: (kv: *Kv, key: kv.Key) -> kv.Value, bool {
|
||||||
slot, ok := find_slot(kv, get_hash(kv, key));
|
slot, ok := find_slot(kv, get_hash(kv, key));
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|
@ -124,9 +118,9 @@ mark_slot_for_reuse :: (kv: *Kv, index: int) {
|
||||||
}
|
}
|
||||||
|
|
||||||
try_lazy_init :: inline (kv: *Kv) {
|
try_lazy_init :: inline (kv: *Kv) {
|
||||||
if kv.allocator.proc == null {
|
mem.lazy_set_allocator(kv);
|
||||||
init(kv, context.allocator);
|
mem.lazy_set_allocator(*kv.slots);
|
||||||
}
|
mem.lazy_set_allocator(*kv.free_slots);
|
||||||
}
|
}
|
||||||
|
|
||||||
mem :: #import "jc/memory";
|
mem :: #import "jc/memory";
|
||||||
|
|
|
||||||
|
|
@ -147,6 +147,31 @@ release_memory :: inline (str: string) {
|
||||||
release_memory(str.data.(*void));
|
release_memory(str.data.(*void));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// @todo(judah): why can't we use $T/struct{ allocator: Allocator }?
|
||||||
|
lazy_set_allocator :: (thing: *$T, allocator := context.allocator) #modify {
|
||||||
|
info := T.(*Type_Info_Struct);
|
||||||
|
|
||||||
|
ok := false;
|
||||||
|
if info.type == .STRUCT ok = true;
|
||||||
|
|
||||||
|
if ok for info.members if it.name == "allocator" && it.type == Allocator.(*Type_Info) {
|
||||||
|
ok = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ok, "can only set allocator on struct with allocator field or dynamic array";
|
||||||
|
} #expand {
|
||||||
|
if thing.allocator.proc == null {
|
||||||
|
thing.allocator = allocator;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
lazy_set_allocator :: (array: *[..]$T, allocator := context.allocator) #expand {
|
||||||
|
if array.allocator.proc == null {
|
||||||
|
array.allocator = allocator;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#load "allocators.jai";
|
#load "allocators.jai";
|
||||||
|
|
||||||
#scope_file;
|
#scope_file;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue