当前位置: 首页>>代码示例>>Golang>>正文


Golang C.calloc函数代码示例

本文整理汇总了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
}
开发者ID:joerocklin,项目名称:go-rtlsdr,代码行数:28,代码来源:rtlsdr.go

示例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)
}
开发者ID:mmatey,项目名称:cef2go,代码行数:12,代码来源:cef.go

示例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
	}
}
开发者ID:rockxsj,项目名称:go-labs,代码行数:15,代码来源:demo_db.go

示例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
}
开发者ID:wid,项目名称:git2go,代码行数:31,代码来源:remote.go

示例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
}
开发者ID:sungit,项目名称:gopcap,代码行数:29,代码来源:pcap.go

示例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
}
开发者ID:dlintw,项目名称:gopcap,代码行数:28,代码来源:pcap.go

示例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
}
开发者ID:xlab,项目名称:portaudio-go,代码行数:9,代码来源:cgo_helpers.go

示例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
}
开发者ID:vova616,项目名称:goalloc,代码行数:7,代码来源:cgo.go

示例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
}
开发者ID:xlab,项目名称:portaudio-go,代码行数:9,代码来源:cgo_helpers.go

示例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)
}
开发者ID:pombredanne,项目名称:geard,代码行数:34,代码来源:pcap.go

示例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
}
开发者ID:xlab,项目名称:vorbis-go,代码行数:9,代码来源:cgo_helpers.go

示例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
}
开发者ID:ldnvnbl,项目名称:gopacket,代码行数:38,代码来源:pcap.go

示例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
}
开发者ID:xiqingping,项目名称:golibs,代码行数:31,代码来源:browser.go

示例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
}
开发者ID:mmatey,项目名称:cef2go,代码行数:7,代码来源:cef.go

示例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
}
开发者ID:eliza411,项目名称:pipeviz-1,代码行数:28,代码来源:clone.go


注:本文中的C.calloc函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。