-
Notifications
You must be signed in to change notification settings - Fork 21
/
gc_data.go
57 lines (48 loc) · 1.44 KB
/
gc_data.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
package main
import (
"fmt"
"reflect"
"unsafe"
)
// 前置条件: go.1.18之后 修改源码包
// /Users/python/go/go1.18/src/reflect/type.go
/*
package reflect
// 导出rtype
type Rtype = rtype
*/
func bar() []*int {
t := make([]*int, 8)
return t
}
func main() {
t := bar()
v := reflect.TypeOf(t)
rtyp, ok := v.(*reflect.Rtype)
if !ok {
println("error")
return
}
r := (*rtype)(unsafe.Pointer(rtyp))
fmt.Printf("%#v\n", *r)
fmt.Printf("*gcdata = %d\n", *(r.gcdata))
}
type tflag uint8
type nameOff int32 // offset to a name
type typeOff int32 // offset to an *rtype
// gcdata也是非导出字段并且是一个指针,我们要想对其解引用,我们这里又在本地定义了一个本地rtype类型,用于输出gcdata指向的内存的值。
type rtype struct {
size uintptr
ptrdata uintptr // number of bytes in the type that can contain pointers
hash uint32 // hash of type; avoids computation in hash tables
tflag tflag // extra type information flags
align uint8 // alignment of variable with this type
fieldAlign uint8 // alignment of struct field with this type
kind uint8 // enumeration for C
// function for comparing objects of this type
// (ptr to object A, ptr to object B) -> ==?
equal func(unsafe.Pointer, unsafe.Pointer) bool
gcdata *byte // garbage collection data
str nameOff // string form
ptrToThis typeOff // type for pointer to this type, may be zero
}