memory

package
v18.0.0-...-e99480f Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2025 License: Apache-2.0, BSD-3-Clause Imports: 12 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func BitClear

func BitClear(arr []byte, index int)

Clear bit at position index in array

func BitIsSet

func BitIsSet(arr []byte, index int) bool

Return 1 if bit at position index in array is set to 1

func BitSet

func BitSet(arr []byte, index int)

Set bit at position index in array to 1

func BlkAddr

func BlkAddr(l, bi int) int

Convert a block index at layer l back into an offset

func BlkIndex

func BlkIndex(l, offset int) int

Compute the block index for offset at layer l

func BlkIndexNext

func BlkIndexNext(l, offset int) int

Compute the first block index at layer l after offset

func BlkSize

func BlkSize(l int) int

Compute block size at layer l

func FirstLayer

func FirstLayer(n int) int

Return the first layer whose block size is larger than n

func FreeMemory

func FreeMemory()

Free all the memory allocated for arenas. TODO(joechenrh): check if there are anyone using the arenas.

func ReleaseBuffers

func ReleaseBuffers(buffers []*Buffer)

func Set

func Set(buf []byte, c byte)

Set assigns the value c to every element of the slice buf.

func SetMaxMemoryUsage

func SetMaxMemoryUsage(size int)

func UnsafeGetBlkAddr

func UnsafeGetBlkAddr(slice []byte) uintptr

Convert slice to an uintptr. This value is used as key in map.

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 BuddyAllocator

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

func (*BuddyAllocator) Allocate

func (b *BuddyAllocator) Allocate(size int) []byte

func (*BuddyAllocator) Allocated

func (b *BuddyAllocator) Allocated() int64

func (*BuddyAllocator) Close

func (b *BuddyAllocator) Close()

Close return the allocated memory to the pool

func (*BuddyAllocator) Free

func (b *BuddyAllocator) Free(bs []byte)

func (*BuddyAllocator) Init

func (b *BuddyAllocator) Init(_ int)

func (*BuddyAllocator) Reallocate

func (b *BuddyAllocator) Reallocate(size int, bs []byte) []byte

type Buffer

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

Buffer is a wrapper type for a buffer of bytes.

func NewBufferBytes

func NewBufferBytes(data []byte) *Buffer

NewBufferBytes creates a fixed-size buffer from the specified data.

func NewBufferWithAllocator

func NewBufferWithAllocator(data []byte, mem Allocator) *Buffer

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

func NewResizableBuffer(mem Allocator) *Buffer

NewResizableBuffer creates a mutable, resizable buffer with an Allocator for managing memory.

func SliceBuffer

func SliceBuffer(buf *Buffer, offset, length int) *Buffer

func (*Buffer) Buf

func (b *Buffer) Buf() []byte

Buf returns the slice of memory allocated by the Buffer, which is adjusted by calling Reserve.

func (*Buffer) Bytes

func (b *Buffer) Bytes() []byte

Bytes returns a slice of size Len, which is adjusted by calling Resize.

func (*Buffer) Cap

func (b *Buffer) Cap() int

Cap returns the capacity of the buffer.

func (*Buffer) Len

func (b *Buffer) Len() int

Len returns the length of the buffer.

func (*Buffer) Mutable

func (b *Buffer) Mutable() bool

Mutable returns a bool indicating whether the buffer is mutable or not.

func (*Buffer) Parent

func (b *Buffer) Parent() *Buffer

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) Reserve

func (b *Buffer) Reserve(capacity int)

Reserve reserves the provided amount of capacity for the buffer.

func (*Buffer) Reset

func (b *Buffer) Reset(buf []byte)

Reset resets the buffer for reuse.

func (*Buffer) Resize

func (b *Buffer) Resize(newSize int)

Resize resizes the buffer to the target size.

func (*Buffer) ResizeNoShrink

func (b *Buffer) ResizeNoShrink(newSize int)

ResizeNoShrink resizes the buffer to the target size, but will not shrink it.

func (*Buffer) Retain

func (b *Buffer) Retain()

Retain increases the reference count by 1.

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

type TestingT

type TestingT interface {
	Errorf(format string, args ...interface{})
	Helper()
}

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.

Jump to

Keyboard shortcuts

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