本文整理汇总了Golang中unsafe.Alignof函数的典型用法代码示例。如果您正苦于以下问题:Golang Alignof函数的具体用法?Golang Alignof怎么用?Golang Alignof使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Alignof函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: PrintSgfDbTypeSizes
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))
}
示例2: f
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"
}
示例3: init
// 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)
}
}
示例4: little
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))
}
示例5: main
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()
}
示例6: main
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)
}
示例7: main
// 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)
}
示例8: main
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))
}
示例9: newBuffer
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
}
示例10: unalignBytes
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
}
示例11: main
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
}
}
示例12: benchmarkSize
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])
}
}
示例13: main
// 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)
}
示例14: WriteBinary
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
}
示例15: main
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)
}