random changes in the standard library
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 17 May 2022 02:43:36 +0000 (21:43 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 17 May 2022 02:43:36 +0000 (21:43 -0500)
core/alloc.onyx
core/alloc/arena.onyx
core/io/reader.onyx
core/io/writer.onyx
core/os/dir.onyx
core/os/file.onyx
core/runtime/info/helper.onyx
core/stdio.onyx
tests/struct_use_pointer_member

index 32c22d0a9d8217df69671af88466de2ee6102746..b4984f69bfae3d6aeb653455cb76ad04fd61eadf 100644 (file)
@@ -25,20 +25,24 @@ array_from_stack :: macro ($T: type_expr, size: u32) -> [] T {
     return (cast(^T) __stack_top)[0 .. size];
 }
 
-TEMPORARY_ALLOCATOR_SIZE :: 1 << 12; // 4Kb
+TEMPORARY_ALLOCATOR_SIZE :: 1 << 16; // 16Kb
 
 // The global heap allocator, set up upon program intialization.
 heap_allocator : Allocator;
 
 // The global temp allocator, set up upon program intialization.
 #local
-temp_state     : ring.RingState;
+temp_state     : arena.ArenaState;
 temp_allocator : Allocator;
 
 init :: () {
     heap.init();
 
-    temp_buffer := cast(^u8) raw_alloc(heap_allocator, TEMPORARY_ALLOCATOR_SIZE);
-    temp_state = ring.make(temp_buffer[0 .. TEMPORARY_ALLOCATOR_SIZE]);
-    temp_allocator = ring.make_allocator(^temp_state);
+    temp_state = arena.make(heap_allocator, TEMPORARY_ALLOCATOR_SIZE);
+    temp_allocator = as_allocator(^temp_state);
 }
+
+clear_temp_allocator :: () {
+    arena.clear(^temp_state);
+}
+
index 3428bb8c39bb5b029e2f33dde3bb59d131f2f153..fba77287cc63a5fe34d2cb161798a5c4da08dda8 100644 (file)
@@ -114,12 +114,23 @@ free :: (arena: ^ArenaState) {
     arena.size          = 0;
 }
 
+// Clears and frees every page, except for first page.
+clear :: (arena: ^ArenaState) {
+    walker := arena.first_arena.next;
+    while walker != null {
+        raw_free(arena.backing_allocator, walker);
+        walker = walker.next;
+    }
+
+    arena.size = sizeof rawptr;
+}
+
 auto :: #match {
-    macro (size := 32 * 1024) {
+    macro (size := 32 * 1024, $dest: Code = #(context.allocator)) {
         alloc :: package core.alloc
 
         arena := alloc.arena.make(alloc.heap_allocator, size);
-        context.allocator = alloc.arena.make_allocator(^arena);
+        (#unquote dest) = alloc.arena.make_allocator(^arena);
         defer alloc.arena.free(^arena);
     },
 
index fcd3abec93360d15ed5eea39273120c1c595a0f5..83b105865288673166843a53fe58bd7e083b1d52 100644 (file)
@@ -18,6 +18,7 @@ Reader :: struct {
 
     done : bool; // If an .EOF was reached.
 
+    is_empty :: reader_empty;
     read_all :: read_all;
     read_byte :: read_byte;
     unread_byte :: unread_byte;
@@ -389,7 +390,7 @@ read_word :: (use reader: ^Reader, numeric_allowed := false, allocator := contex
     return out;
 }
 
-read_until :: (use reader: ^Reader, until: u8, skip: u32 = 0, allocator := context.allocator, consume_end := false, inplace := false, shift_buffer := true) -> str {
+read_until :: (use reader: ^Reader, until: u8, skip: u32 = 0, allocator := context.allocator, consume_end := false, inplace := false) -> str {
     //
     // Reading in place is special. It does not make a special allocation for the data,
     // instead just return a pointer to the internal data.
index 364f5d302975090b22a3d267887bfe1816a05a1c..ed466b7e00bdaeae911426c9587d5b13dea50c7c 100644 (file)
@@ -87,6 +87,10 @@ write_range :: (use writer: ^Writer, r: range, sep := " ") {
 }
 
 write_format :: (use writer: ^Writer, format: str, va: ..any) {
+    write_format_va(writer, format, ~~ va);
+}
+
+write_format_va :: (use writer: ^Writer, format: str, va: [] any) {
     flush :: (writer, to_output) => {
         write_str(writer, to_output);
         return true;
index 7728b980e83108d069bf2e1722924109ac159620..863fce0e8aea8576985f1bb01b5646049dc74c95 100644 (file)
@@ -39,3 +39,5 @@ dir_close :: (dir: Directory) {
 dir_read :: (dir: Directory, out_entry: ^DirectoryEntry) -> bool {
     return fs.__dir_read(dir, out_entry);
 }
+
+dir_exists :: fs.__file_exists
index 3705ba1f771fb76d7b0ce12ff95832b1ebb9b5dd..0e8367ce0a082d62350d6eacc5c774643867b6fe 100644 (file)
@@ -35,9 +35,7 @@ File :: struct {
     }
 }
 
-file_exists :: (path: str) -> bool {
-    return fs.__file_exists(path);
-}
+file_exists :: fs.__file_exists
 
 get_contents_from_file :: (file: ^File) -> str {
     size := cast(u32) io.stream_size(file);
index e0aed03a787096703fd0a493fa1a8c1b3cdeaba3..a5e70d01239a197b11583f3849625f9594ea6cfb 100644 (file)
@@ -188,20 +188,32 @@ size_of :: (t: type_expr) -> u32 {
     return 0;
 }
 
-offset_of :: (T: type_expr, member: str) -> u32 {
+offset_of :: (T: type_expr, member_name: str) -> u32 {
     info := get_type_info(T);
     if info == null         do return 0;
     if info.kind != .Struct do return 0;
 
     struct_info := cast(^Type_Info_Struct) info;
     for ^m: struct_info.members {
-        if m.name == member do return m.offset;
+        if m.name == member_name do return m.offset;
     }
 
     // Should this return something else if the member was not found?
     return 0;
 }
 
+get_struct_member :: (S: type_expr, member_name: str) -> ^Type_Info_Struct.Member {
+    info := cast(^Type_Info_Struct) get_type_info(S);
+    if info == null do return null;
+    if info.kind != .Struct do return null;
+
+    for^ info.members {
+        if it.name == member_name do return it;
+    }
+
+    return null;
+}
+
 get_tags_for_member :: (S: type_expr, member_name: str) -> [] any {
     ti := get_type_info(S);
     if ti.kind != .Struct do return .[];
index f859943e9260db9e970886e07d1111d87e479091..e9358c4d3cde146e458be3a2d5530dfd01b55ba3 100644 (file)
@@ -50,7 +50,7 @@ printf :: (format: str, va: ..any) {
         }
 
         buffer: [1024] u8;
-        print(conv.format_va(buffer, format, va, .{null, flush}));
+        (package runtime).__output_error(conv.format_va(buffer, format, va, .{null, flush}));
     }
 }
 
@@ -61,6 +61,13 @@ aprintf :: (format: str, va: ..any) -> str {
     return string.alloc_copy(out);
 }
 
+// Print to a TEMPORARY dynamically allocated string.
+tprintf :: (format: str, va: ..any) -> str {
+    buffer: [8196] u8;
+    out := conv.format_va(buffer, format, va);
+    return string.alloc_copy(out, allocator=context.temp_allocator);
+}
+
 byte_dump :: (ptr: rawptr, byte_count: u32, bytes_per_line := 8) {
     temp: [3] u8;
 
@@ -133,6 +140,7 @@ __flush_stdio :: () {
 
 #local stdin_vtable := io.Stream_Vtable.{
     read = (_: ^io.Stream, buffer: [] u8) -> (io.Error, u32) {
+        __flush_stdio();
         bytes_read := runtime.__read_from_input(buffer);
         if bytes_read == 0 do return .ReadPending, 0;
         if bytes_read <  0 do return .EOF, 0;
@@ -141,6 +149,7 @@ __flush_stdio :: () {
     },
 
     read_byte = (_: ^io.Stream) -> (io.Error, u8) {
+        __flush_stdio();
         buf: [1] u8;
         bytes_read := runtime.__read_from_input(buf);
         if bytes_read <= 0 do return .EOF, 0;
@@ -149,4 +158,4 @@ __flush_stdio :: () {
     }
 }
 
-stdin: io.Stream;
\ No newline at end of file
+stdin: io.Stream;
index e8005b2ec5fd331cc4e3242f2317386664f13aaf..260ae42896c6be3d1b7dc8d32eb7bbd464bd9474 100644 (file)
@@ -1,2 +1,2 @@
 Hello, I am Billy!
-Go away!! func[51]
+Go away!! func[52]