| 1 |
// errorcheck -+ -0 -l -d=wb |
| 2 |
|
| 3 |
// Copyright 2016 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 write barrier elimination for notinheap. |
| 8 |
|
| 9 |
//go:build cgo |
| 10 |
|
| 11 |
package p |
| 12 |
|
| 13 |
import "runtime/cgo" |
| 14 |
|
| 15 |
type t1 struct { |
| 16 |
x *nih |
| 17 |
s []nih |
| 18 |
y [1024]byte // Prevent write decomposition |
| 19 |
} |
| 20 |
|
| 21 |
type t2 struct { |
| 22 |
x *ih |
| 23 |
s []ih |
| 24 |
y [1024]byte |
| 25 |
} |
| 26 |
|
| 27 |
type nih struct { |
| 28 |
_ cgo.Incomplete |
| 29 |
x uintptr |
| 30 |
} |
| 31 |
|
| 32 |
type ih struct { // In-heap type |
| 33 |
x uintptr |
| 34 |
} |
| 35 |
|
| 36 |
var ( |
| 37 |
v1 t1 |
| 38 |
v2 t2 |
| 39 |
|
| 40 |
v1s []t1 |
| 41 |
v2s []t2 |
| 42 |
) |
| 43 |
|
| 44 |
func f() { |
| 45 |
// Test direct writes |
| 46 |
v1.x = nil // no barrier |
| 47 |
v2.x = nil // ERROR "write barrier" |
| 48 |
v1.s = []nih(nil) // no barrier |
| 49 |
v2.s = []ih(nil) // ERROR "write barrier" |
| 50 |
} |
| 51 |
|
| 52 |
func g() { |
| 53 |
// Test aggregate writes |
| 54 |
v1 = t1{x: nil} // no barrier |
| 55 |
v2 = t2{x: nil} // ERROR "write barrier" |
| 56 |
} |
| 57 |
|
| 58 |
func h() { |
| 59 |
// Test copies and appends. |
| 60 |
copy(v1s, v1s[1:]) // no barrier |
| 61 |
copy(v2s, v2s[1:]) // ERROR "write barrier" |
| 62 |
_ = append(v1s, v1s...) // no barrier |
| 63 |
_ = append(v2s, v2s...) // ERROR "write barrier" |
| 64 |
} |
| 65 |
|
| 66 |
// Slice clearing |
| 67 |
|
| 68 |
var ( |
| 69 |
sliceIH []*ih |
| 70 |
sliceNIH []*nih |
| 71 |
) |
| 72 |
|
| 73 |
func sliceClear() { |
| 74 |
for i := range sliceIH { |
| 75 |
sliceIH[i] = nil // ERROR "write barrier" |
| 76 |
} |
| 77 |
for i := range sliceNIH { |
| 78 |
sliceNIH[i] = nil // no barrier |
| 79 |
} |
| 80 |
} |