Migrated util.go and main.go to the new pkgui convention and C file. Also replaced C.CBytes() with C.malloc() (this bumps our minimum version requirement to 1.8, but it's better than keeping a massive slice around at all times).
This commit is contained in:
parent
62ac252773
commit
766f9ed028
|
@ -1,45 +0,0 @@
|
||||||
// 12 december 2015
|
|
||||||
|
|
||||||
package ui
|
|
||||||
|
|
||||||
import (
|
|
||||||
"unsafe"
|
|
||||||
)
|
|
||||||
|
|
||||||
// #include <stdlib.h>
|
|
||||||
// #include "util.h"
|
|
||||||
import "C"
|
|
||||||
|
|
||||||
// We want Go itself to complain when we're out of memory.
|
|
||||||
// The allocators in cgo *should* do this, but there isn't a
|
|
||||||
// C.CMalloc(). There *is* a C.CBytes(), however, for transferring
|
|
||||||
// binary blobs from Go to C. If we pass this an arbitrary slice
|
|
||||||
// of the desired length, we get our C.CMalloc(). Using a slice
|
|
||||||
// that's always initialized to zero gives us the memset(0)
|
|
||||||
// (or ZeroMemory()) for free.
|
|
||||||
var allocBytes = make([]byte, 1024) // 1024 bytes first
|
|
||||||
|
|
||||||
//export pkguiAlloc
|
|
||||||
func pkguiAlloc(n C.size_t) unsafe.Pointer {
|
|
||||||
if n > C.size_t(len(allocBytes)) {
|
|
||||||
// TODO round n up to a multiple of a power of 2?
|
|
||||||
// for instance 0x1234 bytes -> 0x1800 bytes
|
|
||||||
allocBytes = make([]byte, n)
|
|
||||||
}
|
|
||||||
return C.CBytes(allocBytes[:n])
|
|
||||||
}
|
|
||||||
|
|
||||||
func freestr(str *C.char) {
|
|
||||||
C.free(unsafe.Pointer(str))
|
|
||||||
}
|
|
||||||
|
|
||||||
func tobool(b C.int) bool {
|
|
||||||
return b != 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func frombool(b bool) C.int {
|
|
||||||
if b {
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
|
|
@ -9,12 +9,7 @@ import (
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
// #include "ui.h"
|
// #include "pkgui.h"
|
||||||
// extern void doQueueMain(void *);
|
|
||||||
// extern int doOnShouldQuit(void *);
|
|
||||||
// // see golang/go#19835
|
|
||||||
// typedef void (*queueMainCallback)(void *);
|
|
||||||
// typedef int (*onShouldQuitCallback)(void *);
|
|
||||||
import "C"
|
import "C"
|
||||||
|
|
||||||
// make sure main() runs on the first thread created by the OS
|
// make sure main() runs on the first thread created by the OS
|
||||||
|
@ -33,15 +28,15 @@ func init() {
|
||||||
// nil. If package ui fails to initialize, Main returns an appropriate
|
// nil. If package ui fails to initialize, Main returns an appropriate
|
||||||
// error.
|
// error.
|
||||||
func Main(f func()) error {
|
func Main(f func()) error {
|
||||||
// TODO HEAP SAFETY
|
opts := C.pkguiAllocInitOptions()
|
||||||
opts := C.uiInitOptions{}
|
estr := C.uiInit(opts)
|
||||||
estr := C.uiInit(&opts)
|
C.pkguiFreeInitOptions(opts)
|
||||||
if estr != nil {
|
if estr != nil {
|
||||||
err := errors.New(C.GoString(estr))
|
err := errors.New(C.GoString(estr))
|
||||||
C.uiFreeInitError(estr)
|
C.uiFreeInitError(estr)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
C.uiOnShouldQuit(C.onShouldQuitCallback(C.doOnShouldQuit), nil)
|
C.pkguiOnShouldQuit()
|
||||||
QueueMain(f)
|
QueueMain(f)
|
||||||
C.uiMain()
|
C.uiMain()
|
||||||
return nil
|
return nil
|
||||||
|
@ -90,11 +85,11 @@ func QueueMain(f func()) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
qmmap[n] = f
|
qmmap[n] = f
|
||||||
C.uiQueueMain(C.queueMainCallback(C.doQueueMain), unsafe.Pointer(n))
|
C.pkguiQueueMain(C.uintptr_t(n))
|
||||||
}
|
}
|
||||||
|
|
||||||
//export doQueueMain
|
//export pkguiDoQueueMain
|
||||||
func doQueueMain(nn unsafe.Pointer) {
|
func pkguiDoQueueMain(nn unsafe.Pointer) {
|
||||||
qmlock.Lock()
|
qmlock.Lock()
|
||||||
|
|
||||||
n := uintptr(nn)
|
n := uintptr(nn)
|
||||||
|
@ -121,8 +116,8 @@ func OnShouldQuit(f func() bool) {
|
||||||
shouldQuitFunc = f
|
shouldQuitFunc = f
|
||||||
}
|
}
|
||||||
|
|
||||||
//export doOnShouldQuit
|
//export pkguiDoOnShouldQuit
|
||||||
func doOnShouldQuit(unused unsafe.Pointer) C.int {
|
func pkguiDoOnShouldQuit(unused unsafe.Pointer) C.int {
|
||||||
if shouldQuitFunc == nil {
|
if shouldQuitFunc == nil {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
// 26 august 2018
|
||||||
|
#include "pkgui.h"
|
||||||
|
#include "xxxxx"
|
||||||
|
|
||||||
|
uiInitOptions *pkguiAllocInitOptions(void)
|
||||||
|
{
|
||||||
|
return (uiInitOptions *) pkguiAlloc(sizeof (uiInitOptions));
|
||||||
|
}
|
||||||
|
|
||||||
|
void pkguiFreeInitOptions(uiInitOptions *o)
|
||||||
|
{
|
||||||
|
free(o);
|
||||||
|
}
|
||||||
|
|
||||||
|
void pkguiQueueMain(uintptr_t n)
|
||||||
|
{
|
||||||
|
uiQueueMain(pkguiDoQueueMain, (void *) n);
|
||||||
|
}
|
||||||
|
|
||||||
|
void pkguiOnShouldQuit(void)
|
||||||
|
{
|
||||||
|
uiOnShouldQuit(pkguiDoOnShouldQuit, NULL);
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
// 12 august 2018
|
||||||
|
#include <stdio.h>
|
||||||
|
#include "ui.h"
|
||||||
|
|
||||||
|
// main.go
|
||||||
|
extern uiInitOptions *pkguiAllocInitOptions(void);
|
||||||
|
extern void pkguiFreeInitOptions(uiInitOptions *o);
|
||||||
|
extern void pkguiQueueMain(uintptr_t n);
|
||||||
|
extern void pkguiOnShouldQuit(void);
|
|
@ -0,0 +1,31 @@
|
||||||
|
// 12 december 2015
|
||||||
|
|
||||||
|
package ui
|
||||||
|
|
||||||
|
import (
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
// #include "pkgui.h"
|
||||||
|
import "C"
|
||||||
|
|
||||||
|
//export pkguiAlloc
|
||||||
|
func pkguiAlloc(n C.size_t) unsafe.Pointer {
|
||||||
|
// cgo turns C.malloc() into a panic-on-OOM version; use it
|
||||||
|
return C.malloc(n)
|
||||||
|
}
|
||||||
|
|
||||||
|
func freestr(str *C.char) {
|
||||||
|
C.free(unsafe.Pointer(str))
|
||||||
|
}
|
||||||
|
|
||||||
|
func tobool(b C.int) bool {
|
||||||
|
return b != 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func frombool(b bool) C.int {
|
||||||
|
if b {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
Loading…
Reference in New Issue