2021-04-14 07:50:50 +02:00
|
|
|
module builtin
|
|
|
|
|
2022-01-06 13:10:37 +01:00
|
|
|
import dlmalloc
|
|
|
|
|
|
|
|
__global global_allocator dlmalloc.Dlmalloc
|
|
|
|
|
2021-04-14 07:50:50 +02:00
|
|
|
[unsafe]
|
2021-09-08 12:09:32 +02:00
|
|
|
pub fn memcpy(dest &C.void, src &C.void, n usize) &C.void {
|
2021-04-17 01:38:41 +02:00
|
|
|
dest_ := unsafe { &byte(dest) }
|
|
|
|
src_ := unsafe { &byte(src) }
|
2021-04-14 07:50:50 +02:00
|
|
|
unsafe {
|
2021-04-17 01:38:41 +02:00
|
|
|
for i in 0 .. int(n) {
|
|
|
|
dest_[i] = src_[i]
|
2021-04-14 07:50:50 +02:00
|
|
|
}
|
|
|
|
}
|
2021-05-07 14:58:48 +02:00
|
|
|
return unsafe { dest }
|
2021-04-14 07:50:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
[export: 'malloc']
|
|
|
|
[unsafe]
|
2021-09-08 12:09:32 +02:00
|
|
|
fn __malloc(n usize) &C.void {
|
2022-01-06 13:10:37 +01:00
|
|
|
return unsafe { global_allocator.malloc(n) }
|
2021-04-14 07:50:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
[unsafe]
|
2021-09-08 12:09:32 +02:00
|
|
|
fn strlen(_s &C.void) usize {
|
2021-04-17 01:38:41 +02:00
|
|
|
s := unsafe { &byte(_s) }
|
2021-04-14 07:50:50 +02:00
|
|
|
mut i := 0
|
|
|
|
for ; unsafe { s[i] } != 0; i++ {}
|
2021-09-08 12:09:32 +02:00
|
|
|
return usize(i)
|
2021-04-14 07:50:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
[unsafe]
|
2021-09-08 12:09:32 +02:00
|
|
|
fn realloc(old_area &C.void, new_size usize) &C.void {
|
2021-04-14 07:50:50 +02:00
|
|
|
if old_area == 0 {
|
2021-04-17 01:38:41 +02:00
|
|
|
return unsafe { malloc(int(new_size)) }
|
2021-04-14 07:50:50 +02:00
|
|
|
}
|
2021-09-08 12:09:32 +02:00
|
|
|
if new_size == usize(0) {
|
2021-04-14 07:50:50 +02:00
|
|
|
unsafe { free(old_area) }
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
old_size := unsafe { *(&u64(old_area - sizeof(u64))) }
|
2021-04-17 01:38:41 +02:00
|
|
|
if u64(new_size) <= old_size {
|
2021-05-07 14:58:48 +02:00
|
|
|
return unsafe { old_area }
|
2021-04-14 07:50:50 +02:00
|
|
|
} else {
|
2021-04-17 01:38:41 +02:00
|
|
|
new_area := unsafe { malloc(int(new_size)) }
|
2021-09-08 12:09:32 +02:00
|
|
|
unsafe { memmove(new_area, old_area, usize(old_size)) }
|
2021-04-14 07:50:50 +02:00
|
|
|
unsafe { free(old_area) }
|
|
|
|
return new_area
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[unsafe]
|
2021-09-08 12:09:32 +02:00
|
|
|
fn memset(s &C.void, c int, n usize) &C.void {
|
2021-04-17 01:38:41 +02:00
|
|
|
mut s_ := unsafe { &char(s) }
|
2021-04-14 07:50:50 +02:00
|
|
|
for i in 0 .. int(n) {
|
|
|
|
unsafe {
|
2021-04-17 01:38:41 +02:00
|
|
|
s_[i] = char(c)
|
2021-04-14 07:50:50 +02:00
|
|
|
}
|
|
|
|
}
|
2021-05-07 14:58:48 +02:00
|
|
|
return unsafe { s }
|
2021-04-14 07:50:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
[unsafe]
|
2021-09-08 12:09:32 +02:00
|
|
|
fn memmove(dest &C.void, src &C.void, n usize) &C.void {
|
2021-04-17 01:38:41 +02:00
|
|
|
dest_ := unsafe { &byte(dest) }
|
|
|
|
src_ := unsafe { &byte(src) }
|
|
|
|
mut temp_buf := unsafe { malloc(int(n)) }
|
2021-04-14 07:50:50 +02:00
|
|
|
for i in 0 .. int(n) {
|
|
|
|
unsafe {
|
2021-04-17 01:38:41 +02:00
|
|
|
temp_buf[i] = src_[i]
|
2021-04-14 07:50:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i in 0 .. int(n) {
|
|
|
|
unsafe {
|
2021-04-17 01:38:41 +02:00
|
|
|
dest_[i] = temp_buf[i]
|
2021-04-14 07:50:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
unsafe { free(temp_buf) }
|
2021-05-07 14:58:48 +02:00
|
|
|
return unsafe { dest }
|
2021-04-14 07:50:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
[export: 'calloc']
|
|
|
|
[unsafe]
|
2021-09-08 12:09:32 +02:00
|
|
|
fn __calloc(nmemb usize, size usize) &C.void {
|
2021-04-17 01:38:41 +02:00
|
|
|
new_area := unsafe { malloc(int(nmemb) * int(size)) }
|
2021-04-14 07:50:50 +02:00
|
|
|
unsafe { memset(new_area, 0, nmemb * size) }
|
|
|
|
return new_area
|
|
|
|
}
|
|
|
|
|
|
|
|
fn getchar() int {
|
|
|
|
x := byte(0)
|
2021-04-17 01:38:41 +02:00
|
|
|
sys_read(0, &x, 1)
|
2021-04-14 07:50:50 +02:00
|
|
|
return int(x)
|
|
|
|
}
|
|
|
|
|
2021-09-08 12:09:32 +02:00
|
|
|
fn memcmp(a &C.void, b &C.void, n usize) int {
|
2021-04-17 01:38:41 +02:00
|
|
|
a_ := unsafe { &byte(a) }
|
|
|
|
b_ := unsafe { &byte(b) }
|
2021-04-14 07:50:50 +02:00
|
|
|
for i in 0 .. int(n) {
|
2021-04-17 01:38:41 +02:00
|
|
|
if unsafe { a_[i] != b_[i] } {
|
2021-04-14 07:50:50 +02:00
|
|
|
unsafe {
|
2021-04-17 01:38:41 +02:00
|
|
|
return a_[i] - b_[i]
|
2021-04-14 07:50:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
[export: 'free']
|
|
|
|
[unsafe]
|
2021-04-17 01:38:41 +02:00
|
|
|
fn __free(ptr &C.void) {
|
2022-01-06 13:10:37 +01:00
|
|
|
/*
|
2021-04-14 07:50:50 +02:00
|
|
|
err := mm_free(ptr)
|
|
|
|
if err != .enoerror {
|
|
|
|
eprintln('free error:')
|
|
|
|
panic(err)
|
2022-01-06 13:10:37 +01:00
|
|
|
}*/
|
|
|
|
unsafe {
|
|
|
|
global_allocator.free_(ptr)
|
2021-04-14 07:50:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn vsprintf(str &char, format &char, ap &byte) int {
|
|
|
|
panic('vsprintf(): string interpolation is not supported in `-freestanding`')
|
|
|
|
}
|
|
|
|
|
2021-09-08 12:09:32 +02:00
|
|
|
fn vsnprintf(str &char, size usize, format &char, ap &byte) int {
|
2021-04-14 07:50:50 +02:00
|
|
|
panic('vsnprintf(): string interpolation is not supported in `-freestanding`')
|
|
|
|
}
|
|
|
|
|
|
|
|
// not really needed
|
|
|
|
fn bare_read(buf &byte, count u64) (i64, Errno) {
|
|
|
|
return sys_read(0, buf, count)
|
|
|
|
}
|
|
|
|
|
2022-01-06 13:10:37 +01:00
|
|
|
pub fn bare_print(buf &byte, len u64) {
|
2021-04-14 07:50:50 +02:00
|
|
|
sys_write(1, buf, len)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bare_eprint(buf &byte, len u64) {
|
|
|
|
sys_write(2, buf, len)
|
|
|
|
}
|
2021-04-17 01:38:41 +02:00
|
|
|
|
2021-06-23 12:18:17 +02:00
|
|
|
pub fn write(fd i64, buf &byte, count u64) i64 {
|
|
|
|
x, _ := sys_write(fd, buf, count)
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
2021-07-04 19:24:19 +02:00
|
|
|
[noreturn]
|
2021-04-17 01:38:41 +02:00
|
|
|
fn bare_panic(msg string) {
|
|
|
|
println('V panic' + msg)
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bare_backtrace() string {
|
|
|
|
return 'backtraces are not available with `-freestanding`'
|
|
|
|
}
|
|
|
|
|
|
|
|
[export: 'exit']
|
2021-07-04 19:24:19 +02:00
|
|
|
[noreturn]
|
2021-04-17 01:38:41 +02:00
|
|
|
fn __exit(code int) {
|
|
|
|
sys_exit(code)
|
|
|
|
}
|
2021-08-12 20:46:38 +02:00
|
|
|
|
|
|
|
[export: 'qsort']
|
2021-09-08 12:09:32 +02:00
|
|
|
fn __qsort(base voidptr, nmemb usize, size usize, sort_cb FnSortCB) {
|
2021-08-12 20:46:38 +02:00
|
|
|
panic('qsort() is not yet implemented in `-freestanding`')
|
|
|
|
}
|
2022-01-06 13:10:37 +01:00
|
|
|
|
|
|
|
fn init_global_allocator() {
|
|
|
|
global_allocator = dlmalloc.new(get_linux_allocator())
|
|
|
|
}
|