Skip to content

Commit 766f9ed

Browse files
committed
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).
1 parent 62ac252 commit 766f9ed

File tree

6 files changed

+73
-63
lines changed

6 files changed

+73
-63
lines changed

BBB_GOFILES/util.go

Lines changed: 0 additions & 45 deletions
This file was deleted.

BBB_GOFILES/main.go renamed to main.go

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@ import (
99
"unsafe"
1010
)
1111

12-
// #include "ui.h"
13-
// extern void doQueueMain(void *);
14-
// extern int doOnShouldQuit(void *);
15-
// // see golang/go#19835
16-
// typedef void (*queueMainCallback)(void *);
17-
// typedef int (*onShouldQuitCallback)(void *);
12+
// #include "pkgui.h"
1813
import "C"
1914

2015
// make sure main() runs on the first thread created by the OS
@@ -33,15 +28,15 @@ func init() {
3328
// nil. If package ui fails to initialize, Main returns an appropriate
3429
// error.
3530
func Main(f func()) error {
36-
// TODO HEAP SAFETY
37-
opts := C.uiInitOptions{}
38-
estr := C.uiInit(&opts)
31+
opts := C.pkguiAllocInitOptions()
32+
estr := C.uiInit(opts)
33+
C.pkguiFreeInitOptions(opts)
3934
if estr != nil {
4035
err := errors.New(C.GoString(estr))
4136
C.uiFreeInitError(estr)
4237
return err
4338
}
44-
C.uiOnShouldQuit(C.onShouldQuitCallback(C.doOnShouldQuit), nil)
39+
C.pkguiOnShouldQuit()
4540
QueueMain(f)
4641
C.uiMain()
4742
return nil
@@ -90,11 +85,11 @@ func QueueMain(f func()) {
9085
}
9186
}
9287
qmmap[n] = f
93-
C.uiQueueMain(C.queueMainCallback(C.doQueueMain), unsafe.Pointer(n))
88+
C.pkguiQueueMain(C.uintptr_t(n))
9489
}
9590

96-
//export doQueueMain
97-
func doQueueMain(nn unsafe.Pointer) {
91+
//export pkguiDoQueueMain
92+
func pkguiDoQueueMain(nn unsafe.Pointer) {
9893
qmlock.Lock()
9994

10095
n := uintptr(nn)
@@ -121,8 +116,8 @@ func OnShouldQuit(f func() bool) {
121116
shouldQuitFunc = f
122117
}
123118

124-
//export doOnShouldQuit
125-
func doOnShouldQuit(unused unsafe.Pointer) C.int {
119+
//export pkguiDoOnShouldQuit
120+
func pkguiDoOnShouldQuit(unused unsafe.Pointer) C.int {
126121
if shouldQuitFunc == nil {
127122
return 0
128123
}

pkgui.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// 26 august 2018
2+
#include "pkgui.h"
3+
#include "xxxxx"
4+
5+
uiInitOptions *pkguiAllocInitOptions(void)
6+
{
7+
return (uiInitOptions *) pkguiAlloc(sizeof (uiInitOptions));
8+
}
9+
10+
void pkguiFreeInitOptions(uiInitOptions *o)
11+
{
12+
free(o);
13+
}
14+
15+
void pkguiQueueMain(uintptr_t n)
16+
{
17+
uiQueueMain(pkguiDoQueueMain, (void *) n);
18+
}
19+
20+
void pkguiOnShouldQuit(void)
21+
{
22+
uiOnShouldQuit(pkguiDoOnShouldQuit, NULL);
23+
}

pkgui.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// 12 august 2018
2+
#include <stdio.h>
3+
#include "ui.h"
4+
5+
// main.go
6+
extern uiInitOptions *pkguiAllocInitOptions(void);
7+
extern void pkguiFreeInitOptions(uiInitOptions *o);
8+
extern void pkguiQueueMain(uintptr_t n);
9+
extern void pkguiOnShouldQuit(void);

util.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// 12 december 2015
2+
3+
package ui
4+
5+
import (
6+
"unsafe"
7+
)
8+
9+
// #include "pkgui.h"
10+
import "C"
11+
12+
//export pkguiAlloc
13+
func pkguiAlloc(n C.size_t) unsafe.Pointer {
14+
// cgo turns C.malloc() into a panic-on-OOM version; use it
15+
return C.malloc(n)
16+
}
17+
18+
func freestr(str *C.char) {
19+
C.free(unsafe.Pointer(str))
20+
}
21+
22+
func tobool(b C.int) bool {
23+
return b != 0
24+
}
25+
26+
func frombool(b bool) C.int {
27+
if b {
28+
return 1
29+
}
30+
return 0
31+
}

util.h

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)