本文整理匯總了Golang中C.malloc函數的典型用法代碼示例。如果您正苦於以下問題:Golang malloc函數的具體用法?Golang malloc怎麽用?Golang malloc使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了malloc函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: MakeOIDBytes
// 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
}
示例2: NewTrailDBConstructor
func NewTrailDBConstructor(path string, ofields ...string) (*TrailDBConstructor, error) {
cons := C.tdb_cons_init()
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
var ofield_p *C.char
ptrSize := unsafe.Sizeof(ofield_p)
// Allocate the char** list.
ptr := C.malloc(C.size_t(len(ofields)) * C.size_t(ptrSize))
defer C.free(ptr)
// Assign each byte slice to its appropriate offset.
for i := 0; i < len(ofields); i++ {
element := (**C.char)(unsafe.Pointer(uintptr(ptr) + uintptr(i)*ptrSize))
cofield := C.CString(ofields[i])
defer C.free(unsafe.Pointer(cofield))
*element = cofield
}
if err := C.tdb_cons_open(cons, cpath, (**C.char)(ptr), C.uint64_t(len(ofields))); err != 0 {
return nil, errors.New(errToString(err))
}
return &TrailDBConstructor{
cons: cons,
path: path,
ofields: ofields,
valueLengths: make([]C.uint64_t, len(ofields)),
valuePtr: C.malloc(C.size_t(len(ofields)) * C.size_t(ptrSize)),
}, nil
}
示例3: New
func New(seedHash []byte, block pow.Block) (*Ethash, error) {
params := new(C.ethash_params)
n, err := blockNum(block)
if err != nil {
return &Ethash{}, err
}
C.ethash_params_init(params, C.uint32_t(n))
log.Println("Params", params)
var mem unsafe.Pointer
mem = C.malloc(C.size_t(params.full_size))
cache := new(C.ethash_cache)
C.ethash_cache_init(cache, mem)
log.Println("making full data")
start := time.Now()
C.ethash_compute_full_data(mem, params, cache)
log.Println("took:", time.Since(start))
var hash *C.uint8_t
hash = (*C.uint8_t)(C.malloc(32))
return &Ethash{
turbo: false,
params: params,
cache: cache,
mem: mem,
hash: hash,
}, nil
}
示例4: _CGO
// The C side of things will still need to allocate memory, due to the slices.
// Assumes Configuration is valid.
func (config *Configuration) _CGO() *C.CGO_Configuration {
INFO.Println("Converting Config: ", config)
size := C.size_t(unsafe.Sizeof(C.CGO_Configuration{}))
c := (*C.CGO_Configuration)(C.malloc(size))
// Need to convert each IceServer struct individually.
total := len(config.IceServers)
if total > 0 {
sizeof := unsafe.Sizeof(C.CGO_IceServer{})
cServers := unsafe.Pointer(C.malloc(C.size_t(sizeof * uintptr(total))))
ptr := uintptr(cServers)
for _, server := range config.IceServers {
*(*C.CGO_IceServer)(unsafe.Pointer(ptr)) = server._CGO()
ptr += sizeof
}
c.iceServers = (*C.CGO_IceServer)(cServers)
}
c.numIceServers = C.int(total)
// c.iceServers = (*C.CGO_IceServer)(unsafe.Pointer(&config.IceServers))
c.iceTransportPolicy = C.int(config.IceTransportPolicy)
c.bundlePolicy = C.int(config.BundlePolicy)
// [ED] c.RtcpMuxPolicy = C.int(config.RtcpMuxPolicy)
c.peerIdentity = C.CString(config.PeerIdentity)
// [ED] c.Certificates = config.Certificates
// [ED] c.IceCandidatePoolSize = C.int(config.IceCandidatePoolSize)
return c
}
示例5: LaunchKernel
func LaunchKernel(f Function, gridDimX, gridDimY, gridDimZ int, blockDimX, blockDimY, blockDimZ int, sharedMemBytes int, stream Stream, kernelParams []unsafe.Pointer) {
// Since Go 1.6, a cgo argument cannot have a Go pointer to Go pointer,
// so we copy the argument values go C memory first.
argv := C.malloc(C.size_t(len(kernelParams) * pointerSize))
argp := C.malloc(C.size_t(len(kernelParams) * pointerSize))
defer C.free(argv)
defer C.free(argp)
for i := range kernelParams {
*((*unsafe.Pointer)(offset(argp, i))) = offset(argv, i) // argp[i] = &argv[i]
*((*uint64)(offset(argv, i))) = *((*uint64)(kernelParams[i])) // argv[i] = *kernelParams[i]
}
err := Result(C.cuLaunchKernel(
C.CUfunction(unsafe.Pointer(uintptr(f))),
C.uint(gridDimX),
C.uint(gridDimY),
C.uint(gridDimZ),
C.uint(blockDimX),
C.uint(blockDimY),
C.uint(blockDimZ),
C.uint(sharedMemBytes),
C.CUstream(unsafe.Pointer(uintptr(stream))),
(*unsafe.Pointer)(argp),
(*unsafe.Pointer)(unsafe.Pointer(uintptr(0)))))
if err != SUCCESS {
panic(err)
}
}
示例6: Store
//////////////////////////
// 寫入接口 //
//////////////////////////
func Store(table *Table,
primary_key string, // key after encode
timestamp int64,
index_list []Index,
data_list []string) error {
var c_request C.mdt_store_request_t
var c_response C.mdt_store_response_t
// convert request
c_request.primary_key.data = C.CString(primary_key)
c_request.primary_key.size = C.size_t(len(primary_key))
c_request.timestamp = C.int64_t(timestamp)
c_request.index_list = nil
c_request.index_list_len = C.size_t(len(index_list))
c_request.index_list = (*C.mdt_index_t)(C.malloc(32 * c_request.index_list_len))
c_index_list := (*[1 << 30]C.mdt_index_t)(unsafe.Pointer(c_request.index_list))
for i := C.size_t(0); i < c_request.index_list_len; i++ {
c_index_list[i].index_name.data = C.CString(index_list[i].IndexName)
c_index_list[i].index_name.size = C.size_t(len(index_list[i].IndexName))
c_index_list[i].index_key.data = C.CString(index_list[i].IndexKey)
c_index_list[i].index_key.size = C.size_t(len(index_list[i].IndexKey))
}
// new vector write interface
c_request.data_list = nil
c_request.data_list_len = C.size_t(data_list)
c_request.data_list = (*C.mdt_slice_t)(C.malloc(16 * c_request.data_list_len))
c_data_list := (*[1 << 30]C.mdt_slice_t)(unsafe.Pointer(c_request.data_list))
for i := C.size_t(0); i < c_request.data_list_len; data_i++ {
c_data_list[i].data = C.CString(data_list[i])
c_data_list[i].size = C.size_t(len(data_list[i]))
}
// old write interface
//c_request.data.data = C.CString(data)
//c_request.data.size = C.size_t(len(data))
// invoke C API
C.mdt_store(table.rep, &c_request, &c_response, nil, nil)
// convert result
err := int(c_response.error)
// free request memory
C.free(unsafe.Pointer(c_request.primary_key.data))
for i := C.size_t(0); i < c_request.index_list_len; i++ {
C.free(unsafe.Pointer(c_index_list[i].index_name.data))
C.free(unsafe.Pointer(c_index_list[i].index_key.data))
}
C.free(unsafe.Pointer(c_request.index_list))
for i := C.size_t(0); i < c_request.data_list_len; i++ {
C.free(unsafe.Pointer(c_data_list[i].data))
}
C.free(unsafe.Pointer(c_request.data_list))
//C.free(unsafe.Pointer(c_request.data.data))
return GetError(err)
}
示例7: generateCmac
func generateCmac(key []byte, msg []byte) []byte {
if len(key) != 16 {
panic("Invalid length.")
}
cMac := (*C.uchar)(C.malloc(16))
defer C.free(unsafe.Pointer(cMac))
var msgStart *C.uchar
if len(msg) > 0 {
msgStart = (*C.uchar)(&msg[0])
} else {
// Avoid indexing into empty slice/C array.
msgStart = (*C.uchar)(C.malloc(1))
defer C.free(unsafe.Pointer(msgStart))
}
C.AES_CMAC(
(*C.uchar)(&key[0]),
msgStart,
C.int(len(msg)),
cMac)
return C.GoBytes(unsafe.Pointer(cMac), 16)
}
示例8: SetOmap
// Append the map `pairs` to the omap `oid`
func (ioctx *IOContext) SetOmap(oid string, pairs map[string][]byte) error {
c_oid := C.CString(oid)
defer C.free(unsafe.Pointer(c_oid))
var s C.size_t
var c *C.char
ptrSize := unsafe.Sizeof(c)
c_keys := C.malloc(C.size_t(len(pairs)) * C.size_t(ptrSize))
c_values := C.malloc(C.size_t(len(pairs)) * C.size_t(ptrSize))
c_lengths := C.malloc(C.size_t(len(pairs)) * C.size_t(unsafe.Sizeof(s)))
defer C.free(unsafe.Pointer(c_keys))
defer C.free(unsafe.Pointer(c_values))
defer C.free(unsafe.Pointer(c_lengths))
i := 0
for key, value := range pairs {
// key
c_key_ptr := (**C.char)(unsafe.Pointer(uintptr(c_keys) + uintptr(i)*ptrSize))
*c_key_ptr = C.CString(key)
defer C.free(unsafe.Pointer(*c_key_ptr))
// value and its length
c_value_ptr := (**C.char)(unsafe.Pointer(uintptr(c_values) + uintptr(i)*ptrSize))
var c_length C.size_t
if len(value) > 0 {
*c_value_ptr = (*C.char)(unsafe.Pointer(&value[0]))
c_length = C.size_t(len(value))
} else {
*c_value_ptr = nil
c_length = C.size_t(0)
}
c_length_ptr := (*C.size_t)(unsafe.Pointer(uintptr(c_lengths) + uintptr(i)*ptrSize))
*c_length_ptr = c_length
i++
}
op := C.rados_create_write_op()
C.rados_write_op_omap_set(
op,
(**C.char)(c_keys),
(**C.char)(c_values),
(*C.size_t)(c_lengths),
C.size_t(len(pairs)))
ret := C.rados_write_op_operate(op, ioctx.ioctx, c_oid, nil, 0)
C.rados_release_write_op(op)
if ret == 0 {
return nil
} else {
return RadosError(int(ret))
}
}
示例9: GetNthAircraftModel
func GetNthAircraftModel(index int) (fileName, path string) {
nameBuf := (*C.char)(C.malloc(256))
defer C.free(unsafe.Pointer(nameBuf))
pathBuf := (*C.char)(C.malloc(512))
defer C.free(unsafe.Pointer(pathBuf))
C.XPLMGetNthAircraftModel((C.int)(index), nameBuf, pathBuf)
fileName = C.GoString(nameBuf)
path = C.GoString(pathBuf)
return
}
示例10: Spawn
func Spawn(loop *Loop, options ProcessOptions) (err error) {
if loop == nil {
loop = DefaultLoop()
}
var opt C.uv_process_options_t
defer func() {
/*
C.free(unsafe.Pointer(opt.file))
if len(options.Args) > 0 {
for n := 0; n < len(options.Args); n++ {
C.free(unsafe.Pointer(((*[1<<24]*C.char)(unsafe.Pointer(&opt.args)))[n]))
}
C.free(unsafe.Pointer(opt.args))
}
if len(options.Env) > 0 {
for n := 0; n < len(options.Env); n++ {
C.free(unsafe.Pointer(((*[1<<24]*C.char)(unsafe.Pointer(&opt.env)))[n]))
}
C.free(unsafe.Pointer(opt.env))
}
C.free(unsafe.Pointer(opt.cwd))
*/
}()
if len(options.File) > 0 {
opt.file = C.CString(options.File)
}
if len(options.Args) > 0 {
opt.args = (**C.char)(C.malloc(C.size_t(4 * (len(options.Args) + 1))))
for n := 0; n < len(options.Args); n++ {
((*[1 << 24]*C.char)(unsafe.Pointer(&opt.args)))[n] = C.CString(options.Args[n])
}
((*[1 << 24]*C.char)(unsafe.Pointer(&opt.args)))[len(options.Args)] = nil
}
if len(options.Env) > 0 {
opt.env = (**C.char)(C.malloc(C.size_t(4 * (len(options.Env) + 1))))
for n := 0; n < len(options.Args); n++ {
((*[1 << 24]*C.char)(unsafe.Pointer(&opt.env)))[n] = C.CString(options.Args[n])
}
((*[1 << 24]*C.char)(unsafe.Pointer(&opt.env)))[len(options.Args)] = nil
}
if len(options.Cwd) > 0 {
opt.cwd = C.CString(options.Cwd)
}
// TODO: uv_stdio_container_t
var p C.uv_process_t
p.data = unsafe.Pointer(&callback_info{exit_cb: options.Exit_cb})
r := uv_spawn(loop.l, &p, opt)
if r != 0 {
return loop.LastError().Error()
}
return nil
}
示例11: test6390
func test6390(t *testing.T) {
p1 := C.malloc(1024)
if p1 == nil {
t.Fatalf("C.malloc(1024) returned nil")
}
p2 := C.malloc(0)
if p2 == nil {
t.Fatalf("C.malloc(0) returned nil")
}
C.free(p1)
C.free(p2)
}
示例12: ListLockers
// List clients that have locked the named object lock and information about the lock.
// The number of bytes required in each buffer is put in the corresponding size out parameter.
// If any of the provided buffers are too short, -ERANGE is returned after these sizes are filled in.
func (ioctx *IOContext) ListLockers(oid, name string) (*LockInfo, error) {
c_oid := C.CString(oid)
c_name := C.CString(name)
c_tag := (*C.char)(C.malloc(C.size_t(1024)))
c_clients := (*C.char)(C.malloc(C.size_t(1024)))
c_cookies := (*C.char)(C.malloc(C.size_t(1024)))
c_addrs := (*C.char)(C.malloc(C.size_t(1024)))
var c_exclusive C.int
c_tag_len := C.size_t(1024)
c_clients_len := C.size_t(1024)
c_cookies_len := C.size_t(1024)
c_addrs_len := C.size_t(1024)
defer C.free(unsafe.Pointer(c_oid))
defer C.free(unsafe.Pointer(c_name))
defer C.free(unsafe.Pointer(c_tag))
defer C.free(unsafe.Pointer(c_clients))
defer C.free(unsafe.Pointer(c_cookies))
defer C.free(unsafe.Pointer(c_addrs))
ret := C.rados_list_lockers(
ioctx.ioctx,
c_oid,
c_name,
&c_exclusive,
c_tag,
&c_tag_len,
c_clients,
&c_clients_len,
c_cookies,
&c_cookies_len,
c_addrs,
&c_addrs_len)
splitCString := func(items *C.char, itemsLen C.size_t) []string {
currLen := 0
clients := []string{}
for currLen < int(itemsLen) {
client := C.GoString(C.nextChunk(&items))
clients = append(clients, client)
currLen += len(client) + 1
}
return clients
}
if ret < 0 {
return nil, RadosError(int(ret))
} else {
return &LockInfo{int(ret), c_exclusive == 1, C.GoString(c_tag), splitCString(c_clients, c_clients_len), splitCString(c_cookies, c_cookies_len), splitCString(c_addrs, c_addrs_len)}, nil
}
}
示例13: generateSubkey
func generateSubkey(key []byte) (k1 []byte, k2 []byte) {
if len(key) != 16 {
panic("Invalid length.")
}
cK1 := (*C.uchar)(C.malloc(16))
defer C.free(unsafe.Pointer(cK1))
cK2 := (*C.uchar)(C.malloc(16))
defer C.free(unsafe.Pointer(cK2))
C.generate_subkey((*C.uchar)(&key[0]), cK1, cK2)
return C.GoBytes(unsafe.Pointer(cK1), 16), C.GoBytes(unsafe.Pointer(cK2), 16)
}
示例14: doFinish
func doFinish(call rpc.ClientCall, numResults int) (C.SwiftByteArrayArray, error) {
// Have all the results be decoded into *vdl.Value.
resultPtrs := make([]interface{}, numResults)
for i := 0; i < numResults; i++ {
value := new(vdl.Value)
resultPtrs[i] = &value
}
if err := call.Finish(resultPtrs...); err != nil {
// Invocation error.
return EmptySwiftByteArrayArray(), err
}
// VOM-encode the results. Note in the future we'll want a pathway where we can get the original VOM results
// from finish so we don't end up wasting CPU & memory here.
// Prepare the byte array array that can be accessed from Swift via C.malloc
vomResultsMemory := C.malloc(C.size_t(numResults * int(C.sizeofSwiftByteArray)))
// Make that malloc'd memory available as a slice to Go.
vomResultsPtrsHdr := reflect.SliceHeader{
Data: uintptr(vomResultsMemory),
Len: numResults,
Cap: numResults,
}
vomResults := *(*[]C.SwiftByteArray)(unsafe.Pointer(&vomResultsPtrsHdr))
// Create the C Struct to return that encapsulates our byte array array
var cVomResults C.SwiftByteArrayArray
cVomResults.length = C._GoUint64(numResults)
cVomResults.data = (*C.SwiftByteArray)(vomResultsMemory)
// For each result, VOM encode into a byte array that we stick into the returned struct
for i, resultPtr := range resultPtrs {
// Remove the pointer from the result. Simply *resultPtr doesn't work
// as resultPtr is of type interface{}.
result := interface{}(sutil.DerefOrDie(resultPtr))
var vomResult []byte
var err error
if vomResult, err = vom.Encode(result); err != nil {
return EmptySwiftByteArrayArray(), err
}
cVomResultCopy := C.malloc(C.size_t(len(vomResult)))
C.memcpy(cVomResultCopy, unsafe.Pointer(&vomResult[0]), C.size_t(len(vomResult)))
var cVomResult C.SwiftByteArray
cVomResult.length = C._GoUint64(len(vomResult))
cVomResult.data = cVomResultCopy
vomResults[i] = cVomResult
}
return cVomResults, nil
}
示例15: GetShaderInfoLog
func GetShaderInfoLog(s Shader) string {
infoLen := GetShaderi(s, INFO_LOG_LENGTH)
buf := C.malloc(C.size_t(infoLen))
defer C.free(buf)
C.glGetShaderInfoLog(s.c(), C.GLsizei(infoLen), nil, (*C.GLchar)(buf))
return C.GoString((*C.char)(buf))
}