func embed(s cairo.Surface, mime mime, bs []byte) error { raw := s.XtensionRaw() len := C.size_t(uintptr(len(bs))) //We make this copy as bs will be GC'd. //We could track the object lifetime and cache the []byte in this package //until it is destroyed, which is a fine optimization but necessitates a lot //of code. data := (*C.uchar)(C.malloc(len)) C.memcpy(unsafe.Pointer(data), unsafe.Pointer(&bs[0]), len) st := C.cairo_surface_set_mime_data(raw, mime.c(), data, C.ulong(len), C.gocairo_free_get(), nil) if st != C.CAIRO_STATUS_SUCCESS { return errors.New("could not set mime data") } return nil }
// //static void gocairo_free(void* data) { // free(data); //} // //static cairo_destroy_func_t gocairo_free_get() { // return &gocairo_free; //} import "C" import ( "sync" "unsafe" ) var free = C.gocairo_free_get() type ( id uint64 subtypeID uint64 ) func (s subtypeID) c() unsafe.Pointer { return C.cgo_id_malloc(C.uint64_t(s)) } var ( nextID uint64 idmux = &sync.Mutex{} idkey = &C.cairo_user_data_key_t{} stkey = &C.cairo_user_data_key_t{}