libatbus_buffer

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package libatbus_buffer provides buffer management utilities for libatbus. This file implements StaticBufferBlock, a Go port of C++ static_buffer_block.

Index

Constants

View Source
const DataAlignSize = 8

DataAlignSize is the alignment size for buffer data

Variables

View Source
var (
	ErrSuccess    = errors.New("success")
	ErrNoData     = errors.New("no data available")
	ErrBuffLimit  = errors.New("buffer limit reached")
	ErrMalloc     = errors.New("memory allocation failed")
	ErrInvalidArg = errors.New("invalid argument")
)

Error codes

Functions

func FullSize

func FullSize(s int) int

FullSize returns the total size needed for a buffer block of size s

func HeadSize

func HeadSize(s int) int

HeadSize returns the overhead size for a buffer block header In Go, we don't need actual header storage since we use struct fields, but we keep this for compatibility with size calculations

func MimallocPaddingSize

func MimallocPaddingSize(originSize int) int

MimallocPaddingSize calculates padded size following mimalloc-style size class patterns. This helps reduce memory fragmentation and improve malloc efficiency.

func PaddingSize

func PaddingSize(s int) int

PaddingSize calculates the padded size aligned to DataAlignSize

func ReadVint

func ReadVint(data []byte) (uint64, int)

ReadVint reads a variable-length encoded integer from the buffer. Each byte uses 7 bits for data and the highest bit (0x80) indicates continuation. Returns the decoded value and the number of bytes consumed. If the buffer is empty or truncated (continuation bit set but no more data), returns (0, 0).

func VintEncodedSize

func VintEncodedSize(value uint64) int

VintEncodedSize returns the number of bytes needed to encode the given value.

func WriteVint

func WriteVint(value uint64, data []byte) int

WriteVint writes a variable-length encoded integer to the buffer. Each byte uses 7 bits for data and the highest bit (0x80) indicates continuation. Returns the number of bytes written. If the buffer is too small to hold the encoded value, returns 0.

Types

type BufferBlock

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

BufferBlock represents a buffer block with size and used tracking It manages a slice of bytes with a "used" offset for pop operations

func NewBufferBlock

func NewBufferBlock(size int) *BufferBlock

NewBufferBlock creates a new BufferBlock with the specified size

func NewBufferBlockFromSlice

func NewBufferBlockFromSlice(data []byte) *BufferBlock

NewBufferBlockFromSlice creates a BufferBlock wrapping an existing slice

func (*BufferBlock) Clone

func (b *BufferBlock) Clone() *BufferBlock

Clone creates a deep copy of the buffer block

func (*BufferBlock) Data

func (b *BufferBlock) Data() []byte

Data returns the unread portion of the buffer (after used offset)

func (*BufferBlock) Pop

func (b *BufferBlock) Pop(s int) []byte

Pop advances the used pointer by s bytes, reducing the available size Returns the new data slice after the pop

func (*BufferBlock) RawData

func (b *BufferBlock) RawData() []byte

RawData returns the entire buffer from the beginning

func (*BufferBlock) RawSize

func (b *BufferBlock) RawSize() int

RawSize returns the total size of the buffer

func (*BufferBlock) Reset

func (b *BufferBlock) Reset()

Reset resets the used counter to 0

func (*BufferBlock) SetUsed

func (b *BufferBlock) SetUsed(used int)

SetUsed sets the used counter directly

func (*BufferBlock) Size

func (b *BufferBlock) Size() int

Size returns the remaining size (total - used)

func (*BufferBlock) Used

func (b *BufferBlock) Used() int

Used returns how many bytes have been popped

type BufferManager

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

BufferManager manages a collection of buffer blocks It can operate in dynamic mode (using linked list) or static mode (using circular buffer)

func NewBufferManager

func NewBufferManager() *BufferManager

NewBufferManager creates a new buffer manager in dynamic mode

func (*BufferManager) Back

func (m *BufferManager) Back() *BufferBlock

Back returns the last buffer block

func (*BufferManager) BackData

func (m *BufferManager) BackData() (data []byte, nread, nwrite int, err error)

BackData returns the data pointer, read size, and write size of the back block

func (*BufferManager) Count

func (m *BufferManager) Count() int

Count returns the number of buffer blocks

func (*BufferManager) Empty

func (m *BufferManager) Empty() bool

Empty returns true if there are no buffer blocks

func (*BufferManager) ForEach

func (m *BufferManager) ForEach(fn func(block *BufferBlock) bool)

ForEach iterates over all buffer blocks

func (*BufferManager) Front

func (m *BufferManager) Front() *BufferBlock

Front returns the first buffer block

func (*BufferManager) FrontData

func (m *BufferManager) FrontData() (data []byte, nread, nwrite int, err error)

FrontData returns the data pointer, read size, and write size of the front block

func (*BufferManager) IsDynamicMode

func (m *BufferManager) IsDynamicMode() bool

IsDynamicMode returns true if the manager is in dynamic mode

func (*BufferManager) IsStaticMode

func (m *BufferManager) IsStaticMode() bool

IsStaticMode returns true if the manager is in static mode

func (*BufferManager) Limit

func (m *BufferManager) Limit() Limit

Limit returns the current limit configuration

func (*BufferManager) MergeBack

func (m *BufferManager) MergeBack(size int) (data []byte, err error)

MergeBack extends the back block by additional size

func (*BufferManager) MergeFront

func (m *BufferManager) MergeFront(size int) (data []byte, err error)

MergeFront extends the front block by additional size

func (*BufferManager) PopBack

func (m *BufferManager) PopBack(size int, freeUnwritable bool) error

PopBack pops bytes from the back block

func (*BufferManager) PopFront

func (m *BufferManager) PopFront(size int, freeUnwritable bool) error

PopFront pops bytes from the front block

func (*BufferManager) PushBack

func (m *BufferManager) PushBack(size int) (data []byte, err error)

PushBack allocates a new block at the back and returns the data slice

func (*BufferManager) PushFront

func (m *BufferManager) PushFront(size int) (data []byte, err error)

PushFront allocates a new block at the front and returns the data slice

func (*BufferManager) Reset

func (m *BufferManager) Reset()

Reset clears all data and returns to initial state

func (*BufferManager) SetLimit

func (m *BufferManager) SetLimit(maxSize, maxNumber int) bool

SetLimit sets limits when in dynamic mode Returns true on success, false if in static mode

func (*BufferManager) SetMode

func (m *BufferManager) SetMode(maxSize, maxNumber int)

SetMode switches to static mode with a fixed buffer size and max number of blocks

type Limit

type Limit struct {
	CostNumber  int // Current number of blocks
	CostSize    int // Current total size used
	LimitNumber int // Maximum number of blocks (0 = unlimited)
	LimitSize   int // Maximum total size (0 = unlimited)
}

Limit tracks the buffer usage limits and current costs

type StaticBufferBlock

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

StaticBufferBlock is a buffer block designed for temporary buffer allocation. It tracks the total allocated size and the used portion.

This is the Go equivalent of C++ static_buffer_block from libatbus. Unlike the C++ version which uses unique_ptr<unsigned char[]>, we use a []byte slice.

func AllocateTemporaryBufferBlock

func AllocateTemporaryBufferBlock(originSize int) *StaticBufferBlock

AllocateTemporaryBufferBlock allocates a temporary buffer with size padding.

func NewStaticBufferBlock

func NewStaticBufferBlock(size int) *StaticBufferBlock

NewStaticBufferBlock creates a new StaticBufferBlock with the given size. The buffer is allocated but used is set to 0.

func NewStaticBufferBlockFromData

func NewStaticBufferBlockFromData(data []byte) *StaticBufferBlock

NewStaticBufferBlockFromData creates a StaticBufferBlock from existing data. The data is copied (not referenced) to avoid aliasing issues.

func NewStaticBufferBlockWithUsed

func NewStaticBufferBlockWithUsed(size, used int) *StaticBufferBlock

NewStaticBufferBlockWithUsed creates a StaticBufferBlock with specified size and used.

func (*StaticBufferBlock) Data

func (s *StaticBufferBlock) Data() []byte

Data returns the underlying buffer data pointer (up to size).

func (*StaticBufferBlock) IsEmpty

func (s *StaticBufferBlock) IsEmpty() bool

IsEmpty returns true if no bytes are used.

func (*StaticBufferBlock) MaxSpan

func (s *StaticBufferBlock) MaxSpan() []byte

MaxSpan returns a slice of the entire allocated buffer.

func (*StaticBufferBlock) Reset

func (s *StaticBufferBlock) Reset()

Reset clears the used counter to 0 (does not zero the buffer).

func (*StaticBufferBlock) SetUsed

func (s *StaticBufferBlock) SetUsed(used int)

SetUsed sets the used size, clamped to [0, size].

func (*StaticBufferBlock) Size

func (s *StaticBufferBlock) Size() int

Size returns the total allocated size of the buffer.

func (*StaticBufferBlock) Used

func (s *StaticBufferBlock) Used() int

Used returns the number of bytes actually used.

func (*StaticBufferBlock) UsedSpan

func (s *StaticBufferBlock) UsedSpan() []byte

UsedSpan returns a slice of only the used portion of the buffer.

func (*StaticBufferBlock) Write

func (s *StaticBufferBlock) Write(data []byte) int

Write appends data to the buffer, updating used. Returns the number of bytes written.

func (*StaticBufferBlock) WriteAt

func (s *StaticBufferBlock) WriteAt(offset int, data []byte) int

WriteAt writes data at a specific offset, not updating used. Returns the number of bytes written.

Jump to

Keyboard shortcuts

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