From d11ed66143660b9998de1b844ddf915ec6bf0d0f Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Mon, 16 May 2022 21:43:36 -0500 Subject: [PATCH] random changes in the standard library --- core/alloc.onyx | 14 +++++++++----- core/alloc/arena.onyx | 15 +++++++++++++-- core/io/reader.onyx | 3 ++- core/io/writer.onyx | 4 ++++ core/os/dir.onyx | 2 ++ core/os/file.onyx | 4 +--- core/runtime/info/helper.onyx | 16 ++++++++++++++-- core/stdio.onyx | 13 +++++++++++-- tests/struct_use_pointer_member | 2 +- 9 files changed, 57 insertions(+), 16 deletions(-) diff --git a/core/alloc.onyx b/core/alloc.onyx index 32c22d0a..b4984f69 100644 --- a/core/alloc.onyx +++ b/core/alloc.onyx @@ -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); +} + diff --git a/core/alloc/arena.onyx b/core/alloc/arena.onyx index 3428bb8c..fba77287 100644 --- a/core/alloc/arena.onyx +++ b/core/alloc/arena.onyx @@ -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); }, diff --git a/core/io/reader.onyx b/core/io/reader.onyx index fcd3abec..83b10586 100644 --- a/core/io/reader.onyx +++ b/core/io/reader.onyx @@ -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. diff --git a/core/io/writer.onyx b/core/io/writer.onyx index 364f5d30..ed466b7e 100644 --- a/core/io/writer.onyx +++ b/core/io/writer.onyx @@ -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; diff --git a/core/os/dir.onyx b/core/os/dir.onyx index 7728b980..863fce0e 100644 --- a/core/os/dir.onyx +++ b/core/os/dir.onyx @@ -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 diff --git a/core/os/file.onyx b/core/os/file.onyx index 3705ba1f..0e8367ce 100644 --- a/core/os/file.onyx +++ b/core/os/file.onyx @@ -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); diff --git a/core/runtime/info/helper.onyx b/core/runtime/info/helper.onyx index e0aed03a..a5e70d01 100644 --- a/core/runtime/info/helper.onyx +++ b/core/runtime/info/helper.onyx @@ -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 .[]; diff --git a/core/stdio.onyx b/core/stdio.onyx index f859943e..e9358c4d 100644 --- a/core/stdio.onyx +++ b/core/stdio.onyx @@ -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; diff --git a/tests/struct_use_pointer_member b/tests/struct_use_pointer_member index e8005b2e..260ae428 100644 --- a/tests/struct_use_pointer_member +++ b/tests/struct_use_pointer_member @@ -1,2 +1,2 @@ Hello, I am Billy! -Go away!! func[51] +Go away!! func[52] -- 2.25.1