From 044e3b4b18fe86cc67833252b53b30a5cc6a9011 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Thu, 7 Apr 2022 15:58:42 -0500 Subject: [PATCH] added builtin 'delete' to complement 'make' --- core/builtin.onyx | 6 ++++++ core/container/array.onyx | 24 ++++++++++++++++++------ core/container/map.onyx | 30 ++++++++++++++++++------------ core/container/set.onyx | 6 +++--- core/memory.onyx | 8 ++++---- core/net/tcp.onyx | 4 ++-- tests/atomics.onyx | 6 +++--- tests/baked_parameters.onyx | 2 +- 8 files changed, 55 insertions(+), 31 deletions(-) diff --git a/core/builtin.onyx b/core/builtin.onyx index b48e89aa..aa889e9a 100644 --- a/core/builtin.onyx +++ b/core/builtin.onyx @@ -193,6 +193,12 @@ __make_overload :: #match { }, } +delete :: #match { + #precedence 1000 macro (x: ^$T, allocator := context.allocator) -> ^T { + raw_free(allocator, x); + } +} + Iterator :: struct (Iter_Type: type_expr) { data: rawptr; diff --git a/core/container/array.onyx b/core/container/array.onyx index 99a82ed2..959a5624 100644 --- a/core/container/array.onyx +++ b/core/container/array.onyx @@ -43,13 +43,25 @@ free :: (arr: ^[..] $T) { arr.data = null; } -copy :: (arr: ^[..] $T, allocator := context.allocator) -> [..] T { - new_arr : [..] T; - init(^new_arr, arr.count, allocator); - new_arr.count = arr.count; +#match (package builtin).delete macro (x: ^[..] $T) { + (package core.array).free(x); +} - for i: 0 .. arr.count do new_arr.data[i] = arr.data[i]; - return new_arr; +copy :: #match { + (arr: ^[..] $T, allocator := context.allocator) -> [..] T { + new_arr : [..] T; + init(^new_arr, arr.count, allocator); + new_arr.count = arr.count; + + for i: 0 .. arr.count do new_arr.data[i] = arr.data[i]; + return new_arr; + }, + + (arr: [] $T, allocator := context.allocator) -> [] T { + new_arr := make([] T, arr.count); + for i: 0 .. arr.count do new_arr.data[i] = arr.data[i]; + return new_arr; + } } copy_range :: (arr: ^[..] $T, r: range, allocator := context.allocator) -> [..] T { diff --git a/core/container/map.onyx b/core/container/map.onyx index d6dd2a50..25df3bd2 100644 --- a/core/container/map.onyx +++ b/core/container/map.onyx @@ -39,15 +39,19 @@ Map :: struct (Key_Type: type_expr, Value_Type: type_expr) where ValidKey(Key_Ty value : V; } - init :: init - has :: has - get :: get - get_ptr :: get_ptr - put :: put - delete :: delete - update :: update - clear :: clear - empty :: empty + // + // These need to have aliases because some of them like + // 'delete', collide with the global 'delete', which + // causes it to map to the wrong function. + init :: (package core.map).init + has :: (package core.map).has + get :: (package core.map).get + get_ptr :: (package core.map).get_ptr + put :: (package core.map).put + delete :: (package core.map).delete + update :: (package core.map).update + clear :: (package core.map).clear + empty :: (package core.map).empty } make :: ($Key: type_expr, $Value: type_expr, default := __zero_value(Value)) -> Map(Key, Value) { @@ -63,7 +67,7 @@ init :: (use map: ^Map($K, $V), default := __zero_value(V)) { default_value = default; memory.alloc_slice(^hashes, 8, allocator=allocator); - memory.fill_slice(hashes, -1); + array.fill(hashes, -1); array.init(^entries, allocator=allocator); } @@ -73,6 +77,8 @@ free :: (use map: ^Map) { if map.entries.data != null do array.free(^entries); } +#match (package builtin).delete (package core.map).free + put :: (use map: ^Map, key: map.Key_Type, value: map.Value_Type) { lr := lookup(map, key); @@ -199,8 +205,8 @@ format_map :: (output: ^conv.Format_Output, format: ^conv.Format, x: ^Map($K, $V rehash :: (use map: ^Map, new_size: i32) { memory.free_slice(^hashes, allocator); - memory.alloc_slice(^hashes, new_size, allocator); - memory.fill_slice(hashes, -1); + hashes = make([] u32, new_size, allocator=allocator); + array.fill(hashes, -1); for ^entry: entries do entry.next = -1; diff --git a/core/container/set.onyx b/core/container/set.onyx index b3a57d7a..a565bacc 100644 --- a/core/container/set.onyx +++ b/core/container/set.onyx @@ -49,7 +49,7 @@ init :: (set: ^Set($T), default := __zero_value(T), allocator := context.allocat set.default_value = default; memory.alloc_slice(^set.hashes, 8, allocator=allocator); - memory.fill_slice(set.hashes, -1); + array.fill(set.hashes, -1); array.init(^set.entries, 4, allocator=allocator); } @@ -180,8 +180,8 @@ iterator :: (set: ^Set($T)) -> Iterator(T) { rehash :: (use set: ^Set, new_size: i32) { memory.free_slice(^hashes, allocator); - memory.alloc_slice(^hashes, new_size, allocator); - memory.fill_slice(hashes, -1); + hashes = make([] u32, new_size, allocator=allocator); + array.fill(hashes, -1); for ^entry: entries do entry.next = -1; diff --git a/core/memory.onyx b/core/memory.onyx index 88433372..5ea845b1 100644 --- a/core/memory.onyx +++ b/core/memory.onyx @@ -41,6 +41,10 @@ free_slice :: (sl: ^[] $T, allocator := context.allocator) { sl.count = 0; } +#match (package builtin).delete macro (x: ^[] $T) { + (package core.memory).free_slice(x); +} + copy_slice :: (sl: [] $T, allocator := context.allocator) -> [] T { data := raw_alloc(allocator, sl.count * sizeof T); copy(data, sl.data, sl.count * sizeof T); @@ -58,10 +62,6 @@ resize_slice :: (sl: [] $T, new_size: i32, allocator := context.allocator) -> [] return new_slice; } -fill_slice :: macro (sl: [] $T, v: T) { - for ^it: sl do *it = v; -} - align :: #match { (size: ^u64, align: u64) { if *size % align != 0 { diff --git a/core/net/tcp.onyx b/core/net/tcp.onyx index 375bdbcd..e845ea18 100644 --- a/core/net/tcp.onyx +++ b/core/net/tcp.onyx @@ -139,8 +139,8 @@ tcp_server_make :: (max_clients := 32, allocator := context.allocator) -> ^TCP_S server.client_count = 0; server.client_allocator = allocator; - memory.alloc_slice(^server.clients, max_clients, allocator=allocator); - memory.fill_slice(server.clients, null); + server.clients = make([] ^TCP_Server.Client, max_clients, allocator=allocator); + array.fill(server.clients, null); return server; } diff --git a/tests/atomics.onyx b/tests/atomics.onyx index 892913ca..7c3b34b2 100644 --- a/tests/atomics.onyx +++ b/tests/atomics.onyx @@ -28,7 +28,7 @@ print_from_other_thread :: (sd) => { } // Using the thread-local variable - memory.alloc_slice(^partial_arr, 5); + partial_arr = make([] i32, 5); for i: 5 do partial_arr[i] = i * i; // Not printing on threads since the test case looks for EXACT string matches @@ -42,8 +42,8 @@ main :: (args) => { println(test); sd: Shared_Data; - sd.arr = memory.make_slice(i32, 1000); - memory.fill_slice(sd.arr, 0); + sd.arr = make([] i32, 1000); + array.fill(sd.arr, 0); threads : [4] thread.Thread; for ^t: threads { diff --git a/tests/baked_parameters.onyx b/tests/baked_parameters.onyx index 77bd3a63..41ff1e83 100644 --- a/tests/baked_parameters.onyx +++ b/tests/baked_parameters.onyx @@ -32,7 +32,7 @@ main :: (args: [] cstr) { // This is so much cleaner than the alternative. ages := map.make(str, u32, default=0); - defer map.free(^ages); + defer delete(^ages); map.put(^ages, "Dwight", 32); map.put(^ages, "Jim", 25); -- 2.25.1