forked from porthos-rpc/porthos-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcorrelation.go
37 lines (34 loc) · 809 Bytes
/
correlation.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
package porthos
import (
"encoding/binary"
"fmt"
"unsafe"
)
// UintptrToBytes converts a pointer to a bytes array.
func UintptrToBytes(value uintptr) []byte {
size := unsafe.Sizeof(value)
b := make([]byte, size)
switch size {
case 4:
binary.LittleEndian.PutUint32(b, uint32(value))
case 8:
binary.LittleEndian.PutUint64(b, uint64(value))
default:
panic(fmt.Sprintf("unknown uintptr size: %v", size))
}
return b
}
// BytesToUintptr converts a bytes array to a pointer.
func BytesToUintptr(bytes []byte) uintptr {
var intptr uintptr
size := unsafe.Sizeof(intptr)
switch size {
case 4:
intptr = uintptr(binary.LittleEndian.Uint32(bytes))
case 8:
intptr = uintptr(binary.LittleEndian.Uint64(bytes))
default:
panic(fmt.Sprintf("unknown uintptr size: %v", size))
}
return intptr
}