xunsafe

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2025 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package unsafe provides a more convenient interface for performing unsafe operations than Go's built-in package unsafe.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add

func Add[P ~*E, E any, I Int](p P, n I) P

Add adds the given offset to p, scaled by the size of T.

func AnyBytes

func AnyBytes(v any) []byte

AnyBytes extracts a slice pointing to the variable-length data of an any.

func AnyData

func AnyData(v any) *byte

AnyData extracts the pointer value from an any.

func AnyType

func AnyType(v any) uintptr

AnyType extracts the opaque type from an any.

func AssertInlinedAny

func AssertInlinedAny[T any](t testing.TB)

AssertInlinedAny is a helper for testing that T does not allocate when converted to an interface.

func BitCast

func BitCast[To, From any](v From) To

BitCast performs an unsafe bitcast from one type to another.

func BoundsCheck

func BoundsCheck(n, len int)

BoundsCheck emulates a bounds check on a slice with the given index and length.

func ByteAdd

func ByteAdd[T any, P ~*E, E any, I Int](p P, n I) *T

ByteAdd adds the given offset to p, without scaling.

It also throws in a cast for free.

func ByteLoad

func ByteLoad[T any, P ~*E, E any, I Int](p P, n I) T

ByteLoad loads a value of the given type at the given byte offset.

func ByteStore

func ByteStore[T any, P ~*E, E any, I Int](p P, n I, v T)

ByteLoad stores a value of the given type at the given byte offset.

func ByteSub

func ByteSub[P1 ~*E1, P2 ~*E2, E1, E2 any](p1 P1, p2 P2) int

Sub computes the difference between two pointers, without scaling.

func Bytes

func Bytes[P ~*E, E any](p P) []byte

Bytes converts a pointer into a slice of its contents.

func Cast

func Cast[To, From any](p *From) *To

Cast casts one pointer type to another.

func Clear

func Clear[P ~*E, E any, I Int](p P, n I)

Clear zeros n elements at p.

func Copy

func Copy[P ~*E, E any, I Int](dst, src P, n I)

Copy copies n elements from one pointer to the other.

func Escape

func Escape[P ~*E, E any](p P) P

Escape escapes a pointer to the heap.

func IsDirect

func IsDirect[T any]() bool

IsDirect returns whether converting T into an interface requires calling the allocator.

This is true for any type which is one of the inlined primitives (pointers, interfaces, channels, maps) or one-field structs/arrays whose sole element is inlined. Note that this does *not* include all pointer-shaped structs, such as struct{struct{}; int}.

func IsDirectAny

func IsDirectAny(v any) bool

IsDirectAny returns whether or not this any is a direct interface.

This is much slower than IsDirect, because we can't use the same trick we use in IsDirect without making a heap allocation in some cases.

func Load

func Load[P ~*E, E any, I Int](p P, n I) E

Load loads a value of the given type at the given index.

func LoadSlice

func LoadSlice[S ~[]E, E any, I Int](s S, n I) E

LoadSlice loads a slice without performing a bounds check.

func MakeAny

func MakeAny(typ uintptr, data *byte) any

MakeAny builds an any out of the given data.

func NoEscape

func NoEscape[P ~*E, E any](p P) P

NoEscape hides a pointer from escape analysis, preventing it from escaping to the heap.

func Ping

func Ping[P ~*E, E any](p P)

Ping reminds the processor that *p should be loaded into the data cache.

func SliceToString

func SliceToString[S ~[]E, E any](s S) string

SliceToString converts a slice into a string, multiplying the slice length as appropriate.

func Store

func Store[P ~*E, E any, I Int](p P, n I, v E)

Store stores a value at the given index.

func StoreNoWB

func StoreNoWB[P ~*E, E any](p *P, q P)

StoreNoWB performs a store without generating any write barriers.

func StoreNoWBUntyped

func StoreNoWBUntyped[P ~unsafe.Pointer](p *P, q P)

StoreNoWBUntyped performs a store without generating any write barriers.

func StringToSlice

func StringToSlice[S ~[]E, E any](s string) S

StringToSlice converts a string into a slice, multiplying the slice length as appropriate.

func Sub

func Sub[P ~*E, E any](p1, p2 P) int

Sub computes the difference between two pointers, scaled by the size of T.

Types

type Addr

type Addr[T any] intptr

Addr is a typed raw address.

The underlying type is an int64 in order to work around a Go codegen bug. The bug is essentially that we want to do an arithmetic shift on the value, which requires casting what would normally be a uintptr to int64. For some reason, when in a generic context, this confuses Go's inliner *just enough* to cause things to fail to inline, resulting in a generic function call on the critical path.

func AddrOf

func AddrOf[P ~*E, E any](p P) Addr[E]

AddrOf gets the address of a pointer.

func EndOf

func EndOf[S ~[]E, E any](s S) Addr[E]

EndOf calculates the one-past-the-end address of s without creating an intermediate one-past-the-end pointer.

func (Addr[T]) Add

func (a Addr[T]) Add(n int) Addr[T]

Add adds the given offset to this address.

func (Addr[T]) AssertValid

func (a Addr[T]) AssertValid() *T

AssertValid asserts that this address is a valid pointer.

func (Addr[T]) ByteAdd

func (a Addr[T]) ByteAdd(n int) Addr[T]

ByteAdd adds the given unscaled offset to this address.

func (Addr[T]) ClearSignBit

func (a Addr[T]) ClearSignBit() Addr[T]

ClearSignBit clears the sign bit of this address, flipping all of the other bits in the process.

func (Addr[T]) Format

func (a Addr[T]) Format(state fmt.State, verb rune)

Format implements fmt.Formatter.

func (Addr[T]) Padding

func (a Addr[T]) Padding(align int) int

Padding returns the number of bytes between this address and the next address aligned to the given alignment, which must be a power of two.

func (Addr[T]) RoundUpTo

func (a Addr[T]) RoundUpTo(align int) Addr[T]

RoundUp rounds this address upwards to align, which must be a power of two.

func (Addr[T]) SignBit

func (a Addr[T]) SignBit() bool

SignBit returns whether this address has its sign bit set.

Pointers with the high bits set are never used by Go, so we can use this bit to store extra information.

func (Addr[T]) SignBitMask

func (a Addr[T]) SignBitMask() Addr[T]

SignBitMask returns either all zeros or all ones, according to the sign bit of a.

func (Addr[T]) Sub

func (a Addr[T]) Sub(b Addr[T]) int

Add adds the given offset to this address.

type Int

type Int = layout.Int

Int is any integer type.

type NoCopy

type NoCopy [0]sync.Mutex

NoCopy is a type that go vet will complain about having been moved.

It does so by implementing sync.Locker.

type PC

type PC[F any] uintptr

PC is a raw function pointer, which can be used to store captureless funcs.

Suppose a func() is in rax. Go implements calling it by emitting the following code:

mov  rdx, rax
mov  rcx, [rdx]
call rcx

For a captureless func, this load will be of a constant containing the PC of the function to call. This can result in cache misses. This type works around that by keeping the PC local, so the resulting load avoids this problem.

func NewPC

func NewPC[F any](f F) PC[F]

NewPC wraps a func. This performs no checking that the func does not capture any variables.

func (*PC[F]) Get

func (pc *PC[F]) Get() F

Get returns the func this PC wraps.

type VLA

type VLA[T any] [0]T

VLA is a mechanism for accessing a variable-length array that follows some struct.

func Beyond

func Beyond[T, Header any](p *Header) *VLA[T]

Beyond obtains the VLA past the end of p.

Address calculation assumes that p is well-aligned.

func (*VLA[T]) ByteGet

func (a *VLA[T]) ByteGet(n int) *T

Get returns a pointer to the element of this array at the given byte offset.

func (*VLA[T]) Get

func (a *VLA[T]) Get(n int) *T

Get returns a pointer to the nth element of this array.

func (*VLA[T]) Slice

func (a *VLA[T]) Slice(n int) []T

Slice converts this VLA into a slice of the given length.

Directories

Path Synopsis
Package support ensures that this module is only compiled for supported architectures.
Package support ensures that this module is only compiled for supported architectures.

Jump to

Keyboard shortcuts

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