-
Notifications
You must be signed in to change notification settings - Fork 0
/
unsafe.go
42 lines (33 loc) · 954 Bytes
/
unsafe.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
// +build !js
package sindex
import (
"reflect"
"unsafe"
)
type unsafeSlice struct {
size uintptr
base unsafe.Pointer
header unsafe.Pointer
}
func setUnsafeSliceBase(s *List) {
s.size = s.sliceV.Type().Elem().Size()
if s.sliceV.Cap() > 0 {
oldLen := s.sliceV.Len()
s.sliceV.SetLen(1)
s.base = unsafe.Pointer(s.sliceV.Index(0).Addr().Pointer())
s.sliceV.SetLen(oldLen)
}
s.header = unsafe.Pointer(s.sliceV.Addr().Pointer())
}
func copySlice(s *List, dstI, srcI, len int) {
lenBytes := int(s.size * uintptr(len))
data := uintptr(s.base) + s.size*uintptr(dstI)
dst := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{Data: data, Len: lenBytes, Cap: lenBytes}))
data = uintptr(s.base) + s.size*uintptr(srcI)
src := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{Data: data, Len: lenBytes, Cap: lenBytes}))
copy(dst, src)
}
func setSliceLen(s *List, len int) {
header := (*reflect.SliceHeader)(s.header)
header.Len = len
}