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

@@ -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)
}
}