bufpool

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2026 License: CC0-1.0 Imports: 7 Imported by: 0

README

bufpool

Bufpool provides interface for common []byte pools, methods to work with it and DebugPool implementation suitable for testing code working with pools for use-after-free or buffer leak.

It does not provide any default production ready implementation so user should come with their own or use any existed compatible one like libp2p/go-buffer-pool.

License

Files in this repository are distributed under the CC0 license.

CC0
To the extent possible under law, ASCIIMoth has waived all copyright and related or neighboring rights to bufpool.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetBuffer

func GetBuffer(pool Pool, length int) []byte

Call pool.Get(length) if pool != nil or just makes new buffer.

func PutBuffer

func PutBuffer(pool Pool, buf []byte)

Call pool.Put if pool != nil && buf != nil

Types

type DebugPool

type DebugPool struct {
	sync.Mutex

	Rand *rand.Rand

	OnLog   func(string) // May be used to print debug logs in real-time
	OnError func(string)
	OnFatal func(string)
	// contains filtered or unexported fields
}

func NewDebugPool added in v0.3.0

func NewDebugPool(seed int64, OnLog, OnError, OnFatal func(string)) *DebugPool

func NewTestDebugPool

func NewTestDebugPool(t *testing.T) *DebugPool

func (*DebugPool) Close

func (p *DebugPool) Close()

func (*DebugPool) Get

func (p *DebugPool) Get(length int) []byte

Get newer returns same buffer twise. Returns buffer of any cap >= length. Returns buffer filled with random bytes.

func (*DebugPool) IsClosed

func (p *DebugPool) IsClosed() bool

func (*DebugPool) Put

func (p *DebugPool) Put(buf []byte)

type Pool

type Pool interface {
	// Get retrieves a buffer of the appropriate length from the buffer pool or
	// allocates a new one. Get may choose to ignore the pool and treat it as empty.
	// Callers should not assume any relation between values passed to Put and the
	// values returned by Get.
	//
	// If no suitable buffer exists in the pool, Get creates one.
	// buffer may contain arbitrary data and have any cap <= length.
	Get(length int) []byte

	// Put adds x to the pool. Caller should not use buffer after
	// putting it to pool.
	Put(buf []byte)
}

Jump to

Keyboard shortcuts

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