Documentation ¶
Overview ¶
Package memory provides support for allocating and manipulating memory at a low level.
The build tag 'mallocator' will switch the default allocator to one backed by libc malloc. This also requires CGO.
Index ¶
- func ReleaseBuffers(buffers []*Buffer)
- func Set(buf []byte, c byte)
- type Allocator
- type Buffer
- func (b *Buffer) Buf() []byte
- func (b *Buffer) Bytes() []byte
- func (b *Buffer) Cap() int
- func (b *Buffer) Len() int
- func (b *Buffer) Mutable() bool
- func (b *Buffer) Parent() *Buffer
- func (b *Buffer) Release()
- func (b *Buffer) Reserve(capacity int)
- func (b *Buffer) Reset(buf []byte)
- func (b *Buffer) Resize(newSize int)
- func (b *Buffer) ResizeNoShrink(newSize int)
- func (b *Buffer) Retain()
- type CheckedAllocator
- type CheckedAllocatorScope
- type GoAllocator
- type TestingT
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ReleaseBuffers ¶
func ReleaseBuffers(buffers []*Buffer)
Types ¶
type Allocator ¶
type Allocator interface { Allocate(size int) []byte Reallocate(size int, b []byte) []byte Free(b []byte) }
var DefaultAllocator Allocator = NewGoAllocator()
DefaultAllocator is a default implementation of Allocator and can be used anywhere an Allocator is required.
DefaultAllocator is safe to use from multiple goroutines.
type Buffer ¶
type Buffer struct {
// contains filtered or unexported fields
}
Buffer is a wrapper type for a buffer of bytes.
func NewBufferBytes ¶
NewBufferBytes creates a fixed-size buffer from the specified data.
func NewBufferWithAllocator ¶
NewBufferWithAllocator returns a buffer with the mutable flag set as false. The intention here is to allow wrapping a byte slice along with an allocator as a buffer to track the lifetime via refcounts in order to call Free when the refcount goes to zero.
The primary example this is used for, is currently importing data through the c data interface and tracking the lifetime of the imported buffers.
func NewResizableBuffer ¶
NewResizableBuffer creates a mutable, resizable buffer with an Allocator for managing memory.
func SliceBuffer ¶
func (*Buffer) Buf ¶
Buf returns the slice of memory allocated by the Buffer, which is adjusted by calling Reserve.
func (*Buffer) Parent ¶
Parent returns either nil or a pointer to the parent buffer if this buffer was sliced from another.
func (*Buffer) Release ¶
func (b *Buffer) Release()
Release decreases the reference count by 1. When the reference count goes to zero, the memory is freed.
func (*Buffer) ResizeNoShrink ¶
ResizeNoShrink resizes the buffer to the target size, but will not shrink it.
type CheckedAllocator ¶
type CheckedAllocator struct {
// contains filtered or unexported fields
}
func NewCheckedAllocator ¶
func NewCheckedAllocator(mem Allocator) *CheckedAllocator
func (*CheckedAllocator) Allocate ¶
func (a *CheckedAllocator) Allocate(size int) []byte
func (*CheckedAllocator) AssertSize ¶
func (a *CheckedAllocator) AssertSize(t TestingT, sz int)
func (*CheckedAllocator) CurrentAlloc ¶
func (a *CheckedAllocator) CurrentAlloc() int
func (*CheckedAllocator) Free ¶
func (a *CheckedAllocator) Free(b []byte)
func (*CheckedAllocator) Reallocate ¶
func (a *CheckedAllocator) Reallocate(size int, b []byte) []byte
type CheckedAllocatorScope ¶
type CheckedAllocatorScope struct {
// contains filtered or unexported fields
}
func NewCheckedAllocatorScope ¶
func NewCheckedAllocatorScope(alloc *CheckedAllocator) *CheckedAllocatorScope
func (*CheckedAllocatorScope) CheckSize ¶
func (c *CheckedAllocatorScope) CheckSize(t TestingT)
type GoAllocator ¶
type GoAllocator struct{}
func NewGoAllocator ¶
func NewGoAllocator() *GoAllocator
func (*GoAllocator) Allocate ¶
func (a *GoAllocator) Allocate(size int) []byte
func (*GoAllocator) Free ¶
func (a *GoAllocator) Free(b []byte)
func (*GoAllocator) Reallocate ¶
func (a *GoAllocator) Reallocate(size int, b []byte) []byte
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package mallocator defines an allocator implementation for memory.Allocator which defers to libc malloc.
|
Package mallocator defines an allocator implementation for memory.Allocator which defers to libc malloc. |