Documentation
¶
Index ¶
- func Alignof[ArbitraryType _ArbitraryType](x ArbitraryType) uintptr
- func ClearOut(baseAddr unsafe.Pointer, clearout [][2]uintptr)
- func F[F interface{ ... }](f F, typ ...Type) field
- func Field[F interface{ ... }](f F, typ ...Type) field
- func FindPointerFields(t reflect.Type) [][2]uintptr
- func Malloc(size uintptr, zerofill bool, typ ...reflect.Type) unsafe.Pointer
- func New[T any, PtrT interface{ ... }](typ ...reflect.Type) *T
- func NewZero[T any](typ ...reflect.Type) *Tdeprecated
- func Sizeof[ArbitraryType _ArbitraryType](x ArbitraryType) uintptr
- func Slice[ArbitraryType _ArbitraryType, IntegerType _IntegerType](ptr *ArbitraryType, len IntegerType) []ArbitraryType
- func SliceData[ArbitraryType _ArbitraryType](slice []ArbitraryType) *ArbitraryType
- func String[IntegerType _IntegerType](ptr *byte, len IntegerType) string
- func StringData(str string) *byte
- func Value[T any](strct any, f field) T
- type Pointer
- type Type
- type UnsafeClearOutFields
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ClearOut ¶ added in v1.4.0
ClearOut zeros out selected blocks of memory. clearout is a slice containing offset-size pairs. The offset is always relative to baseAddr.
NOTE: It uses runtime.memclrNoHeapPointers so make sure you read documentation on when it is safe to use.
func Field ¶
Field selects a field in a struct. The field can be selected by passing an integer or a (name) string. An optional type constraint can be provided as a safety precaution to ensure that the field's type is what you expected.
func FindPointerFields ¶ added in v1.2.0
FindPointerFields finds in a struct the offsets (and corresponding sizes) of all fields that contain pointers and other reference types.
func Malloc ¶ added in v1.2.0
Malloc allocates contiguous blocks of memory without zeroing. However, the memory will be cleared when zerofill is set.
All allocated memory must sensibly be filled by the developer immediately, since the pre-existing data may contain garbage.
WARNING: If the returned pointer will be cast to a struct with fields containing pointers, then typ must be provided to inform the garbage collector. As an optimization, typ can be stored in a global variable and reused.
NOTE: Reference types (eg. strings, slices, interfaces, channels, maps etc.) contain pointers.
Example:
type Person struct {
name [50]byte
age int
phone int
}
ptr := Malloc(unsafe.Sizeof(Person{}), false)
ClearOut(ptr, [][2]uintptr{})
p := *(*Person)(ptr)
func New ¶ added in v1.2.0
func New[T any, PtrT interface { *T UnsafeClearOutFields }](typ ...reflect.Type, ) *T
New allocates memory for a struct without zeroing. A pointer to the newly allocated value is returned. The returned struct is not guaranteed to be the zero value.
NOTE: UnsafeClearOutFields must be implemented as a method with a pointer receiver. UnsafeClearOutFields is used to selectively zero fields.
WARNING: T must not have fields containing pointers. ...But if it does contain pointers, provide typ=nil to inform the garbage collector. As an optimization, typ can be stored in a global variable and reused.
NOTE: Reference types (eg. strings, slices, interfaces, channels, maps etc.) contain pointers.
Example:
type Person struct {
name [50]byte
age int
phone int
}
func (p *Person) UnsafeClearOutFields() [][2]uintptr {
return [][2]uintptr {
{unsafe.Offsetof(Person{}.name), unsafe.Sizeof(Person{}.name)},
{unsafe.Offsetof(Person{}.phone), unsafe.Sizeof(Person{}.phone)},
}
return nil
}
func main() {
p := New[Person]() // like p := new(Person)
}
func NewZero
deprecated
added in
v1.2.2
NewZero allocates memory for a zero-value struct. A pointer to the newly allocated value is returned. It is equivalent to new(T) (but seems to be faster).
WARNING: T must not have fields containing pointers. ...But if it does contain pointers, provide typ=nil to inform the garbage collector. As an optimization, typ can be stored in a global variable and reused.
NOTE: Reference types (eg. strings, slices, interfaces, channels, maps etc.) contain pointers.
Example:
p := NewZero[T]()
Deprecated: From Go1.26+, new(T) will be on par or faster than NewZero.
func Slice ¶
func Slice[ArbitraryType _ArbitraryType, IntegerType _IntegerType](ptr *ArbitraryType, len IntegerType) []ArbitraryType
Slice
func SliceData ¶
func SliceData[ArbitraryType _ArbitraryType](slice []ArbitraryType) *ArbitraryType
SliceData
func StringData ¶
StringData
Types ¶
type Pointer ¶
Pointer
type Type ¶ added in v1.1.2
Type
type UnsafeClearOutFields ¶ added in v1.4.0
type UnsafeClearOutFields interface {
// UnsafeClearOutFields must be implemented on the pointer type *T.
// The return values can be hardcoded using unsafe.Offsetof() and unsafe.Sizeof().
UnsafeClearOutFields() [][2]uintptr
}
UnsafeClearOutFields is used to return the offset and size of each field of a struct that should be cleared.