本文整理匯總了Golang中C.calloc函數的典型用法代碼示例。如果您正苦於以下問題:Golang calloc函數的具體用法?Golang calloc怎麽用?Golang calloc使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了calloc函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: GetDeviceUsbStrings
// GetDeviceUsbStrings Gets USB device strings for manufacturer name, product
// name, and serial number if they are available.
func GetDeviceUsbStrings(index int) (string, string, string, error) {
var manufacturer *C.char
manufacturer = (*C.char)(C.calloc(1, 256))
defer C.free(unsafe.Pointer(manufacturer))
var product *C.char
product = (*C.char)(C.calloc(1, 256))
defer C.free(unsafe.Pointer(product))
var serial *C.char
serial = (*C.char)(C.calloc(1, 256))
defer C.free(unsafe.Pointer(serial))
// Returns 0 on success
retval := C.rtlsdr_get_device_usb_strings(
C.uint32_t(index),
manufacturer,
product,
serial)
if retval != 0 {
return "", "", "", fmt.Errorf("GetDeviceUsbStrings returned error value: %d", retval)
}
return C.GoString(manufacturer), C.GoString(product), C.GoString(serial), nil
}
示例2: _InitializeGlobalCStructures
func _InitializeGlobalCStructures() {
_MainArgs = (*C.struct__cef_main_args_t)(C.calloc(1, C.sizeof_struct__cef_main_args_t))
go_AddRef(unsafe.Pointer(_MainArgs))
_AppHandler = (*C.cef_app_t)(C.calloc(1, C.sizeof_cef_app_t))
go_AddRef(unsafe.Pointer(_AppHandler))
C.initialize_app_handler(_AppHandler)
_ClientHandler = (*C.struct__cef_client_t)(C.calloc(1, C.sizeof_struct__cef_client_t))
go_AddRef(unsafe.Pointer(_ClientHandler))
C.initialize_client_handler(_ClientHandler)
}
示例3: init
func init() {
size := 100000
head := (*reflect.SliceHeader)(unsafe.Pointer(&myTable))
head.Data = uintptr(C.calloc(C.size_t(size), C.size_t(unsafe.Sizeof(MyTable{}))))
head.Cap = size
head.Len = size
sizeofChildData := C.size_t(unsafe.Sizeof(ChildData{}))
for i := 0; i < size; i++ {
myTable[i].C01 = i
myTable[i].Child = (*ChildData)(C.calloc(1, sizeofChildData))
myTable[i].Child.C01 = i
}
}
示例4: Fetch
// Fetch performs a fetch operation. refspecs specifies which refspecs
// to use for this fetch, use an empty list to use the refspecs from
// the configuration; msg specifies what to use for the reflog
// entries. Leave "" to use defaults.
func (o *Remote) Fetch(refspecs []string, opts *FetchOptions, msg string) error {
var cmsg *C.char = nil
if msg != "" {
cmsg = C.CString(msg)
defer C.free(unsafe.Pointer(cmsg))
}
crefspecs := C.git_strarray{}
crefspecs.count = C.size_t(len(refspecs))
crefspecs.strings = makeCStringsFromStrings(refspecs)
defer freeStrarray(&crefspecs)
coptions := (*C.git_fetch_options)(C.calloc(1, C.size_t(unsafe.Sizeof(C.git_fetch_options{}))))
defer C.free(unsafe.Pointer(coptions))
populateFetchOptions(coptions, opts)
defer untrackCalbacksPayload(&coptions.callbacks)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_remote_fetch(o.ptr, &crefspecs, coptions, cmsg)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
示例5: Findalldevs
func Findalldevs() (ifs []Interface, err string) {
var buf *C.char
buf = (*C.char)(C.calloc(ERRBUF_SIZE, 1))
var alldevsp *C.struct_pcap_if
if -1 == C.pcap_findalldevs((**C.pcap_if_t)(unsafe.Pointer(&alldevsp)), buf) {
ifs = nil
err = C.GoString(buf)
} else {
dev := alldevsp
var i uint32
for i = 0; dev != nil; dev = dev.next {
i++
}
ifs = make([]Interface, i)
dev = alldevsp
for j := uint32(0); dev != nil; dev = dev.next {
var iface Interface
iface.Name = C.GoString(dev.name)
iface.Description = C.GoString(dev.description)
// TODO: add more elements
ifs[j] = iface
j++
}
C.pcap_freealldevs((*C.pcap_if_t)(alldevsp))
}
C.free(unsafe.Pointer(buf))
return
}
示例6: Findalldevs
func Findalldevs() (ifs []Interface, err error) {
var buf *C.char
buf = (*C.char)(C.calloc(ERRBUF_SIZE, 1))
defer C.free(unsafe.Pointer(buf))
var alldevsp *C.pcap_if_t
if -1 == C.pcap_findalldevs((**C.pcap_if_t)(&alldevsp), buf) {
return nil, errors.New(C.GoString(buf))
}
defer C.pcap_freealldevs((*C.pcap_if_t)(alldevsp))
dev := alldevsp
var i uint32
for i = 0; dev != nil; dev = (*C.pcap_if_t)(dev.next) {
i++
}
ifs = make([]Interface, i)
dev = alldevsp
for j := uint32(0); dev != nil; dev = (*C.pcap_if_t)(dev.next) {
var iface Interface
iface.Name = C.GoString(dev.name)
iface.Description = C.GoString(dev.description)
iface.Addresses = findalladdresses(dev.addresses)
// TODO: add more elements
ifs[j] = iface
j++
}
return
}
示例7: allocStreamCallbackTimeInfoMemory
// allocStreamCallbackTimeInfoMemory allocates memory for type C.PaStreamCallbackTimeInfo in C.
// The caller is responsible for freeing the this memory via C.free.
func allocStreamCallbackTimeInfoMemory(n int) unsafe.Pointer {
mem, err := C.calloc(C.size_t(n), (C.size_t)(sizeOfStreamCallbackTimeInfoValue))
if err != nil {
panic("memory alloc error: " + err.Error())
}
return mem
}
示例8: alloc
func alloc(size int) (unsafe.Pointer, error) {
p := C.calloc(C.size_t(size), C.size_t(1))
if p == nil {
return nil, AllocationFailed
}
return p, nil
}
示例9: allocStreamParametersMemory
// allocStreamParametersMemory allocates memory for type C.PaStreamParameters in C.
// The caller is responsible for freeing the this memory via C.free.
func allocStreamParametersMemory(n int) unsafe.Pointer {
mem, err := C.calloc(C.size_t(n), (C.size_t)(sizeOfStreamParametersValue))
if err != nil {
panic("memory alloc error: " + err.Error())
}
return mem
}
示例10: OpenLive
// OpenLive opens a device and returns a *Handle.
// It takes as arguments the name of the device ("eth0"), the maximum size to
// read for each packet (snaplen), whether to put the interface in promiscuous
// mode, and a timeout.
func OpenLive(device string, snaplen int32, promisc bool, timeout time.Duration) (handle *Handle, _ error) {
var buf *C.char
buf = (*C.char)(C.calloc(errorBufferSize, 1))
defer C.free(unsafe.Pointer(buf))
var pro C.int
if promisc {
pro = 1
}
dev := C.CString(device)
defer C.free(unsafe.Pointer(dev))
// This copies a bunch of the pcap_open_live implementation from pcap.c:
cptr := C.pcap_create(dev, buf)
if cptr == nil {
return nil, errors.New(C.GoString(buf))
}
var status C.int
if status = C.pcap_set_snaplen(cptr, C.int(snaplen)); status < 0 {
goto fail
} else if status = C.pcap_set_promisc(cptr, pro); status < 0 {
goto fail
} else if status = C.pcap_set_timeout(cptr, C.int(timeout/time.Millisecond)); status < 0 {
goto fail
}
return newHandle(cptr), nil
fail:
C.pcap_close(cptr)
return nil, statusError(status)
}
示例11: allocPFloatMemory
// allocPFloatMemory allocates memory for type *C.float in C.
// The caller is responsible for freeing the this memory via C.free.
func allocPFloatMemory(n int) unsafe.Pointer {
mem, err := C.calloc(C.size_t(n), (C.size_t)(sizeOfPFloatValue))
if err != nil {
panic("memory alloc error: " + err.Error())
}
return mem
}
示例12: compileBPFFilter
// compileBPFFilter always returns an allocated _Ctype_struct_bpf_program
// It is the callers responsibility to free the memory again, e.g.
//
// C.pcap_freecode(&bpf)
//
func (p *Handle) compileBPFFilter(expr string) (_Ctype_struct_bpf_program, error) {
errorBuf := (*C.char)(C.calloc(errorBufferSize, 1))
defer C.free(unsafe.Pointer(errorBuf))
var netp uint32
var maskp uint32
// Only do the lookup on network interfaces.
// No device indicates we're handling a pcap file.
if len(p.device) > 0 {
dev := C.CString(p.device)
defer C.free(unsafe.Pointer(dev))
if -1 == C.pcap_lookupnet(
dev,
(*C.bpf_u_int32)(unsafe.Pointer(&netp)),
(*C.bpf_u_int32)(unsafe.Pointer(&maskp)),
errorBuf,
) {
// We can't lookup the network, but that could be because the interface
// doesn't have an IPv4.
}
}
var bpf _Ctype_struct_bpf_program
cexpr := C.CString(expr)
defer C.free(unsafe.Pointer(cexpr))
if -1 == C.pcap_compile(p.cptr, &bpf, cexpr, 1, C.bpf_u_int32(maskp)) {
return bpf, p.Error()
}
return bpf, nil
}
示例13: CreateBrowser
func CreateBrowser(hwnd unsafe.Pointer, browserSettings BrowserSettings, url string) *Browser {
log.Debug("CreateBrowser, url:", url)
// Initialize cef_window_info_t structure.
var windowInfo *C.cef_window_info_t
windowInfo = (*C.cef_window_info_t)(
C.calloc(1, C.sizeof_cef_window_info_t))
FillWindowInfo(windowInfo, hwnd)
// Do not create the browser synchronously using the
// cef_browser_host_create_browser_sync() function, as
// it is unreliable. Instead obtain browser object in
// life_span_handler::on_after_created. In that callback
// keep CEF browser objects in a global map (cef window
// handle -> cef browser) and introduce
// a GetBrowserByWindowHandle() function. This function
// will first guess the CEF window handle using for example
// WinAPI functions and then search the global map of cef
// browser objects.
C.cef_browser_host_create_browser(windowInfo, _ClientHandler, CEFString(url),
browserSettings.ToCStruct(), nil)
b, err := globalLifespanHandler.RegisterAndWaitForBrowser()
if err != nil {
log.Error("ERROR:", err)
panic("Failed to create a browser")
}
b.RenderHandler = &DefaultRenderHandler{b}
browsers[b.Id] = b
return b
}
示例14: CEFString
func CEFString(original string) (final *C.cef_string_t) {
final = (*C.cef_string_t)(C.calloc(1, C.sizeof_cef_string_t))
charString := C.CString(original)
defer C.free(unsafe.Pointer(charString))
C.cef_string_from_utf8(charString, C.strlen(charString), final)
return final
}
示例15: Clone
func Clone(url string, path string, options *CloneOptions) (*Repository, error) {
repo := new(Repository)
curl := C.CString(url)
defer C.free(unsafe.Pointer(curl))
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
copts := (*C.git_clone_options)(C.calloc(1, C.size_t(unsafe.Sizeof(C.git_clone_options{}))))
populateCloneOptions(copts, options)
defer freeCloneOptions(copts)
if len(options.CheckoutBranch) != 0 {
copts.checkout_branch = C.CString(options.CheckoutBranch)
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_clone(&repo.ptr, curl, cpath, copts)
if ret < 0 {
return nil, MakeGitError(ret)
}
runtime.SetFinalizer(repo, (*Repository).Free)
return repo, nil
}