Documentation
¶
Overview ¶
Package heapref lets off-heap memory refer to values on the Go heap.
Off-heap memory may not hold Go pointers, because the garbage collector cannot see them there. A Table keeps values of any type, such as funcs, interfaces, maps or closures, in an ordinary Go slice the collector does scan, and names each by a pointer-free Ref that may be stored off-heap. Weak does the same without keeping its targets alive, and Funcs names functions registered once by a 4-byte FuncID.
A Ref carries the generation of its slot, so with the guards compiled in, using a Ref after Delete, or one from another Table, panics. Building with the memory_unchecked tag compiles these checks out, leaving only Go's bounds checks.
None of the types here is safe for concurrent use.
Example ¶
package main
import (
"fmt"
memory "github.com/stubbedev/memory-go"
"github.com/stubbedev/memory-go/heapref"
)
func main() {
var a memory.Local
defer a.Close()
var handlers heapref.Table[func(int) string]
type timer struct {
Deadline uint64
OnFire heapref.Ref[func(int) string]
}
prefix := "fired"
onFire, err := handlers.Put(func(n int) string { return fmt.Sprint(prefix, " ", n) })
if err != nil {
panic(err)
}
t, err := memory.New[timer](&a)
if err != nil {
panic(err)
}
*t.Value() = timer{Deadline: 100, OnFire: onFire}
fmt.Println(handlers.Get(t.Value().OnFire)(int(t.Value().Deadline)))
handlers.Delete(t.Value().OnFire)
fmt.Println(handlers.Len())
}
Output: fired 100 0
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FuncID ¶
FuncID names a function held by a Funcs in four bytes and may be stored off-heap. The zero FuncID is nil.
type Funcs ¶
type Funcs[F any] struct { // contains filtered or unexported fields }
Funcs registers functions of type F for good and names each by a FuncID, for callbacks named from off-heap memory. Registering the same function twice yields two IDs: func values cannot be compared, and two closures over different variables share their code, so register each function once, typically at initialisation, and keep its ID. The zero Funcs is empty and ready to use; it must not be copied after first use. It is not safe for concurrent use.
Example ¶
package main
import (
"fmt"
"github.com/stubbedev/memory-go/heapref"
)
func main() {
var ops heapref.Funcs[func(a, b int) int]
add, _ := ops.Register(func(a, b int) int { return a + b })
mul, _ := ops.Register(func(a, b int) int { return a * b })
type instr struct {
Op heapref.FuncID[func(a, b int) int]
A, B int32
}
prog := []instr{{add, 2, 3}, {mul, 4, 5}}
for _, in := range prog {
fmt.Println(ops.Get(in.Op)(int(in.A), int(in.B)))
}
}
Output: 5 20
func (*Funcs[F]) Get ¶
Get returns the function id names. It panics if id is nil, and with the guards compiled in, if id comes from another Funcs with more functions.
type Ref ¶
type Ref[T any] struct { // contains filtered or unexported fields }
Ref names a value held by a Table in eight bytes and may be stored off-heap. The zero Ref is nil.
type Table ¶
type Table[T any] struct { // contains filtered or unexported fields }
Table holds values on the Go heap until they are deleted. The zero Table is empty and ready to use; it must not be copied after first use. It is not safe for concurrent use.
func (*Table[T]) Delete ¶
Delete removes the value r names, so the collector may reclaim it once nothing else refers to it, and invalidates r. It panics as Get does, including when r was already deleted.
func (*Table[T]) Get ¶
Get returns the value r names. It panics if r is nil, and with the guards compiled in, if r was deleted or comes from another Table.
type Weak ¶
type Weak[T any] struct { // contains filtered or unexported fields }
Weak holds pointers without keeping their targets alive, so an off-heap cache can refer to Go objects the program may still drop. A slot outlives its target: Get then returns nil, and the slot stays in use until Delete. The zero Weak is empty and ready to use; it must not be copied after first use. It is not safe for concurrent use.
Example ¶
package main
import (
"fmt"
"strings"
"github.com/stubbedev/memory-go/heapref"
)
func main() {
var cache heapref.Weak[strings.Builder]
b := &strings.Builder{}
r, err := cache.Put(b)
if err != nil {
panic(err)
}
cache.Get(r).WriteString("still alive")
fmt.Println(b.String())
}
Output: still alive
func (*Weak[T]) Delete ¶
Delete frees the slot r names and invalidates r, panicking as Table.Delete does.
func (*Weak[T]) Get ¶
Get returns the pointer r names, or nil once its target has been collected. It panics as Table.Get does.
func (*Weak[T]) Len ¶
Len returns the number of slots in use, including those whose target has been collected.