buffer

package
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2024 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Buffer

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

Buffer is equivalent of bytes.Buffer without the ability to read. It is NOT thread-safe.

In UseCalloc mode, z.Calloc is used to allocate memory, which depending upon how the code is compiled could use jemalloc for allocations.

In UseMmap mode, Buffer uses file mmap to allocate memory. This allows us to store big data structures without using physical memory.

MaxSize can be set to limit the memory usage.

func NewBuffer

func NewBuffer(capacity int) *Buffer

func NewBufferSlice

func NewBufferSlice(slice []byte) *Buffer

func (*Buffer) Allocate

func (b *Buffer) Allocate(n int) []byte

Allocate is a way to get a slice of size n back from the buffer. This slice can be directly written to. Warning: Allocate is not thread-safe. The byte slice returned MUST be used before further calls to Buffer.

func (*Buffer) AllocateOffset

func (b *Buffer) AllocateOffset(n int) int

AllocateOffset works the same way as allocate, but instead of returning a byte slice, it returns the offset of the allocation.

func (*Buffer) Bytes

func (b *Buffer) Bytes() []byte

Bytes would return all the written bytes as a slice.

func (*Buffer) Data

func (b *Buffer) Data(offset int) []byte

func (*Buffer) Grow

func (b *Buffer) Grow(n int)

Grow would grow the buffer to have at least n more bytes. In case the buffer is at capacity, it would reallocate twice the size of current capacity + n, to ensure n bytes can be written to the buffer without further allocation. In UseMmap mode, this might result in underlying file expansion.

func (*Buffer) IsEmpty

func (b *Buffer) IsEmpty() bool

func (*Buffer) LenNoPadding

func (b *Buffer) LenNoPadding() int

LenNoPadding would return the number of bytes written to the buffer so far (without the padding).

func (*Buffer) LenWithPadding

func (b *Buffer) LenWithPadding() int

LenWithPadding would return the number of bytes written to the buffer so far plus the padding at the start of the buffer.

func (*Buffer) Release

func (b *Buffer) Release() error

Release would free up the memory allocated by the buffer. Once the usage of buffer is done, it is important to call Release, otherwise a memory leak can happen.

func (*Buffer) Reset

func (b *Buffer) Reset()

Reset would reset the buffer to be reused.

func (*Buffer) Slice

func (b *Buffer) Slice(offset int) ([]byte, int)

Slice would return the slice written at offset.

func (*Buffer) SliceAllocate

func (b *Buffer) SliceAllocate(sz int) []byte

SliceAllocate would encode the size provided into the buffer, followed by a call to Allocate, hence returning the slice of size sz. This can be used to allocate a lot of small buffers into this big buffer. Note that SliceAllocate should NOT be mixed with normal calls to Write.

func (*Buffer) SliceIterate

func (b *Buffer) SliceIterate(f func(slice []byte) error) error

func (*Buffer) SliceOffsets

func (b *Buffer) SliceOffsets() []int

SliceOffsets is an expensive function. Use sparingly.

func (*Buffer) SortSlice

func (b *Buffer) SortSlice(less func(left, right []byte) bool)

SortSlice is like SortSliceBetween but sorting over the entire buffer.

func (*Buffer) SortSliceBetween

func (b *Buffer) SortSliceBetween(start, end int, less LessFunc)

func (*Buffer) StartOffset

func (b *Buffer) StartOffset() int

func (*Buffer) WithMaxSize

func (b *Buffer) WithMaxSize(size int) *Buffer

func (*Buffer) Write

func (b *Buffer) Write(p []byte) (n int, err error)

Write would write p bytes to the buffer.

func (*Buffer) WriteSlice

func (b *Buffer) WriteSlice(slice []byte)

type LessFunc

type LessFunc func(a, b []byte) bool

Jump to

Keyboard shortcuts

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