当前位置: 首页>>代码示例>>Golang>>正文


Golang unsafe.Alignof函数代码示例

本文整理汇总了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))
}
开发者ID:Ken1JF,项目名称:sgfdb,代码行数:7,代码来源:sgfdb.go

示例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"
}
开发者ID:qunhu,项目名称:go_src_comment,代码行数:58,代码来源:issue4654.go

示例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)
	}
}
开发者ID:horzadome,项目名称:gcsfuse,代码行数:10,代码来源:out_message.go

示例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))
}
开发者ID:souravbh,项目名称:lattice-release,代码行数:7,代码来源:seccomp_unix.go

示例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()
}
开发者ID:qunhu,项目名称:go_src_comment,代码行数:26,代码来源:sizeof.go

示例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)
}
开发者ID:sthapaun,项目名称:ex,代码行数:25,代码来源:memory-footprint2.go

示例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)
}
开发者ID:JacobJohansen,项目名称:gotraining,代码行数:33,代码来源:example1.go

示例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))
}
开发者ID:0x9e3779b9,项目名称:golang_lab,代码行数:8,代码来源:slice.go

示例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
}
开发者ID:zacheryph,项目名称:gocode,代码行数:9,代码来源:main.go

示例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
}
开发者ID:enceve,项目名称:crypto,代码行数:10,代码来源:siphash_test.go

示例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
	}
}
开发者ID:2thetop,项目名称:go,代码行数:23,代码来源:bug279.go

示例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])
	}
}
开发者ID:gnanderson,项目名称:go,代码行数:15,代码来源:md5_test.go

示例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)
}
开发者ID:gaurav36,项目名称:gotraining,代码行数:49,代码来源:example1.go

示例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
}
开发者ID:postfix,项目名称:fslm,代码行数:45,代码来源:sorted.go

示例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)
}
开发者ID:sthapaun,项目名称:ex,代码行数:41,代码来源:memory-footprint.go


注:本文中的unsafe.Alignof函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。