slice

package
Version: v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2022 License: BSD-3-Clause Imports: 2 Imported by: 0

Documentation

Overview

Package slice implements pools of pointers to slices.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pool

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

Pool implements a pool of pointers to slices.

Example usage pattern (assuming pool is, for example, a *[]byte Pool)

p := pool.Get(size).(*[]byte)
b := *p	// Now you can use b in any way you need.
...
// When b will not be used anymore
pool.Put(p)
...
// If b or p are not going out of scope soon, optionally
b = nil
p = nil

Otherwise the pool cannot release the slice on garbage collection.

Do not do

p := pool.Get(size).(*[]byte)
b := *p
...
pool.Put(&b)

or

b := *pool.Get(size).(*[]byte)
...
pool.Put(&b)
var (
	// Bytes is a ready to use *[]byte Pool.
	Bytes *Pool
	// Ints is a ready to use *[]int Pool.
	Ints *Pool
)

func NewPool

func NewPool(
	create func(size int) interface{},
	clear func(interface{}),
	setSize func(p interface{}, size int),
	cap func(p interface{}) int,
) *Pool

NewPool returns a newly created Pool. Assuming the desired slice type is []T:

The create function returns a *[]T of len == cap == size.

The argument of clear is *[]T and the function sets all the slice elements to the respective zero value.

The setSize function gets a *[]T and sets its len to size.

The cap function gets a *[]T and returns its capacity.

func (*Pool) CGet

func (p *Pool) CGet(size int) interface{}

CGet returns a *[]T of len size. The pointed to slice is zeroed up to its cap. CGet panics for size < 0.

CGet is safe for concurrent use by multiple goroutines.

func (*Pool) Get

func (p *Pool) Get(size int) interface{}

Get returns a *[]T of len size. The pointed to slice is not zeroed. Get panics for size < 0.

Get is safe for concurrent use by multiple goroutines.

func (*Pool) Put

func (p *Pool) Put(b interface{})

Put puts a *[]T into a pool for possible later reuse by CGet or Get. Put panics is its argument is not of type *[]T.

Put is safe for concurrent use by multiple goroutines.

Source Files

Jump to

Keyboard shortcuts

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