Documentation
¶
Overview ¶
Package unsafe provides a more convenient interface for performing unsafe operations than Go's built-in package unsafe.
Index ¶
- func Add[P ~*E, E any, I Int](p P, n I) P
- func AnyBytes(v any) []byte
- func AnyData(v any) *byte
- func AnyType(v any) uintptr
- func AssertInlinedAny[T any](t testing.TB)
- func BitCast[To, From any](v From) To
- func BoundsCheck(n, len int)
- func ByteAdd[T any, P ~*E, E any, I Int](p P, n I) *T
- func ByteLoad[T any, P ~*E, E any, I Int](p P, n I) T
- func ByteStore[T any, P ~*E, E any, I Int](p P, n I, v T)
- func ByteSub[P1 ~*E1, P2 ~*E2, E1, E2 any](p1 P1, p2 P2) int
- func Bytes[P ~*E, E any](p P) []byte
- func Cast[To, From any](p *From) *To
- func Clear[P ~*E, E any, I Int](p P, n I)
- func Copy[P ~*E, E any, I Int](dst, src P, n I)
- func Escape[P ~*E, E any](p P) P
- func IsDirect[T any]() bool
- func IsDirectAny(v any) bool
- func Load[P ~*E, E any, I Int](p P, n I) E
- func LoadSlice[S ~[]E, E any, I Int](s S, n I) E
- func MakeAny(typ uintptr, data *byte) any
- func NoEscape[P ~*E, E any](p P) P
- func Ping[P ~*E, E any](p P)
- func SliceToString[S ~[]E, E any](s S) string
- func Store[P ~*E, E any, I Int](p P, n I, v E)
- func StoreNoWB[P ~*E, E any](p *P, q P)
- func StoreNoWBUntyped[P ~unsafe.Pointer](p *P, q P)
- func StringToSlice[S ~[]E, E any](s string) S
- func Sub[P ~*E, E any](p1, p2 P) int
- type Addr
- func (a Addr[T]) Add(n int) Addr[T]
- func (a Addr[T]) AssertValid() *T
- func (a Addr[T]) ByteAdd(n int) Addr[T]
- func (a Addr[T]) ClearSignBit() Addr[T]
- func (a Addr[T]) Format(state fmt.State, verb rune)
- func (a Addr[T]) Padding(align int) int
- func (a Addr[T]) RoundUpTo(align int) Addr[T]
- func (a Addr[T]) SignBit() bool
- func (a Addr[T]) SignBitMask() Addr[T]
- func (a Addr[T]) Sub(b Addr[T]) int
- type Int
- type NoCopy
- type PC
- type VLA
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertInlinedAny ¶
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 ¶
ByteAdd adds the given offset to p, without scaling.
It also throws in a cast for free.
func IsDirect ¶
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 ¶
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 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 ¶
SliceToString converts a slice into a string, multiplying the slice length as appropriate.
func StoreNoWB ¶
func StoreNoWB[P ~*E, E any](p *P, q P)
StoreNoWB performs a store without generating any write barriers.
func StoreNoWBUntyped ¶
StoreNoWBUntyped performs a store without generating any write barriers.
func StringToSlice ¶
StringToSlice converts a string into a slice, multiplying the slice length as appropriate.
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 EndOf ¶
EndOf calculates the one-past-the-end address of s without creating an intermediate one-past-the-end pointer.
func (Addr[T]) AssertValid ¶
func (a Addr[T]) AssertValid() *T
AssertValid asserts that this address is a valid pointer.
func (Addr[T]) ClearSignBit ¶
ClearSignBit clears the sign bit of this address, flipping all of the other bits in the process.
func (Addr[T]) Format ¶
Format implements fmt.Formatter.
func (Addr[T]) Padding ¶
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 ¶
RoundUp rounds this address upwards to align, which must be a power of two.
func (Addr[T]) SignBit ¶
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 ¶
SignBitMask returns either all zeros or all ones, according to the sign bit of a.
type NoCopy ¶
NoCopy is a type that go vet will complain about having been moved.
It does so by implementing sync.Locker.
type PC ¶
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.
type VLA ¶
type VLA[T any] [0]T
VLA is a mechanism for accessing a variable-length array that follows some struct.
func Beyond ¶
Beyond obtains the VLA past the end of p.
Address calculation assumes that p is well-aligned.
func (*VLA[T]) ByteGet ¶
Get returns a pointer to the element of this array at the given byte offset.