// MakeOIDBytes makes an OID encapsulating a byte slice. Note that it does not // duplicate the data, but rather it points to it directly. func (lib *Lib) MakeOIDBytes(data []byte) (*OID, error) { oid := lib.NewOID() s := C.malloc(C.gss_OID_size) // s for struct if s == nil { return nil, ErrMallocFailed } C.memset(s, 0, C.gss_OID_size) l := C.size_t(len(data)) e := C.malloc(l) // c for contents if e == nil { return nil, ErrMallocFailed } C.memmove(e, (unsafe.Pointer)(&data[0]), l) oid.C_gss_OID = C.gss_OID(s) oid.alloc = allocMalloc // because of the alignment issues I can't access o.oid's fields from go, // so invoking a C function to do the same as: // oid.C_gss_OID.length = l // oid.C_gss_OID.elements = c C.helper_gss_OID_desc_set_elements(oid.C_gss_OID, C.OM_uint32(l), e) return oid, nil }
func GoEval2(expr *C.char, result **C.char) int { var res string var ret int src := C.GoString(expr) world := eval.NewWorld() world.DefineVar("remote_expr", eval.NewFuncType([]eval.Type{eval.StringType, eval.StringType}, false, []eval.Type{eval.StringType}), &funcV{&w_remote_expr{}}) code, err := world.Compile(fset, src) if err != nil { res = err.String() ret = -1 } else { value, err := code.Run() if err != nil { res = err.String() ret = -1 } else { res = value.String() ret = 0 } } p := C.CString(res) defer C.free(unsafe.Pointer(p)) *result = (*C.char)(C.vimremote_malloc((C.size_t)(len(res) + 1))) C.memmove(unsafe.Pointer(*result), unsafe.Pointer(p), (C.size_t)(len(res)+1)) return ret }
func swiftBytesCopy(data []byte) C.SwiftByteArray { var a C.SwiftByteArray a.length = C._GoUint64(len(data)) a.data = C.malloc(C.size_t(len(data))) if a.data == nil { panic(fmt.Errorf("Unable to allocate %d bytes", a.length)) } C.memmove(a.data, unsafe.Pointer(&data[0]), C.size_t(len(data))) return a }
func memmove(dest, src []byte) int { n := len(src) if len(dest) < len(src) { n = len(dest) } if n == 0 { return 0 } C.memmove(unsafe.Pointer(&dest[0]), unsafe.Pointer(&src[0]), C.size_t(n)) return n }
//export GoEval func GoEval(expr *C.char, result **C.char) int { var ret int func() { defer func() { if x := recover(); x != nil { ret = -1 res := fmt.Sprintf("%s", x) p := C.CString(res) defer C.free(unsafe.Pointer(p)) *result = (*C.char)(C.vimremote_malloc((C.size_t)(len(res) + 1))) C.memmove(unsafe.Pointer(*result), unsafe.Pointer(p), (C.size_t)(len(res)+1)) } }() ret = GoEval2(expr, result) }() return ret }
// Read pulls bytes out of the shared memory segment. func (shma *SharedMemMount) Read(p []byte) (int, error) { var err error l := uint(len(p)) if l > (shma.length - shma.offset) { l = shma.length - shma.offset err = io.EOF } if l == 0 { return 0, err } src := unsafe.Pointer(uintptr(shma.ptr) + uintptr(shma.offset)) dest := unsafe.Pointer(&p[0]) C.memmove(dest, src, C.size_t(l)) shma.offset += l return int(l), err }
//export go_read_callback func go_read_callback(result *C.struct_go_read_result, key uint64, buffer_key uint64) { context, err := Pool.Get(key) if err != nil { panic("Unable to find read callback") } callback := context.(func(*readResult)) Result := &readResult{ cmd: NewDnetCmd(result.cmd), addr: NewDnetAddr(result.addr), ioattr: NewDnetIOAttr(result.io_attribute), err: nil, } if buffer_key != 0 { buffer_context, err := Pool.Get(buffer_key) if err != nil { panic("Unable to find buffer key context") } buffer := buffer_context.([]byte) size := uint64(len(buffer)) if uint64(result.size) < size { size = uint64(result.size) } C.memmove(unsafe.Pointer(&buffer[0]), unsafe.Pointer(result.file), C.size_t(size)) Result.ioattr.Size = size Result.data = buffer[:size] } else { if result.size > 0 && result.file != nil { Result.data = C.GoBytes(unsafe.Pointer(result.file), C.int(result.size)) } else { Result.data = make([]byte, 0) } Result.ioattr.Size = uint64(len(Result.data)) } // All data from C++ has been copied here. callback(Result) }
func test6506() { // nothing to run, just make sure this compiles var x C.size_t C.calloc(x, x) C.malloc(x) C.realloc(nil, x) C.memcpy(nil, nil, x) C.memcmp(nil, nil, x) C.memmove(nil, nil, x) C.strncpy(nil, nil, x) C.strncmp(nil, nil, x) C.strncat(nil, nil, x) x = C.strxfrm(nil, nil, x) C.memchr(nil, 0, x) x = C.strcspn(nil, nil) x = C.strspn(nil, nil) C.memset(nil, 0, x) x = C.strlen(nil) _ = x }
// Write places bytes into the shared memory segment. func (shma *SharedMemMount) Write(p []byte) (int, error) { if shma.readonly { // see comment on readonly field above return 0, errors.New("Read-Only shared mem attachment") } var err error l := uint(len(p)) if l > (shma.length - shma.offset) { l = shma.length - shma.offset err = io.ErrShortWrite } if l == 0 { return 0, err } dest := unsafe.Pointer(uintptr(shma.ptr) + uintptr(shma.offset)) src := unsafe.Pointer(&p[0]) C.memmove(dest, src, C.size_t(l)) shma.offset += l return int(l), err }
// MakeBufferBytes makes a Buffer encapsulating a byte slice. func (lib *Lib) MakeBufferBytes(data []byte) (*Buffer, error) { if len(data) == 0 { return lib.GSS_C_NO_BUFFER, nil } // have to allocate the memory in C land and copy b, err := lib.MakeBuffer(allocMalloc) if err != nil { return nil, err } l := C.size_t(len(data)) c := C.malloc(l) if b == nil { return nil, ErrMallocFailed } C.memmove(c, (unsafe.Pointer)(&data[0]), l) b.C_gss_buffer_t.length = l b.C_gss_buffer_t.value = c b.alloc = allocMalloc return b, nil }
func (a *arena) Allocate(obj []byte) (C.CK_VOID_PTR, C.CK_ULONG) { cobj := C.calloc(C.size_t(len(obj)), 1) *a = append(*a, cobj) C.memmove(cobj, unsafe.Pointer(&obj[0]), C.size_t(len(obj))) return C.CK_VOID_PTR(cobj), C.CK_ULONG(len(obj)) }