func PrintSgfDbTypeSizes() { // sgfdb.go var tr TraceRec var cdr DirectoryProcessRequest printSizeAlign("TraceRec", unsafe.Sizeof(tr), unsafe.Alignof(tr)) printSizeAlign("DirectoryProcessRequest", unsafe.Sizeof(cdr), unsafe.Alignof(cdr)) }
func f() { defer int(0) // ERROR "defer requires function call, not conversion" go string([]byte("abc")) // ERROR "go requires function call, not conversion" var c complex128 var f float64 var t struct{ X int } var x []int defer append(x, 1) // ERROR "defer discards result of append" defer cap(x) // ERROR "defer discards result of cap" defer complex(1, 2) // ERROR "defer discards result of complex" defer complex(f, 1) // ERROR "defer discards result of complex" defer imag(1i) // ERROR "defer discards result of imag" defer imag(c) // ERROR "defer discards result of imag" defer len(x) // ERROR "defer discards result of len" defer make([]int, 1) // ERROR "defer discards result of make" defer make(chan bool) // ERROR "defer discards result of make" defer make(map[string]int) // ERROR "defer discards result of make" defer new(int) // ERROR "defer discards result of new" defer real(1i) // ERROR "defer discards result of real" defer real(c) // ERROR "defer discards result of real" defer append(x, 1) // ERROR "defer discards result of append" defer append(x, 1) // ERROR "defer discards result of append" defer unsafe.Alignof(t.X) // ERROR "defer discards result of unsafe.Alignof" defer unsafe.Offsetof(t.X) // ERROR "defer discards result of unsafe.Offsetof" defer unsafe.Sizeof(t) // ERROR "defer discards result of unsafe.Sizeof" defer copy(x, x) // ok m := make(map[int]int) defer delete(m, 1) // ok defer panic(1) // ok defer print(1) // ok defer println(1) // ok defer recover() // ok int(0) // ERROR "int\(0\) evaluated but not used" string([]byte("abc")) // ERROR "string\(.*\) evaluated but not used" append(x, 1) // ERROR "not used" cap(x) // ERROR "not used" complex(1, 2) // ERROR "not used" complex(f, 1) // ERROR "not used" imag(1i) // ERROR "not used" imag(c) // ERROR "not used" len(x) // ERROR "not used" make([]int, 1) // ERROR "not used" make(chan bool) // ERROR "not used" make(map[string]int) // ERROR "not used" new(int) // ERROR "not used" real(1i) // ERROR "not used" real(c) // ERROR "not used" append(x, 1) // ERROR "not used" append(x, 1) // ERROR "not used" unsafe.Alignof(t.X) // ERROR "not used" unsafe.Offsetof(t.X) // ERROR "not used" unsafe.Sizeof(t) // ERROR "not used" }
// Make sure alignment works out correctly, at least for the header. func init() { a := unsafe.Alignof(OutMessage{}) o := unsafe.Offsetof(OutMessage{}.storage) e := unsafe.Alignof(fusekernel.OutHeader{}) if a%e != 0 || o%e != 0 { log.Panicf("Bad alignment or offset: %d, %d, need %d", a, o, e) } }
func (endianSupport) little(idx uint32) uint32 { if idx < 0 || idx >= 6 { return 0 } return uint32(unsafe.Offsetof(secData.args)) + uint32(unsafe.Alignof(secData.args[0]))*idx + uint32(unsafe.Sizeof(secData.arch)) }
func main() { // Test unsafe.Sizeof, unsafe.Alignof, and unsafe.Offsetof all return uintptr. isUintptr(unsafe.Sizeof(t)) isUintptr(unsafe.Alignof(t)) isUintptr(unsafe.Offsetof(t.X)) // Test correctness of Offsetof with respect to embedded fields (issue 4909). if unsafe.Offsetof(t2.C) != 8 { println(unsafe.Offsetof(t2.C), "!= 8") panic("unsafe.Offsetof(t2.C) != 8") } if unsafe.Offsetof(p2.C) != 8 { println(unsafe.Offsetof(p2.C), "!= 8") panic("unsafe.Offsetof(p2.C) != 8") } if unsafe.Offsetof(t2.U2.C) != 4 { println(unsafe.Offsetof(t2.U2.C), "!= 4") panic("unsafe.Offsetof(t2.U2.C) != 4") } if unsafe.Offsetof(p2.U2.C) != 4 { println(unsafe.Offsetof(p2.U2.C), "!= 4") panic("unsafe.Offsetof(p2.U2.C) != 4") } testDeep() testNotEmbedded() }
func main() { example := &Example{ BoolValue: true, } exampleNext := &Example{ BoolValue: true, } alignmentBoundary := unsafe.Alignof(example) sizeBool := unsafe.Sizeof(example.BoolValue) offsetBool := unsafe.Offsetof(example.BoolValue) sizeBoolNext := unsafe.Sizeof(exampleNext.BoolValue) offsetBoolNext := unsafe.Offsetof(exampleNext.BoolValue) fmt.Printf("Alignment Boundary: %d\n", alignmentBoundary) fmt.Printf("BoolValue = Size: %d Offset: %d Addr: %v\n", sizeBool, offsetBool, &example.BoolValue) fmt.Printf("Next = Size: %d Offset: %d Addr: %v\n", sizeBoolNext, offsetBoolNext, &exampleNext.BoolValue) }
// main is the entry point for the application. func main() { var e example alignmentBoundary := unsafe.Alignof(e) sizeE := unsafe.Sizeof(e) sizeBool := unsafe.Sizeof(e.flag) offsetBool := unsafe.Offsetof(e.flag) sizeInt := unsafe.Sizeof(e.counter) offsetInt := unsafe.Offsetof(e.counter) sizeBool2 := unsafe.Sizeof(e.flag2) offsetBool2 := unsafe.Offsetof(e.flag2) sizeFloat := unsafe.Sizeof(e.pi) offsetFloat := unsafe.Offsetof(e.pi) fmt.Printf("Alignment(%d):\tSize: %d\n", alignmentBoundary, sizeE) fmt.Printf("flag:\t\tSize: %d\t\tOffset: %d\tAddr: %v\n", sizeBool, offsetBool, &e.flag) fmt.Printf("counter:\tSize: %d\t\tOffset: %d\tAddr: %v\n", sizeInt, offsetInt, &e.counter) fmt.Printf("flag1:\t\tSize: %d\t\tOffset: %d\tAddr: %v\n", sizeBool2, offsetBool2, &e.flag2) fmt.Printf("pi\t\tSize: %d\t\tOffset: %d\tAddr: %v\n", sizeFloat, offsetFloat, &e.pi) }
func main() { s := []int{1, 2, 3, 4, 5, 6} p := unsafe.Pointer(&s) size := *(*int)(unsafe.Pointer(uintptr(p) + uintptr(unsafe.Alignof(reflect.SliceHeader.Len)))) fmt.Printf("sizeof = %d\n", size) //p2 := (*[]byte)(unsafe.Pointer(uintptr(p))) //fmt.Printf("str = %v\n", string(*p2)) }
func newBuffer(b []byte) *buffer { buf := &buffer{ b: align(b, unsafe.Alignof((*C.char)(nil))), } if buf.b == nil { buf.err = syscall.ERANGE } return buf }
func unalignBytes(in []byte) []byte { out := make([]byte, len(in)+1) if uintptr(unsafe.Pointer(&out[0]))&(unsafe.Alignof(uint32(0))-1) == 0 { out = out[1:] } else { out = out[:len(in)] } copy(out, in) return out }
func main() { n := unsafe.Sizeof(0) if n != 4 && n != 8 { println("BUG sizeof 0", n) return } n = unsafe.Alignof(0) if n != 4 && n != 8 { println("BUG alignof 0", n) return } n = unsafe.Sizeof("") if n != 8 && n != 16 { println("BUG sizeof \"\"", n) return } n = unsafe.Alignof("") if n != 4 && n != 8 { println("BUG alignof \"\"", n) return } }
func benchmarkSize(b *testing.B, size int, unaligned bool) { b.SetBytes(int64(size)) buf := buf if unaligned { if uintptr(unsafe.Pointer(&buf[0]))&(unsafe.Alignof(uint32(0))-1) == 0 { buf = buf[1:] } } b.ResetTimer() for i := 0; i < b.N; i++ { bench.Reset() bench.Write(buf[:size]) bench.Sum(sum[:0]) } }
// main is the entry point for the application. func main() { // Declare variable of type example and init using // a struct literal. e := example{ flag: true, counter: 10, pi: 3.141592, } // Declare variable of type example and init using // a struct literal. eNext := example{ flag: true, counter: 10, pi: 3.141592, } // By placing both value one after the other on the stack // we can see the actual padding and where it is located. alignmentBoundary := unsafe.Alignof(e) sizeBool := unsafe.Sizeof(e.flag) offsetBool := unsafe.Offsetof(e.flag) sizeInt := unsafe.Sizeof(e.counter) offsetInt := unsafe.Offsetof(e.counter) sizeFloat := unsafe.Sizeof(e.pi) offsetFloat := unsafe.Offsetof(e.pi) sizeBoolNext := unsafe.Sizeof(eNext.flag) offsetBoolNext := unsafe.Offsetof(eNext.flag) fmt.Printf("Alignment Boundary: %d\n", alignmentBoundary) fmt.Printf("flag = Size: %d Offset: %d Addr: %v\n", sizeBool, offsetBool, &e.flag) fmt.Printf("counter = Size: %d Offset: %d Addr: %v\n", sizeInt, offsetInt, &e.counter) fmt.Printf("pi = Size: %d Offset: %d Addr: %v\n", sizeFloat, offsetFloat, &e.pi) fmt.Printf("Next = Size: %d Offset: %d Addr: %v\n", sizeBoolNext, offsetBoolNext, &eNext.flag) }
func (m *Sorted) WriteBinary(path string) (err error) { w, err := os.Create(path) if err != nil { return } defer w.Close() bw := byteblock.NewByteBlockWriter(w) if err = bw.WriteString(MAGIC_SORTED, 0); err != nil { return } // Header header, err := m.header() if err != nil { return } if err = bw.Write(header, 0); err != nil { return } // Raw entries. // Go over the transitions to see how many entries there are in total. numEntries := int64(0) for _, i := range m.transitions { numEntries += int64(len(i)) } // Ask for a large new block and then incrementally write out the // data. align := int64(unsafe.Alignof(xqwEntry{})) size := int64(unsafe.Sizeof(xqwEntry{})) if err = bw.NewBlock(align, size*numEntries); err != nil { return } for _, i := range m.transitions { iHeader := (*reflect.SliceHeader)(unsafe.Pointer(&i)) var bytes []byte bytesHeader := (*reflect.SliceHeader)(unsafe.Pointer(&bytes)) bytesHeader.Data = iHeader.Data bytesHeader.Len = int(int64(iHeader.Len) * size) bytesHeader.Cap = bytesHeader.Len if err = bw.Append(bytes); err != nil { return } } return nil }
func main() { example := &Example{ BoolValue: true, IntValue: 10, FloatValue: 3.141592, } exampleNext := &Example{ BoolValue: true, IntValue: 10, FloatValue: 3.141592, } alignmentBoundary := unsafe.Alignof(example) sizeBool := unsafe.Sizeof(example.BoolValue) offsetBool := unsafe.Offsetof(example.BoolValue) sizeInt := unsafe.Sizeof(example.IntValue) offsetInt := unsafe.Offsetof(example.IntValue) sizeFloat := unsafe.Sizeof(example.FloatValue) offsetFloat := unsafe.Offsetof(example.FloatValue) sizeBoolNext := unsafe.Sizeof(exampleNext.BoolValue) offsetBoolNext := unsafe.Offsetof(exampleNext.BoolValue) fmt.Printf("Alignment Boundary: %d\n", alignmentBoundary) fmt.Printf("BoolValue = Size: %d Offset: %d Addr: %v\n", sizeBool, offsetBool, &example.BoolValue) fmt.Printf("IntValue = Size: %d Offset: %d Addr: %v\n", sizeInt, offsetInt, &example.IntValue) fmt.Printf("FloatValue = Size: %d Offset: %d Addr: %v\n", sizeFloat, offsetFloat, &example.FloatValue) fmt.Printf("Next = Size: %d Offset: %d Addr: %v\n", sizeBoolNext, offsetBoolNext, &exampleNext.BoolValue) }
func (c *LDCache) parse() error { var header Header1 // Check for the old format (< glibc-2.2) if c.Len() <= int(unsafe.Sizeof(header)) { return ErrInvalidCache } if strn(c.data, len(magicString1)) == magicString1 { if err := binary.Read(c, binary.LittleEndian, &header); err != nil { return err } n := int64(header.NLibs) * int64(unsafe.Sizeof(Entry1{})) offset, err := c.Seek(n, 1) // skip old entries if err != nil { return err } n = (-offset) & int64(unsafe.Alignof(c.header)-1) _, err = c.Seek(n, 1) // skip padding if err != nil { return err } } c.libs = c.data[c.Size()-int64(c.Len()):] // kv offsets start here if err := binary.Read(c, binary.LittleEndian, &c.header); err != nil { return err } if c.Magic() != magicString2 || c.Version() != magicVersion { return ErrInvalidCache } c.entries = make([]Entry2, c.header.NLibs) if err := binary.Read(c, binary.LittleEndian, &c.entries); err != nil { return err } return nil }
func main() { var b bool var i int var i8 int8 var i16 int16 var i32 int32 var i64 int64 var u uint var u8 uint8 var u16 uint16 var u32 uint32 var u64 uint64 var f32 float32 var f64 float64 var c64 complex64 var c128 complex128 var s string var p unsafe.Pointer var up uintptr println(unsafe.Sizeof(b)) println(unsafe.Sizeof(i)) println(unsafe.Sizeof(i8)) println(unsafe.Sizeof(i16)) println(unsafe.Sizeof(i32)) println(unsafe.Sizeof(i64)) println(unsafe.Sizeof(u)) println(unsafe.Sizeof(u8)) println(unsafe.Sizeof(u16)) println(unsafe.Sizeof(u32)) println(unsafe.Sizeof(u64)) println(unsafe.Sizeof(f32)) println(unsafe.Sizeof(f64)) println(unsafe.Sizeof(c64)) println(unsafe.Sizeof(c128)) println(unsafe.Sizeof(s)) println(unsafe.Sizeof(p)) println(unsafe.Sizeof(up)) println(unsafe.Alignof(b)) println(unsafe.Alignof(i)) println(unsafe.Alignof(i8)) println(unsafe.Alignof(i16)) println(unsafe.Alignof(i32)) println(unsafe.Alignof(i64)) println(unsafe.Alignof(u)) println(unsafe.Alignof(u8)) println(unsafe.Alignof(u16)) println(unsafe.Alignof(u32)) println(unsafe.Alignof(u64)) println(unsafe.Alignof(f32)) println(unsafe.Alignof(f64)) println(unsafe.Alignof(c64)) println(unsafe.Alignof(c128)) println(unsafe.Alignof(s)) println(unsafe.Alignof(p)) println(unsafe.Alignof(up)) }
func main() { isUintptr(unsafe.Sizeof(t)) isUintptr(unsafe.Alignof(t)) isUintptr(unsafe.Offsetof(t.X)) }
func PrintAhTypeSizes() { // board.go var d Direction var pt PointType var cv ColValue var rv RowValue var csz ColSize var rsz RowSize var nl NodeLoc var nll NodeLocList var m MoveRecord //var bp BoardPoint var b Board var ss SearchStack var gnlf GraphNodeLocFunc var nlfb NodeLocFuncBool printSizeAlign("Direction", unsafe.Sizeof(d), unsafe.Alignof(d)) printSizeAlign("PointType", unsafe.Sizeof(pt), unsafe.Alignof(pt)) printSizeAlign("ColValue", unsafe.Sizeof(cv), unsafe.Alignof(cv)) printSizeAlign("RowValue", unsafe.Sizeof(rv), unsafe.Alignof(rv)) printSizeAlign("ColSize", unsafe.Sizeof(csz), unsafe.Alignof(csz)) printSizeAlign("RowSize", unsafe.Sizeof(rsz), unsafe.Alignof(rsz)) printSizeAlign("NodeLoc", unsafe.Sizeof(nl), unsafe.Alignof(nl)) printSizeAlign("NodeLocList", unsafe.Sizeof(nll), unsafe.Alignof(nll)) printSizeAlign("MoveRecord", unsafe.Sizeof(m), unsafe.Alignof(m)) // printSizeAlign("BoardPoint", unsafe.Sizeof(bp), unsafe.Alignof(bp)) printSizeAlign("Board", unsafe.Sizeof(b), unsafe.Alignof(b)) printSizeAlign("SearchStack", unsafe.Sizeof(ss), unsafe.Alignof(ss)) printSizeAlign("GraphNodeLocFunc", unsafe.Sizeof(gnlf), unsafe.Alignof(gnlf)) printSizeAlign("NodeLocFuncBool", unsafe.Sizeof(nlfb), unsafe.Alignof(nlfb)) // trans.go var bt BoardTrans printSizeAlign("BoardTrans", unsafe.Sizeof(bt), unsafe.Alignof(bt)) // graph.go var ai ArcIdx var gm GraphMark var gn GraphNode var ga GraphArc var csf CompStateFunc var chreq ChangeRequest var g Graph var glnf NodeLocFunc var af ArcFunc printSizeAlign("ArcIdx", unsafe.Sizeof(ai), unsafe.Alignof(ai)) printSizeAlign("GraphMark", unsafe.Sizeof(gm), unsafe.Alignof(gm)) printSizeAlign("GraphNode", unsafe.Sizeof(gn), unsafe.Alignof(gn)) printSizeAlign("GraphArc", unsafe.Sizeof(ga), unsafe.Alignof(ga)) printSizeAlign("CompStateFunc", unsafe.Sizeof(csf), unsafe.Alignof(csf)) printSizeAlign("ChangeRequest", unsafe.Sizeof(chreq), unsafe.Alignof(chreq)) printSizeAlign("Graph", unsafe.Sizeof(g), unsafe.Alignof(g)) printSizeAlign("NodeLocFunc", unsafe.Sizeof(glnf), unsafe.Alignof(glnf)) printSizeAlign("ArcFunc", unsafe.Sizeof(af), unsafe.Alignof(af)) // comp.go var ps PointStatus printSizeAlign("PointStatus", unsafe.Sizeof(ps), unsafe.Alignof(ps)) // update.go var gl GraphLevel var ah AbstHier var ns NodeStatus printSizeAlign("GraphLevel", unsafe.Sizeof(gl), unsafe.Alignof(gl)) printSizeAlign("AbstHier", unsafe.Sizeof(ah), unsafe.Alignof(ah)) printSizeAlign("NodeStatus", unsafe.Sizeof(ns), unsafe.Alignof(ns)) // gostrings.go var sst StringStatus printSizeAlign("StringStatus", unsafe.Sizeof(sst), unsafe.Alignof(sst)) }
func PrintSGFTypeSizes() { // token.go var t Token var pos ah.Position printSizeAlign("Token", unsafe.Sizeof(t), unsafe.Alignof(t)) printSizeAlign("ah.Position", unsafe.Sizeof(pos), unsafe.Alignof(pos)) // tree.go var tnt TreeNodeType var tni TreeNodeIdx var pi PropIdx var tn TreeNode var pv PropertyValue printSizeAlign("TreeNodeType", unsafe.Sizeof(tnt), unsafe.Alignof(tnt)) printSizeAlign("TreeNodeIdx", unsafe.Sizeof(tni), unsafe.Alignof(tni)) printSizeAlign("PropIdx", unsafe.Sizeof(pi), unsafe.Alignof(pi)) printSizeAlign("TreeNode", unsafe.Sizeof(tn), unsafe.Alignof(tn)) printSizeAlign("PropertyValue", unsafe.Sizeof(pv), unsafe.Alignof(pv)) // parser.go var pt GameTree var pr Parser var pyi PlayerInfo var dbs DBStatistics printSizeAlign("GameTree", unsafe.Sizeof(pt), unsafe.Alignof(pt)) printSizeAlign("Parser", unsafe.Sizeof(pr), unsafe.Alignof(pr)) printSizeAlign("PlayerInfo", unsafe.Sizeof(pyi), unsafe.Alignof(pyi)) printSizeAlign("DBStatistics", unsafe.Sizeof(dbs), unsafe.Alignof(dbs)) // sgf.go var ff4n FF4Note var sgfpnt SGFPropNodeType var qt QualifierType var vt PropValueType var prop Property var pdi PropertyDefIdx var idca ID_CountArray printSizeAlign("FF4Note", unsafe.Sizeof(ff4n), unsafe.Alignof(ff4n)) printSizeAlign("SGFPropNodeType", unsafe.Sizeof(sgfpnt), unsafe.Alignof(sgfpnt)) printSizeAlign("QualifierType", unsafe.Sizeof(qt), unsafe.Alignof(qt)) printSizeAlign("PropValueType", unsafe.Sizeof(vt), unsafe.Alignof(vt)) printSizeAlign("Property", unsafe.Sizeof(prop), unsafe.Alignof(prop)) printSizeAlign("PropertyDefIdx", unsafe.Sizeof(pdi), unsafe.Alignof(pdi)) printSizeAlign("ID_CountArray", unsafe.Sizeof(idca), unsafe.Alignof(idca)) // scanner.go var s Scanner printSizeAlign("Scanner", unsafe.Sizeof(s), unsafe.Alignof(s)) // errors.go var eh ErrorHandler var el ah.ErrorList printSizeAlign("ErrorHandler", unsafe.Sizeof(eh), unsafe.Alignof(eh)) printSizeAlign("ah.ErrorList", unsafe.Sizeof(el), unsafe.Alignof(el)) // game.go var k Komi var r Result printSizeAlign("Komi", unsafe.Sizeof(k), unsafe.Alignof(k)) printSizeAlign("Result", unsafe.Sizeof(r), unsafe.Alignof(r)) }
func (s *SizeSuite) TestSize(c *C) { d := *IntegerWithValue(42) fmt.Printf("Data size is %d bytes\n", unsafe.Sizeof(d)) fmt.Printf("Data alignment is %d\n", unsafe.Alignof(d)) }
// Initialize the fields for encoding and decoding. func (p *Properties) setEncAndDec(typ reflect.Type) { var vbool bool var vbyte byte var vint32 int32 var vint64 int64 var vfloat32 float32 var vfloat64 float64 var vstring string var vslice []byte p.enc = nil p.dec = nil switch t1 := typ; t1.Kind() { default: fmt.Fprintf(os.Stderr, "proto: no coders for %T\n", t1) break case reflect.Ptr: switch t2 := t1.Elem(); t2.Kind() { default: fmt.Fprintf(os.Stderr, "proto: no encoder function for %T -> %T\n", t1, t2) break case reflect.Bool: p.enc = (*Buffer).enc_bool p.dec = (*Buffer).dec_bool p.alignof = unsafe.Alignof(vbool) p.sizeof = unsafe.Sizeof(vbool) case reflect.Int32, reflect.Uint32: p.enc = (*Buffer).enc_int32 p.dec = (*Buffer).dec_int32 p.alignof = unsafe.Alignof(vint32) p.sizeof = unsafe.Sizeof(vint32) case reflect.Int64, reflect.Uint64: p.enc = (*Buffer).enc_int64 p.dec = (*Buffer).dec_int64 p.alignof = unsafe.Alignof(vint64) p.sizeof = unsafe.Sizeof(vint64) case reflect.Float32: p.enc = (*Buffer).enc_int32 // can just treat them as bits p.dec = (*Buffer).dec_int32 p.alignof = unsafe.Alignof(vfloat32) p.sizeof = unsafe.Sizeof(vfloat32) case reflect.Float64: p.enc = (*Buffer).enc_int64 // can just treat them as bits p.dec = (*Buffer).dec_int64 p.alignof = unsafe.Alignof(vfloat64) p.sizeof = unsafe.Sizeof(vfloat64) case reflect.String: p.enc = (*Buffer).enc_string p.dec = (*Buffer).dec_string p.alignof = unsafe.Alignof(vstring) p.sizeof = unsafe.Sizeof(vstring) + startSize*unsafe.Sizeof(vbyte) case reflect.Struct: p.stype = t1 if p.Wire == "bytes" { p.enc = (*Buffer).enc_struct_message p.dec = (*Buffer).dec_struct_message } else { p.enc = (*Buffer).enc_struct_group p.dec = (*Buffer).dec_struct_group } } case reflect.Slice: switch t2 := t1.Elem(); t2.Kind() { default: logNoSliceEnc(t1, t2) break case reflect.Bool: if p.Packed { p.enc = (*Buffer).enc_slice_packed_bool } else { p.enc = (*Buffer).enc_slice_bool } p.dec = (*Buffer).dec_slice_bool p.packedDec = (*Buffer).dec_slice_packed_bool p.alignof = unsafe.Alignof(vbool) p.sizeof = startSize * unsafe.Sizeof(vbool) case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: switch t2.Bits() { case 32: if p.Packed { p.enc = (*Buffer).enc_slice_packed_int32 } else { p.enc = (*Buffer).enc_slice_int32 } p.dec = (*Buffer).dec_slice_int32 p.packedDec = (*Buffer).dec_slice_packed_int32 p.alignof = unsafe.Alignof(vint32) p.sizeof = startSize * unsafe.Sizeof(vint32) case 64: if p.Packed { p.enc = (*Buffer).enc_slice_packed_int64 } else { p.enc = (*Buffer).enc_slice_int64 } p.dec = (*Buffer).dec_slice_int64 p.packedDec = (*Buffer).dec_slice_packed_int64 p.alignof = unsafe.Alignof(vint64) p.sizeof = startSize * unsafe.Sizeof(vint64) case 8: if t2.Kind() == reflect.Uint8 { p.enc = (*Buffer).enc_slice_byte p.dec = (*Buffer).dec_slice_byte p.alignof = unsafe.Alignof(vbyte) p.sizeof = startSize * unsafe.Sizeof(vbyte) } default: logNoSliceEnc(t1, t2) break } case reflect.Float32, reflect.Float64: switch t2.Bits() { case 32: // can just treat them as bits if p.Packed { p.enc = (*Buffer).enc_slice_packed_int32 } else { p.enc = (*Buffer).enc_slice_int32 } p.dec = (*Buffer).dec_slice_int32 p.packedDec = (*Buffer).dec_slice_packed_int32 p.alignof = unsafe.Alignof(vfloat32) p.sizeof = startSize * unsafe.Sizeof(vfloat32) case 64: // can just treat them as bits if p.Packed { p.enc = (*Buffer).enc_slice_packed_int64 } else { p.enc = (*Buffer).enc_slice_int64 } p.dec = (*Buffer).dec_slice_int64 p.packedDec = (*Buffer).dec_slice_packed_int64 p.alignof = unsafe.Alignof(vfloat64) p.sizeof = startSize * unsafe.Sizeof(vfloat64) default: logNoSliceEnc(t1, t2) break } case reflect.String: p.enc = (*Buffer).enc_slice_string p.dec = (*Buffer).dec_slice_string p.alignof = unsafe.Alignof(vstring) p.sizeof = startSize * unsafe.Sizeof(vstring) case reflect.Ptr: switch t3 := t2.Elem(); t3.Kind() { default: fmt.Fprintf(os.Stderr, "proto: no ptr oenc for %T -> %T -> %T\n", t1, t2, t3) break case reflect.Struct: p.stype = t2 p.enc = (*Buffer).enc_slice_struct_group p.dec = (*Buffer).dec_slice_struct_group if p.Wire == "bytes" { p.enc = (*Buffer).enc_slice_struct_message p.dec = (*Buffer).dec_slice_struct_message } p.alignof = unsafe.Alignof(vslice) p.sizeof = startSize * unsafe.Sizeof(vslice) } case reflect.Slice: switch t2.Elem().Kind() { default: fmt.Fprintf(os.Stderr, "proto: no slice elem oenc for %T -> %T -> %T\n", t1, t2, t2.Elem()) break case reflect.Uint8: p.enc = (*Buffer).enc_slice_slice_byte p.dec = (*Buffer).dec_slice_slice_byte p.alignof = unsafe.Alignof(vslice) p.sizeof = startSize * unsafe.Sizeof(vslice) } } } // precalculate tag code wire := p.WireType if p.Packed { wire = WireBytes } x := uint32(p.Tag)<<3 | uint32(wire) i := 0 for i = 0; x > 127; i++ { p.tagbuf[i] = 0x80 | uint8(x&0x7F) x >>= 7 } p.tagbuf[i] = uint8(x) p.tagcode = p.tagbuf[0 : i+1] }
// GetProperties returns the list of properties for the type represented by t. func GetProperties(t reflect.Type) *StructProperties { mutex.Lock() if prop, ok := propertiesMap[t]; ok { mutex.Unlock() stats.Chit++ return prop } stats.Cmiss++ prop := new(StructProperties) // build properties prop.Prop = make([]*Properties, t.NumField()) prop.order = make([]int, t.NumField()) for i := 0; i < t.NumField(); i++ { f := t.Field(i) p := new(Properties) p.Init(f.Type, f.Name, f.Tag.Get("protobuf"), f.Offset) if f.Name == "XXX_extensions" { // special case var vmap map[int32][]byte p.enc = (*Buffer).enc_map p.dec = nil // not needed p.alignof = unsafe.Alignof(vmap) p.sizeof = unsafe.Sizeof(vmap) } prop.Prop[i] = p prop.order[i] = i if debug { print(i, " ", f.Name, " ", t.String(), " ") if p.Tag > 0 { print(p.String()) } print("\n") } if p.enc == nil && !strings.HasPrefix(f.Name, "XXX_") { fmt.Fprintln(os.Stderr, "proto: no encoder for", f.Name, f.Type.String(), "[GetProperties]") } } // Re-order prop.order. sort.Sort(prop) // build required counts // build scratch offsets // build tags reqCount := 0 scratch := uintptr(0) prop.tags = make(map[int]int) prop.origNames = make(map[string]int) for i, p := range prop.Prop { if p.Required { reqCount++ } scratch = align(scratch, p.alignof) p.scratch = scratch scratch += p.sizeof prop.tags[p.Tag] = i prop.origNames[p.OrigName] = i } prop.reqCount = reqCount prop.nscratch = scratch propertiesMap[t] = prop mutex.Unlock() return prop }
func block(dig *digest, p []byte) { a := dig.s[0] b := dig.s[1] c := dig.s[2] d := dig.s[3] var X *[16]uint32 var xbuf [16]uint32 for len(p) >= chunk { aa, bb, cc, dd := a, b, c, d // This is a constant condition - it is not evaluated on each iteration. if runtime.GOARCH == "amd64" || runtime.GOARCH == "386" { // MD5 was designed so that x86 processors can just iterate // over the block data directly as uint32s, and we generate // less code and run 1.3x faster if we take advantage of that. // My apologies. X = (*[16]uint32)(unsafe.Pointer(&p[0])) } else if uintptr(unsafe.Pointer(&p[0]))&(unsafe.Alignof(uint32(0))-1) == 0 { X = (*[16]uint32)(unsafe.Pointer(&p[0])) } else { X = &xbuf j := 0 for i := 0; i < 16; i++ { X[i&15] = uint32(p[j]) | uint32(p[j+1])<<8 | uint32(p[j+2])<<16 | uint32(p[j+3])<<24 j += 4 } } // Round 1. a += (((c ^ d) & b) ^ d) + X[0] + 3614090360 a = a<<7 | a>>(32-7) + b d += (((b ^ c) & a) ^ c) + X[1] + 3905402710 d = d<<12 | d>>(32-12) + a c += (((a ^ b) & d) ^ b) + X[2] + 606105819 c = c<<17 | c>>(32-17) + d b += (((d ^ a) & c) ^ a) + X[3] + 3250441966 b = b<<22 | b>>(32-22) + c a += (((c ^ d) & b) ^ d) + X[4] + 4118548399 a = a<<7 | a>>(32-7) + b d += (((b ^ c) & a) ^ c) + X[5] + 1200080426 d = d<<12 | d>>(32-12) + a c += (((a ^ b) & d) ^ b) + X[6] + 2821735955 c = c<<17 | c>>(32-17) + d b += (((d ^ a) & c) ^ a) + X[7] + 4249261313 b = b<<22 | b>>(32-22) + c a += (((c ^ d) & b) ^ d) + X[8] + 1770035416 a = a<<7 | a>>(32-7) + b d += (((b ^ c) & a) ^ c) + X[9] + 2336552879 d = d<<12 | d>>(32-12) + a c += (((a ^ b) & d) ^ b) + X[10] + 4294925233 c = c<<17 | c>>(32-17) + d b += (((d ^ a) & c) ^ a) + X[11] + 2304563134 b = b<<22 | b>>(32-22) + c a += (((c ^ d) & b) ^ d) + X[12] + 1804603682 a = a<<7 | a>>(32-7) + b d += (((b ^ c) & a) ^ c) + X[13] + 4254626195 d = d<<12 | d>>(32-12) + a c += (((a ^ b) & d) ^ b) + X[14] + 2792965006 c = c<<17 | c>>(32-17) + d b += (((d ^ a) & c) ^ a) + X[15] + 1236535329 b = b<<22 | b>>(32-22) + c // Round 2. a += (((b ^ c) & d) ^ c) + X[(1+5*0)&15] + 4129170786 a = a<<5 | a>>(32-5) + b d += (((a ^ b) & c) ^ b) + X[(1+5*1)&15] + 3225465664 d = d<<9 | d>>(32-9) + a c += (((d ^ a) & b) ^ a) + X[(1+5*2)&15] + 643717713 c = c<<14 | c>>(32-14) + d b += (((c ^ d) & a) ^ d) + X[(1+5*3)&15] + 3921069994 b = b<<20 | b>>(32-20) + c a += (((b ^ c) & d) ^ c) + X[(1+5*4)&15] + 3593408605 a = a<<5 | a>>(32-5) + b d += (((a ^ b) & c) ^ b) + X[(1+5*5)&15] + 38016083 d = d<<9 | d>>(32-9) + a c += (((d ^ a) & b) ^ a) + X[(1+5*6)&15] + 3634488961 c = c<<14 | c>>(32-14) + d b += (((c ^ d) & a) ^ d) + X[(1+5*7)&15] + 3889429448 b = b<<20 | b>>(32-20) + c a += (((b ^ c) & d) ^ c) + X[(1+5*8)&15] + 568446438 a = a<<5 | a>>(32-5) + b d += (((a ^ b) & c) ^ b) + X[(1+5*9)&15] + 3275163606 d = d<<9 | d>>(32-9) + a c += (((d ^ a) & b) ^ a) + X[(1+5*10)&15] + 4107603335 c = c<<14 | c>>(32-14) + d b += (((c ^ d) & a) ^ d) + X[(1+5*11)&15] + 1163531501 b = b<<20 | b>>(32-20) + c a += (((b ^ c) & d) ^ c) + X[(1+5*12)&15] + 2850285829 a = a<<5 | a>>(32-5) + b d += (((a ^ b) & c) ^ b) + X[(1+5*13)&15] + 4243563512 d = d<<9 | d>>(32-9) + a c += (((d ^ a) & b) ^ a) + X[(1+5*14)&15] + 1735328473 c = c<<14 | c>>(32-14) + d b += (((c ^ d) & a) ^ d) + X[(1+5*15)&15] + 2368359562 b = b<<20 | b>>(32-20) + c // Round 3. a += (b ^ c ^ d) + X[(5+3*0)&15] + 4294588738 a = a<<4 | a>>(32-4) + b d += (a ^ b ^ c) + X[(5+3*1)&15] + 2272392833 d = d<<11 | d>>(32-11) + a c += (d ^ a ^ b) + X[(5+3*2)&15] + 1839030562 c = c<<16 | c>>(32-16) + d b += (c ^ d ^ a) + X[(5+3*3)&15] + 4259657740 b = b<<23 | b>>(32-23) + c a += (b ^ c ^ d) + X[(5+3*4)&15] + 2763975236 a = a<<4 | a>>(32-4) + b d += (a ^ b ^ c) + X[(5+3*5)&15] + 1272893353 d = d<<11 | d>>(32-11) + a c += (d ^ a ^ b) + X[(5+3*6)&15] + 4139469664 c = c<<16 | c>>(32-16) + d b += (c ^ d ^ a) + X[(5+3*7)&15] + 3200236656 b = b<<23 | b>>(32-23) + c a += (b ^ c ^ d) + X[(5+3*8)&15] + 681279174 a = a<<4 | a>>(32-4) + b d += (a ^ b ^ c) + X[(5+3*9)&15] + 3936430074 d = d<<11 | d>>(32-11) + a c += (d ^ a ^ b) + X[(5+3*10)&15] + 3572445317 c = c<<16 | c>>(32-16) + d b += (c ^ d ^ a) + X[(5+3*11)&15] + 76029189 b = b<<23 | b>>(32-23) + c a += (b ^ c ^ d) + X[(5+3*12)&15] + 3654602809 a = a<<4 | a>>(32-4) + b d += (a ^ b ^ c) + X[(5+3*13)&15] + 3873151461 d = d<<11 | d>>(32-11) + a c += (d ^ a ^ b) + X[(5+3*14)&15] + 530742520 c = c<<16 | c>>(32-16) + d b += (c ^ d ^ a) + X[(5+3*15)&15] + 3299628645 b = b<<23 | b>>(32-23) + c // Round 4. a += (c ^ (b | ^d)) + X[(7*0)&15] + 4096336452 a = a<<6 | a>>(32-6) + b d += (b ^ (a | ^c)) + X[(7*1)&15] + 1126891415 d = d<<10 | d>>(32-10) + a c += (a ^ (d | ^b)) + X[(7*2)&15] + 2878612391 c = c<<15 | c>>(32-15) + d b += (d ^ (c | ^a)) + X[(7*3)&15] + 4237533241 b = b<<21 | b>>(32-21) + c a += (c ^ (b | ^d)) + X[(7*4)&15] + 1700485571 a = a<<6 | a>>(32-6) + b d += (b ^ (a | ^c)) + X[(7*5)&15] + 2399980690 d = d<<10 | d>>(32-10) + a c += (a ^ (d | ^b)) + X[(7*6)&15] + 4293915773 c = c<<15 | c>>(32-15) + d b += (d ^ (c | ^a)) + X[(7*7)&15] + 2240044497 b = b<<21 | b>>(32-21) + c a += (c ^ (b | ^d)) + X[(7*8)&15] + 1873313359 a = a<<6 | a>>(32-6) + b d += (b ^ (a | ^c)) + X[(7*9)&15] + 4264355552 d = d<<10 | d>>(32-10) + a c += (a ^ (d | ^b)) + X[(7*10)&15] + 2734768916 c = c<<15 | c>>(32-15) + d b += (d ^ (c | ^a)) + X[(7*11)&15] + 1309151649 b = b<<21 | b>>(32-21) + c a += (c ^ (b | ^d)) + X[(7*12)&15] + 4149444226 a = a<<6 | a>>(32-6) + b d += (b ^ (a | ^c)) + X[(7*13)&15] + 3174756917 d = d<<10 | d>>(32-10) + a c += (a ^ (d | ^b)) + X[(7*14)&15] + 718787259 c = c<<15 | c>>(32-15) + d b += (d ^ (c | ^a)) + X[(7*15)&15] + 3951481745 b = b<<21 | b>>(32-21) + c a += aa b += bb c += cc d += dd p = p[chunk:] } dig.s[0] = a dig.s[1] = b dig.s[2] = c dig.s[3] = d }
func main() { unsafe.Alignof(0) // ERROR "unsafe\.Alignof|value computed is not used" f(time.Wednesday) // ERROR "time.Wednesday|incompatible type" }
func ffi_type_for(typ CType, elements **_C_ffi_type) _C_ffi_type { // I'm hoping here that GO's alignof gives us the right result var aid_uint8 uint8 var aid_uint16 uint16 var aid_uint32 uint32 var aid_uint64 uint64 var aid_sint8 int8 var aid_sint16 int16 var aid_sint32 int32 var aid_sint64 int64 var aid_float float32 var aid_double float64 var aid_pointer unsafe.Pointer var uint8_align = unsafe.Alignof(aid_uint8) var uint16_align = unsafe.Alignof(aid_uint16) var uint32_align = unsafe.Alignof(aid_uint32) var uint64_align = unsafe.Alignof(aid_uint64) var sint8_align = unsafe.Alignof(aid_sint8) var sint16_align = unsafe.Alignof(aid_sint16) var sint32_align = unsafe.Alignof(aid_sint32) var sint64_align = unsafe.Alignof(aid_sint64) var pointer_align = unsafe.Alignof(aid_pointer) var double_align = unsafe.Alignof(aid_double) var float_align = unsafe.Alignof(aid_float) var uint8_size = unsafe.Sizeof(aid_uint8) var uint16_size = unsafe.Sizeof(aid_uint16) var uint32_size = unsafe.Sizeof(aid_uint32) var uint64_size = unsafe.Sizeof(aid_uint64) var sint8_size = unsafe.Sizeof(aid_sint8) var sint16_size = unsafe.Sizeof(aid_sint16) var sint32_size = unsafe.Sizeof(aid_sint32) var sint64_size = unsafe.Sizeof(aid_sint64) var pointer_size = unsafe.Sizeof(aid_pointer) var double_size = unsafe.Sizeof(aid_double) var float_size = unsafe.Sizeof(aid_float) switch typ { case TYPE_VOID: return ffi_type_make(1, 1, typ, nil) case TYPE_POINTER: return ffi_type_make(pointer_size, pointer_align, typ, nil) case TYPE_UINT8: return ffi_type_make(uint8_size, uint8_align, typ, nil) case TYPE_SINT8: return ffi_type_make(sint8_size, sint8_align, typ, nil) case TYPE_UINT16: return ffi_type_make(uint16_size, uint16_align, typ, nil) case TYPE_SINT16: return ffi_type_make(sint16_size, sint16_align, typ, nil) case TYPE_UINT32: return ffi_type_make(uint32_size, uint32_align, typ, nil) case TYPE_SINT32: return ffi_type_make(sint32_size, sint32_align, typ, nil) case TYPE_UINT64: return ffi_type_make(uint64_size, uint64_align, typ, nil) case TYPE_SINT64: return ffi_type_make(sint64_size, sint64_align, typ, nil) case TYPE_FLOAT: return ffi_type_make(float_size, float_align, typ, nil) case TYPE_DOUBLE: return ffi_type_make(double_size, double_align, typ, nil) default: // struct return ffi_type_make(0, 0, typ, elements) } // we cannot get here, but go complains, so ... return ffi_type_make(1, 1, typ, nil) }
func F() { var a []int var c chan int var m map[int]int var s struct{ f int } append(a, 0) // ERROR "not used" cap(a) // ERROR "not used" complex(1, 2) // ERROR "not used" imag(1i) // ERROR "not used" len(a) // ERROR "not used" make([]int, 10) // ERROR "not used" new(int) // ERROR "not used" real(1i) // ERROR "not used" unsafe.Alignof(a) // ERROR "not used" unsafe.Offsetof(s.f) // ERROR "not used" unsafe.Sizeof(a) // ERROR "not used" close(c) copy(a, a) delete(m, 0) panic(0) print("foo") println("bar") recover() (close(c)) (copy(a, a)) (delete(m, 0)) (panic(0)) (print("foo")) (println("bar")) (recover()) go append(a, 0) // ERROR "discards result" go cap(a) // ERROR "discards result" go complex(1, 2) // ERROR "discards result" go imag(1i) // ERROR "discards result" go len(a) // ERROR "discards result" go make([]int, 10) // ERROR "discards result" go new(int) // ERROR "discards result" go real(1i) // ERROR "discards result" go unsafe.Alignof(a) // ERROR "discards result" go unsafe.Offsetof(s.f) // ERROR "discards result" go unsafe.Sizeof(a) // ERROR "discards result" go close(c) go copy(a, a) go delete(m, 0) go panic(0) go print("foo") go println("bar") go recover() defer append(a, 0) // ERROR "discards result" defer cap(a) // ERROR "discards result" defer complex(1, 2) // ERROR "discards result" defer imag(1i) // ERROR "discards result" defer len(a) // ERROR "discards result" defer make([]int, 10) // ERROR "discards result" defer new(int) // ERROR "discards result" defer real(1i) // ERROR "discards result" defer unsafe.Alignof(a) // ERROR "discards result" defer unsafe.Offsetof(s.f) // ERROR "discards result" defer unsafe.Sizeof(a) // ERROR "discards result" defer close(c) defer copy(a, a) defer delete(m, 0) defer panic(0) defer print("foo") defer println("bar") defer recover() }
func (ts *taskSched) init() { var vt []exce.Vector vtlen := 1 << irtExp() vtsize := vtlen * int(unsafe.Sizeof(exce.Vector{})) Heap = allocTop( unsafe.Pointer(&vt), Heap, vtlen, unsafe.Sizeof(exce.Vector{}), unsafe.Alignof(exce.Vector{}), uintptr(vtsize), ) if Heap == nil { panicMemory() } Heap = allocTop( unsafe.Pointer(&ts.tasks), Heap, MaxTasks(), unsafe.Sizeof(taskInfo{}), unsafe.Alignof(taskInfo{}), unsafe.Alignof(taskInfo{}), ) if Heap == nil { panicMemory() } // Set taskInfo for initial (current) task. ts.tasks[0].prio = 255 ts.tasks[0].setState(taskReady) // Use PSP as stack pointer for thread mode. cortexm.SetPSP(unsafe.Pointer(cortexm.MSP())) barrier.Sync() cortexm.SetCtrl(cortexm.Ctrl() | cortexm.UsePSP) cortexm.ISB() // Now MSP is used only by exceptions handlers. cortexm.SetMSP(unsafe.Pointer(initSP(len(ts.tasks)))) // Setup interrupt table. // Consider setup at link time using GCC weak functions to support Cortex-M0 // and (in case of Cortex-M3,4) to allow vector load on the ICode bus // simultaneously with registers stacking on DCode bus. vt[exce.NMI] = exce.VectorFor(nmiHandler) vt[exce.HardFault] = exce.VectorFor(hardFaultHandler) vt[exce.MemFault] = exce.VectorFor(memFaultHandler) vt[exce.BusFault] = exce.VectorFor(busFaultHandler) vt[exce.UsageFault] = exce.VectorFor(usageFaultHandler) vt[exce.SVCall] = exce.VectorFor(svcHandler) vt[exce.PendSV] = exce.VectorFor(pendSVHandler) vt[exce.SysTick] = exce.VectorFor(sysTickHandler) exce.UseTable(vt) exce.MemFault.Enable() exce.BusFault.Enable() exce.UsageFault.Enable() exce.SVCall.SetPrio(exce.Lowest) exce.PendSV.SetPrio(exce.Lowest) sysTickStart() tasker.forceNext = -1 tasker.run() }
func main() { unsafe.Alignof(0) // ERROR "unsafe\.Alignof|value computed is not used" }