Source file src/pkg/unsafe/unsafe.go
1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 /* 6 Package unsafe contains operations that step around the type safety of Go programs. 7 */ 8 package unsafe 9 10 // ArbitraryType is here for the purposes of documentation only and is not actually 11 // part of the unsafe package. It represents the type of an arbitrary Go expression. 12 type ArbitraryType int 13 14 // Pointer represents a pointer to an arbitrary type. There are three special operations 15 // available for type Pointer that are not available for other types. 16 // 1) A pointer value of any type can be converted to a Pointer. 17 // 2) A Pointer can be converted to a pointer value of any type. 18 // 3) A uintptr can be converted to a Pointer. 19 // 4) A Pointer can be converted to a uintptr. 20 // Pointer therefore allows a program to defeat the type system and read and write 21 // arbitrary memory. It should be used with extreme care. 22 type Pointer *ArbitraryType 23 24 // Sizeof returns the size in bytes occupied by the value v. The size is that of the 25 // "top level" of the value only. For instance, if v is a slice, it returns the size of 26 // the slice descriptor, not the size of the memory referenced by the slice. 27 func Sizeof(v ArbitraryType) int 28 29 // Offsetof returns the offset within the struct of the field represented by v, 30 // which must be of the form struct_value.field. In other words, it returns the 31 // number of bytes between the start of the struct and the start of the field. 32 func Offsetof(v ArbitraryType) int 33 34 // Alignof returns the alignment of the value v. It is the maximum value m such 35 // that the address of a variable with the type of v will always always be zero mod m. 36 // If v is of the form obj.f, it returns the alignment of field f within struct object obj. 37 func Alignof(v ArbitraryType) int 38 39 // Typeof returns the type of an interface value, a runtime.Type. 40 func Typeof(i interface{}) (typ interface{}) 41 42 // Reflect unpacks an interface value into its type and the address of a copy of the 43 // internal value. 44 func Reflect(i interface{}) (typ interface{}, addr Pointer) 45 46 // Unreflect inverts Reflect: Given a type and a pointer to a value, it returns an 47 // empty interface value with contents the type and the value (not the pointer to 48 // the value). The typ is assumed to contain a pointer to a runtime type; the type 49 // information in the interface{} is ignored, so that, for example, both 50 // *reflect.structType and *runtime.StructType can be passed for typ. 51 func Unreflect(typ interface{}, addr Pointer) (ret interface{}) 52 53 // New allocates and returns a pointer to memory for a new value of the given type. 54 // The typ is assumed to hold a pointer to a runtime type. 55 // Callers should use reflect.MakeZero instead of invoking unsafe.New directly. 56 func New(typ interface{}) Pointer 57 58 // NewArray allocates and returns a pointer to an array of n elements of the given type. 59 // The typ is assumed to hold a pointer to a runtime type. 60 // Callers should use reflect.MakeSlice instead of invoking unsafe.NewArray directly. 61 func NewArray(typ interface{}, n int) Pointer