From cfea745dc745dcb0af8704d7cdb50d9725714b04 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sun, 12 Aug 2018 10:24:24 -0400 Subject: Migrated util.go back and formalized pkguiAlloc(). --- AAA_GOFILES/util.go | 53 ----------------------------------------------------- datetimepicker.go | 4 ++-- util.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ util.h | 3 +++ 4 files changed, 50 insertions(+), 55 deletions(-) delete mode 100644 AAA_GOFILES/util.go create mode 100644 util.go create mode 100644 util.h diff --git a/AAA_GOFILES/util.go b/AAA_GOFILES/util.go deleted file mode 100644 index b097004..0000000 --- a/AAA_GOFILES/util.go +++ /dev/null @@ -1,53 +0,0 @@ -// 12 december 2015 - -package ui - -import ( - "unsafe" -) - -// #include -// // TODO remove when switching to Go 1.7 -// #include -import "C" - -// TODO move this to C.CBytes() when switching to Go 1.7 - -// 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 ZeroMemory() -// for free. -var uimallocBytes = make([]byte, 1024) // 1024 bytes first - -//export uimalloc -func uimalloc(n C.size_t) unsafe.Pointer { - if n > C.size_t(len(uimallocBytes)) { - // TODO round n up to a multiple of a power of 2? - // for instance 0x1234 bytes -> 0x1800 bytes - uimallocBytes = make([]byte, n) - } - p := C.malloc(n) - if p == nil { - panic("out of memory in uimalloc()") - } - C.memset(p, 0, n) - return p -} - -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 -} diff --git a/datetimepicker.go b/datetimepicker.go index 0859a32..6e64e34 100644 --- a/datetimepicker.go +++ b/datetimepicker.go @@ -10,10 +10,10 @@ import ( // #include // #include // #include "ui.h" +// #include "util.h" // static inline struct tm *allocTimeStruct(void) // { -// /* TODO handle error */ -// return (struct tm *) malloc(sizeof (struct tm)); +// return (struct tm *) pkguiAlloc(sizeof (struct tm)); // } // extern void doDateTimePickerOnChanged(uiDateTimePicker *, void *); import "C" diff --git a/util.go b/util.go new file mode 100644 index 0000000..bcd4d33 --- /dev/null +++ b/util.go @@ -0,0 +1,45 @@ +// 12 december 2015 + +package ui + +import ( + "unsafe" +) + +// #include +// #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 +} diff --git a/util.h b/util.h new file mode 100644 index 0000000..3354234 --- /dev/null +++ b/util.h @@ -0,0 +1,3 @@ +// 12 august 2018 + +extern void *pkguiAlloc(size_t); -- cgit v1.2.3