internal: github.com/cznic/internal/slice Index | Files

package slice

import "github.com/cznic/internal/slice"

Package slice implements pools of pointers to slices.

Index

Package Files

pool.go

type Pool Uses

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 Uses

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 Uses

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 Uses

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 Uses

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.

Package slice imports 2 packages (graph) and is imported by 1 packages. Updated 2017-05-29. Refresh now. Tools for package owners.