Documentation
¶
Overview ¶
Package common contains basic routines needed by other modules in go-dpdk package.
Index ¶
- Variables
- func Assert(t testing.TB, fail bool) func(bool, ...interface{})
- func CBytes(a Allocator, b []byte) unsafe.Pointer
- func CString(a Allocator, s string) *C.char
- func CallocT(a Allocator, ptr interface{}, nmemb interface{}) unsafe.Pointer
- func CopyFromBytes(dst unsafe.Pointer, src []byte, max int) int
- func CopyToBytes(dst []byte, src unsafe.Pointer, max int) int
- func DoOnce(fn func() error) func() error
- func FprintStackFrames(w io.Writer, pc []uintptr)
- func IntErr(n int64) error
- func IntOrErr(n interface{}) (int, error)
- func IntToErr(n interface{}) error
- func MakeSlice(buf unsafe.Pointer, max int) []byte
- func MallocT(a Allocator, ptr interface{}) unsafe.Pointer
- func Memset(p unsafe.Pointer, init byte, n uintptr)
- func PutUint16(b binary.ByteOrder, dst unsafe.Pointer, d uint16)
- func PutUint32(b binary.ByteOrder, dst unsafe.Pointer, d uint32)
- func PutUint64(b binary.ByteOrder, dst unsafe.Pointer, d uint64)
- func RteErrno() error
- func SplitFunc(s *Splitter) bufio.SplitFunc
- func TransformPOD(a Allocator, ptr interface{}) (unsafe.Pointer, func(unsafe.Pointer))
- type Allocator
- type AllocatorSession
- type CStruct
- type Map
- type ObjectID
- type Registry
- type RteAlloc
- type Set
- type Splitter
- type StdAlloc
- type Transformer
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrNoConfig = errors.New("Missing rte_config") ErrSecondary = errors.New("Operation not allowed in secondary processes") )
Custom RTE induced errors.
var ( ErrUnprintable = errors.New("unprintable char") ErrOpenQuote = errors.New("no closing quote") )
Splitter parsing errors.
var DefaultSplitter = &Splitter{ unicode.IsSpace, func(r rune) (rune, bool) { if r == '"' { return '"', true } if r == '\'' { return '\'', true } return ' ', false }, false, }
DefaultSplitter parses tokens as space-separated words treating double and signal quotation mark as 'quotes'.
Functions ¶
func Assert ¶
Assert allows to perform one-lined tests and, optionally, print some diagnostic info if the test failed.
If fail is true, test failure will cause panic and cease test execution.
func CBytes ¶ added in v0.0.3
CBytes creates a copy of byte slice with given Allocator. It's analogous to C.CBytes.
func CString ¶ added in v0.0.3
CString a copy of a string with given Allocator. It's analogous to C.CString.
func CallocT ¶ added in v0.0.4
CallocT allocates an array of objects by its type. The type and its size is derived from ptr which is a pointer to pointer of required type where new object will be stored. For example:
var x *int
a := NewAllocatorSession(&StdAlloc{})
defer a.Flush()
CallocT(a, &x, 2)
/* x is now an allocated pointer */
func CopyFromBytes ¶ added in v0.0.3
CopyFromBytes copies no more than max bytes from src to an area pointed to by dst.
func CopyToBytes ¶ added in v0.0.3
CopyToBytes copies no more than max bytes from an area pointed to by src to dst.
func DoOnce ¶ added in v0.0.4
DoOnce decorates fn in a way that it will effectively run only once returning the resulting error value in this and all subsequent calls. Useful in unit testing when initialization must be performed only once.
func FprintStackFrames ¶ added in v0.0.4
FprintStackFrames prints calling stack of the error into specified writer. Program counters are specified in pc.
func IntOrErr ¶
IntOrErr returns error as in Errno in case n is negative. Otherwise, the value itself with nil error will be returned.
If n is nil, then n = RteErrno() if n is not nil and not a signed integer, function panics.
func IntToErr ¶ added in v0.0.3
func IntToErr(n interface{}) error
IntToErr converts n into an 'errno' error. If n is not a signed integer it will panic.
func MallocT ¶ added in v0.0.3
MallocT allocates an object by its type. The type and its size is derived from ptr which is a pointer to pointer of required type where new object will be stored. For example:
var x *int
a := NewAllocatorSession(&StdAlloc{})
defer a.Flush()
MallocT(a, &x)
/* x is now an allocated pointer */
Types ¶
type Allocator ¶ added in v0.0.3
type Allocator interface {
// Malloc allocates memory of length size.
Malloc(size uintptr) unsafe.Pointer
// Free releases previously allocated memory pointed to by p.
Free(p unsafe.Pointer)
// Realloc allocates memory of length size.
Realloc(p unsafe.Pointer, size uintptr) unsafe.Pointer
}
Allocator provides allocating and freeing of objects. It should be used with Cgo to withstand the rule of not allowing Go pointers inside a Go pointer. The allocator allows to defer freeing of objects so instead of freeing objects individually you may delete them by Flush at once and abandon allocator instance.
type AllocatorSession ¶ added in v0.0.3
type AllocatorSession struct {
// contains filtered or unexported fields
}
AllocatorSession wraps Allocator and storage for allocated pointers. Useful to perform allocations and free them with one call.
func NewAllocatorSession ¶ added in v0.0.3
func NewAllocatorSession(mem Allocator) *AllocatorSession
NewAllocatorSession creates new AllocatorSession.
func (*AllocatorSession) Flush ¶ added in v0.0.3
func (s *AllocatorSession) Flush()
Flush releases all previously allocated memory in this session.
func (*AllocatorSession) Free ¶ added in v0.0.3
func (s *AllocatorSession) Free(p unsafe.Pointer)
Free implements Allocator.
type CStruct ¶ added in v0.0.6
type CStruct struct {
// Ptr is a pointer to the beginning of the C array.
Ptr unsafe.Pointer
// Len is the size of memory area pointed by Ptr.
Len int
}
CStruct is a GO structure representation of a C array. CStruct has a certain length, don't try to extend it.
type Map ¶ added in v0.0.4
type Map struct {
// contains filtered or unexported fields
}
Map is an []int array-based implementation of a Set.
func NewMap ¶ added in v0.0.4
func NewMap(i interface{}) *Map
NewMap creates instance of a Map.
i may represent a Set, an array or a slice of integers, a map with integer keys. Otherwise, the function would panic.
Example ¶
package main
import (
"fmt"
"github.com/yerden/go-dpdk/common"
)
func main() {
x := common.NewMap([]int{0, 1, 2, 3})
fmt.Println(x)
}
Output: f
type Registry ¶
type Registry interface {
Create(interface{}) ObjectID
Read(ObjectID) interface{}
Update(ObjectID, interface{})
Delete(ObjectID)
}
Registry implements CRUD operations to map ID and objects.
func NewRegistryArray ¶
func NewRegistryArray() Registry
NewRegistryArray creates new Registry as a linear array.
func NewRegistryMap ¶
func NewRegistryMap() Registry
NewRegistryMap creates new Registry which stores all objects in a map.
type RteAlloc ¶ added in v0.0.3
type RteAlloc struct {
// Requested alignment.
Align uint
// Requested NUMA node. Set to SocketIDAny if meaningless.
Socket int
}
RteAlloc implements allocator based on DPDK rte_malloc.h.
type Set ¶ added in v0.0.4
type Set interface {
// IsSet tests if given int is inside the Set.
IsSet(int) bool
// Count returns the number of integers in the Set.
Count() int
// Set stores integer in the Set
Set(int)
}
Set represents a set of integer numbers.
type Splitter ¶
type Splitter struct {
// True if rune is a white space.
IsSpace func(rune) bool
// True if rune is quote. Any rune embraced by the one of these
// pairs is considered a part of a token even if IsSpace returns
// true. A pairs must not contradict white space and another
// pair.
//
// If true, return closing quote rune.
IsQuote func(rune) (rune, bool)
// If true, final token is allowed not to contain closing quote.
// If false, ErrOpenQuote error will be returned if no closing
// quote found.
AllowOpenQuote bool
}
Splitter is used to parse string into words. Quotes are used to protect words from being separated into separate token.
type StdAlloc ¶ added in v0.0.3
type StdAlloc struct{}
StdAlloc wraps system malloc/free memory allocation.
type Transformer ¶ added in v0.0.9
type Transformer interface {
// Transform allocates itself from Allocator and returns pointer
// to the allocation along with destructor function. Use should
// call destructor upon allocated object to avoid memory leak,
// e.g.:
// alloc := &common.StdAlloc{}
// x, dtor := obj.Transform(alloc)
// defer dtor(x)
//
// Implementations may use Allocator as a hint. If it is nil the
// implementation may choose an allocator at its will.
Transform(Allocator) (unsafe.Pointer, func(unsafe.Pointer))
}
Transformer is an object that can recreate some representation of itself allocating memory from Allocator.