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)
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 ¶
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.