-
Notifications
You must be signed in to change notification settings - Fork 1
/
memory.go
139 lines (125 loc) · 2.68 KB
/
memory.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// SizeStruct project sizestruct.go
package btcec
import (
"fmt"
"reflect"
)
type sStruct struct {
npm map[uintptr]bool
exNum int
}
func SizeOf(data interface{}) int {
var npm = &sStruct{make(map[uintptr]bool), 0}
num := npm.sizeof(reflect.ValueOf(data))
return num //+ npm.exNum
}
func SizeTOf(data interface{}) int {
var npm = &sStruct{make(map[uintptr]bool), 0}
num := npm.sizeof(reflect.ValueOf(data))
return num + npm.exNum
}
func (s *sStruct) sizeof(v reflect.Value) int {
switch v.Kind() {
case reflect.Map:
sum := 0
keys := v.MapKeys()
for i := 0; i < len(keys); i++ {
mapkey := keys[i]
num := s.sizeof(mapkey)
if num < 0 {
return -1
}
sum += num
num = s.sizeof(v.MapIndex(mapkey))
if num < 0 {
return -1
}
sum += num
}
s.exNum += int(v.Type().Size())
return sum
case reflect.Slice:
sum := 0
for i, n := 0, v.Len(); i < n; i++ {
num := s.sizeof(v.Index(i))
if num < 0 {
return -1
}
sum += num
}
s.exNum += int(v.Type().Size())
return sum
case reflect.Array:
sum := 0
for i, n := 0, v.Len(); i < n; i++ {
num := s.sizeof(v.Index(i))
if num < 0 {
return -1
}
sum += num
}
return sum
case reflect.String:
sum := 0
for i, n := 0, v.Len(); i < n; i++ {
num := s.sizeof(v.Index(i))
if num < 0 {
return -1
}
sum += num
}
s.exNum += int(v.Type().Size())
return sum
case reflect.Ptr:
s.exNum += int(v.Type().Size())
if v.IsNil() {
return 0
}
//fmt.Println(v.Pointer())
if _, ok := s.npm[v.Pointer()]; ok {
return 0
} else {
s.npm[v.Pointer()] = true
}
return s.sizeof(v.Elem())
case reflect.Interface:
s.exNum += int(v.Type().Size())
if v.IsNil() {
return 0
}
return s.sizeof(v.Elem())
case reflect.Uintptr: //Don't think it's Pointer 不认为是指针
return int(v.Type().Size())
case reflect.UnsafePointer: //Don't think it's Pointer 不认为是指针
return int(v.Type().Size())
case reflect.Struct:
sum := 0
for i, n := 0, v.NumField(); i < n; i++ {
if v.Type().Field(i).Tag.Get("ss") == "-" {
continue
}
num := s.sizeof(v.Field(i))
if num < 0 {
return -1
}
sum += num
}
return sum
case reflect.Func, reflect.Chan:
s.exNum += int(v.Type().Size())
if v.IsNil() {
return 0
}
return 0 //Temporary non handling func,chan.
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128,
reflect.Int:
return int(v.Type().Size())
case reflect.Bool:
return int(v.Type().Size())
default:
fmt.Println("t.Kind() no found:", v.Kind())
}
return -1
}