Documentation
¶
Overview ¶
Package memory allocates typed values outside the Go heap without cgo.
Memory comes from modernc.org/memory, a pure Go allocator that maps pages from the operating system directly. The garbage collector neither scans nor frees it, so every value placed there must be free of Go pointers: no pointers, slices, strings, maps, channels, functions or interfaces. Every allocation checks this and returns ErrHoldsPointers otherwise. The handle types of this package, Ptr, Ref, Slice and String, point only into memory the garbage collector does not manage and may be stored off-heap.
Local serves a single goroutine; Shared serialises access for several.
Misuse panics instead of corrupting memory. Every allocation carries a 16-byte header naming its allocator and whether it is live, so a double free, a free or resize through the wrong allocator, and reading a freed Ptr, Slice or String through its handle all panic, as does using a Local from several goroutines at once. A handle to memory that has since been freed and handed out again cannot be told apart from its new owner, and Refs and Go pointers obtained from handles are not checked.
Example ¶
package main
import (
"fmt"
memory "github.com/stubbedev/memory-go"
)
func main() {
var a memory.Local
defer a.Close()
type vec3 struct{ X, Y, Z float64 }
v, err := memory.New[vec3](&a)
if err != nil {
panic(err)
}
*v.Value() = vec3{X: 1, Y: 2, Z: 3}
fmt.Println(*v.Value())
s, err := memory.MakeSlice[int](&a, 3)
if err != nil {
panic(err)
}
copy(s.Elems(), []int{10, 20, 30})
if s, err = memory.ResizeSlice(&a, s, 4); err != nil {
panic(err)
}
fmt.Println(s.Elems())
_, err = memory.New[string](&a)
fmt.Println(err)
if err := memory.FreeSlice(&a, s); err != nil {
panic(err)
}
if err := memory.Free(&a, v); err != nil {
panic(err)
}
fmt.Println(a.Stats())
}
Output: {1 2 3} [10 20 30 0] memory: string: type holds Go pointers {0 0}
Example (LinkedValues) ¶
package main
import (
"fmt"
memory "github.com/stubbedev/memory-go"
)
func main() {
var a memory.Local
defer a.Close()
type node struct {
Name memory.String
Next memory.Ptr[node]
}
var head memory.Ptr[node]
for _, name := range []string{"c", "b", "a"} {
n, err := memory.New[node](&a)
if err != nil {
panic(err)
}
label, err := memory.NewString(&a, name)
if err != nil {
panic(err)
}
*n.Value() = node{Name: label, Next: head}
head = n
}
for n := head; !n.IsNil(); {
fmt.Print(n.Value().Name, " ")
next := n.Value().Next
if err := memory.FreeString(&a, n.Value().Name); err != nil {
panic(err)
}
if err := memory.Free(&a, n); err != nil {
panic(err)
}
n = next
}
fmt.Println()
}
Output: a b c
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrHoldsPointers reports a type that holds Go pointers and so cannot // live off-heap. ErrHoldsPointers = errors.New("type holds Go pointers") // ErrNegativeLen reports a negative length or capacity. ErrNegativeLen = errors.New("negative length") // ErrTooLarge reports a size that overflows int. ErrTooLarge = errors.New("allocation size overflows int") )
Functions ¶
func Free ¶
Free releases p, which must come from New on the same allocator. Freeing a nil Ptr does nothing. Freeing p twice, or through another allocator, panics.
Types ¶
type Allocator ¶
type Allocator interface {
// contains filtered or unexported methods
}
Allocator is the memory source every function and container in this module takes. It is implemented by *Local and *Shared only.
type Local ¶
type Local struct {
// contains filtered or unexported fields
}
Local is an Allocator for a single goroutine. Its zero value is ready for use, and it must not be copied after first use. Using it from several goroutines at once panics; use Shared for that.
func (*Local) Close ¶
Close unmaps every page of l, invalidating everything allocated from it, and leaves l ready for reuse. Handles from before Close are rejected afterwards.
type Ptr ¶
type Ptr[T any] struct { // contains filtered or unexported fields }
Ptr owns a single off-heap T allocated by New. Free releases it. The zero Ptr is nil.
type Ref ¶
type Ref[T any] struct { // contains filtered or unexported fields }
Ref is a non-owning reference to an off-heap T, such as an element of a Slice or a value in a typedarena. It cannot be freed on its own. The zero Ref is nil.
type Shared ¶
type Shared struct {
// contains filtered or unexported fields
}
Shared is an Allocator safe for concurrent use: a mutex serialises every call. Its zero value is ready for use. Containers built on it are still not safe for concurrent use.
func (*Shared) Close ¶
Close unmaps every page of s, invalidating everything allocated from it, and leaves s ready for reuse.
type Slice ¶
type Slice[T any] struct { // contains filtered or unexported fields }
Slice owns n consecutive off-heap Ts allocated by MakeSlice or ResizeSlice. FreeSlice releases them. The zero Slice is empty.
func ResizeSlice ¶
ResizeSlice changes the length of s to n, keeping the common prefix and zeroing any new elements. On error s is left intact. n == 0 frees s. s must come from the same allocator and be live, or ResizeSlice panics.
type Stats ¶
type Stats struct {
// Live is the number of allocations not yet freed.
Live int
// Bytes is the usable size of those allocations, including rounding.
Bytes int
}
Stats describes the allocations an allocator currently holds.
type String ¶
type String struct {
// contains filtered or unexported fields
}
String owns off-heap UTF-8 bytes. Unlike a Go string it may be stored off-heap. FreeString releases it. The zero String is empty.
func AppendString ¶
AppendString appends more to s, returning the grown String. On error s is left intact.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package hashmap provides a hash map whose entries live off the Go heap.
|
Package hashmap provides a hash map whose entries live off the Go heap. |
|
internal
|
|
|
memtest
Package memtest provides test helpers shared by the memory-go packages.
|
Package memtest provides test helpers shared by the memory-go packages. |
|
Package linkedlist provides a doubly linked list whose nodes live off the Go heap in one contiguous pool and link to each other by index.
|
Package linkedlist provides a doubly linked list whose nodes live off the Go heap in one contiguous pool and link to each other by index. |
|
Package minheap provides a binary heap whose elements live off the Go heap.
|
Package minheap provides a binary heap whose elements live off the Go heap. |
|
Package typedarena provides a chunked bump allocator for a single type whose values live off the Go heap and are freed all at once.
|
Package typedarena provides a chunked bump allocator for a single type whose values live off the Go heap and are freed all at once. |
|
Package vector provides a growable slice whose elements live off the Go heap.
|
Package vector provides a growable slice whose elements live off the Go heap. |