Skip to content

Commit 2cd1c9e

Browse files
authored
builtin,os: enable no warnings for gg programs like v -gc boehm_leak -cg -keepc run examples/gg/minimal.v (part 1 - before the gg loop) (#24749)
1 parent c6feed9 commit 2cd1c9e

3 files changed

Lines changed: 92 additions & 23 deletions

File tree

‎vlib/builtin/string.v‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,9 @@ pub fn (s string) replace_each(vals []string) string {
455455
// of the new string to do just one allocation.
456456
mut new_len := s.len
457457
mut idxs := []RepIndex{cap: 6}
458+
defer {
459+
unsafe { idxs.free() }
460+
}
458461
mut idx := 0
459462
s_ := s.clone()
460463
for rep_i := 0; rep_i < vals.len; rep_i += 2 {
@@ -845,6 +848,8 @@ fn (s string) plus_two(a string, b string) string {
845848
@[direct_array_access]
846849
pub fn (s string) split_any(delim string) []string {
847850
mut res := []string{}
851+
unsafe { res.flags.set(.noslices) }
852+
defer { unsafe { res.flags.clear(.noslices) } }
848853
mut i := 0
849854
// check empty source string
850855
if s.len > 0 {
@@ -874,6 +879,8 @@ pub fn (s string) split_any(delim string) []string {
874879
@[direct_array_access]
875880
pub fn (s string) rsplit_any(delim string) []string {
876881
mut res := []string{}
882+
unsafe { res.flags.set(.noslices) }
883+
defer { unsafe { res.flags.clear(.noslices) } }
877884
mut i := s.len - 1
878885
if s.len > 0 {
879886
if delim.len <= 0 {
@@ -964,6 +971,8 @@ pub fn (s string) split_n(delim string, n int) []string {
964971
@[direct_array_access]
965972
pub fn (s string) split_nth(delim string, nth int) []string {
966973
mut res := []string{}
974+
unsafe { res.flags.set(.noslices) } // allow freeing of old data during <<
975+
defer { unsafe { res.flags.clear(.noslices) } }
967976

968977
match delim.len {
969978
0 {
@@ -1023,6 +1032,8 @@ pub fn (s string) split_nth(delim string, nth int) []string {
10231032
@[direct_array_access]
10241033
pub fn (s string) rsplit_nth(delim string, nth int) []string {
10251034
mut res := []string{}
1035+
unsafe { res.flags.set(.noslices) } // allow freeing of old data during <<
1036+
defer { unsafe { res.flags.clear(.noslices) } }
10261037

10271038
match delim.len {
10281039
0 {
@@ -1082,6 +1093,8 @@ pub fn (s string) split_into_lines() []string {
10821093
if s.len == 0 {
10831094
return res
10841095
}
1096+
unsafe { res.flags.set(.noslices) } // allow freeing of old data during <<
1097+
defer { unsafe { res.flags.clear(.noslices) } }
10851098
cr := `\r`
10861099
lf := `\n`
10871100
mut line_start := 0
@@ -1110,6 +1123,8 @@ pub fn (s string) split_into_lines() []string {
11101123
// Repeated, trailing or leading whitespaces will be omitted.
11111124
pub fn (s string) split_by_space() []string {
11121125
mut res := []string{}
1126+
unsafe { res.flags.set(.noslices) }
1127+
defer { unsafe { res.flags.clear(.noslices) } }
11131128
for word in s.split_any(' \n\t\v\f\r') {
11141129
if word != '' {
11151130
res << word
@@ -2466,6 +2481,8 @@ pub fn (s string) repeat(count int) string {
24662481
// Example: assert ' sss ssss'.fields() == ['sss', 'ssss']
24672482
pub fn (s string) fields() []string {
24682483
mut res := []string{}
2484+
unsafe { res.flags.set(.noslices) }
2485+
defer { unsafe { res.flags.clear(.noslices) } }
24692486
mut word_start := 0
24702487
mut word_len := 0
24712488
mut is_in_word := false

‎vlib/os/asset/asset.v‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,39 @@ import os
66
// of a resource in it. On desktop systems, it returns a path relative to the location of the executable.
77
// On Android, it will return just the relative_path segment, allowing you to later use os.read_apk_asset
88
// to read from it.
9+
@[manualfree]
910
pub fn get_path(base_folder string, relative_path string) string {
1011
$if android {
11-
return relative_path
12+
return relative_path.clone()
1213
} $else {
13-
return os.resource_abs_path(os.join_path(base_folder, relative_path))
14+
fpath := os.join_path_single(base_folder, relative_path)
15+
defer { unsafe { fpath.free() } }
16+
return os.resource_abs_path(fpath)
1417
}
1518
}
1619

1720
// read_bytes will read all of the given asset, specified by its base_folder and relative path in it.
1821
// On Android, it will use os.read_apk_asset, relying that the asset base folder has been prepared, and
1922
// prepackaged inside your APK. On desktop systems, it will use the base_folder and relative_path, to
2023
// locate the file, in a way, that is relative to the executable.
24+
@[manualfree]
2125
pub fn read_bytes(base_folder string, relative_path string) ![]u8 {
26+
fpath := get_path(base_folder, relative_path)
27+
defer { unsafe { fpath.free() } }
28+
mut f_read := os.read_bytes
2229
$if android {
23-
return os.read_apk_asset(get_path(base_folder, relative_path))
24-
} $else {
25-
return os.read_bytes(get_path(base_folder, relative_path))
30+
f_read = os.read_apk_asset
2631
}
32+
res := f_read(fpath)!
33+
return res
2734
}
2835

2936
// read_text will return the full content of the given asset as a string.
3037
// See also read_bytes.
38+
@[manualfree]
3139
pub fn read_text(base_folder string, relative_path string) !string {
32-
res := read_bytes(base_folder, relative_path)!
33-
return res.bytestr()
40+
bytes := read_bytes(base_folder, relative_path)!
41+
defer { unsafe { bytes.free() } }
42+
res := bytes.bytestr()
43+
return res
3444
}

‎vlib/os/font/font.v‎

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ fn debug_font_println(s string) {
2222
// If the env variable `VUI_FONT` is set this is used instead.
2323
// NOTE that, in some cases, the function calls out to external OS programs
2424
// so running this in a hot loop is not advised.
25+
@[manualfree]
2526
pub fn default() string {
2627
env_font := os.getenv('VUI_FONT')
2728
if env_font != '' && os.exists(env_font) {
2829
return env_font
2930
}
31+
unsafe { env_font.free() }
3032
$if windows {
3133
if os.exists('C:\\Windows\\Fonts\\segoeui.ttf') {
3234
debug_font_println('Using font "C:\\Windows\\Fonts\\segoeui.ttf"')
@@ -44,6 +46,7 @@ pub fn default() string {
4446
return font
4547
}
4648
}
49+
unsafe { fonts.free() }
4750
}
4851
$if android {
4952
xml_files := ['/system/etc/system_fonts.xml', '/system/etc/fonts.xml',
@@ -54,6 +57,7 @@ pub fn default() string {
5457
if os.is_file(xml_file) && os.is_readable(xml_file) {
5558
xml := os.read_file(xml_file) or { continue }
5659
lines := xml.split('\n')
60+
unsafe { xml.free() }
5761
mut candidate_font := ''
5862
for line in lines {
5963
if line.contains('<font') {
@@ -65,12 +69,16 @@ pub fn default() string {
6569
debug_font_println('Using font "${candidate_path}"')
6670
return candidate_path
6771
}
72+
unsafe { candidate_path.free() }
6873
}
6974
}
7075
}
7176
}
77+
unsafe { candidate_font.free() }
7278
}
7379
}
80+
unsafe { font_locations.free() }
81+
unsafe { xml_files.free() }
7482
}
7583
mut fm := os.execute("fc-match --format='%{file}\n' -s")
7684
if fm.exit_code == 0 {
@@ -81,59 +89,93 @@ pub fn default() string {
8189
return l
8290
}
8391
}
92+
unsafe { lines.free() }
8493
} else {
8594
panic('fc-match failed to fetch system font')
8695
}
96+
unsafe { fm.free() }
8797
panic('failed to init the font')
8898
}
8999

90100
// get_path_variant returns the `font_path` file name replaced with the
91101
// file name of the font's `variant` version if it exists.
102+
@[manualfree]
92103
pub fn get_path_variant(font_path string, variant Variant) string {
93104
// TODO: find some way to make this shorter and more eye-pleasant
94105
// NotoSans, LiberationSans, DejaVuSans, Arial and SFNS should work
95106
mut file := os.file_name(font_path)
107+
defer { unsafe { file.free() } }
108+
96109
mut fpath := font_path.replace(file, '')
97-
file = file.replace('.ttf', '')
110+
defer { unsafe { fpath.free() } }
111+
112+
mut_replace(file, '.ttf', '')
113+
114+
flower := file.to_lower()
115+
defer { unsafe { flower.free() } }
98116

99117
match variant {
100118
.normal {}
101119
.bold {
102120
if fpath.ends_with('-Regular') {
103-
file = file.replace('-Regular', '-Bold')
121+
mut_replace(file, '-Regular', '-Bold')
104122
} else if file.starts_with('DejaVuSans') {
105-
file += '-Bold'
106-
} else if file.to_lower().starts_with('arial') {
107-
file += 'bd'
123+
mut_plus(file, '-Bold')
124+
} else if flower.starts_with('arial') {
125+
mut_plus(file, 'bd')
108126
} else {
109-
file += '-bold'
127+
mut_plus(file, '-bold')
110128
}
111129
$if macos {
112130
if os.exists('SFNS-bold') {
113-
file = 'SFNS-bold'
131+
mut_assign(file, 'SFNS-bold')
114132
}
115133
}
116134
}
117135
.italic {
118136
if file.ends_with('-Regular') {
119-
file = file.replace('-Regular', '-Italic')
137+
mut_replace(file, '-Regular', '-Italic')
120138
} else if file.starts_with('DejaVuSans') {
121-
file += '-Oblique'
122-
} else if file.to_lower().starts_with('arial') {
123-
file += 'i'
139+
mut_plus(file, '-Oblique')
140+
} else if flower.starts_with('arial') {
141+
mut_plus(file, 'i')
124142
} else {
125-
file += 'Italic'
143+
mut_plus(file, 'Italic')
126144
}
127145
}
128146
.mono {
129147
if !file.ends_with('Mono-Regular') && file.ends_with('-Regular') {
130-
file = file.replace('-Regular', 'Mono-Regular')
131-
} else if file.to_lower().starts_with('arial') {
148+
mut_replace(file, '-Regular', 'Mono-Regular')
149+
} else if flower.starts_with('arial') {
132150
// Arial has no mono variant
133151
} else {
134-
file += 'Mono'
152+
mut_plus(file, 'Mono')
135153
}
136154
}
137155
}
138-
return '${fpath}${file}.ttf'
156+
res := '${fpath}${file}.ttf'
157+
return res
158+
}
159+
160+
fn mut_replace(s &string, find string, replacement string) {
161+
new := (*s).replace(find, replacement)
162+
unsafe { s.free() }
163+
unsafe {
164+
*s = new
165+
}
166+
}
167+
168+
fn mut_plus(s &string, tail string) {
169+
new := (*s) + tail
170+
unsafe { s.free() }
171+
unsafe {
172+
*s = new
173+
}
174+
}
175+
176+
fn mut_assign(s &string, value string) {
177+
unsafe { s.free() }
178+
unsafe {
179+
*s = value.clone()
180+
}
139181
}

0 commit comments

Comments
 (0)