Debian Codegraph @

test/escape_sync_atomic.go

raw
1 // errorcheck -0 -m -l
2
3 // Copyright 2019 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
6
7 // Test escape analysis for sync/atomic.
8
9 package escape
10
11 import (
12 "sync/atomic"
13 "unsafe"
14 )
15
16 // BAD: should be "leaking param: addr to result ~r1 level=1$".
17 func LoadPointer(addr *unsafe.Pointer) unsafe.Pointer { // ERROR "leaking param: addr$"
18 return atomic.LoadPointer(addr)
19 }
20
21 var ptr unsafe.Pointer
22
23 func StorePointer() {
24 var x int // ERROR "moved to heap: x"
25 atomic.StorePointer(&ptr, unsafe.Pointer(&x))
26 }
27
28 func SwapPointer() {
29 var x int // ERROR "moved to heap: x"
30 atomic.SwapPointer(&ptr, unsafe.Pointer(&x))
31 }
32
33 func CompareAndSwapPointer() {
34 // BAD: x doesn't need to be heap allocated
35 var x int // ERROR "moved to heap: x"
36 var y int // ERROR "moved to heap: y"
37 atomic.CompareAndSwapPointer(&ptr, unsafe.Pointer(&x), unsafe.Pointer(&y))
38 }