ptr

package
v0.8.3 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2025 License: Apache-2.0 Imports: 5 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GoString

func GoString(charPtr unsafe.Pointer) string

GoString converts a C string to a Go string. This is analoguous to C.GoString, but avoids unnecessary memory allcations and copies. The string length is determined by invoking strlen on the passed memory pointer. Note that the returned string is an aliased view of the underlying C-allocated memory. As such, writing inside the memory will cause the string contents to change. Accordingly, unsafe memory management, such as unexpectedly free-ing the underlying C memory, can cause non-deterministic behavior on the Go routines using the returned string.

Types

type BytesReadWriter

type BytesReadWriter interface {
	io.ReadWriteSeeker
	//
	// BufferPtr returns an unsafe.Pointer that points to the underlying memory buffer.
	BufferPtr() unsafe.Pointer
	//
	// Len returns the total number of accessible bytes for reading and writing.
	Len() int64
	//
	// SetLen sets the total number of accessible bytes for reading and writing.
	// The new length value should not be larger than the underlying memory buffer capacity.
	// If a greater value is given, the length is set to be equal to the capacity.
	// If a value less than zero is given, the length is set to be zero.
	SetLen(len int64)
	//
	// Offset returns the current cursor position relatively to the underlying buffer.
	// The cursor position represents the index of the next byte in the buffer that will
	// be available for read\write operations. This value is altered through the usage of
	// Seek, Read, and Write. By definition, we have that 0 <= Offset() <= Len().
	Offset() int64
}

BytesReadWriter is an opaque wrapper for fixed-size memory buffers, that can safely be used in the plugin framework in a Go-friendly way. The purpose is to allow safe memory access through the read/write interface primitives, regardless of how the buffer is physically allocated under the hood. For instance, this can be used to wrap C-allocated buffers to hide both the type conversion magic and prevent illegal memory operations.

The io.ReadWriteSeeker interface is leveraged to implement the safe random memory access semantic. Note, read-only or write-only access to the memory buffer can easily be accomplished by casting instances of this interface to either a io.Reader or a io.Writer.

func NewBytesReadWriter

func NewBytesReadWriter(buffer unsafe.Pointer, length, capacity int64) (BytesReadWriter, error)

NewBytesReadWriter creates a new instance of BytesReadWriter by wrapping the memory pointed by the buffer argument. The length argument is the total number of accessible bytes for reading and writing. The capacity argument is the number of bytes in the given buffer.

Note that the capacity cannot be changed after creation, and that the length cannot ever exceed the capacity.

type StringBuffer

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

StringBuffer represents a buffer for C-allocated null-terminated strings in a Go-friendly way. This is an implementation of the sdk.StringBuffer interface. The underlying memory buffer is allocated and resized automatically. The buffer allocation happens lazily at the first call to Write. If during a call to Write the converted string is too large to fit in the buffer, it gets resized automatically to a proper size.

func (*StringBuffer) CharPtr

func (s *StringBuffer) CharPtr() unsafe.Pointer

func (*StringBuffer) Free

func (s *StringBuffer) Free()

func (*StringBuffer) String

func (s *StringBuffer) String() string

func (*StringBuffer) Write

func (s *StringBuffer) Write(str string)

Jump to

Keyboard shortcuts

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