membuf

package
v5.3.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2021 License: Apache-2.0 Imports: 0 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Allocator

type Allocator interface {
	Alloc(n int) []byte
	Free([]byte)
}

Allocator is the abstract interface for allocating and freeing memory.

type Buffer

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

Buffer represents the reuse buffer.

func NewBuffer

func NewBuffer() *Buffer

NewBuffer creates a new buffer in global pool.

func (*Buffer) AddBytes

func (b *Buffer) AddBytes(bytes []byte) []byte

AddBytes adds the bytes into this Buffer.

func (*Buffer) AllocBytes

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

AllocBytes allocates bytes with the given length.

func (*Buffer) Destroy

func (b *Buffer) Destroy()

Destroy frees all buffers.

func (*Buffer) Reset

func (b *Buffer) Reset()

Reset resets the buffer.

func (*Buffer) TotalSize

func (b *Buffer) TotalSize() int64

TotalSize represents the total memory size of this Buffer.

type Pool

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

Pool is like `sync.Pool`, which manages memory for all bytes buffers.

NOTE: we don't used a `sync.Pool` because when will sync.Pool release is depending on the garbage collector which always release the memory so late. Use a fixed size chan to reuse can decrease the memory usage to 1/3 compare with sync.Pool.

func NewPool

func NewPool(size int, allocator Allocator) *Pool

NewPool creates a new pool.

func (*Pool) NewBuffer

func (p *Pool) NewBuffer() *Buffer

NewBuffer creates a new buffer in current pool.

Jump to

Keyboard shortcuts

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