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

vlib: use malloc_noscan() where possible (#10465)

This commit is contained in:
Uwe Krüger
2021-06-15 13:47:11 +02:00
committed by GitHub
parent af60eba5e6
commit 60c880a0cc
28 changed files with 56 additions and 83 deletions

View File

@@ -49,7 +49,7 @@ pub fn fd_read(fd int, maxbytes int) (string, int) {
return '', 0
}
unsafe {
mut buf := malloc(maxbytes + 1)
mut buf := malloc_noscan(maxbytes + 1)
nbytes := C.read(fd, buf, maxbytes)
if nbytes < 0 {
free(buf)

View File

@@ -108,7 +108,7 @@ pub fn read_file(path string) ?string {
// C.fseek(fp, 0, SEEK_SET) // same as `C.rewind(fp)` below
C.rewind(fp)
unsafe {
mut str := malloc(fsize + 1)
mut str := malloc_noscan(fsize + 1)
nelements := int(C.fread(str, 1, fsize, fp))
is_eof := int(C.feof(fp))
is_error := int(C.ferror(fp))
@@ -496,7 +496,7 @@ pub fn get_raw_line() string {
$if windows {
unsafe {
max_line_chars := 256
buf := malloc(max_line_chars * 2)
buf := malloc_noscan(max_line_chars * 2)
h_input := C.GetStdHandle(C.STD_INPUT_HANDLE)
mut bytes_read := u32(0)
if is_atty(0) > 0 {
@@ -538,7 +538,7 @@ pub fn get_raw_stdin() []byte {
unsafe {
block_bytes := 512
mut old_size := block_bytes
mut buf := malloc(block_bytes)
mut buf := malloc_noscan(block_bytes)
h_input := C.GetStdHandle(C.STD_INPUT_HANDLE)
mut bytes_read := 0
mut offset := 0
@@ -584,7 +584,7 @@ pub fn read_file_array<T>(path string) []T {
C.rewind(fp)
// read the actual data from the file
len := fsize / tsize
buf := unsafe { malloc(fsize) }
buf := unsafe { malloc_noscan(fsize) }
nread := C.fread(buf, tsize, len, fp)
C.fclose(fp)
return unsafe {
@@ -619,7 +619,7 @@ pub fn on_segfault(f voidptr) {
[manualfree]
pub fn executable() string {
$if linux {
mut xresult := vcalloc(max_path_len)
mut xresult := vcalloc_noscan(max_path_len)
count := C.readlink(c'/proc/self/exe', &char(xresult), max_path_len)
if count < 0 {
eprintln('os.executable() failed at reading /proc/self/exe to get exe path')
@@ -630,7 +630,7 @@ pub fn executable() string {
$if windows {
max := 512
size := max * 2 // max_path_len * sizeof(wchar_t)
mut result := unsafe { &u16(vcalloc(size)) }
mut result := unsafe { &u16(vcalloc_noscan(size)) }
len := C.GetModuleFileName(0, result, max)
// determine if the file is a windows symlink
attrs := C.GetFileAttributesW(result)
@@ -639,7 +639,7 @@ pub fn executable() string {
// gets handle with GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0
file := C.CreateFile(result, 0x80000000, 1, 0, 3, 0x80, 0)
if file != voidptr(-1) {
final_path := unsafe { &u16(vcalloc(size)) }
final_path := unsafe { &u16(vcalloc_noscan(size)) }
// https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfinalpathnamebyhandlew
final_len := C.GetFinalPathNameByHandleW(file, final_path, size, 0)
if final_len < size {
@@ -655,7 +655,7 @@ pub fn executable() string {
return unsafe { string_from_wide2(result, len) }
}
$if macos {
mut result := vcalloc(max_path_len)
mut result := vcalloc_noscan(max_path_len)
pid := C.getpid()
ret := proc_pidpath(pid, result, max_path_len)
if ret <= 0 {
@@ -665,7 +665,7 @@ pub fn executable() string {
return unsafe { result.vstring() }
}
$if freebsd {
mut result := vcalloc(max_path_len)
mut result := vcalloc_noscan(max_path_len)
mib := [1 /* CTL_KERN */, 14 /* KERN_PROC */, 12 /* KERN_PROC_PATHNAME */, -1]
size := max_path_len
unsafe { C.sysctl(mib.data, 4, result, &size, 0, 0) }
@@ -679,7 +679,7 @@ pub fn executable() string {
$if haiku {
}
$if netbsd {
mut result := vcalloc(max_path_len)
mut result := vcalloc_noscan(max_path_len)
count := C.readlink(c'/proc/curproc/exe', &char(result), max_path_len)
if count < 0 {
eprintln('os.executable() failed at reading /proc/curproc/exe to get exe path')
@@ -688,7 +688,7 @@ pub fn executable() string {
return unsafe { result.vstring_with_len(count) }
}
$if dragonfly {
mut result := vcalloc(max_path_len)
mut result := vcalloc_noscan(max_path_len)
count := C.readlink(c'/proc/curproc/file', &char(result), max_path_len)
if count < 0 {
eprintln('os.executable() failed at reading /proc/curproc/file to get exe path')
@@ -751,7 +751,7 @@ pub fn getwd() string {
$if windows {
max := 512 // max_path_len * sizeof(wchar_t)
unsafe {
buf := &u16(vcalloc(max * 2))
buf := &u16(vcalloc_noscan(max * 2))
if C._wgetcwd(buf, max) == 0 {
free(buf)
return ''
@@ -759,7 +759,7 @@ pub fn getwd() string {
return string_from_wide(buf)
}
} $else {
buf := vcalloc(max_path_len)
buf := vcalloc_noscan(max_path_len)
unsafe {
if C.getcwd(&char(buf), max_path_len) == 0 {
free(buf)
@@ -782,7 +782,7 @@ pub fn real_path(fpath string) string {
$if windows {
// GetFullPathName doesn't work with symbolic links,
// so if it is not a file, get full path
fullpath = unsafe { &u16(vcalloc(max_path_len * 2)) }
fullpath = unsafe { &u16(vcalloc_noscan(max_path_len * 2)) }
// TODO: check errors if path len is not enough
ret := C.GetFullPathName(fpath.to_wide(), max_path_len, fullpath, 0)
if ret == 0 {
@@ -791,7 +791,7 @@ pub fn real_path(fpath string) string {
}
res = unsafe { string_from_wide(fullpath) }
} $else {
fullpath = vcalloc(max_path_len)
fullpath = vcalloc_noscan(max_path_len)
ret := &char(C.realpath(&char(fpath.str), &char(fullpath)))
if ret == 0 {
unsafe { free(fullpath) }

View File

@@ -17,30 +17,3 @@ pub const (
sys_mkdir = 83
sys_creat = 85
)
/*
// TODO no pub => error
pub fn write(fd int, data voidptr, nbytes int) int {
return syscall5(
1, // SYS_write
fd,
data,
nbytes,
0, // ignored
0 // ignored
)
}
pub fn println(s string) {
write(1, (s + '\n').str, s.len)
}
fn mmap(start voidptr, len, prot, flags, fd, off int) byteptr {
return syscall6(9, start, len, prot, flags, fd, off) // sys_mmap
}
pub fn malloc(n int) byteptr {
println('malloc($n)')
return mmap(0, n, 3, 4098, //prot_read|prot_write,
-1,0) //map_private|map_anonymous,
*/

View File

@@ -68,7 +68,7 @@ pub fn uname() Uname {
mut u := Uname{}
utsize := sizeof(C.utsname)
unsafe {
x := malloc(int(utsize))
x := malloc_noscan(int(utsize))
d := &C.utsname(x)
if C.uname(d) == 0 {
u.sysname = cstring_to_vstring(d.sysname)
@@ -85,7 +85,7 @@ pub fn uname() Uname {
pub fn hostname() string {
mut hstnme := ''
size := 256
mut buf := unsafe { &char(malloc(size)) }
mut buf := unsafe { &char(malloc_noscan(size)) }
if C.gethostname(buf, size) == 0 {
hstnme = unsafe { cstring_to_vstring(buf) }
unsafe { free(buf) }
@@ -203,7 +203,7 @@ pub fn execute(cmd string) Result {
output: 'exec("$cmd") failed'
}
}
buf := unsafe { malloc(4096) }
buf := unsafe { malloc_noscan(4096) }
mut res := strings.new_builder(1024)
defer {
unsafe { res.free() }

View File

@@ -167,7 +167,7 @@ pub fn get_file_handle(path string) HANDLE {
pub fn get_module_filename(handle HANDLE) ?string {
unsafe {
mut sz := 4096 // Optimized length
mut buf := &u16(malloc(4096))
mut buf := &u16(malloc_noscan(4096))
for {
status := int(C.GetModuleFileNameW(handle, voidptr(&buf), sz))
match status {