Skip to content

Commit

Permalink
merge libc_impl.v
Browse files Browse the repository at this point in the history
  • Loading branch information
JalonSolov committed Nov 1, 2023
1 parent 289b29b commit a7e7027
Showing 1 changed file with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import dlmalloc
__global global_allocator dlmalloc.Dlmalloc

[unsafe]
pub fn memcpy(dest &C.void, src &C.void, n usize) &C.void {
pub fn memcpy(dest voidptr, src voidptr, n usize) voidptr {
dest_ := unsafe { &u8(dest) }
src_ := unsafe { &u8(src) }
unsafe {
Expand All @@ -18,28 +18,28 @@ pub fn memcpy(dest &C.void, src &C.void, n usize) &C.void {

[export: 'malloc']
[unsafe]
fn __malloc(n usize) &C.void {
fn __malloc(n usize) voidptr {
return unsafe { global_allocator.malloc(n) }
}

[unsafe]
fn strlen(_s &C.void) usize {
fn strlen(_s voidptr) usize {
s := unsafe { &u8(_s) }
mut i := 0
for ; unsafe { s[i] } != 0; i++ {}
return usize(i)
}

[unsafe]
fn realloc(old_area &C.void, new_size usize) &C.void {
fn realloc(old_area voidptr, new_size usize) voidptr {
if old_area == 0 {
return unsafe { malloc(int(new_size)) }
}
if new_size == usize(0) {
unsafe { free(old_area) }
return 0
}
old_size := unsafe { *(&u64(old_area - sizeof(u64))) }
old_size := unsafe { *(&u64(&u64(old_area) - sizeof(u64))) }
if u64(new_size) <= old_size {
return unsafe { old_area }
} else {
Expand All @@ -51,7 +51,7 @@ fn realloc(old_area &C.void, new_size usize) &C.void {
}

[unsafe]
fn memset(s &C.void, c int, n usize) &C.void {
fn memset(s voidptr, c int, n usize) voidptr {
mut s_ := unsafe { &char(s) }
for i in 0 .. int(n) {
unsafe {
Expand All @@ -62,7 +62,7 @@ fn memset(s &C.void, c int, n usize) &C.void {
}

[unsafe]
fn memmove(dest &C.void, src &C.void, n usize) &C.void {
fn memmove(dest voidptr, src voidptr, n usize) voidptr {
dest_ := unsafe { &u8(dest) }
src_ := unsafe { &u8(src) }
mut temp_buf := unsafe { malloc(int(n)) }
Expand All @@ -83,7 +83,7 @@ fn memmove(dest &C.void, src &C.void, n usize) &C.void {

[export: 'calloc']
[unsafe]
fn __calloc(nmemb usize, size usize) &C.void {
fn __calloc(nmemb usize, size usize) voidptr {
new_area := unsafe { malloc(int(nmemb) * int(size)) }
unsafe { memset(new_area, 0, nmemb * size) }
return new_area
Expand All @@ -95,7 +95,7 @@ fn getchar() int {
return int(x)
}

fn memcmp(a &C.void, b &C.void, n usize) int {
fn memcmp(a voidptr, b voidptr, n usize) int {
a_ := unsafe { &u8(a) }
b_ := unsafe { &u8(b) }
for i in 0 .. int(n) {
Expand All @@ -110,7 +110,7 @@ fn memcmp(a &C.void, b &C.void, n usize) int {

[export: 'free']
[unsafe]
fn __free(ptr &C.void) {
fn __free(ptr voidptr) {
unsafe {
global_allocator.free_(ptr)
}
Expand Down

0 comments on commit a7e7027

Please sign in to comment.