buf

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2020 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package buf manages allocation of temporary short-term buffers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Buf

type Buf struct {
	Data []byte
	// contains filtered or unexported fields
}

Buf represents allocated slice of memory pool. At the end of using the buffer, Release() must be called to reclaim memory.

func (*Buf) IsPooled

func (b *Buf) IsPooled() bool

IsPooled determines whether data slice is part of a pool.

func (*Buf) Release

func (b *Buf) Release()

Release returns the slice back to the pool.

type Pool

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

Pool manages allocations of short-term data buffers from a pool.

Note that buffers managed by the pool are meant to be extremely short lived and are suitable for in-memory operations, such as encryption, compression, etc, but not for I/O buffers of any kind. It is EXTREMELY important to always release memory allocated from the Pool. Failure to do so will result in memory leaks.

The pool uses N segments, with each segment tracking its high water mark usage.

Allocation simply advances the high water mark within first segment that has capacity and increments per-segment refcount.

On Buf.Release() the refcount is decremented and when it hits zero, the entire segment becomes instantly freed.

As an extra optimization, when Buf.Release() is called in LIFO order, it will also lower the high water mark making its memory available for immediate reuse.

If no segment has available capacity, the pool waits a few times until memory becomes released and falls back to allocating from the heap.

func NewPool

func NewPool(ctx context.Context, segmentSize int, poolID string) *Pool

NewPool creates a buffer pool, composed of fixed-length segments of specified maximum size.

func (*Pool) AddSegments

func (p *Pool) AddSegments(n int)

AddSegments n segments to the pool.

func (*Pool) Allocate

func (p *Pool) Allocate(n int) Buf

Allocate allocates from the buffer a slice of size n.

func (*Pool) Close

func (p *Pool) Close()

Close closes the pool.

func (*Pool) SetSegmentSize

func (p *Pool) SetSegmentSize(maxSize int)

SetSegmentSize sets the segment size for future segments that will be created.

Jump to

Keyboard shortcuts

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