欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用golang-unsafe包的注意事項及說明

 更新時間:2023年02月10日 09:23:07   作者:sky527759  
這篇文章主要介紹了使用golang-unsafe包的注意事項及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

基于golang 15.5

總結(詳細的內容可以往下看)

1.不能使用unsafe包里的ArbitraryType類型

2.Pointer類型可以表示任意類型的指針,所以可以用Pointer類型作為中介進行兩種不同類型指針的轉換,為保證作為中介的Pointer類型數(shù)據(jù)有效,必須保證所有轉換在同一個表達式中,如:

func Float64bits(f float64) uint64 {
	return *(*uint64)(unsafe.Pointer(&f))
}

3.Pointer類型可以安全有效的轉換成uintptr,不能以任何形式(包含變量)保存轉換后的uintptr值,轉換后的uintptr值僅在轉換所在的表達式中有效的。

4.因為uintptr只是內存地址值,并沒有指針語義,所以uintptr轉換成Pointer通常不會是有效的

下面列舉有效的轉換方式:

1:在同一個表達式內,對Pointer轉換成的uintptr值進行算術運算(包括加減偏移量等,和c不同,指向初始分配內存的end邊界點是無效的),然后在轉換回Pointer

2:使用syscall.Syscall. syscall包的Syscall函數(shù)直接將uintptr傳遞給操作系統(tǒng),根據(jù)調用的細節(jié),會將他們中的一些重新轉換為指針,系統(tǒng)調用將會隱式轉換uintptr成pointer,如果一個指針作為函數(shù)的實參,而對應的形參是uintptr,那這個轉換必須寫在調用函數(shù)的表達式上,如

syscall.Syscall(SYS_READ, uintptr(fd), uintptr(unsafe.Pointer(p)), uintptr(n))

3:反射包的Value類型的方法Pointer()和UnsafeAddr()返回uintptr而不是unsafe.Pointer,以防止在沒有導入unsafe包情況下,調用者將結果更改為任意類型。但是這意味著結果是不穩(wěn)定的,必須在調用后立即在同一表達式中將其轉換為Pointer,如:

p := (*int)(unsafe.Pointer(reflect.ValueOf(new(int)).Pointer()))

4:反射包的結構體SliceHeader和StringHeader將字段Data聲明為uintptr,以防止調用者在不沒有導入unsafe包的情況下將結果更改為任意類型。

但是,這意味著SliceHeader和StringHeader僅在解釋實際切片或字符串值的內容時才有效。如:

var s string
hdr := (*reflect.StringHeader)(unsafe.Pointer(&s)) // case 1
hdr.Data = uintptr(unsafe.Pointer(p))              // case 6 (this case)
hdr.Len = n

詳細內容

1.ArbitraryType類型在本文檔里表示任意一種類型,但并非一個實際存在于unsafe包的類型。

2.Pointer類型用于表示任意類型的指針。有4個特殊的只能用于Pointer類型的操作:

  • 任意類型的指針可以轉換為一個Pointer類型值
  • 一個Pointer類型值可以轉換為任意類型的指針
  • 一個uintptr類型值可以轉換為一個Pointer類型值
  • 一個Pointer類型值可以轉換為一個uintptr類型值

因此,Pointer類型允許程序繞過類型系統(tǒng)讀寫任意內存。使用它時必須謹慎。

3.以下使用Pointer類型的示范操作都能確保所使用的Pointer是有效的,不遵循這些示范的操作在后續(xù)的golang版本的迭代中不保證操作包含的Pointer是有效的

Conversion of a *T1 to Pointer to *T2

Sizeof(T2)必須小于等于Sizeof(T1),下面是例子

func Float64bits(f float64) uint64 {
	return *(*uint64)(unsafe.Pointer(&f))
}

Conversion of a Pointer to a uintptr (but not back to Pointer)

將Pointer轉換成uintptr會用Pointer產(chǎn)生一個不帶指針語義的整數(shù)值并賦值給uintptr,這個整數(shù)值是一個內存地址值,盡管是內存地址值,但是當這個內存地址對應的對象被移動至其他內存區(qū)域或者對應的對象被回收時,gc并不會更新這個值,所以將一個uintptr轉換成Pointer通常不會是有效的,下面列舉了將uintptr轉換成Pointer有效的幾種方式

Conversion of a Pointer to a uintptr and back, with arithmetic.

這種方式通常用于獲取結構體字段值或者數(shù)組元素值,下面是例子

p = unsafe.Pointer(uintptr(p) + offset)

// equivalent to f := unsafe.Pointer(&s.f)
f := unsafe.Pointer(uintptr(unsafe.Pointer(&s)) + unsafe.Offsetof(s.f))

// equivalent to e := unsafe.Pointer(&x[i])
e := unsafe.Pointer(uintptr(unsafe.Pointer(&x[0])) + i*unsafe.Sizeof(x[0]))

使用這種方式對指針進行加減偏移量(即±Offsetof)也是有效的,對指針進行&^操作也是有效的,通常用于對齊。

所有的方式,結果都必須指向最初被分配的對象。

和c不同,指向初始分配內存的end邊界點是無效的,下面是例子

// INVALID: end points outside allocated space.
var s thing
end = unsafe.Pointer(uintptr(unsafe.Pointer(&s)) + unsafe.Sizeof(s))

// INVALID: end points outside allocated space.
b := make([]byte, n)
end = unsafe.Pointer(uintptr(unsafe.Pointer(&b[0])) + uintptr(n))

所有的轉換都必須在相同的表達式內,他們之間也只能有算術操作,下面是無效的例子

// INVALID: uintptr cannot be stored in variable
// before conversion back to Pointer.
u := uintptr(p)
p = unsafe.Pointer(u + offset)

指針必須指向一個已分配的對象,這樣才不會接觸到nil指針,下面是無效的例子

// INVALID: conversion of nil pointer
u := unsafe.Pointer(nil)
p := unsafe.Pointer(uintptr(u) + offset)

Conversion of a Pointer to a uintptr when calling syscall.Syscall.

syscall包的Syscall函數(shù)直接將uintptr傳遞給操作系統(tǒng),根據(jù)調用的細節(jié),會將他們中的一些重新轉換為指針,系統(tǒng)調用將會隱式轉換uintptr成pointer,如果一個指針作為函數(shù)的實參,而對應的形參是uintptr,那這個轉換必須寫在調用函數(shù)的表達式上,下面是例子

syscall.Syscall(SYS_READ, uintptr(fd), uintptr(unsafe.Pointer(p)), uintptr(n))

編譯器通過安排,保留所引用的分配對象(如果有的話),并且在調用完成之前不會移動分配的對象,在匯編中實現(xiàn)的函數(shù)調用的參數(shù)列表中將Pointer轉換為uintptr,即使從類型本身來看,在調用過程中不再需要該對象。為了編譯器能識別這種轉換,轉換表達式必須顯式出現(xiàn)在實參列表中。下面是錯誤的例子:

// INVALID: uintptr cannot be stored in variable
// before implicit conversion back to Pointer during system call.
u := uintptr(unsafe.Pointer(p))
syscall.Syscall(SYS_READ, uintptr(fd), u, uintptr(n)

Conversion of the result of reflect.Value.Pointer or reflect.Value.UnsafeAddr from uintptr to Pointer.

反射包的Value類型的方法Pointer()和UnsafeAddr()返回uintptr而不是unsafe.Pointer,以防止在沒有導入unsafe包情況下,調用者將結果更改為任意類型。

但是這意味著結果是不穩(wěn)定的,必須在調用后立即在同一表達式中將其轉換為Pointer,下面是正確的例子:

p := (*int)(unsafe.Pointer(reflect.ValueOf(new(int)).Pointer()))

與上面的例子相反,在轉換前用變量保存結果(反射包的Value類型的方法Pointer()和UnsafeAddr()的返回值)是無效的,下面是例子:

// INVALID: uintptr cannot be stored in variable
// before conversion back to Pointer.
u := reflect.ValueOf(new(int)).Pointer()
p := (*int)(unsafe.Pointer(u))

Conversion of a reflect.SliceHeader or reflect.StringHeader Data field to or from Pointer.

與前面的情況一樣,反射包的結構體SliceHeader和StringHeader將字段Data聲明為uintptr,以防止調用者在不沒有導入unsafe包的情況下將結果更改為任意類型。

但是,這意味著SliceHeader和StringHeader僅在解釋實際切片或字符串值的內容時才有效。下面是例子:

var s string
hdr := (*reflect.StringHeader)(unsafe.Pointer(&s)) // case 1
hdr.Data = uintptr(unsafe.Pointer(p))              // case 6 (this case)
hdr.Len = n

在這種用法中,hdr.Data實際上是引用字符串基礎指針的替代方法,而不是uintptr變量本身。

通常,reflect.SliceHeader和reflect.StringHeader只能用作*reflect.SliceHeader和*reflect.StringHeader指向實際的切片或字符串的情況,而不能用作普通結構體。

程序不應聲明或分配這些結構體類型的變量。下面是無效的例子:

// INVALID: a directly-declared header will not hold Data as a reference.
var hdr reflect.StringHeader
hdr.Data = uintptr(unsafe.Pointer(p))
hdr.Len = n
s := *(*string)(unsafe.Pointer(&hdr)) // p possibly already lost

最后是unsafe包提供的幾種方法的解釋:

// Sizeof 返回類型v本身數(shù)據(jù)所占用的字節(jié)數(shù)。
// 返回值是“頂層”的數(shù)據(jù)占有的字節(jié)數(shù)。
// 例如,若v是一個切片,它會返回該切片描述符的大小,
// 而非該切片底層引用的內存的大小。
func Sizeof(x ArbitraryType) uintptr
// Offsetof 返回類型v所代表的結構體字段在結構體中的偏移量,
// 它必須為結構體類型的字段的形式。
// 換句話說,它返回該結構起始處與該字段起始處之間的字節(jié)數(shù)。
func Offsetof(x ArbitraryType) uintptr
// Alignof 返回類型v的對齊方式(即類型v在內存中占用的字節(jié)數(shù));
// 若是結構體類型的字段的形式,它會返回字段f在該結構體中的對齊方式。
func Alignof(x ArbitraryType) uintptr

以下是unsafe.go的原內容

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

/*
	Package unsafe contains operations that step around the type safety of Go programs.

	Packages that import unsafe may be non-portable and are not protected by the
	Go 1 compatibility guidelines.
*/
package unsafe

// ArbitraryType is here for the purposes of documentation only and is not actually
// part of the unsafe package. It represents the type of an arbitrary Go expression.
type ArbitraryType int

// Pointer represents a pointer to an arbitrary type. There are four special operations
// available for type Pointer that are not available for other types:
//	- A pointer value of any type can be converted to a Pointer.
//	- A Pointer can be converted to a pointer value of any type.
//	- A uintptr can be converted to a Pointer.
//	- A Pointer can be converted to a uintptr.
// Pointer therefore allows a program to defeat the type system and read and write
// arbitrary memory. It should be used with extreme care.
//
// The following patterns involving Pointer are valid.
// Code not using these patterns is likely to be invalid today
// or to become invalid in the future.
// Even the valid patterns below come with important caveats.
//
// Running "go vet" can help find uses of Pointer that do not conform to these patterns,
// but silence from "go vet" is not a guarantee that the code is valid.
//
// (1) Conversion of a *T1 to Pointer to *T2.
//
// Provided that T2 is no larger than T1 and that the two share an equivalent
// memory layout, this conversion allows reinterpreting data of one type as
// data of another type. An example is the implementation of
// math.Float64bits:
//
//	func Float64bits(f float64) uint64 {
//		return *(*uint64)(unsafe.Pointer(&f))
//	}
//
// (2) Conversion of a Pointer to a uintptr (but not back to Pointer).
//
// Converting a Pointer to a uintptr produces the memory address of the value
// pointed at, as an integer. The usual use for such a uintptr is to print it.
//
// Conversion of a uintptr back to Pointer is not valid in general.
//
// A uintptr is an integer, not a reference.
// Converting a Pointer to a uintptr creates an integer value
// with no pointer semantics.
// Even if a uintptr holds the address of some object,
// the garbage collector will not update that uintptr's value
// if the object moves, nor will that uintptr keep the object
// from being reclaimed.
//
// The remaining patterns enumerate the only valid conversions
// from uintptr to Pointer.
//
// (3) Conversion of a Pointer to a uintptr and back, with arithmetic.
//
// If p points into an allocated object, it can be advanced through the object
// by conversion to uintptr, addition of an offset, and conversion back to Pointer.
//
//	p = unsafe.Pointer(uintptr(p) + offset)
//
// The most common use of this pattern is to access fields in a struct
// or elements of an array:
//
//	// equivalent to f := unsafe.Pointer(&s.f)
//	f := unsafe.Pointer(uintptr(unsafe.Pointer(&s)) + unsafe.Offsetof(s.f))
//
//	// equivalent to e := unsafe.Pointer(&x[i])
//	e := unsafe.Pointer(uintptr(unsafe.Pointer(&x[0])) + i*unsafe.Sizeof(x[0]))
//
// It is valid both to add and to subtract offsets from a pointer in this way.
// It is also valid to use &^ to round pointers, usually for alignment.
// In all cases, the result must continue to point into the original allocated object.
//
// Unlike in C, it is not valid to advance a pointer just beyond the end of
// its original allocation:
//
//	// INVALID: end points outside allocated space.
//	var s thing
//	end = unsafe.Pointer(uintptr(unsafe.Pointer(&s)) + unsafe.Sizeof(s))
//
//	// INVALID: end points outside allocated space.
//	b := make([]byte, n)
//	end = unsafe.Pointer(uintptr(unsafe.Pointer(&b[0])) + uintptr(n))
//
// Note that both conversions must appear in the same expression, with only
// the intervening arithmetic between them:
//
//	// INVALID: uintptr cannot be stored in variable
//	// before conversion back to Pointer.
//	u := uintptr(p)
//	p = unsafe.Pointer(u + offset)
//
// Note that the pointer must point into an allocated object, so it may not be nil.
//
//	// INVALID: conversion of nil pointer
//	u := unsafe.Pointer(nil)
//	p := unsafe.Pointer(uintptr(u) + offset)
//
// (4) Conversion of a Pointer to a uintptr when calling syscall.Syscall.
//
// The Syscall functions in package syscall pass their uintptr arguments directly
// to the operating system, which then may, depending on the details of the call,
// reinterpret some of them as pointers.
// That is, the system call implementation is implicitly converting certain arguments
// back from uintptr to pointer.
//
// If a pointer argument must be converted to uintptr for use as an argument,
// that conversion must appear in the call expression itself:
//
//	syscall.Syscall(SYS_READ, uintptr(fd), uintptr(unsafe.Pointer(p)), uintptr(n))
//
// The compiler handles a Pointer converted to a uintptr in the argument list of
// a call to a function implemented in assembly by arranging that the referenced
// allocated object, if any, is retained and not moved until the call completes,
// even though from the types alone it would appear that the object is no longer
// needed during the call.
//
// For the compiler to recognize this pattern,
// the conversion must appear in the argument list:
//
//	// INVALID: uintptr cannot be stored in variable
//	// before implicit conversion back to Pointer during system call.
//	u := uintptr(unsafe.Pointer(p))
//	syscall.Syscall(SYS_READ, uintptr(fd), u, uintptr(n))
//
// (5) Conversion of the result of reflect.Value.Pointer or reflect.Value.UnsafeAddr
// from uintptr to Pointer.
//
// Package reflect's Value methods named Pointer and UnsafeAddr return type uintptr
// instead of unsafe.Pointer to keep callers from changing the result to an arbitrary
// type without first importing "unsafe". However, this means that the result is
// fragile and must be converted to Pointer immediately after making the call,
// in the same expression:
//
//	p := (*int)(unsafe.Pointer(reflect.ValueOf(new(int)).Pointer()))
//
// As in the cases above, it is invalid to store the result before the conversion:
//
//	// INVALID: uintptr cannot be stored in variable
//	// before conversion back to Pointer.
//	u := reflect.ValueOf(new(int)).Pointer()
//	p := (*int)(unsafe.Pointer(u))
//
// (6) Conversion of a reflect.SliceHeader or reflect.StringHeader Data field to or from Pointer.
//
// As in the previous case, the reflect data structures SliceHeader and StringHeader
// declare the field Data as a uintptr to keep callers from changing the result to
// an arbitrary type without first importing "unsafe". However, this means that
// SliceHeader and StringHeader are only valid when interpreting the content
// of an actual slice or string value.
//
//	var s string
//	hdr := (*reflect.StringHeader)(unsafe.Pointer(&s)) // case 1
//	hdr.Data = uintptr(unsafe.Pointer(p))              // case 6 (this case)
//	hdr.Len = n
//
// In this usage hdr.Data is really an alternate way to refer to the underlying
// pointer in the string header, not a uintptr variable itself.
//
// In general, reflect.SliceHeader and reflect.StringHeader should be used
// only as *reflect.SliceHeader and *reflect.StringHeader pointing at actual
// slices or strings, never as plain structs.
// A program should not declare or allocate variables of these struct types.
//
//	// INVALID: a directly-declared header will not hold Data as a reference.
//	var hdr reflect.StringHeader
//	hdr.Data = uintptr(unsafe.Pointer(p))
//	hdr.Len = n
//	s := *(*string)(unsafe.Pointer(&hdr)) // p possibly already lost
//
type Pointer *ArbitraryType

// Sizeof takes an expression x of any type and returns the size in bytes
// of a hypothetical variable v as if v was declared via var v = x.
// The size does not include any memory possibly referenced by x.
// For instance, if x is a slice, Sizeof returns the size of the slice
// descriptor, not the size of the memory referenced by the slice.
// The return value of Sizeof is a Go constant.
func Sizeof(x ArbitraryType) uintptr

// Offsetof returns the offset within the struct of the field represented by x,
// which must be of the form structValue.field. In other words, it returns the
// number of bytes between the start of the struct and the start of the field.
// The return value of Offsetof is a Go constant.
func Offsetof(x ArbitraryType) uintptr

// Alignof takes an expression x of any type and returns the required alignment
// of a hypothetical variable v as if v was declared via var v = x.
// It is the largest value m such that the address of v is always zero mod m.
// It is the same as the value returned by reflect.TypeOf(x).Align().
// As a special case, if a variable s is of struct type and f is a field
// within that struct, then Alignof(s.f) will return the required alignment
// of a field of that type within a struct. This case is the same as the
// value returned by reflect.TypeOf(s.f).FieldAlign().
// The return value of Alignof is a Go constant.
func Alignof(x ArbitraryType) uintptr

總結

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • go語言import報錯處理圖文詳解

    go語言import報錯處理圖文詳解

    今天本來想嘗試一下go語言中公有和私有的方法,結果import其他包的時候直接報錯了,下面這篇文章主要給大家介紹了關于go語言import報錯處理的相關資料,需要的朋友可以參考下
    2023-04-04
  • golang執(zhí)行命令獲取執(zhí)行結果狀態(tài)(推薦)

    golang執(zhí)行命令獲取執(zhí)行結果狀態(tài)(推薦)

    這篇文章主要介紹了golang執(zhí)行命令獲取執(zhí)行結果狀態(tài)的相關知識,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧
    2019-11-11
  • golang代碼檢測工具之goimports解讀

    golang代碼檢測工具之goimports解讀

    這篇文章主要介紹了golang代碼檢測工具之goimports使用,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • 深入了解GoLang中的工廠設計模式

    深入了解GoLang中的工廠設計模式

    這篇文章主要介紹了深入了解GoLang中的工廠設計模式,工廠模式是一種常用的設計模式,它屬于創(chuàng)建型模式,它的主要目的是封裝對象的創(chuàng)建過程,將對象的創(chuàng)建過程與對象的使用過程分離,從而提高代碼的可維護性和可擴展性,需要詳細了解可以參考下文
    2023-05-05
  • Golang并發(fā)編程之main goroutine的創(chuàng)建與調度詳解

    Golang并發(fā)編程之main goroutine的創(chuàng)建與調度詳解

    這篇文章主要為大家詳細介紹了Golang并發(fā)編程中main goroutine的創(chuàng)建與調度,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2023-03-03
  • 詳解Golang time包中的time.Duration類型

    詳解Golang time包中的time.Duration類型

    在日常開發(fā)過程中,會頻繁遇到對時間進行操作的場景,使用 Golang 中的 time 包可以很方便地實現(xiàn)對時間的相關操作,本文講解一下 time 包中的 time.Duration 類型,需要的朋友可以參考下
    2023-07-07
  • golang int64轉int的方法

    golang int64轉int的方法

    這篇文章主要介紹了golang int64轉int,本文給大家提供兩種方法 ,將 golang int64 轉換為golang int,結合實例代碼給大家分享轉換方法,需要的朋友可以參考下
    2023-01-01
  • go原生庫的中bytes.Buffer用法

    go原生庫的中bytes.Buffer用法

    這篇文章主要介紹了go原生庫的中bytes.Buffer用法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • Go語言題解LeetCode下一個更大元素示例詳解

    Go語言題解LeetCode下一個更大元素示例詳解

    這篇文章主要為大家介紹了Go語言題解LeetCode下一個更大元素示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • Go中init()執(zhí)行順序詳解

    Go中init()執(zhí)行順序詳解

    go允許定義多個init(),多個init()會涉及到執(zhí)行先后的問題,本文將詳細講解Go中init()執(zhí)行順序,感興趣的朋友一起看看吧
    2022-09-09

最新評論