-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstruct_align_demo.go
89 lines (77 loc) · 1.62 KB
/
struct_align_demo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
// struct for demo
// structlayout -json github.com/NewbMiao/Dig101-Go Ag|structlayout-svg -t "align-guarantee" > ag.svg.
type Ag struct {
arr [2]int8 // 2
bl bool // 1 padding 5
sl []int16 // 24
ptr *int64 // 8
st struct { // 16
str string
}
m map[string]int16
i interface{}
}
// structlayout -json github.com/NewbMiao/Dig101-Go tooMuchPadding|structlayout-optimize -r.
type tooMuchPadding struct {
i16 int16
i64 int64
i8 int8
i32 int32
ptr *string
b bool
}
// tooMuchPadding optimized.
type optimized struct {
i64 int64
ptr *string
i32 int32
i16 int16
i8 int8
b bool
}
// 64word align gurrantee on 32-bit arch
// GOARCH=386 structlayout -json github.com/NewbMiao/Dig101-Go c2 | structlayout-svg -t "int64 first field" > i64_first.svg.
type c2 struct {
val int64 // pos 0
val2 int64 // pos 8
valid bool // pos 16
}
type T struct {
val2 int64
_ int16
}
type c3 struct {
val T
valid bool
}
type c4 struct {
val int64 // pos 0
valid bool // pos 8
// 或者 _ uint32
_ [4]byte // pos 9; to correct padding one more 4bytes
val2 int64 // pos 16
}
type c5 struct {
val int64
valid bool
// the first element in slices of 64-bit
// elements will be correctly aligned
// 此处切片相当指针,数据是指向底层开辟的64位字数组,如c1
val2 []int64
}
type c51 struct {
val int64
valid bool
// the first element in slices of 64-bit
// elements will be correctly aligned
// 此处切片相当指针,数据是指向底层开辟的64位字数组,如c1
val2 [2]int64
}
type c6 struct {
val int64
valid bool
val2 *int64
}
func main() {
}