diff options
| author | Pietro Gagliardi <[email protected]> | 2016-04-12 23:27:29 -0400 |
|---|---|---|
| committer | Pietro Gagliardi <[email protected]> | 2016-04-12 23:27:29 -0400 |
| commit | 42d273bea8068e47a1672f550e23f9cca2f1be27 (patch) | |
| tree | 9d8a950172e3b4785003c076b286851e91843cbf /util.go | |
| parent | 53722b5b08344442a2e67b2bca3fd01776f96d3e (diff) | |
More C.CBytes() preparatory work.
Diffstat (limited to 'util.go')
| -rw-r--r-- | util.go | 14 |
1 files changed, 14 insertions, 0 deletions
@@ -13,8 +13,22 @@ 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()") |
