mirror of
https://github.com/vlang/v.git
synced 2025-09-13 14:32:26 +03:00
cgen: add @[reused]
attribute to mark methods, reusing the receiver memory on return (needed for autofree) (fix #25221) (#25235)
Some checks are pending
Graphics CI / gg-regressions (push) Waiting to run
vlib modules CI / build-module-docs (push) Waiting to run
native backend CI / native-backend-ubuntu (push) Waiting to run
native backend CI / native-backend-windows (push) Waiting to run
Shy and PV CI / v-compiles-puzzle-vibes (push) Waiting to run
Sanitized CI / sanitize-undefined-gcc (push) Waiting to run
Sanitized CI / sanitize-undefined-clang (push) Waiting to run
Sanitized CI / tests-sanitize-address-clang (push) Waiting to run
Sanitized CI / sanitize-address-msvc (push) Waiting to run
Sanitized CI / sanitize-address-gcc (push) Waiting to run
Sanitized CI / sanitize-memory-clang (push) Waiting to run
sdl CI / v-compiles-sdl-examples (push) Waiting to run
Time CI / time-linux (push) Waiting to run
Time CI / time-macos (push) Waiting to run
Time CI / time-windows (push) Waiting to run
toml CI / toml-module-pass-external-test-suites (push) Waiting to run
Tools CI / tools-linux (clang) (push) Waiting to run
Tools CI / tools-linux (gcc) (push) Waiting to run
Tools CI / tools-linux (tcc) (push) Waiting to run
Tools CI / tools-macos (clang) (push) Waiting to run
Tools CI / tools-windows (gcc) (push) Waiting to run
Tools CI / tools-windows (msvc) (push) Waiting to run
Tools CI / tools-windows (tcc) (push) Waiting to run
Tools CI / tools-docker-ubuntu-musl (push) Waiting to run
vab CI / vab-compiles-v-examples (push) Waiting to run
vab CI / v-compiles-os-android (push) Waiting to run
wasm backend CI / wasm-backend (ubuntu-22.04) (push) Waiting to run
wasm backend CI / wasm-backend (windows-2022) (push) Waiting to run
Some checks are pending
Graphics CI / gg-regressions (push) Waiting to run
vlib modules CI / build-module-docs (push) Waiting to run
native backend CI / native-backend-ubuntu (push) Waiting to run
native backend CI / native-backend-windows (push) Waiting to run
Shy and PV CI / v-compiles-puzzle-vibes (push) Waiting to run
Sanitized CI / sanitize-undefined-gcc (push) Waiting to run
Sanitized CI / sanitize-undefined-clang (push) Waiting to run
Sanitized CI / tests-sanitize-address-clang (push) Waiting to run
Sanitized CI / sanitize-address-msvc (push) Waiting to run
Sanitized CI / sanitize-address-gcc (push) Waiting to run
Sanitized CI / sanitize-memory-clang (push) Waiting to run
sdl CI / v-compiles-sdl-examples (push) Waiting to run
Time CI / time-linux (push) Waiting to run
Time CI / time-macos (push) Waiting to run
Time CI / time-windows (push) Waiting to run
toml CI / toml-module-pass-external-test-suites (push) Waiting to run
Tools CI / tools-linux (clang) (push) Waiting to run
Tools CI / tools-linux (gcc) (push) Waiting to run
Tools CI / tools-linux (tcc) (push) Waiting to run
Tools CI / tools-macos (clang) (push) Waiting to run
Tools CI / tools-windows (gcc) (push) Waiting to run
Tools CI / tools-windows (msvc) (push) Waiting to run
Tools CI / tools-windows (tcc) (push) Waiting to run
Tools CI / tools-docker-ubuntu-musl (push) Waiting to run
vab CI / vab-compiles-v-examples (push) Waiting to run
vab CI / v-compiles-os-android (push) Waiting to run
wasm backend CI / wasm-backend (ubuntu-22.04) (push) Waiting to run
wasm backend CI / wasm-backend (windows-2022) (push) Waiting to run
This commit is contained in:
parent
2b4253caf9
commit
9a0166701c
5 changed files with 46 additions and 9 deletions
|
@ -3,7 +3,7 @@ module builtin
|
|||
// Note: this file will be removed soon
|
||||
|
||||
// byteptr.vbytes() - makes a V []u8 structure from a C style memory buffer. Note: the data is reused, NOT copied!
|
||||
@[unsafe]
|
||||
@[reused; unsafe]
|
||||
pub fn (data byteptr) vbytes(len int) []u8 {
|
||||
return unsafe { voidptr(data).vbytes(len) }
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ pub fn (data byteptr) vbytes(len int) []u8 {
|
|||
// vstring converts a C style string to a V string. Note: the string data is reused, NOT copied.
|
||||
// strings returned from this function will be normal V strings beside that (i.e. they would be
|
||||
// freed by V's -autofree mechanism, when they are no longer used).
|
||||
@[unsafe]
|
||||
@[reused; unsafe]
|
||||
pub fn (bp byteptr) vstring() string {
|
||||
return string{
|
||||
str: bp
|
||||
|
@ -21,7 +21,7 @@ pub fn (bp byteptr) vstring() string {
|
|||
|
||||
// vstring_with_len converts a C style string to a V string.
|
||||
// Note: the string data is reused, NOT copied.
|
||||
@[unsafe]
|
||||
@[reused; unsafe]
|
||||
pub fn (bp byteptr) vstring_with_len(len int) string {
|
||||
return string{
|
||||
str: bp
|
||||
|
@ -32,7 +32,7 @@ pub fn (bp byteptr) vstring_with_len(len int) string {
|
|||
|
||||
// vstring converts C char* to V string.
|
||||
// Note: the string data is reused, NOT copied.
|
||||
@[unsafe]
|
||||
@[reused; unsafe]
|
||||
pub fn (cp charptr) vstring() string {
|
||||
return string{
|
||||
str: byteptr(cp)
|
||||
|
@ -43,7 +43,7 @@ pub fn (cp charptr) vstring() string {
|
|||
|
||||
// vstring_with_len converts C char* to V string.
|
||||
// Note: the string data is reused, NOT copied.
|
||||
@[unsafe]
|
||||
@[reused; unsafe]
|
||||
pub fn (cp charptr) vstring_with_len(len int) string {
|
||||
return string{
|
||||
str: byteptr(cp)
|
||||
|
@ -59,7 +59,7 @@ pub fn (cp charptr) vstring_with_len(len int) string {
|
|||
// This is suitable for readonly strings, C string literals etc,
|
||||
// that can be read by the V program, but that should not be
|
||||
// managed by it, for example `os.args` is implemented using it.
|
||||
@[unsafe]
|
||||
@[reused; unsafe]
|
||||
pub fn (bp byteptr) vstring_literal() string {
|
||||
return string{
|
||||
str: bp
|
||||
|
@ -70,7 +70,7 @@ pub fn (bp byteptr) vstring_literal() string {
|
|||
|
||||
// vstring_with_len converts a C style string to a V string.
|
||||
// Note: the string data is reused, NOT copied.
|
||||
@[unsafe]
|
||||
@[reused; unsafe]
|
||||
pub fn (bp byteptr) vstring_literal_with_len(len int) string {
|
||||
return string{
|
||||
str: bp
|
||||
|
@ -82,7 +82,7 @@ pub fn (bp byteptr) vstring_literal_with_len(len int) string {
|
|||
// vstring_literal converts C char* to V string.
|
||||
// See also vstring_literal defined on byteptr for more details.
|
||||
// Note: the string data is reused, NOT copied.
|
||||
@[unsafe]
|
||||
@[reused; unsafe]
|
||||
pub fn (cp charptr) vstring_literal() string {
|
||||
return string{
|
||||
str: byteptr(cp)
|
||||
|
@ -94,7 +94,7 @@ pub fn (cp charptr) vstring_literal() string {
|
|||
// vstring_literal_with_len converts C char* to V string.
|
||||
// See also vstring_literal_with_len defined on byteptr.
|
||||
// Note: the string data is reused, NOT copied.
|
||||
@[unsafe]
|
||||
@[reused; unsafe]
|
||||
pub fn (cp charptr) vstring_literal_with_len(len int) string {
|
||||
return string{
|
||||
str: byteptr(cp)
|
||||
|
|
|
@ -98,6 +98,21 @@ fn (mut g Gen) autofree_scope_vars2(scope &ast.Scope, start_pos int, end_pos int
|
|||
if obj.expr is ast.IfGuardExpr {
|
||||
continue
|
||||
}
|
||||
if obj.expr is ast.UnsafeExpr && obj.expr.expr is ast.CallExpr
|
||||
&& (obj.expr.expr as ast.CallExpr).is_method {
|
||||
if left_var := scope.objects[obj.expr.expr.left.str()] {
|
||||
if func := g.table.find_method(g.table.final_sym(left_var.typ),
|
||||
obj.expr.expr.name)
|
||||
{
|
||||
if func.attrs.contains('reused') && left_var is ast.Var
|
||||
&& left_var.expr is ast.CastExpr {
|
||||
if left_var.expr.expr.is_literal() {
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
g.autofree_variable(obj)
|
||||
}
|
||||
else {}
|
||||
|
|
10
vlib/v/gen/c/testdata/autofree_reused.c.must_have
vendored
Normal file
10
vlib/v/gen/c/testdata/autofree_reused.c.must_have
vendored
Normal file
|
@ -0,0 +1,10 @@
|
|||
VV_LOC void main__main(void) {
|
||||
byteptr b = ((byteptr)("a"));
|
||||
Array_u8 s = byteptr_vbytes(b, 1);
|
||||
string _t1 = Array_u8_str(s); println(_t1); string_free(&_t1);
|
||||
;
|
||||
byteptr bb = ((byteptr)("a"));
|
||||
string ss = byteptr_vstring(bb);
|
||||
println(ss);
|
||||
}
|
||||
|
2
vlib/v/gen/c/testdata/autofree_reused.out
vendored
Normal file
2
vlib/v/gen/c/testdata/autofree_reused.out
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
[97]
|
||||
a
|
10
vlib/v/gen/c/testdata/autofree_reused.vv
vendored
Normal file
10
vlib/v/gen/c/testdata/autofree_reused.vv
vendored
Normal file
|
@ -0,0 +1,10 @@
|
|||
// vtest vflags: -autofree
|
||||
fn main() {
|
||||
b := byteptr(c'a')
|
||||
s := unsafe { b.vbytes(1) }
|
||||
println(s)
|
||||
|
||||
bb := byteptr(c'a')
|
||||
ss := unsafe { bb.vstring() }
|
||||
println(ss)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue