heapref

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: MIT Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

type FuncID[F any] uint32

FuncID names a function held by a Funcs in four bytes and may be stored off-heap. The zero FuncID is nil.

func (FuncID[F]) IsNil

func (id FuncID[F]) IsNil() bool

IsNil reports whether id refers to nothing.

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

func (fs *Funcs[F]) Get(id FuncID[F]) F

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.

func (*Funcs[F]) Len

func (fs *Funcs[F]) Len() int

Len returns the number of registered functions.

func (*Funcs[F]) Register

func (fs *Funcs[F]) Register(f F) (FuncID[F], error)

Register stores f and returns its FuncID. It fails as Table.Put does.

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.

func (Ref[T]) IsNil

func (r Ref[T]) IsNil() bool

IsNil reports whether r refers to nothing.

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

func (t *Table[T]) Delete(r Ref[T])

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

func (t *Table[T]) Get(r Ref[T]) T

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.

func (*Table[T]) Len

func (t *Table[T]) Len() int

Len returns the number of values held.

func (*Table[T]) Put

func (t *Table[T]) Put(v T) (Ref[T], error)

Put stores v and returns its Ref. It fails only once 2³²−1 slots are in use.

func (*Table[T]) Set

func (t *Table[T]) Set(r Ref[T], v T)

Set replaces the value r names, panicking as Get does.

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

func (w *Weak[T]) Delete(r WeakRef[T])

Delete frees the slot r names and invalidates r, panicking as Table.Delete does.

func (*Weak[T]) Get

func (w *Weak[T]) Get(r WeakRef[T]) *T

Get returns the pointer r names, or nil once its target has been collected. It panics as Table.Get does.

func (*Weak[T]) Len

func (w *Weak[T]) Len() int

Len returns the number of slots in use, including those whose target has been collected.

func (*Weak[T]) Put

func (w *Weak[T]) Put(p *T) (WeakRef[T], error)

Put stores a weak pointer to p and returns its WeakRef. It fails as Table.Put does.

func (*Weak[T]) Set

func (w *Weak[T]) Set(r WeakRef[T], p *T)

Set makes r name p instead, panicking as Table.Get does.

type WeakRef

type WeakRef[T any] struct {
	// contains filtered or unexported fields
}

WeakRef names a pointer held by a Weak in eight bytes and may be stored off-heap. The zero WeakRef is nil.

func (WeakRef[T]) IsNil

func (r WeakRef[T]) IsNil() bool

IsNil reports whether r refers to nothing.

Jump to

Keyboard shortcuts

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