Documentation
¶
Overview ¶
Package slab provides a 100% golang slab allocator for byte slices.
Index ¶
- type Arena
- func (s *Arena) AddRef(buf []byte)
- func (s *Arena) Alloc(bufLen int) (buf []byte)
- func (s *Arena) BufToLoc(buf []byte) Loc
- func (s *Arena) DecRef(buf []byte) bool
- func (s *Arena) GetNext(buf []byte) (bufNext []byte)
- func (s *Arena) LocAddRef(loc Loc)
- func (s *Arena) LocDecRef(loc Loc)
- func (s *Arena) LocToBuf(loc Loc) []byte
- func (s *Arena) Owns(buf []byte) bool
- func (s *Arena) SetNext(buf, bufNext []byte)
- func (s *Arena) Stats(m map[string]int64) map[string]int64
- type Loc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Arena ¶
type Arena struct {
// contains filtered or unexported fields
}
An Arena manages a set of slab classes and memory.
func NewArena ¶
func NewArena(startChunkSize int, slabSize int, growthFactor float64, malloc func(size int) []byte) *Arena
NewArena returns an Arena to manage byte slice memory based on a slab allocator approach.
The startChunkSize and slabSize should be > 0. The growthFactor should be > 1.0. The malloc() func is invoked when Arena needs memory for a new slab. When malloc() is nil, then Arena defaults to make([]byte, size).
func (*Arena) AddRef ¶
AddRef increase the ref count on a buf. The input buf must be from an Alloc() from the same Arena.
func (*Arena) Alloc ¶
Alloc may return nil on errors, such as if no more free chunks are available and new slab memory was not allocatable (such as if malloc() returns nil). The returned buf may not be append()'ed to for growth. The returned buf must be DecRef()'ed for memory reuse.
func (*Arena) BufToLoc ¶
BufToLoc returns a Loc that represents an Arena-managed buf. Does not affect the reference count of the buf. The buf slice must have start position 0 (or bufStart of 0).
func (*Arena) DecRef ¶
DecRef decreases the ref count on a buf. The input buf must be from an Alloc() from the same Arena. Once the buf's ref-count drops to 0, the Arena may reuse the buf. Returns true if this was the last DecRef() invocation (ref count reached 0).
func (*Arena) GetNext ¶
GetNext returns the next chained buf for the given input buf. The buf's managed by an Arena can be chained. The returned bufNext may be nil. When the returned bufNext is non-nil, the caller owns a ref-count on bufNext and must invoke DecRef(bufNext) when the caller is finished using bufNext.
func (*Arena) LocToBuf ¶
LocToBuf returns a buf for an Arena-managed Loc. Does not affect the reference count of the buf. The Loc may have come from Loc.Slice().
func (*Arena) SetNext ¶
SetNext associates the next chain buf following the input buf to be bufNext. The buf's from an Arena can be chained, where buf will own an AddRef() on bufNext. When buf's ref-count goes to zero, it will call DecRef() on bufNext. The bufNext may be nil. The bufNext must have start position 0 (or bufStart of 0) with respect to its backing buffer.
type Loc ¶
type Loc struct {
// contains filtered or unexported fields
}
An opaque reference to bytes managed by an Arena. See Arena.BufToLoc/LocToBuf(). A Loc struct is GC friendly in that a Loc does not have direct pointer fields into the Arena's memory that the GC's scanner must traverse.
func (Loc) Slice ¶
Slice returns a Loc that a represents a different slice of the backing buffer, where the bufStart and bufLen are relative to the backing buffer. Does not change the ref-count of the underlying buffer.
NOTE: Many API's (such as BufToLoc) do not correctly handle Loc's with non-zero bufStart, so use sliced Loc's with caution.