From 5c2e82fc8bc10b89260e41ddd7cbc1b5ec4e07c6 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Wed, 19 Jul 2023 02:46:43 +0200 Subject: [PATCH] More use of temp allocator. --- lib/std/core/string.c3 | 7 +++++-- lib/std/encoding/csv.c3 | 4 ++-- lib/std/io/io_printf.c3 | 4 ++-- lib/std/io/io_stream.c3 | 6 ++++-- lib/std/io/os/ls.c3 | 9 +++++---- lib/std/io/os/temp_directory.c3 | 6 +++--- lib/std/io/path.c3 | 8 ++++---- 7 files changed, 25 insertions(+), 19 deletions(-) diff --git a/lib/std/core/string.c3 b/lib/std/core/string.c3 index 4b06e99d4..d063b83a6 100644 --- a/lib/std/core/string.c3 +++ b/lib/std/core/string.c3 @@ -68,9 +68,9 @@ fn String join(String[] s, String joiner, Allocator* using = mem::heap()) { total_size += str.len; } - @stack_mem(256; Allocator* mem) + @pool(using) { - DString res = dstring::new_with_capacity(total_size, .using = mem); + DString res = dstring::tnew_with_capacity(total_size); res.append(s[0]); foreach (String* &str : s[1..]) { @@ -416,6 +416,9 @@ fn String! from_wstring(WString wstring, Allocator* using = mem::heap()) return from_utf16(utf16, using); } +fn String! temp_from_wstring(WString wstring) => from_wstring(wstring) @inline; +fn String! temp_from_utf16(Char16[] utf16) => temp_from_utf16(utf16) @inline; + fn usz String.utf8_codepoints(s) { usz len = 0; diff --git a/lib/std/encoding/csv.c3 b/lib/std/encoding/csv.c3 index 95fce2dff..0ffc7364d 100644 --- a/lib/std/encoding/csv.c3 +++ b/lib/std/encoding/csv.c3 @@ -21,9 +21,9 @@ fn void CsvReader.init(&self, Stream stream, String separator = ",") fn String[]! CsvReader.read_row(self, Allocator* using = mem::heap()) { - @stack_mem(512; Allocator* mem) + @pool() { - return self.stream.readline(mem).split(self.separator, .using = using); + return self.stream.treadline().split(self.separator, .using = using); }; } diff --git a/lib/std/io/io_printf.c3 b/lib/std/io/io_printf.c3 index 063f48ccd..b7e68c800 100644 --- a/lib/std/io/io_printf.c3 +++ b/lib/std/io/io_printf.c3 @@ -135,9 +135,9 @@ macro bool! Formatter.print_with_function(&self, any arg) self.width = old_width; self.prec = old_prec; } - @stack_mem(512; Allocator* mem) + @pool() { - self.out_substr(arg.to_string(mem))!; + self.out_substr(arg.to_string(mem::temp()))!; return true; }; } diff --git a/lib/std/io/io_stream.c3 b/lib/std/io/io_stream.c3 index 0d8bd631d..5de4fa07a 100644 --- a/lib/std/io/io_stream.c3 +++ b/lib/std/io/io_stream.c3 @@ -111,6 +111,8 @@ fn usz! Stream.read_all(self, char[] buffer) @inline return n; } +fn String! Stream.treadline(self) => self.readline(mem::temp()) @inline; + fn String! Stream.readline(self, Allocator* using = mem::heap()) { ReadByteStreamFn func; @@ -118,9 +120,9 @@ fn String! Stream.readline(self, Allocator* using = mem::heap()) bool read = false; char val = func(self)!; if (val == '\n') return ""; - @stack_mem(256 + 64; Allocator* mem) + @pool(using) { - DString str = dstring::new_with_capacity(256, .using = mem); + DString str = dstring::tnew_with_capacity(256); if (val != '\r') str.append(val); while (1) { diff --git a/lib/std/io/os/ls.c3 b/lib/std/io/os/ls.c3 index e7d30309f..14aa85195 100644 --- a/lib/std/io/os/ls.c3 +++ b/lib/std/io/os/ls.c3 @@ -26,9 +26,10 @@ fn PathList! native_ls(Path dir, bool no_dirs, bool no_symlinks, String mask, Al { PathList list; list.init(.using = using); - @stack_mem(1024; Allocator* mem) + + @pool(using) { - WString result = dir.as_str().concat(`\*`).to_wstring(.using = mem)!!; + WString result = dir.as_str().tconcat(`\*`).to_temp_wstring()!!; Win32_WIN32_FIND_DATAW find_data; Win32_HANDLE find = win32::findFirstFileW(result, &find_data); if (find == win32::INVALID_HANDLE_VALUE) return IoError.CANNOT_READ_DIR?; @@ -36,9 +37,9 @@ fn PathList! native_ls(Path dir, bool no_dirs, bool no_symlinks, String mask, Al do { if (no_dirs && (find_data.dwFileAttributes & win32::FILE_ATTRIBUTE_DIRECTORY)) continue; - @stack_mem(480; Allocator* mem2) + @pool(using) { - String filename = string::from_wstring((WString)&find_data.cFileName, mem2)!; + String filename = string::temp_from_wstring((WString)&find_data.cFileName)!; if (filename == ".." || filename == ".") continue; list.append(path::new(filename, using)); }; diff --git a/lib/std/io/os/temp_directory.c3 b/lib/std/io/os/temp_directory.c3 index 023d55109..c2821aadf 100644 --- a/lib/std/io/os/temp_directory.c3 +++ b/lib/std/io/os/temp_directory.c3 @@ -12,13 +12,13 @@ fn Path! native_temp_directory(Allocator* using = mem::heap()) @if(!env::WIN32) fn Path! native_temp_directory(Allocator* using = mem::heap()) @if(env::WIN32) { - @stack_mem(256; Allocator* mem) + @pool(using) { Win32_DWORD len = win32::getTempPathW(0, null); if (!len) return IoError.GENERAL_ERROR?; - Char16[] buff = malloc(Char16, len + 1, .using = mem); + Char16[] buff = tmalloc(Char16, len + 1); if (!win32::getTempPathW(len, buff)) return IoError.GENERAL_ERROR?; - return path::new(string::from_utf16(buff[:len], .using = mem), using); + return path::new(string::temp_from_utf16(buff[:len]), using); }; } diff --git a/lib/std/io/path.c3 b/lib/std/io/path.c3 index e082e67df..0d7aa17a5 100644 --- a/lib/std/io/path.c3 +++ b/lib/std/io/path.c3 @@ -29,9 +29,9 @@ enum PathEnv fn Path! getcwd(Allocator* using = mem::heap()) { - @stack_mem(256; Allocator* mem) + @pool(using) { - return new(os::getcwd(mem), using); + return new(os::getcwd(mem::temp()), using); }; } @@ -113,9 +113,9 @@ fn Path! new(String path, Allocator* using = mem::heap(), PathEnv path_env = DEF fn Path! new_win32_wstring(WString path, Allocator* using = mem::heap()) { - @stack_mem(480; Allocator* mem) + @pool(using) { - return path::new(string::from_wstring(path, .using = mem)!, .using = using); + return path::new(string::temp_from_wstring(path)!, .using = using); }; }