1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

free/malloc fixes

This commit is contained in:
Alexander Medvednikov 2019-07-21 12:22:41 +02:00
parent 975286302c
commit 6e6f6bc387
6 changed files with 31 additions and 22 deletions

View File

@ -518,6 +518,13 @@ fn (p mut Parser) fn_call(f Fn, method_ph int, receiver_var, receiver_type strin
p.error('function `$f.name` is private')
}
p.calling_c = f.is_c
if f.is_c && !p.builtin_pkg {
if f.name == 'free' {
p.error('use `free()` instead of `C.free()`')
} else if f.name == 'malloc' {
p.error('use `malloc()` instead of `C.malloc()`')
}
}
cgen_name := p.table.cgen_name(f)
// if p.pref.is_prof {
// p.cur_fn.called_fns << cgen_name

View File

@ -93,11 +93,6 @@ pub fn free(ptr voidptr) {
C.free(ptr)
}
fn _strlen(s byteptr) int {
return C.strlen(s)
}
fn memdup(src voidptr, sz int) voidptr {
mem := malloc(sz)
return C.memcpy(mem, src, sz)

View File

@ -7,25 +7,25 @@ module builtin
pub fn (d double) str() string {
buf := malloc(sizeof(double) * 5 + 1)// TODO
C.sprintf(buf, '%f', d)
return tos(buf, _strlen(buf))
return tos(buf, strlen(buf))
}
pub fn (d f64) str() string {
buf := malloc(sizeof(double) * 5 + 1)// TODO
C.sprintf(buf, '%f', d)
return tos(buf, _strlen(buf))
return tos(buf, strlen(buf))
}
pub fn (d f32) str() string {
buf := malloc(sizeof(double) * 5 + 1)// TODO
C.sprintf(buf, '%f', d)
return tos(buf, _strlen(buf))
return tos(buf, strlen(buf))
}
pub fn ptr_str(ptr voidptr) string {
buf := malloc(sizeof(double) * 5 + 1)// TODO
C.sprintf(buf, '%p', ptr)
return tos(buf, _strlen(buf))
return tos(buf, strlen(buf))
}
// fn (nn i32) str() string {

View File

@ -105,24 +105,21 @@ fn parse_windows_cmd_line(cmd byteptr) []string {
// read_file reads the file in `path` and returns the contents.
pub fn read_file(path string) ?string {
mut res := ''
mut mode := 'rb'
cpath := path.cstr()
fp := C.fopen(cpath, mode.cstr())
if isnil(fp) {
return error('failed to open file "$path"')
//panic('failed to open file "$path"')
}
C.fseek(fp, 0, SEEK_END)
fsize := C.ftell(fp)
// C.fseek(fp, 0, SEEK_SET) // same as C.rewind(fp) below
// C.fseek(fp, 0, SEEK_SET) // same as `C.rewind(fp)` below
C.rewind(fp)
mut str := malloc(fsize + 1)
C.fread(str, fsize, 1, fp)
C.fclose(fp)
str[fsize] = 0
res = tos(str, fsize)
return res
return string(str, fsize)
}
// file_size returns the size of the file located in `path`.
@ -463,7 +460,7 @@ pub fn get_raw_line() string {
if nr_chars == 0 {
return ''
}
return tos(buf, nr_chars)
return string(buf, nr_chars)
}
}
@ -480,6 +477,15 @@ pub fn user_os() string {
$if freebsd {
return 'freebsd'
}
$if openbsd {
return 'openbsd'
}
$if netbsd {
return 'netbsd'
}
$if dragonfly {
return 'dragonfly'
}
return 'unknown'
}
@ -501,7 +507,7 @@ pub fn home_dir() string {
return home
}
// write_file writes text data to a file in `path`.
// write_file writes `text` data to a file in `path`.
pub fn write_file(path, text string) {
f := os.create(path) or {
return
@ -536,11 +542,11 @@ pub fn executable() string {
if count < 0 {
panic('error reading /proc/self/exe to get exe path')
}
return tos(result, count)
return string(result, count)
}
$if windows {
ret := int(C.GetModuleFileName( 0, result, MAX_PATH ))
return tos( result, ret)
return string( result, ret)
}
$if mac {
pid := C.getpid()
@ -567,14 +573,14 @@ pub fn executable() string {
if count < 0 {
panic('error reading /proc/curproc/exe to get exe path')
}
return tos(result, count)
return string(result, count)
}
$if dragonfly {
count := int(C.readlink('/proc/curproc/file', result, MAX_PATH ))
if count < 0 {
panic('error reading /proc/curproc/file to get exe path')
}
return tos(result, count)
return string(result, count)
}
return '.'
}

View File

@ -38,5 +38,5 @@ pub fn (b Builder) cut(n int) {
}
pub fn (b mut Builder) free() {
C.free(b.buf.data)
free(b.buf.data)
}

View File

@ -5,9 +5,10 @@ pub fn repeat(c byte, n int) string {
return ''
}
mut arr := malloc(n + 1)
//mut arr := [byte(0); n + 1]
for i := 0; i < n; i++ {
arr[i] = c
}
arr[n] = `\0`
return tos(arr, n)
return string(arr, n)
}