common

package
v0.1.1-r1 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2025 License: BSD-3-Clause Imports: 17 Imported by: 0

Documentation

Overview

Package common contains basic routines needed by other modules in go-dpdk package.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrNoConfig  = errors.New("Missing rte_config")
	ErrSecondary = errors.New("Operation not allowed in secondary processes")
)

Custom RTE induced errors.

View Source
var (
	ErrUnprintable = errors.New("unprintable char")
	ErrOpenQuote   = errors.New("no closing quote")
)

Splitter parsing errors.

View Source
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

func Assert(t testing.TB, fail bool) func(bool, ...interface{})

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

func CBytes(a Allocator, b []byte) unsafe.Pointer

CBytes creates a copy of byte slice with given Allocator. It's analogous to C.CBytes.

func CString

func CString(a Allocator, s string) *C.char

CString a copy of a string with given Allocator. It's analogous to C.CString.

func CallocT

func CallocT(a Allocator, ptr interface{}, nmemb interface{}) unsafe.Pointer

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

func CopyFromBytes(dst unsafe.Pointer, src []byte, max int) int

CopyFromBytes copies no more than max bytes from src to an area pointed to by dst.

func CopyToBytes

func CopyToBytes(dst []byte, src unsafe.Pointer, max int) int

CopyToBytes copies no more than max bytes from an area pointed to by src to dst.

func DoOnce

func DoOnce(fn func() error) func() error

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

func FprintStackFrames(w io.Writer, pc []uintptr)

FprintStackFrames prints calling stack of the error into specified writer. Program counters are specified in pc.

func IntErr

func IntErr(n int64) error

IntErr returns errno as error.

func IntOrErr

func IntOrErr(n interface{}) (int, error)

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

func IntToErr(n interface{}) error

IntToErr converts n into an 'errno' error. If n is not a signed integer it will panic.

func MakeSlice

func MakeSlice(buf unsafe.Pointer, max int) []byte

MakeSlice returns byte slice specified by pointer and of len max.

func MallocT

func MallocT(a Allocator, ptr interface{}) unsafe.Pointer

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 */

func Memset

func Memset(p unsafe.Pointer, init byte, n uintptr)

Memset initializes memory pointed by p and with length n.

func PutUint16

func PutUint16(b binary.ByteOrder, dst unsafe.Pointer, d uint16)

PutUint16 stores uint16 value into an area pointed to dst.

func PutUint32

func PutUint32(b binary.ByteOrder, dst unsafe.Pointer, d uint32)

PutUint32 stores uint32 value into an area pointed to dst.

func PutUint64

func PutUint64(b binary.ByteOrder, dst unsafe.Pointer, d uint64)

PutUint64 stores uint64 value into an area pointed to dst.

func RteErrno

func RteErrno() error

RteErrno returns rte_errno variable.

func SplitFunc

func SplitFunc(s *Splitter) bufio.SplitFunc

SplitFunc generates bufio.SplitFunc to use in bufio.Scanner.

func TransformPOD

func TransformPOD(a Allocator, ptr interface{}) (unsafe.Pointer, func(unsafe.Pointer))

TransformPOD allocates a copy of an object pointed to by ptr and returns a pointer to the copy and its destructor.

Types

type Allocator

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

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

func NewAllocatorSession(mem Allocator) *AllocatorSession

NewAllocatorSession creates new AllocatorSession.

func (*AllocatorSession) Flush

func (s *AllocatorSession) Flush()

Flush releases all previously allocated memory in this session.

func (*AllocatorSession) Free

func (s *AllocatorSession) Free(p unsafe.Pointer)

Free implements Allocator.

func (*AllocatorSession) Malloc

func (s *AllocatorSession) Malloc(size uintptr) unsafe.Pointer

Malloc implements Allocator.

func (*AllocatorSession) Realloc

func (s *AllocatorSession) Realloc(p unsafe.Pointer, size uintptr) unsafe.Pointer

Realloc implements Allocator.

type CStruct

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.

func (*CStruct) Bytes

func (cs *CStruct) Bytes() (dst []byte)

Bytes converts C array into slice of bytes backed by this array. Returned slice cannot be extended or it will be reallocated into Go memory.

func (*CStruct) Init

func (cs *CStruct) Init(ptr unsafe.Pointer, len int)

Init initializes CStruct instance with specified pointer ptr and length len.

type Map

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

Map is an []int array-based implementation of a Set.

func NewMap

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/rksawyer/go-dpdk/common"
)

func main() {
	x := common.NewMap([]int{0, 1, 2, 3})
	fmt.Println(x)
}
Output:

f

func (*Map) Count

func (m *Map) Count() int

Count implements Set interface.

func (*Map) IsSet

func (m *Map) IsSet(n int) bool

IsSet implements Set interface.

func (*Map) Set

func (m *Map) Set(n int)

Set implements Set interface.

func (*Map) String

func (m *Map) String() string

String implements fmt.Stringer interface.

func (*Map) Zero

func (m *Map) Zero()

Zero zeroes out Map.

type ObjectID

type ObjectID uint64

ObjectID is the ID of some opaque object stored in Registry.

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

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.

func (*RteAlloc) Free

func (mem *RteAlloc) Free(p unsafe.Pointer)

Free implements Allocator.

func (*RteAlloc) Malloc

func (mem *RteAlloc) Malloc(size uintptr) unsafe.Pointer

Malloc implements Allocator.

func (*RteAlloc) Realloc

func (mem *RteAlloc) Realloc(p unsafe.Pointer, size uintptr) unsafe.Pointer

Realloc implements Allocator.

Note: rte_realloc() may not reside on the same NUMA node.

type Set

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

type StdAlloc struct{}

StdAlloc wraps system malloc/free memory allocation.

func (*StdAlloc) Free

func (mem *StdAlloc) Free(p unsafe.Pointer)

Free implements Allocator.

func (*StdAlloc) Malloc

func (mem *StdAlloc) Malloc(size uintptr) unsafe.Pointer

Malloc implements Allocator.

func (*StdAlloc) Realloc

func (mem *StdAlloc) Realloc(p unsafe.Pointer, size uintptr) unsafe.Pointer

Realloc implements Allocator.

type Transformer

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.

Jump to

Keyboard shortcuts

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