Files
golua/_example/alloc.go
T

56 lines
1.0 KiB
Go

package main
import (
"fmt"
"unsafe"
"github.com/vxcontrol/golua/lua"
)
var refHolder = map[unsafe.Pointer][]byte{}
// AllocatorF a terrible allocator!
//meant to be illustrative of the mechanics,
//not usable as an actual implementation
func AllocatorF(ptr unsafe.Pointer, osize uint, nsize uint) unsafe.Pointer {
if nsize == 0 {
if _, ok := refHolder[ptr]; ok {
delete(refHolder, ptr)
}
ptr = unsafe.Pointer(nil)
} else if osize != nsize {
slice := make([]byte, nsize)
if oldslice, ok := refHolder[ptr]; ok {
copy(slice, oldslice)
_ = oldslice
delete(refHolder, ptr)
}
ptr = unsafe.Pointer(&(slice[0]))
refHolder[ptr] = slice
}
return ptr
}
// A2 a wrapper to allocator
func A2(ptr unsafe.Pointer, osize uint, nsize uint) unsafe.Pointer {
return AllocatorF(ptr, osize, nsize)
}
func main() {
L := lua.NewStateAlloc(AllocatorF)
defer L.Close()
L.OpenLibs()
L.SetAllocf(A2)
for i := 0; i < 10; i++ {
L.GetGlobal("print")
L.PushString("Hello World!")
L.Call(1, 0)
}
fmt.Println(len(refHolder))
}