From 46a6cb4c2e123656d3fa6ddc7ecc3e710d8af05b Mon Sep 17 00:00:00 2001 From: Jose Mendoza <56417208+StunxFS@users.noreply.github.com> Date: Thu, 30 Nov 2023 20:53:31 +0000 Subject: [PATCH] refact(core): remove 'internal_*' functions --- lib/core/src/Vector.c.ri | 12 ++++++------ lib/core/src/int.ri | 4 ++-- lib/core/src/lib.ri | 28 ---------------------------- lib/core/src/mem.c.ri | 24 ++++++++++++------------ lib/core/src/rune.c.ri | 2 +- lib/core/src/string.c.ri | 10 +++++----- lib/core/src/uint.ri | 4 ++-- lib/std/src/fs/File.ri | 2 +- lib/std/src/fs/Path.ri | 12 ++++++------ rivetc/src/codegen/__init__.py | 6 +++--- 10 files changed, 38 insertions(+), 66 deletions(-) diff --git a/lib/core/src/Vector.c.ri b/lib/core/src/Vector.c.ri index 3c3a2f0fe..f03b584d1 100644 --- a/lib/core/src/Vector.c.ri +++ b/lib/core/src/Vector.c.ri @@ -12,17 +12,17 @@ struct Vector { #[unsafe; inline] func new(elem_size: uint, cap: uint) -> Self { - return Self(internal_zeroed(cap * elem_size), elem_size, 0, cap); + return Self(mem_zeroed(cap * elem_size), elem_size, 0, cap); } #[unsafe; inline] func new_with_len(elem_size: uint, len: uint, cap: uint) -> Self { - return Self(internal_zeroed(cap * elem_size), elem_size, len, cap); + return Self(mem_zeroed(cap * elem_size), elem_size, len, cap); } #[unsafe] func from_array(arr: rawptr, elem_size: uint, len: uint) -> Self { - vec := Self(internal_zeroed(len * elem_size), elem_size, len, len); + vec := Self(mem_zeroed(len * elem_size), elem_size, len, len); mem_copy(vec.ptr, arr, len * elem_size); return vec; } @@ -94,7 +94,7 @@ struct Vector { old_ptr := self.ptr; new_size := self.len - size; new_cap: uint := if new_size == 0 { 1 } else { new_size }; - self.ptr = mem_zeroed(new_cap * self.elem_size) catch @unreachable(); + self.ptr = mem_zeroed(new_cap * self.elem_size); mem_copy(self.ptr, old_ptr, i * self.elem_size); unsafe { mem_copy( @@ -148,7 +148,7 @@ struct Vector { cap *= 2; } new_size := cap * self.elem_size; - new_ptr := internal_alloc(new_size); + new_ptr := mem_alloc(new_size); mem_copy(new_ptr, self.ptr, self.len * self.elem_size); self.ptr = new_ptr; self.cap = cap; @@ -181,7 +181,7 @@ struct Vector { if size == 0 { size = 1; } - vec := Self(internal_zeroed(size), self.elem_size, self.len, self.cap); + vec := Self(mem_zeroed(size), self.elem_size, self.len, self.cap); mem_copy(vec.ptr, self.ptr, size); return vec; } diff --git a/lib/core/src/int.ri b/lib/core/src/int.ri index 54903e90a..d4df25c3a 100644 --- a/lib/core/src/int.ri +++ b/lib/core/src/int.ri @@ -54,7 +54,7 @@ extend int32 < Stringable { } unsafe { mut index := @as(uint, max); - buf := @as([&]mut uint8, internal_alloc(index + 1)); + buf := @as([&]mut uint8, mem_alloc(index + 1)); buf[index] = 0; index -= 1; @@ -114,7 +114,7 @@ extend int64 < Stringable { unsafe { max := 20; mut index := @as(uint, max); - buf := @as([&]mut uint8, internal_alloc(index + 1)); + buf := @as([&]mut uint8, mem_alloc(index + 1)); buf[index] = 0; index -= 1; diff --git a/lib/core/src/lib.ri b/lib/core/src/lib.ri index a3b5b54e3..bfa15c83d 100644 --- a/lib/core/src/lib.ri +++ b/lib/core/src/lib.ri @@ -17,31 +17,3 @@ func runtime_error(s: string, args: ...Stringable) -> never { bt_print(2); process_exit(100); } - -#[inline] -func internal_alloc(size: uint) -> rawptr { - return mem_alloc(size) catch { - runtime_error("cannot allocate memory" #if _DEBUG_ " (size: {})", size #endif) - }; -} - -#[inline] -func internal_zeroed(size: uint) -> rawptr { - return mem_zeroed(size) catch { - runtime_error("cannot allocate zeroed memory" #if _DEBUG_ " (size: {})", size #endif) - }; -} - -#[inline] -func internal_dup(src: rawptr, sz: uint) -> rawptr { - return mem_dup(src, sz) catch { - runtime_error("cannot duplicate memory" #if _DEBUG_ " (size: {})", sz #endif) - }; -} - -#[inline] -func internal_resize(ptr: ?rawptr, sz: uint) -> rawptr { - return mem_resize(ptr, sz) catch { - runtime_error("cannot resize memory" #if _DEBUG_ " (size: {})", sz #endif) - }; -} diff --git a/lib/core/src/mem.c.ri b/lib/core/src/mem.c.ri index b3d229115..1218d0f6b 100644 --- a/lib/core/src/mem.c.ri +++ b/lib/core/src/mem.c.ri @@ -9,39 +9,39 @@ import c/libc; /// /// NOTE: Unlike the `mem.zeroed` function, `mem.alloc` will /// not zero the memory block. -pub func mem_alloc(size: uint) -> !rawptr { +pub func mem_alloc(size: uint) -> rawptr { if new := unsafe { libc.malloc(size) } { return new; } - throw OutOfMemoryError("insufficient memory is available"); + runtime_error("cannot allocate memory" #if _DEBUG_ " (size: {})", size #endif); } -/// Allocates dynamically a zeroed `n` bytes block of memory on the heap. +/// Allocates dynamically a zeroed `size` bytes block of memory on the heap. /// Returns a pointer to the memory address of the allocated space. -pub func mem_zeroed(n: uint) -> !rawptr { - if ptr := unsafe { libc.calloc(1, n) } { +pub func mem_zeroed(size: uint) -> rawptr { + if ptr := unsafe { libc.calloc(1, size) } { return ptr; } - throw OutOfMemoryError("insufficient memory is available"); + runtime_error("cannot allocate zeroed memory" #if _DEBUG_ " (size: {})", size #endif); } /// Resizes the memory block `ptr` with `sz` bytes. /// /// NOTE: The `ptr` must be a pointer to an existing memory block previously /// allocated with `mem.alloc` or `mem.zeroed`. -pub func mem_resize(ptr: ?rawptr, sz: uint) -> !rawptr { +pub func mem_resize(ptr: ?rawptr, sz: uint) -> rawptr { if sz == 0 { if safe_ptr := ptr { unsafe { mem_dealloc(safe_ptr); } } - throw InvalidArgumentError("argument `sz` is 0"); + runtime_error("argument `sz` is 0"); } if new := unsafe { libc.realloc(ptr, sz) } { return new; } - throw OutOfMemoryError("insufficient memory is available"); + runtime_error("cannot resize memory" #if _DEBUG_ " (size: {})", sz #endif); } /// Fills the first `n` bytes of the memory area pointed to by `s`, with the @@ -80,11 +80,11 @@ pub func mem_move(dest: rawptr, src: rawptr, sz: uint) { /// Allocates dynamically a `sz` bytes block of memory on the heap, then copies /// the contents of `src` into the allocated space and returns a pointer to /// the newly allocated space. -pub func mem_dup(src: rawptr, sz: uint) -> !rawptr { +pub func mem_dup(src: rawptr, sz: uint) -> rawptr { if sz == 0 { - return mem_zeroed(1)!; + return mem_zeroed(1); } - new := mem_alloc(sz)!; + new := mem_alloc(sz); mem_copy(new, src, sz); return new; } diff --git a/lib/core/src/rune.c.ri b/lib/core/src/rune.c.ri index a551eef41..7db6581af 100644 --- a/lib/core/src/rune.c.ri +++ b/lib/core/src/rune.c.ri @@ -62,7 +62,7 @@ extend rune < Stringable { pub func to_string(&self) -> string { len := self.len_utf8(); - res := unsafe { @as([&]mut uint8, internal_alloc(len + 1)) }; + res := unsafe { @as([&]mut uint8, mem_alloc(len + 1)) }; unsafe { _ = utf32_decode_to_buffer(self.*, res); return string.from_raw_with_len(res, len); diff --git a/lib/core/src/string.c.ri b/lib/core/src/string.c.ri index d74b6b65d..151452dc7 100644 --- a/lib/core/src/string.c.ri +++ b/lib/core/src/string.c.ri @@ -38,7 +38,7 @@ pub struct string < Stringable, Hashable, Throwable { } pub func from_byte(byte: uint8) -> Self { - res := unsafe { @as([&]mut uint8, internal_alloc(2)) }; + res := unsafe { @as([&]mut uint8, mem_alloc(2)) }; unsafe { res[0] = byte; res[1] = 0; @@ -52,7 +52,7 @@ pub struct string < Stringable, Hashable, Throwable { if bytes.len == 0 { return emptyString; } - res := unsafe { @as([&]mut uint8, internal_alloc(bytes.len + 1)) }; + res := unsafe { @as([&]mut uint8, mem_alloc(bytes.len + 1)) }; unsafe { mem_copy(res, &bytes[0], bytes.len); res[bytes.len] = 0; @@ -75,7 +75,7 @@ pub struct string < Stringable, Hashable, Throwable { self } else { len := self.len * count; - res := unsafe { @as([&]mut uint8, internal_alloc(len)) }; + res := unsafe { @as([&]mut uint8, mem_alloc(len)) }; unsafe { mut i: uint := 0; while i < count : i += 1 { @@ -468,7 +468,7 @@ pub struct string < Stringable, Hashable, Throwable { return self; } new_len := self.len + idxs.len * (with.len - rep.len); - b := unsafe { @as([&]mut uint8, internal_alloc(new_len)) }; + b := unsafe { @as([&]mut uint8, mem_alloc(new_len)) }; (mut b_i: uint, mut s_idx: uint) := (0, 0); unsafe { for rep_pos in idxs { @@ -810,7 +810,7 @@ pub struct string < Stringable, Hashable, Throwable { if self.len == 0 { return emptyString; } - res := unsafe { @as([&]mut uint8, internal_alloc(self.len + 1)) }; + res := unsafe { @as([&]mut uint8, mem_alloc(self.len + 1)) }; unsafe { mem_copy(res, self.ptr, self.len); res[self.len] = 0; diff --git a/lib/core/src/uint.ri b/lib/core/src/uint.ri index 658860473..323ec22c0 100644 --- a/lib/core/src/uint.ri +++ b/lib/core/src/uint.ri @@ -126,7 +126,7 @@ extend uint32 < Stringable { mut d: uint32 := 0; mut index := max; - buf := @as([&]mut uint8, internal_alloc(max + 1)); + buf := @as([&]mut uint8, mem_alloc(max + 1)); buf[index] = 0; index -= 1; @@ -168,7 +168,7 @@ extend uint64 < Stringable { max := 20; mut index := @as(uint, max); - buf := @as([&]mut uint8, internal_alloc(index + 1)); + buf := @as([&]mut uint8, mem_alloc(index + 1)); buf[index] = 0; index -= 1; diff --git a/lib/std/src/fs/File.ri b/lib/std/src/fs/File.ri index 9554c5395..8388f058c 100644 --- a/lib/std/src/fs/File.ri +++ b/lib/std/src/fs/File.ri @@ -91,7 +91,7 @@ pub struct File { ); } - res := @as([&]mut uint8, mem.alloc(@as(uint, allocate) + 1)!); + res := @as([&]mut uint8, mem.alloc(@as(uint, allocate) + 1)); nelements := libc.fread(res, 1, @as(uint, allocate), self.f); if self.is_eof() and libc.ferror(self.f) != 0 { diff --git a/lib/std/src/fs/Path.ri b/lib/std/src/fs/Path.ri index d1974efc8..a8225ccc1 100644 --- a/lib/std/src/fs/Path.ri +++ b/lib/std/src/fs/Path.ri @@ -174,10 +174,10 @@ pub struct Path { } mut result_index: uint := 0; result := if have_abs { - unsafe { @as([&]mut uint8, mem.alloc(max_size)!) } + unsafe { @as([&]mut uint8, mem.alloc(max_size)) } } else { cwd := core.process_get_cwd()!; - tmp := unsafe { @as([&]mut uint8, mem.alloc(max_size + cwd.len + 1)!) }; + tmp := unsafe { @as([&]mut uint8, mem.alloc(max_size + cwd.len + 1)) }; mem.copy(tmp, cwd.ptr, cwd.len); result_index += cwd.len; tmp @@ -226,7 +226,7 @@ pub struct Path { } return unsafe { string.from_raw_with_len( - @as([&]uint8, mem.resize(result, result_index)!), result_index + @as([&]uint8, mem.resize(result, result_index)), result_index ) }; } @@ -252,7 +252,7 @@ pub struct Path { up_count += 1; } up_index_end := up_count * 3; - result := unsafe { @as([&]mut uint8, mem.alloc(up_index_end + to_rest.len)!) }; + result := unsafe { @as([&]mut uint8, mem.alloc(up_index_end + to_rest.len)) }; defer(error) unsafe { mem.dealloc(result); } @@ -269,7 +269,7 @@ pub struct Path { } if to_rest.len == 0 { // shave off the trailing slash - buf := unsafe { @as([&]mut uint8, mem.resize(result, result_index - 1)!) }; + buf := unsafe { @as([&]mut uint8, mem.resize(result, result_index - 1)) }; unsafe { buf[result_index - 1] = 0; } @@ -324,7 +324,7 @@ pub struct Path { } sum + 1 // + 1 = NLL character }; - buf := unsafe { @as([&]mut uint8, mem.alloc(total_len)!) }; + buf := unsafe { @as([&]mut uint8, mem.alloc(total_len)) }; unsafe { buf[total_len - 1] = 0; defer(error) mem.dealloc(buf); diff --git a/rivetc/src/codegen/__init__.py b/rivetc/src/codegen/__init__.py index 7bdc46360..7c07a0b6c 100644 --- a/rivetc/src/codegen/__init__.py +++ b/rivetc/src/codegen/__init__.py @@ -2758,7 +2758,7 @@ def boxed_instance(self, name, id, custom_name = None): to_fn = self.init_string_lits_fn if custom_name else self.cur_fn inst = ir.Inst( ir.InstKind.Call, - [ir.Name("_R4core14internal_allocF"), + [ir.Name("_R4core9mem_allocF"), ir.Name(f"sizeof({name})")] ) if custom_name: @@ -2790,7 +2790,7 @@ def trait_value(self, value, value_typ, trait_typ): value = ir.Inst(ir.InstKind.GetRef, [value]) value = value if is_ptr else ir.Inst( ir.InstKind.Call, [ - ir.Name("_R4core12internal_dupF"), value, + ir.Name("_R4core7mem_dupF"), value, ir.IntLit(ir.UINT_T, str(size)) ] ) @@ -2841,7 +2841,7 @@ def boxed_enum_value( else: value = ir.Inst( ir.InstKind.Call, [ - ir.Name("_R4core12internal_dupF"), + ir.Name("_R4core7mem_dupF"), ir.Inst(ir.InstKind.GetRef, [arg0]), ir.IntLit(uint_t, str(size)) ]