cgoref

package module
v0.0.0-...-9aa465f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 1, 2016 License: BSD-2-Clause-Views Imports: 3 Imported by: 0

README

CGORef

Documentation: https://godoc.org/github.com/dwbuiten/cgoref/

WARNING

Thing incredibly hard before you use this. Your problem is surely better solved using something like what is described here. I don't even use this package myself; it is more of a novelty. Will only work with Go 1.6+.

Documentation

Overview

Package cgoref a way to tell Go to not move a particular variable around in memory. It is not advised to generally use this package, as it may memory fragmentation, high memory usage, and other funky things.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CRef

type CRef struct {
	// contains filtered or unexported fields
}

func Ref

func Ref(ptr unsafe.Pointer) (*CRef, error)

Ref will hold a given pointer inside a CGO call until UnRef is called. In Go 1.6, this should prevent Go from moving it around in memory and changing its address until it has been unreffed. This will, of course, completey hose Go's garbage collector's ability to be effective. You may end up with OOMs or very fragmented memory, or other wacky things.

Mostly untested.

Example
buf := make([]byte, 1024)

var CThing C.struct_some_thing

// Make sure it won't move during processing.
ref, err := cgoref.Ref(unsafe.Pointer(&buf[0]))
if err != nil {
	return nil, err
}

// Stash it in a C struct for multiple uses. Evil!
CThing.buf = unsafe.Pointer(&buf[0])

length := int(C.some_init(&CThing))

for i := 0; i < length; i++ {
	C.some_process(&CThing)
}

// Hand Go back control.
ref.UnRef()

return buf, nil
Output:

func (*CRef) UnRef

func (this *CRef) UnRef()

UnRef will allow the pointer to leave the CGO call, letting the Go garbage collector clean/move it.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL