Skip to content

Commit

Permalink
refact(core): remove 'internal_*' functions
Browse files Browse the repository at this point in the history
  • Loading branch information
StunxFS committed Nov 30, 2023
1 parent 56b08b9 commit 46a6cb4
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 66 deletions.
12 changes: 6 additions & 6 deletions lib/core/src/Vector.c.ri
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/core/src/int.ri
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down
28 changes: 0 additions & 28 deletions lib/core/src/lib.ri
Original file line number Diff line number Diff line change
Expand Up @@ -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)
};
}
24 changes: 12 additions & 12 deletions lib/core/src/mem.c.ri
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/core/src/rune.c.ri
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions lib/core/src/string.c.ri
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions lib/core/src/uint.ri
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion lib/std/src/fs/File.ri
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions lib/std/src/fs/Path.ri
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)
};
}
Expand All @@ -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);
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions rivetc/src/codegen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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))
]
)
Expand Down Expand Up @@ -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))
]
Expand Down

0 comments on commit 46a6cb4

Please sign in to comment.