2013-06-02 17:10:11 +00:00
|
|
|
// run
|
2011-06-17 20:12:14 +00:00
|
|
|
|
2016-04-10 21:32:26 +00:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
2011-06-17 20:12:14 +00:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import "unsafe"
|
|
|
|
|
|
|
|
type T struct {
|
|
|
|
X int
|
|
|
|
}
|
|
|
|
|
|
|
|
var t T
|
|
|
|
|
|
|
|
func isUintptr(uintptr) {}
|
|
|
|
|
2013-04-05 19:24:07 +00:00
|
|
|
type T2 struct {
|
|
|
|
A int32
|
|
|
|
U2
|
|
|
|
}
|
|
|
|
|
|
|
|
type U2 struct {
|
|
|
|
B int32
|
|
|
|
C int32
|
|
|
|
}
|
|
|
|
|
|
|
|
var t2 T2
|
|
|
|
var p2 *T2
|
|
|
|
|
2011-06-17 20:12:14 +00:00
|
|
|
func main() {
|
2013-04-05 19:24:07 +00:00
|
|
|
// Test unsafe.Sizeof, unsafe.Alignof, and unsafe.Offsetof all return uintptr.
|
2011-06-17 20:12:14 +00:00
|
|
|
isUintptr(unsafe.Sizeof(t))
|
|
|
|
isUintptr(unsafe.Alignof(t))
|
|
|
|
isUintptr(unsafe.Offsetof(t.X))
|
2013-04-05 19:24:07 +00:00
|
|
|
|
|
|
|
// Test correctness of Offsetof with respect to embedded fields (issue 4909).
|
|
|
|
if unsafe.Offsetof(t2.C) != 8 {
|
|
|
|
println(unsafe.Offsetof(t2.C), "!= 8")
|
|
|
|
panic("unsafe.Offsetof(t2.C) != 8")
|
|
|
|
}
|
|
|
|
if unsafe.Offsetof(p2.C) != 8 {
|
|
|
|
println(unsafe.Offsetof(p2.C), "!= 8")
|
|
|
|
panic("unsafe.Offsetof(p2.C) != 8")
|
|
|
|
}
|
|
|
|
if unsafe.Offsetof(t2.U2.C) != 4 {
|
|
|
|
println(unsafe.Offsetof(t2.U2.C), "!= 4")
|
|
|
|
panic("unsafe.Offsetof(t2.U2.C) != 4")
|
|
|
|
}
|
|
|
|
if unsafe.Offsetof(p2.U2.C) != 4 {
|
|
|
|
println(unsafe.Offsetof(p2.U2.C), "!= 4")
|
|
|
|
panic("unsafe.Offsetof(p2.U2.C) != 4")
|
|
|
|
}
|
|
|
|
testDeep()
|
|
|
|
testNotEmbedded()
|
|
|
|
}
|
|
|
|
|
|
|
|
type (
|
|
|
|
S1 struct {
|
2013-06-02 17:10:11 +00:00
|
|
|
A int64
|
2013-04-05 19:24:07 +00:00
|
|
|
S2
|
|
|
|
}
|
|
|
|
S2 struct {
|
2013-06-02 17:10:11 +00:00
|
|
|
B int64
|
2013-04-05 19:24:07 +00:00
|
|
|
S3
|
|
|
|
}
|
|
|
|
S3 struct {
|
2013-06-02 17:10:11 +00:00
|
|
|
C int64
|
2013-04-05 19:24:07 +00:00
|
|
|
S4
|
|
|
|
}
|
|
|
|
S4 struct {
|
2013-06-02 17:10:11 +00:00
|
|
|
D int64
|
2013-04-05 19:24:07 +00:00
|
|
|
S5
|
|
|
|
}
|
|
|
|
S5 struct {
|
2013-06-02 17:10:11 +00:00
|
|
|
E int64
|
2013-04-05 19:24:07 +00:00
|
|
|
S6
|
|
|
|
}
|
|
|
|
S6 struct {
|
2013-06-02 17:10:11 +00:00
|
|
|
F int64
|
2013-04-05 19:24:07 +00:00
|
|
|
S7
|
|
|
|
}
|
|
|
|
S7 struct {
|
2013-06-02 17:10:11 +00:00
|
|
|
G int64
|
2013-04-05 19:24:07 +00:00
|
|
|
S8
|
|
|
|
}
|
|
|
|
S8 struct {
|
2013-06-02 17:10:11 +00:00
|
|
|
H int64
|
2013-04-05 19:24:07 +00:00
|
|
|
*S1
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func testDeep() {
|
|
|
|
var s1 S1
|
|
|
|
switch {
|
|
|
|
case unsafe.Offsetof(s1.A) != 0:
|
|
|
|
panic("unsafe.Offsetof(s1.A) != 0")
|
2013-06-02 17:10:11 +00:00
|
|
|
case unsafe.Offsetof(s1.B) != 8:
|
|
|
|
panic("unsafe.Offsetof(s1.B) != 8")
|
|
|
|
case unsafe.Offsetof(s1.C) != 16:
|
|
|
|
panic("unsafe.Offsetof(s1.C) != 16")
|
|
|
|
case unsafe.Offsetof(s1.D) != 24:
|
|
|
|
panic("unsafe.Offsetof(s1.D) != 24")
|
|
|
|
case unsafe.Offsetof(s1.E) != 32:
|
|
|
|
panic("unsafe.Offsetof(s1.E) != 32")
|
|
|
|
case unsafe.Offsetof(s1.F) != 40:
|
|
|
|
panic("unsafe.Offsetof(s1.F) != 40")
|
|
|
|
case unsafe.Offsetof(s1.G) != 48:
|
|
|
|
panic("unsafe.Offsetof(s1.G) != 48")
|
|
|
|
case unsafe.Offsetof(s1.H) != 56:
|
|
|
|
panic("unsafe.Offsetof(s1.H) != 56")
|
|
|
|
case unsafe.Offsetof(s1.S1) != 64:
|
|
|
|
panic("unsafe.Offsetof(s1.S1) != 64")
|
|
|
|
case unsafe.Offsetof(s1.S1.S2.S3.S4.S5.S6.S7.S8.S1.S2) != 8:
|
|
|
|
panic("unsafe.Offsetof(s1.S1.S2.S3.S4.S5.S6.S7.S8.S1.S2) != 8")
|
2013-04-05 19:24:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testNotEmbedded() {
|
|
|
|
type T2 struct {
|
|
|
|
B int32
|
|
|
|
C int32
|
|
|
|
}
|
|
|
|
type T1 struct {
|
|
|
|
A int32
|
|
|
|
T2
|
|
|
|
}
|
|
|
|
type T struct {
|
|
|
|
Dummy int32
|
|
|
|
F T1
|
|
|
|
P *T1
|
|
|
|
}
|
|
|
|
|
|
|
|
var t T
|
|
|
|
var p *T
|
|
|
|
switch {
|
|
|
|
case unsafe.Offsetof(t.F.B) != 4:
|
|
|
|
panic("unsafe.Offsetof(t.F.B) != 4")
|
|
|
|
case unsafe.Offsetof(t.F.C) != 8:
|
|
|
|
panic("unsafe.Offsetof(t.F.C) != 8")
|
|
|
|
|
|
|
|
case unsafe.Offsetof(t.P.B) != 4:
|
|
|
|
panic("unsafe.Offsetof(t.P.B) != 4")
|
|
|
|
case unsafe.Offsetof(t.P.C) != 8:
|
|
|
|
panic("unsafe.Offsetof(t.P.C) != 8")
|
|
|
|
|
|
|
|
case unsafe.Offsetof(p.F.B) != 4:
|
|
|
|
panic("unsafe.Offsetof(p.F.B) != 4")
|
|
|
|
case unsafe.Offsetof(p.F.C) != 8:
|
|
|
|
panic("unsafe.Offsetof(p.F.C) != 8")
|
|
|
|
|
|
|
|
case unsafe.Offsetof(p.P.B) != 4:
|
|
|
|
panic("unsafe.Offsetof(p.P.B) != 4")
|
|
|
|
case unsafe.Offsetof(p.P.C) != 8:
|
|
|
|
panic("unsafe.Offsetof(p.P.C) != 8")
|
|
|
|
}
|
2011-06-17 20:12:14 +00:00
|
|
|
}
|