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:
parent
975286302c
commit
6e6f6bc387
@ -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.error('function `$f.name` is private')
|
||||||
}
|
}
|
||||||
p.calling_c = f.is_c
|
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)
|
cgen_name := p.table.cgen_name(f)
|
||||||
// if p.pref.is_prof {
|
// if p.pref.is_prof {
|
||||||
// p.cur_fn.called_fns << cgen_name
|
// p.cur_fn.called_fns << cgen_name
|
||||||
|
@ -93,11 +93,6 @@ pub fn free(ptr voidptr) {
|
|||||||
C.free(ptr)
|
C.free(ptr)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn _strlen(s byteptr) int {
|
|
||||||
return C.strlen(s)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fn memdup(src voidptr, sz int) voidptr {
|
fn memdup(src voidptr, sz int) voidptr {
|
||||||
mem := malloc(sz)
|
mem := malloc(sz)
|
||||||
return C.memcpy(mem, src, sz)
|
return C.memcpy(mem, src, sz)
|
||||||
|
@ -7,25 +7,25 @@ module builtin
|
|||||||
pub fn (d double) str() string {
|
pub fn (d double) str() string {
|
||||||
buf := malloc(sizeof(double) * 5 + 1)// TODO
|
buf := malloc(sizeof(double) * 5 + 1)// TODO
|
||||||
C.sprintf(buf, '%f', d)
|
C.sprintf(buf, '%f', d)
|
||||||
return tos(buf, _strlen(buf))
|
return tos(buf, strlen(buf))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (d f64) str() string {
|
pub fn (d f64) str() string {
|
||||||
buf := malloc(sizeof(double) * 5 + 1)// TODO
|
buf := malloc(sizeof(double) * 5 + 1)// TODO
|
||||||
C.sprintf(buf, '%f', d)
|
C.sprintf(buf, '%f', d)
|
||||||
return tos(buf, _strlen(buf))
|
return tos(buf, strlen(buf))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (d f32) str() string {
|
pub fn (d f32) str() string {
|
||||||
buf := malloc(sizeof(double) * 5 + 1)// TODO
|
buf := malloc(sizeof(double) * 5 + 1)// TODO
|
||||||
C.sprintf(buf, '%f', d)
|
C.sprintf(buf, '%f', d)
|
||||||
return tos(buf, _strlen(buf))
|
return tos(buf, strlen(buf))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ptr_str(ptr voidptr) string {
|
pub fn ptr_str(ptr voidptr) string {
|
||||||
buf := malloc(sizeof(double) * 5 + 1)// TODO
|
buf := malloc(sizeof(double) * 5 + 1)// TODO
|
||||||
C.sprintf(buf, '%p', ptr)
|
C.sprintf(buf, '%p', ptr)
|
||||||
return tos(buf, _strlen(buf))
|
return tos(buf, strlen(buf))
|
||||||
}
|
}
|
||||||
|
|
||||||
// fn (nn i32) str() string {
|
// fn (nn i32) str() string {
|
||||||
|
28
vlib/os/os.v
28
vlib/os/os.v
@ -105,24 +105,21 @@ fn parse_windows_cmd_line(cmd byteptr) []string {
|
|||||||
|
|
||||||
// read_file reads the file in `path` and returns the contents.
|
// read_file reads the file in `path` and returns the contents.
|
||||||
pub fn read_file(path string) ?string {
|
pub fn read_file(path string) ?string {
|
||||||
mut res := ''
|
|
||||||
mut mode := 'rb'
|
mut mode := 'rb'
|
||||||
cpath := path.cstr()
|
cpath := path.cstr()
|
||||||
fp := C.fopen(cpath, mode.cstr())
|
fp := C.fopen(cpath, mode.cstr())
|
||||||
if isnil(fp) {
|
if isnil(fp) {
|
||||||
return error('failed to open file "$path"')
|
return error('failed to open file "$path"')
|
||||||
//panic('failed to open file "$path"')
|
|
||||||
}
|
}
|
||||||
C.fseek(fp, 0, SEEK_END)
|
C.fseek(fp, 0, SEEK_END)
|
||||||
fsize := C.ftell(fp)
|
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)
|
C.rewind(fp)
|
||||||
mut str := malloc(fsize + 1)
|
mut str := malloc(fsize + 1)
|
||||||
C.fread(str, fsize, 1, fp)
|
C.fread(str, fsize, 1, fp)
|
||||||
C.fclose(fp)
|
C.fclose(fp)
|
||||||
str[fsize] = 0
|
str[fsize] = 0
|
||||||
res = tos(str, fsize)
|
return string(str, fsize)
|
||||||
return res
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// file_size returns the size of the file located in `path`.
|
// 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 {
|
if nr_chars == 0 {
|
||||||
return ''
|
return ''
|
||||||
}
|
}
|
||||||
return tos(buf, nr_chars)
|
return string(buf, nr_chars)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -480,6 +477,15 @@ pub fn user_os() string {
|
|||||||
$if freebsd {
|
$if freebsd {
|
||||||
return 'freebsd'
|
return 'freebsd'
|
||||||
}
|
}
|
||||||
|
$if openbsd {
|
||||||
|
return 'openbsd'
|
||||||
|
}
|
||||||
|
$if netbsd {
|
||||||
|
return 'netbsd'
|
||||||
|
}
|
||||||
|
$if dragonfly {
|
||||||
|
return 'dragonfly'
|
||||||
|
}
|
||||||
return 'unknown'
|
return 'unknown'
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -501,7 +507,7 @@ pub fn home_dir() string {
|
|||||||
return home
|
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) {
|
pub fn write_file(path, text string) {
|
||||||
f := os.create(path) or {
|
f := os.create(path) or {
|
||||||
return
|
return
|
||||||
@ -536,11 +542,11 @@ pub fn executable() string {
|
|||||||
if count < 0 {
|
if count < 0 {
|
||||||
panic('error reading /proc/self/exe to get exe path')
|
panic('error reading /proc/self/exe to get exe path')
|
||||||
}
|
}
|
||||||
return tos(result, count)
|
return string(result, count)
|
||||||
}
|
}
|
||||||
$if windows {
|
$if windows {
|
||||||
ret := int(C.GetModuleFileName( 0, result, MAX_PATH ))
|
ret := int(C.GetModuleFileName( 0, result, MAX_PATH ))
|
||||||
return tos( result, ret)
|
return string( result, ret)
|
||||||
}
|
}
|
||||||
$if mac {
|
$if mac {
|
||||||
pid := C.getpid()
|
pid := C.getpid()
|
||||||
@ -567,14 +573,14 @@ pub fn executable() string {
|
|||||||
if count < 0 {
|
if count < 0 {
|
||||||
panic('error reading /proc/curproc/exe to get exe path')
|
panic('error reading /proc/curproc/exe to get exe path')
|
||||||
}
|
}
|
||||||
return tos(result, count)
|
return string(result, count)
|
||||||
}
|
}
|
||||||
$if dragonfly {
|
$if dragonfly {
|
||||||
count := int(C.readlink('/proc/curproc/file', result, MAX_PATH ))
|
count := int(C.readlink('/proc/curproc/file', result, MAX_PATH ))
|
||||||
if count < 0 {
|
if count < 0 {
|
||||||
panic('error reading /proc/curproc/file to get exe path')
|
panic('error reading /proc/curproc/file to get exe path')
|
||||||
}
|
}
|
||||||
return tos(result, count)
|
return string(result, count)
|
||||||
}
|
}
|
||||||
return '.'
|
return '.'
|
||||||
}
|
}
|
||||||
|
@ -38,5 +38,5 @@ pub fn (b Builder) cut(n int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn (b mut Builder) free() {
|
pub fn (b mut Builder) free() {
|
||||||
C.free(b.buf.data)
|
free(b.buf.data)
|
||||||
}
|
}
|
||||||
|
@ -5,9 +5,10 @@ pub fn repeat(c byte, n int) string {
|
|||||||
return ''
|
return ''
|
||||||
}
|
}
|
||||||
mut arr := malloc(n + 1)
|
mut arr := malloc(n + 1)
|
||||||
|
//mut arr := [byte(0); n + 1]
|
||||||
for i := 0; i < n; i++ {
|
for i := 0; i < n; i++ {
|
||||||
arr[i] = c
|
arr[i] = c
|
||||||
}
|
}
|
||||||
arr[n] = `\0`
|
arr[n] = `\0`
|
||||||
return tos(arr, n)
|
return string(arr, n)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user