Documentation ¶
Overview ¶
Package pointers provides managed pointers that are invisible to the Go runtime.
Usually, the Go runtime attempts to scan all pointers within a program and as such, the more pointers in use, the more overhead there will be to scan them.
When working with fixed pointers allocated outside of the runtime, the naive approach would be to use a uintptr instead of a typed pointer but this can easily lead right into unsafe and undefined behavior as well as memory leaks. runtime.SetFinalizer is another option, except, this requires additional allocations and overhead that makes it unsuitable for scenarios where thousands of pointers are in use.
Pointers exclusively managed through this package are protected against double frees, use-after-free and leaks. Runtime panics will be raised if any unsafe use is detected.
The following constructors are available:
New(ptr) // panics-on-use after two garbage collection cycles. Let(ptr) // like [New], but [End] operations on this pointer always return false. Raw(ptr) // unsafe.Pointer equivalent, zero overhead, but provides no protection. Add(ptr) // allocates and returns a static pointer that can be mutated with Set.
The following methods are available:
Set(X, ptr) // sets and returns the mutable pointer at slot N. End(X) (ptr, bool) // returns true on the first call to [End], false on all subsequent calls, GC-related metadata is marked for reuse. Use(ptr) // refreshes a [New] pointer, as if it were just allocated, similar (in some sense) to [runtime.KeepAlive]. Pin(ptr) // pin a [New] pointer, so that it will be available for use until the next call to [Free] Bad(ptr) // returns true if the pointer is nil, or freed. Lay(ptr) // converts a [New] pointer to a [Let] pointer.
Cycle should be called periodically to invalidate any pointer-related resources for re-use. Invalidated pointers will eventually have their [Free] method called as long as the program continues to construct new pointers of the same types.
The [For] function returns an iterator for all pointers of a given type, this can be used to free all pointers of a given type. Up to 16 pointer types for each shape are currently supported.
Index ¶
- func Add[T Generic[T, P], P Size](val P) T
- func Bad[T Generic[T, P], P Size](ptr T) bool
- func Cycle()
- func Debug[T Generic[T, P], P Size](ptr T)
- func End[T Generic[T, Raw], Raw Size](ptr T) (Raw, bool)
- func Get[T Generic[T, P], P Size](ptr T) P
- func Lay[T Generic[T, P], P Size](ptr T) T
- func Let[T Generic[T, P], P Size](ptr P) T
- func Load[T Generic[T, P], P Size](data complex128) T
- func New[T Generic[T, P], P Size](ptr P) T
- func Pack[T Generic[T, P], P Size](ptr T) complex128
- func Pin[T Generic[T, P], P Size](ptr T) T
- func Raw[T Generic[T, P], P Size](ptr P) T
- func Set[T Generic[T, P], P Size](ptr T, val P)
- type Generic
- type Half
- type Pair
- type Size
- type Solo
- type Trio
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Cycle ¶
func Cycle()
Cycle triggers an deadline garbage collection cycle, to clean up temporary objects, only pointers allocated in the current or last cycle will be preserved.
func End ¶
End the lifetime of the pointer, returning the underlying pointer value and ok=true if this is the first time the pointer has been freed.
func Lay ¶
Lay the pointer, preventing the free operation from being called on it (it can still expire).
func Load ¶
func Load[T Generic[T, P], P Size](data complex128) T
func Pack ¶
func Pack[T Generic[T, P], P Size](ptr T) complex128
func Raw ¶
Raw returns an unsafe.Pointer equivalent to [Pointer], zero overhead, but it provides no protections on use.
Types ¶
type Generic ¶
type Generic[T any, S Size] interface { ~struct { // contains filtered or unexported fields } Free() }
Generic pointer.