builtin: document all functions in builtin.c.v (#7387)

pull/7398/head^2
Larpon 2020-12-18 18:31:00 +01:00 committed by GitHub
parent a9aee1ce97
commit c32547f382
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 38 additions and 16 deletions

View File

@ -1,13 +1,15 @@
module builtin module builtin
type FnExitCb = fn () type FnExitCb = fn ()
fn C.atexit(f FnExitCb) int fn C.atexit(f FnExitCb) int
// exit terminates execution immediately and returns exit `code` to the shell.
pub fn exit(code int) { pub fn exit(code int) {
C.exit(code) C.exit(code)
} }
// panic_debug - private function that V uses for panics, -cg/-g is passed // panic_debug private function that V uses for panics, -cg/-g is passed
// recent versions of tcc print nicer backtraces automatically // recent versions of tcc print nicer backtraces automatically
// NB: the duplication here is because tcc_backtrace should be called directly // NB: the duplication here is because tcc_backtrace should be called directly
// inside the panic functions. // inside the panic functions.
@ -97,17 +99,20 @@ pub fn eprint(s string) {
C.fflush(C.stderr) C.fflush(C.stderr)
} }
// print prints a message to stdout // print prints a message to stdout. Unlike `println` stdout is not automatically flushed.
// A call to `flush()` will flush the output buffer to stdout.
pub fn print(s string) { pub fn print(s string) {
C.write(1, s.str, s.len) C.write(1, s.str, s.len)
} }
//#include "@VROOT/vlib/darwin/darwin.m" /*
//fn C.nsstring2(s string) voidptr #include "@VROOT/vlib/darwin/darwin.m"
//fn C.NSLog(x voidptr) fn C.nsstring2(s string) voidptr
//#include <asl.h> fn C.NSLog(x voidptr)
// fn C.asl_log(voidptr, voidptr, int, charptr) #include <asl.h>
fn C.asl_log(voidptr, voidptr, int, charptr)
*/
// println prints a message with a line end, to stdout. stdout is flushed.
pub fn println(s string) { pub fn println(s string) {
$if windows { $if windows {
print(s) print(s)
@ -132,6 +137,9 @@ pub fn println(s string) {
} }
} }
// malloc dynamically allocates a `n` bytes block of memory on the heap.
// malloc returns a `byteptr` pointing to the memory address of the allocated space.
// unlike the `calloc` family of functions - malloc will not zero the memory block.
[unsafe] [unsafe]
pub fn malloc(n int) byteptr { pub fn malloc(n int) byteptr {
if n <= 0 { if n <= 0 {
@ -167,9 +175,12 @@ TODO
*/ */
} }
//#include <malloc/malloc.h> /*
//fn malloc_size(b byteptr) int #include <malloc/malloc.h>
fn malloc_size(b byteptr) int
*/
// v_realloc resizes the memory block `b` with `n` bytes.
// The `b byteptr` must be a pointer to an existing memory block previously allocated with `malloc`, `v_calloc` or `vcalloc`.
[unsafe] [unsafe]
pub fn v_realloc(b byteptr, n int) byteptr { pub fn v_realloc(b byteptr, n int) byteptr {
$if prealloc { $if prealloc {
@ -188,11 +199,16 @@ pub fn v_realloc(b byteptr, n int) byteptr {
} }
} }
// v_calloc dynamically allocates a zeroed `n` bytes block of memory on the heap.
// v_calloc returns a `byteptr` pointing to the memory address of the allocated space.
[unsafe] [unsafe]
pub fn v_calloc(n int) byteptr { pub fn v_calloc(n int) byteptr {
return C.calloc(1, n) return C.calloc(1, n)
} }
// vcalloc dynamically allocates a zeroed `n` bytes block of memory on the heap.
// vcalloc returns a `byteptr` pointing to the memory address of the allocated space.
// Unlike `v_calloc` vcalloc checks for negative values given in `n`.
[unsafe] [unsafe]
pub fn vcalloc(n int) byteptr { pub fn vcalloc(n int) byteptr {
if n < 0 { if n < 0 {
@ -203,6 +219,7 @@ pub fn vcalloc(n int) byteptr {
return C.calloc(1, n) return C.calloc(1, n)
} }
// free allows for manually freeing memory allocated at the address `ptr`.
[unsafe] [unsafe]
pub fn free(ptr voidptr) { pub fn free(ptr voidptr) {
$if prealloc { $if prealloc {
@ -211,6 +228,9 @@ pub fn free(ptr voidptr) {
C.free(ptr) C.free(ptr)
} }
// memdup dynamically allocates a `sz` bytes block of memory on the heap
// memdup then copies the contents of `src` into the allocated space and
// returns a pointer to the newly allocated space.
pub fn memdup(src voidptr, sz int) voidptr { pub fn memdup(src voidptr, sz int) voidptr {
if sz == 0 { if sz == 0 {
return vcalloc(1) return vcalloc(1)
@ -221,6 +241,7 @@ pub fn memdup(src voidptr, sz int) voidptr {
} }
} }
// v_ptr_free is used internally to manually free up memory allocated at the address `ptr`.
fn v_ptr_free(ptr voidptr) { fn v_ptr_free(ptr voidptr) {
$if prealloc { $if prealloc {
return return
@ -228,6 +249,7 @@ fn v_ptr_free(ptr voidptr) {
C.free(ptr) C.free(ptr)
} }
// is_atty returns 1 if the `fd` file descriptor is open and refers to a terminal
pub fn is_atty(fd int) int { pub fn is_atty(fd int) int {
$if windows { $if windows {
mut mode := u32(0) mut mode := u32(0)