Migrated util.go back and formalized pkguiAlloc().
This commit is contained in:
parent
8d43eb52ce
commit
cfea745dc7
|
@ -10,10 +10,10 @@ import (
|
||||||
// #include <stdlib.h>
|
// #include <stdlib.h>
|
||||||
// #include <time.h>
|
// #include <time.h>
|
||||||
// #include "ui.h"
|
// #include "ui.h"
|
||||||
|
// #include "util.h"
|
||||||
// static inline struct tm *allocTimeStruct(void)
|
// static inline struct tm *allocTimeStruct(void)
|
||||||
// {
|
// {
|
||||||
// /* TODO handle error */
|
// return (struct tm *) pkguiAlloc(sizeof (struct tm));
|
||||||
// return (struct tm *) malloc(sizeof (struct tm));
|
|
||||||
// }
|
// }
|
||||||
// extern void doDateTimePickerOnChanged(uiDateTimePicker *, void *);
|
// extern void doDateTimePickerOnChanged(uiDateTimePicker *, void *);
|
||||||
import "C"
|
import "C"
|
||||||
|
|
|
@ -7,34 +7,26 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// #include <stdlib.h>
|
// #include <stdlib.h>
|
||||||
// // TODO remove when switching to Go 1.7
|
// #include "util.h"
|
||||||
// #include <string.h>
|
|
||||||
import "C"
|
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.
|
// We want Go itself to complain when we're out of memory.
|
||||||
// The allocators in cgo *should* do this, but there isn't a
|
// The allocators in cgo *should* do this, but there isn't a
|
||||||
// C.CMalloc(). There *is* a C.CBytes(), however, for transferring
|
// C.CMalloc(). There *is* a C.CBytes(), however, for transferring
|
||||||
// binary blobs from Go to C. If we pass this an arbitrary slice
|
// 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
|
// of the desired length, we get our C.CMalloc(). Using a slice
|
||||||
// that's always initialized to zero gives us the ZeroMemory()
|
// that's always initialized to zero gives us the memset(0)
|
||||||
// for free.
|
// (or ZeroMemory()) for free.
|
||||||
var uimallocBytes = make([]byte, 1024) // 1024 bytes first
|
var allocBytes = make([]byte, 1024) // 1024 bytes first
|
||||||
|
|
||||||
//export uimalloc
|
//export pkguiAlloc
|
||||||
func uimalloc(n C.size_t) unsafe.Pointer {
|
func pkguiAlloc(n C.size_t) unsafe.Pointer {
|
||||||
if n > C.size_t(len(uimallocBytes)) {
|
if n > C.size_t(len(allocBytes)) {
|
||||||
// TODO round n up to a multiple of a power of 2?
|
// TODO round n up to a multiple of a power of 2?
|
||||||
// for instance 0x1234 bytes -> 0x1800 bytes
|
// for instance 0x1234 bytes -> 0x1800 bytes
|
||||||
uimallocBytes = make([]byte, n)
|
allocBytes = make([]byte, n)
|
||||||
}
|
}
|
||||||
p := C.malloc(n)
|
return C.CBytes(allocBytes[:n])
|
||||||
if p == nil {
|
|
||||||
panic("out of memory in uimalloc()")
|
|
||||||
}
|
|
||||||
C.memset(p, 0, n)
|
|
||||||
return p
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func freestr(str *C.char) {
|
func freestr(str *C.char) {
|
Loading…
Reference in New Issue