Skip to content

Commit

Permalink
feat(core): add a mem module for memory management
Browse files Browse the repository at this point in the history
Rename `Box` to `Boxedptr` and add more useful functions to it,
in addition to renaming some existing functions.
  • Loading branch information
StunxFS committed Jan 1, 2024
1 parent ddf92b9 commit aea8a35
Show file tree
Hide file tree
Showing 27 changed files with 331 additions and 253 deletions.
36 changes: 0 additions & 36 deletions lib/core/src/Box.ri

This file was deleted.

38 changes: 20 additions & 18 deletions lib/core/src/DynArray.ri
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// source code is governed by an MIT license that can be found in the LICENSE
// file.

import ./mem;

#[boxed]
struct DynArray {
mut ptr: rawptr;
Expand All @@ -11,19 +13,19 @@ struct DynArray {

#[unsafe; inline]
func new(elem_size: uint, cap: uint) -> Self {
return Self(mem_zeroed(cap * elem_size), elem_size, 0, cap);
return Self(mem.raw_zeroed(cap * elem_size), elem_size, 0, cap);
}

#[unsafe; inline]
func new_with_len(elem_size: uint, len: uint, cap: uint) -> Self {
cap_ := if cap < len { len } else { cap };
return Self(mem_zeroed(cap_ * elem_size), elem_size, len, cap);
return Self(mem.raw_zeroed(cap_ * elem_size), elem_size, len, cap);
}

#[unsafe; inline]
func new_with_init(init: rawptr, elem_size: uint, len: uint, cap: uint) -> Self {
cap_ := if cap < len { len } else { cap };
dyn_array := Self(mem_alloc(cap_ * elem_size), elem_size, len, cap);
dyn_array := Self(mem.raw_alloc(cap_ * elem_size), elem_size, len, cap);
mut i: uint := 0;
while i < len : i += 1 {
unsafe {
Expand All @@ -35,8 +37,8 @@ struct DynArray {

#[unsafe]
func from_array(arr: rawptr, elem_size: uint, len: uint) -> Self {
dyn_array := Self(mem_alloc(len * elem_size), elem_size, len, len);
mem_copy(dyn_array.ptr, arr, len * elem_size);
dyn_array := Self(mem.raw_alloc(len * elem_size), elem_size, len, len);
mem.copy(dyn_array.ptr, arr, len * elem_size);
return dyn_array;
}

Expand All @@ -60,7 +62,7 @@ struct DynArray {
#[unsafe; inline]
func raw_set(self, idx: uint, val: rawptr) {
unsafe {
mem_copy(
mem.copy(
@ptr_add(@as([&]mut uint8, self.ptr), self.elem_size * idx),
val, self.elem_size
);
Expand All @@ -72,7 +74,7 @@ struct DynArray {
runtime_error("dynamic array index out of range (index: {}, len: {})", idx, self.len);
}
unsafe {
mem_copy(
mem.copy(
@ptr_add(@as([&]mut uint8, self.ptr), self.elem_size * idx),
val, self.elem_size
);
Expand All @@ -84,7 +86,7 @@ struct DynArray {
self.reserve(self.len + 1);
}
unsafe {
mem_copy(@ptr_add(self.ptr, self.elem_size * self.len), val, self.elem_size);
mem.copy(@ptr_add(self.ptr, self.elem_size * self.len), val, self.elem_size);
}
self.len += 1;
}
Expand Down Expand Up @@ -117,16 +119,16 @@ struct DynArray {
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);
mem_copy(self.ptr, old_ptr, i * self.elem_size);
self.ptr = mem.raw_zeroed(new_cap * self.elem_size);
mem.copy(self.ptr, old_ptr, i * self.elem_size);
unsafe {
mem_copy(
mem.copy(
@ptr_add(self.ptr, i * self.elem_size),
@ptr_add(old_ptr, (i + size) * self.elem_size),
(self.len - i - size) * self.elem_size
);
if no_slices {
mem_dealloc(old_ptr);
mem.raw_dealloc(old_ptr);
}
}
self.len = new_size;
Expand Down Expand Up @@ -154,7 +156,7 @@ struct DynArray {
if self.len != rhs.len {
return false;
}
return mem_cmp(self.ptr, rhs.ptr, self.len) == 0;
return mem.cmp(self.ptr, rhs.ptr, self.len) == 0;
}

#[inline]
Expand All @@ -171,8 +173,8 @@ struct DynArray {
cap *= 2;
}
new_size := cap * self.elem_size;
new_ptr := mem_alloc(new_size);
mem_copy(new_ptr, self.ptr, self.len * self.elem_size);
new_ptr := mem.raw_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 @@ -204,14 +206,14 @@ struct DynArray {
if size == 0 {
size = 1;
}
dyn_array := Self(mem_zeroed(size), self.elem_size, self.len, self.cap);
mem_copy(dyn_array.ptr, self.ptr, size);
dyn_array := Self(mem.raw_zeroed(size), self.elem_size, self.len, self.cap);
mem.copy(dyn_array.ptr, self.ptr, size);
return dyn_array;
}

func __destroy__(self) {
unsafe {
mem_dealloc(self.ptr);
mem.raw_dealloc(self.ptr);
}
}
}
6 changes: 4 additions & 2 deletions lib/core/src/Slice.ri
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// source code is governed by an MIT license that can be found in the LICENSE
// file.

import ./mem;

struct Slice {
ptr: rawptr;
elem_size: uint;
Expand Down Expand Up @@ -29,7 +31,7 @@ struct Slice {
runtime_error("slice index out of range (index: {}, len: {})", idx, self.len);
}
unsafe {
mem_copy(
mem.copy(
@ptr_add(@as([&]mut uint8, self.ptr), self.elem_size * idx),
val, self.elem_size
);
Expand Down Expand Up @@ -71,7 +73,7 @@ struct Slice {
if self.len != rhs.len {
return false;
}
return mem_cmp(self.ptr, rhs.ptr, self.len) == 0;
return mem.cmp(self.ptr, rhs.ptr, self.len) == 0;
}

#[inline]
Expand Down
3 changes: 2 additions & 1 deletion lib/core/src/StringBuilder.c.ri
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// file.

import c/libc;
import ./mem;

#[boxed]
pub struct StringBuilder < Stringable {
Expand Down Expand Up @@ -42,7 +43,7 @@ pub struct StringBuilder < Stringable {
#[unsafe]
pub func write_raw_with_len(mut self, s: [&]uint8, len: uint) {
self.inner.reserve(self.inner.len + len);
mem_copy(
mem.copy(
unsafe { @ptr_add(self.inner.ptr, self.inner.elem_size * self.inner.len) },
s, len
);
Expand Down
3 changes: 2 additions & 1 deletion lib/core/src/array.c.ri
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
// file.

import c/libc;
import ./mem;

func array_init_set(arr: rawptr, elem_size: uint, len: uint, value: rawptr) {
mut i: uint := 0;
while i < len : i += 1 {
unsafe {
mem_copy(@ptr_add(@as([&]mut uint8, arr), elem_size * i), value, elem_size);
mem.copy(@ptr_add(@as([&]mut uint8, arr), elem_size * i), value, elem_size);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/core/src/backtrace.c.ri
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ $if !_RELEASE_ { // skip backtrace in release mode
#![compile_c_source("../thirdparty/libbacktrace/backtrace.c")]

import c;
import ./mem;

struct BacktraceState;

Expand Down Expand Up @@ -40,7 +41,7 @@ $if !_RELEASE_ { // skip backtrace in release mode
) -> int32 {
unsafe {
if safe_fn_ptr := fn_ptr {
if mem_cmp(safe_fn_ptr, c"_R4core4mainF", 13) == 0 {
if mem.cmp(safe_fn_ptr, c"_R4core4mainF", 13) == 0 {
return -1; // stop backtracing
}
}
Expand Down
10 changes: 6 additions & 4 deletions lib/core/src/int.ri
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// source code is governed by an MIT license that can be found in the LICENSE
// file.

import ./mem;

var digit_pairs := "0010203040506070809001112131415161718191021222324252627"
"28292031323334353637383930414243444546474849405152535455565758595061626364656667"
"68696071727374757677787970818283848586878889809192939495969798999";
Expand Down Expand Up @@ -54,7 +56,7 @@ extend int32 < Stringable {
}
unsafe {
mut index := @as(uint, max);
buf := @as([&]mut uint8, mem_alloc(index + 1));
buf := @as([&]mut uint8, mem.raw_alloc(index + 1));
buf[index] = 0;
index -= 1;

Expand Down Expand Up @@ -82,7 +84,7 @@ extend int32 < Stringable {
}

diff := @as(uint, max) - index;
mem_move(buf, @ptr_add(buf, index), diff + 1);
mem.move(buf, @ptr_add(buf, index), diff + 1);
return string.from_raw_with_len(buf, diff);
}
}
Expand Down Expand Up @@ -114,7 +116,7 @@ extend int64 < Stringable {
unsafe {
max := 20;
mut index := @as(uint, max);
buf := @as([&]mut uint8, mem_alloc(index + 1));
buf := @as([&]mut uint8, mem.raw_alloc(index + 1));
buf[index] = 0;
index -= 1;

Expand Down Expand Up @@ -142,7 +144,7 @@ extend int64 < Stringable {
}

diff := @as(uint, max) - index;
mem_move(buf, @ptr_add(buf, index), diff + 1);
mem.move(buf, @ptr_add(buf, index), diff + 1);
return string.from_raw_with_len(buf, diff);
}
}
Expand Down
Loading

0 comments on commit aea8a35

Please sign in to comment.