added builtin 'delete' to complement 'make'
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 7 Apr 2022 20:58:42 +0000 (15:58 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 7 Apr 2022 20:58:42 +0000 (15:58 -0500)
core/builtin.onyx
core/container/array.onyx
core/container/map.onyx
core/container/set.onyx
core/memory.onyx
core/net/tcp.onyx
tests/atomics.onyx
tests/baked_parameters.onyx

index b48e89aaa099371a449b2b8d9dc9425b2413c74f..aa889e9a530f9674a7d13d2d7fb247509a3d37bb 100644 (file)
@@ -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;
index 99a82ed2642a8a344a07055459b7de3220907be8..959a5624107e7abdcfe64f0328ceb820b1aea1f5 100644 (file)
@@ -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 {
index d6dd2a5023f2d9319eff6b8930af2db17cc2ab60..25df3bd2d24504a3be53b8902bfb1c895c43a373 100644 (file)
@@ -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;
 
index b3a57d7a824b0c984f514ee16c54f145462a1c8b..a565baccbe5bd76f40b1a3f24c7b2f95cfd3d9c4 100644 (file)
@@ -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;
 
index 88433372ecce25712d18c0159cc36a11e11a9052..5ea845b199b14242a7743cb9fc7765ccd8927150 100644 (file)
@@ -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 {
index 375bdbcd386fcccaa7e3dec200ccfe7cc770fca8..e845ea18e03faf52d98a0b465f0ffbadfd68dd57 100644 (file)
@@ -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;
 }
index 892913cac5bfcd4ed4ba8c8a39f5e6236fac764c..7c3b34b2bf3658b4dc9df2cd1d2ea7a22a367130 100644 (file)
@@ -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 {
index 77bd3a63bff7e8ef5bc96e3b8240e933a1f3ce1d..41ff1e838d2299a71b92bdfbab4212a08ad93537 100644 (file)
@@ -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);