-
Notifications
You must be signed in to change notification settings - Fork 5
/
mem.go
56 lines (46 loc) · 1.19 KB
/
mem.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
package copy
import (
"reflect"
"unsafe"
)
func memcopy(dst, src unsafe.Pointer, size int) {
var srcSlice []byte
srcSH := (*reflect.SliceHeader)((unsafe.Pointer(&srcSlice)))
srcSH.Data = uintptr(src)
srcSH.Cap = int(size)
srcSH.Len = int(size)
var dstSlice []byte
dstSH := (*reflect.SliceHeader)((unsafe.Pointer(&dstSlice)))
dstSH.Data = uintptr(dst)
dstSH.Cap = int(size)
dstSH.Len = int(size)
copy(dstSlice, srcSlice)
}
func alloc(size int) unsafe.Pointer {
if size == 0 {
return nil
}
size = (size + 7) / 8 // size in int64
return unsafe.Pointer(&(make([]int64, size)[0]))
}
type slice struct {
data unsafe.Pointer
size uintptr
Len int
}
func sliceAt(ptr unsafe.Pointer, size uintptr) slice {
s := (*reflect.SliceHeader)(ptr)
return slice{data: unsafe.Pointer(s.Data), size: size, Len: s.Len}
}
func makeSliceAt(ptr unsafe.Pointer, size uintptr, len int) slice {
s := (*reflect.SliceHeader)(ptr)
if s.Cap < len || s.Cap > len*2 {
s.Data = uintptr(alloc(int(size) * len))
s.Cap = len
}
s.Len = len
return slice{data: unsafe.Pointer(s.Data), size: size, Len: s.Len}
}
func (s slice) Index(i int) unsafe.Pointer {
return unsafe.Pointer(uintptr(s.data) + s.size*uintptr(i))
}