-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathreflect.go
40 lines (34 loc) · 883 Bytes
/
reflect.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
package kiwi
import (
"fmt"
"reflect"
"unsafe"
)
func getDataAddr(v reflect.Value) uintptr {
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
switch v.Kind() {
case reflect.Slice:
sh := (*reflect.SliceHeader)(unsafe.Pointer(v.UnsafeAddr()))
return sh.Data
default:
return reflect.Indirect(v).UnsafeAddr()
}
}
func getDataSize(v reflect.Value) uintptr {
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
switch v.Kind() {
case reflect.Slice, reflect.Array:
size := v.Type().Elem().Size()
return size * uintptr(v.Len())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Float32, reflect.Float64, reflect.Bool:
size := v.Type().Size()
return size
default:
panic(fmt.Sprintf("dataSize: Unsupported type: %s", reflect.TypeOf(v).String()))
}
}