forked from yuin/gopher-lua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alloc.go
64 lines (56 loc) · 1.53 KB
/
alloc.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
package lua
import (
"reflect"
"unsafe"
)
// iface is an internal representation of the go-interface.
type iface struct {
itab unsafe.Pointer
word unsafe.Pointer
}
var _fv float64
var _uv uintptr
// allocator is a fast bulk memory allocator for the LValue.
type allocator struct {
top int
size int
nptrs []LValue
nheader *reflect.SliceHeader
fptrs []float64
fheader *reflect.SliceHeader
itabLNumber unsafe.Pointer
}
func newAllocator(size int) *allocator {
al := &allocator{
top: 0,
size: size,
nptrs: make([]LValue, size),
nheader: nil,
fptrs: make([]float64, size),
fheader: nil,
itabLNumber: unsafe.Pointer(nil),
}
al.nheader = (*reflect.SliceHeader)(unsafe.Pointer(&al.nptrs))
al.fheader = (*reflect.SliceHeader)(unsafe.Pointer(&al.fptrs))
var v LValue = LNumber(0)
vp := (*iface)(unsafe.Pointer(&v))
al.itabLNumber = vp.itab
return al
}
func (al *allocator) LNumber2I(v LNumber) LValue {
if al.top == len(al.nptrs)-1 {
al.top = 0
al.nptrs = make([]LValue, al.size)
al.nheader = (*reflect.SliceHeader)(unsafe.Pointer(&al.nptrs))
al.fptrs = make([]float64, al.size)
al.fheader = (*reflect.SliceHeader)(unsafe.Pointer(&al.fptrs))
}
fptr := (*float64)(unsafe.Pointer(al.fheader.Data + uintptr(al.top)*unsafe.Sizeof(_fv)))
e := *(*LValue)(unsafe.Pointer(al.nheader.Data + uintptr(al.top)*unsafe.Sizeof(_uv)))
al.top++
ep := (*iface)(unsafe.Pointer(&e))
ep.itab = al.itabLNumber
*fptr = float64(v)
ep.word = unsafe.Pointer(fptr)
return e
}