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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
28 changed files with 56 additions and 83 deletions

View File

@ -145,7 +145,7 @@ fn main() {
app.ttf_render << &ttf.TTF_render_Sokol{
bmp: &ttf.BitMap{
tf: &(app.tf[0])
buf: unsafe { malloc(32000000) }
buf: unsafe { malloc_noscan(32000000) }
buf_size: (32000000)
color: 0xFF0000FF
// style: .raw

View File

@ -42,7 +42,7 @@ fn (nn int) str_l(max int) string {
is_neg = true
}
mut index := max
mut buf := malloc(max + 1)
mut buf := malloc_noscan(max + 1)
buf[index] = 0
index--
@ -119,7 +119,7 @@ pub fn (nn u32) str() string {
return '0'
}
max := 12
mut buf := malloc(max + 1)
mut buf := malloc_noscan(max + 1)
mut index := max
buf[index] = 0
index--
@ -163,7 +163,7 @@ pub fn (nn i64) str() string {
return '0'
}
max := 20
mut buf := malloc(max + 1)
mut buf := malloc_noscan(max + 1)
mut is_neg := false
if n < 0 {
n = -n
@ -210,7 +210,7 @@ pub fn (nn u64) str() string {
return '0'
}
max := 20
mut buf := malloc(max + 1)
mut buf := malloc_noscan(max + 1)
mut index := max
buf[index] = 0
index--
@ -423,7 +423,7 @@ pub fn (b byte) str() string {
// Example: assert byte(97).ascii_str() == 'a'
pub fn (b byte) ascii_str() string {
mut str := string{
str: unsafe { malloc(2) }
str: unsafe { malloc_noscan(2) }
len: 1
}
unsafe {

View File

@ -45,7 +45,7 @@ pub fn (b []byte) clone() []byte {
// TODO remove this once runes are implemented
pub fn (b []byte) bytestr() string {
unsafe {
buf := malloc(b.len + 1)
buf := malloc_noscan(b.len + 1)
C.memcpy(buf, b.data, b.len)
buf[b.len] = 0
return tos(buf, b.len)

View File

@ -9,7 +9,7 @@ pub fn (_str string) to_wide() &u16 {
unsafe {
num_chars := (C.MultiByteToWideChar(cp_utf8, 0, &char(_str.str), _str.len,
0, 0))
mut wstr := &u16(malloc((num_chars + 1) * 2)) // sizeof(wchar_t)
mut wstr := &u16(malloc_noscan((num_chars + 1) * 2)) // sizeof(wchar_t)
if wstr != 0 {
C.MultiByteToWideChar(cp_utf8, 0, &char(_str.str), _str.len, wstr, num_chars)
C.memset(&byte(wstr) + num_chars * 2, 0, 2)
@ -38,7 +38,7 @@ pub fn string_from_wide2(_wstr &u16, len int) string {
$if windows {
unsafe {
num_chars := C.WideCharToMultiByte(cp_utf8, 0, _wstr, len, 0, 0, 0, 0)
mut str_to := malloc(num_chars + 1)
mut str_to := malloc_noscan(num_chars + 1)
if str_to != 0 {
C.WideCharToMultiByte(cp_utf8, 0, _wstr, len, &char(str_to), num_chars,
0, 0)

View File

@ -11,7 +11,7 @@ pub fn utf8_char_len(b byte) int {
// utf32 == Codepoint
pub fn utf32_to_str(code u32) string {
unsafe {
mut buffer := malloc(5)
mut buffer := malloc_noscan(5)
return utf32_to_str_no_malloc(code, buffer)
}
}

View File

@ -11,7 +11,7 @@ const (
// read returns an array of `bytes_needed` random bytes read from the OS.
pub fn read(bytes_needed int) ?[]byte {
mut buffer := unsafe { malloc(bytes_needed) }
mut buffer := unsafe { malloc_noscan(bytes_needed) }
mut bytes_read := 0
mut remaining_bytes := bytes_needed
// getrandom syscall wont block if requesting <= 256 bytes

View File

@ -14,7 +14,7 @@ const (
// read returns an array of `bytes_needed` random bytes read from the OS.
pub fn read(bytes_needed int) ?[]byte {
mut buffer := unsafe { malloc(bytes_needed) }
mut buffer := unsafe { malloc_noscan(bytes_needed) }
mut bytes_read := 0
mut remaining_bytes := bytes_needed
// getrandom syscall wont block if requesting <= 256 bytes

View File

@ -42,7 +42,7 @@ pub fn resource_path() string {
panic('CFBundleCopyResourcesDirectoryURL failed')
}
buffer_size := 4096
mut buffer := unsafe { malloc(buffer_size) }
mut buffer := unsafe { malloc_noscan(buffer_size) }
unsafe {
buffer[0] = 0
}

View File

@ -35,7 +35,7 @@ pub fn decode_str(data string) string {
return ''
}
unsafe {
buffer := malloc(size + 1)
buffer := malloc_noscan(size + 1)
buffer[size] = 0
return tos(buffer, decode_in_buffer(data, buffer))
}
@ -62,7 +62,7 @@ fn alloc_and_encode(src &byte, len int) string {
return ''
}
unsafe {
buffer := malloc(size + 1)
buffer := malloc_noscan(size + 1)
buffer[size] = 0
return tos(buffer, encode_from_buffer(buffer, src, len))
}

View File

@ -404,7 +404,7 @@ fn utf8_to_upper(in_cp int) int {
fn up_low(s string, upper_flag bool) string {
mut index := 0
mut tab_char := 0
mut str_res := unsafe { malloc(s.len + 1) }
mut str_res := unsafe { malloc_noscan(s.len + 1) }
for {
ch_len := utf8_char_len(s[index])

View File

@ -129,7 +129,7 @@ fn rotate(m Mat4, angle f32, vec Vec3) Mat4 {
}
*/
fn f32_calloc(n int) &f32 {
return voidptr(vcalloc(n * int(sizeof(f32))))
return voidptr(vcalloc_noscan(n * int(sizeof(f32))))
}
// fn translate(vec Vec3) *f32 {
@ -382,7 +382,7 @@ fn ortho_js(left f32, right f32, bottom f32, top f32) &f32 {
bt := 1.0 / (bottom - top)
nf := f32(1.0) / 1.0 // (mynear -myfar)
unsafe {
mut out := &f32(malloc(int(sizeof(f32) * 16)))
mut out := &f32(malloc_noscan(int(sizeof(f32) * 16)))
out[0] = -2.0 * lr
out[1] = 0
out[2] = 0

View File

@ -126,7 +126,7 @@ pub fn (conn &Connection) tables(wildcard string) ?[]string {
// taking into account the current character set of the connection.
pub fn (conn &Connection) escape_string(s string) string {
unsafe {
to := malloc(2 * s.len + 1)
to := malloc_noscan(2 * s.len + 1)
C.mysql_real_escape_string_quote(conn.conn, to, s.str, s.len, `\'`)
return to.vstring()
}

View File

@ -12,7 +12,7 @@ fn C.new_tls_context() C.TlsContext
fn (req &Request) ssl_do(port int, method Method, host_name string, path string) ?Response {
mut ctx := C.new_tls_context()
C.vschannel_init(&ctx)
mut buff := unsafe { malloc(C.vsc_init_resp_buff_size) }
mut buff := unsafe { malloc_noscan(C.vsc_init_resp_buff_size) }
addr := host_name
sdata := req.build_request_headers(method, host_name, path)
$if trace_http_request ? {

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 {

View File

@ -243,8 +243,8 @@ pub fn new(config Config) &Picoev {
timeout_secs: config.timeout_secs
max_headers: config.max_headers
date: C.get_date()
buf: unsafe { malloc(picoev.max_fds * picoev.max_read + 1) }
out: unsafe { malloc(picoev.max_fds * picoev.max_write + 1) }
buf: unsafe { malloc_noscan(picoev.max_fds * picoev.max_read + 1) }
out: unsafe { malloc_noscan(picoev.max_fds * picoev.max_write + 1) }
}
C.picoev_add(loop, fd, int(Event.read), 0, accept_callback, pv)
go update_date(mut pv)

View File

@ -197,7 +197,7 @@ pub fn string_from_set(charset string, len int) string {
if len == 0 {
return ''
}
mut buf := unsafe { malloc(len + 1) }
mut buf := unsafe { malloc_noscan(len + 1) }
for i in 0 .. len {
unsafe {
buf[i] = charset[intn(charset.len)]
@ -228,7 +228,7 @@ pub fn ascii(len int) string {
// See https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_(random)
pub fn uuid_v4() string {
buflen := 36
mut buf := unsafe { malloc(37) }
mut buf := unsafe { malloc_noscan(37) }
mut i_buf := 0
mut x := u64(0)
mut d := byte(0)
@ -281,7 +281,7 @@ pub fn ulid() string {
// ulid_at_millisecond does the same as `ulid` but takes a custom Unix millisecond timestamp via `unix_time_milli`.
pub fn ulid_at_millisecond(unix_time_milli u64) string {
buflen := 26
mut buf := unsafe { malloc(27) }
mut buf := unsafe { malloc_noscan(27) }
mut t := unix_time_milli
mut i := 9
for i >= 0 {

View File

@ -440,7 +440,7 @@ pub fn format_es(f f64, p BF_param) string {
[direct_array_access]
pub fn remove_tail_zeros(s string) string {
unsafe{
mut buf := malloc(s.len + 1)
mut buf := malloc_noscan(s.len + 1)
mut i_d := 0
mut i_s := 0

View File

@ -5,7 +5,7 @@ pub fn repeat(c byte, n int) string {
if n <= 0 {
return ''
}
mut bytes := unsafe { malloc(n + 1) }
mut bytes := unsafe { malloc_noscan(n + 1) }
unsafe {
C.memset(bytes, c, n)
bytes[n] = `0`
@ -22,7 +22,7 @@ pub fn repeat_string(s string, n int) string {
}
slen := s.len
blen := slen * n
mut bytes := unsafe { malloc(blen + 1) }
mut bytes := unsafe { malloc_noscan(blen + 1) }
for bi in 0 .. n {
bislen := bi * slen
for si in 0 .. slen {

View File

@ -173,7 +173,7 @@ fn get_cursor_position() (int, int) {
print('\033[6n')
mut s := ''
unsafe {
buf := malloc(25)
buf := malloc_noscan(25)
len := C.read(C.STDIN_FILENO, buf, 24)
buf[len] = 0
s = tos(buf, len)
@ -196,7 +196,7 @@ fn supports_truecolor() bool {
print('\x1bP\$qm\x1b\\')
mut s := ''
unsafe {
buf := malloc(25)
buf := malloc_noscan(25)
len := C.read(C.STDIN_FILENO, buf, 24)
buf[len] = 0
s = tos(buf, len)

View File

@ -36,7 +36,7 @@ pub fn parse_rfc2822(s string) ?Time {
pos := months_string.index(fields[2]) or { return error('Invalid time format: $s') }
mm := pos / 3 + 1
unsafe {
tmstr := malloc(s.len * 2)
tmstr := malloc_noscan(s.len * 2)
count := C.snprintf(&char(tmstr), (s.len * 2), c'%s-%02d-%s %s', fields[3].str,
mm, fields[1].str, fields[4].str)
return parse(tos(tmstr, count))

View File

@ -46,7 +46,7 @@ fn find_windows_kit_internal(key RegKey, versions []string) ?string {
continue
}
alloc_length := (required_bytes + 2)
mut value := &u16(malloc(int(alloc_length)))
mut value := &u16(malloc_noscan(int(alloc_length)))
if isnil(value) {
continue
}

View File

@ -237,7 +237,7 @@ fn (s Scanner) num_lit(start int, end int) string {
}
unsafe {
txt := s.text.str
mut b := malloc(end - start + 1) // add a byte for the endstring 0
mut b := malloc_noscan(end - start + 1) // add a byte for the endstring 0
mut i1 := 0
for i := start; i < end; i++ {
if txt[i] != scanner.num_sep {

View File

@ -87,7 +87,7 @@ fn (mut bmp BitMap) format_texture() {
// write out a .ppm file
pub fn (mut bmp BitMap) save_as_ppm(file_name string) {
tmp_buf := bmp.buf
mut buf := unsafe { malloc(bmp.buf_size) }
mut buf := unsafe { malloc_noscan(bmp.buf_size) }
unsafe { C.memcpy(buf, tmp_buf, bmp.buf_size) }
bmp.buf = buf

View File

@ -56,7 +56,7 @@ pub fn (mut tf_skl TTF_render_Sokol) create_text(in_txt string, in_font_size f32
unsafe { free(tf_skl.bmp.buf) }
}
dprintln('create_text Alloc: $sz bytes')
tf_skl.bmp.buf = unsafe { malloc(sz) }
tf_skl.bmp.buf = unsafe { malloc_noscan(sz) }
tf_skl.bmp.buf_size = sz
}
@ -94,7 +94,7 @@ pub fn (mut tf_skl TTF_render_Sokol) create_text_block(in_txt string, in_w int,
unsafe { free(tf_skl.bmp.buf) }
}
dprintln('Alloc: $sz bytes')
tf_skl.bmp.buf = unsafe { malloc(sz) }
tf_skl.bmp.buf = unsafe { malloc_noscan(sz) }
tf_skl.bmp.buf_size = sz
}