• golang中的unsafe.Pointer,指针,引用


    在golang的源码中到处都能看到unsafe.Pointer的使用,它允许程序绕过类型系统读写任意内存,使用它时必须谨慎。

    golang中基本的地址操作

    var a int
    b := &a // 取得a的地址 0xc00000c0a8
    c := *b // 读取地址指向的内存空间
    
    • 1
    • 2
    • 3
    func mm5() {
    	b := Conf{}
    
    	mm6(b)
        fmt.Println(b) // {}
    
    	mm7(&b)
        fmt.Println(b) // {x}
    
    	mm8(&b)
        fmt.Println(b) // {y}
    }
    func mm6(y Conf) {
    	y.Name = "r"
    }
    func mm7(y *Conf) {
    	y.Name = "x"
    }
    func mm8(y *Conf) {
    	*y = Conf{"y"} // 向 y 指向的内存空间赋值
    	y = &Conf{"z"} // 向 y 赋值,指向了另外一个内存地址
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    指针不仅仅是一个地址,它还带有原变量的类型

    var a int
    var b *float64
    f = &a // error
    
    • 1
    • 2
    • 3

    unsafe包

    unsafe 包用于编译阶段可以绕过 Go 语言的类型系统,直接操作内存。例如,利用 unsafe 包操作一个结构体的未导出成员。unsafe 包让我可以直接读写内存的能力。

    // 实际上是整型,用来存储任意类型的指针的值
    // 可以进行整型相关的运算,加减乘除取模比较等等
    // Pointer 和 uintprt 可以相互转换 uintptr(Pointer)
    type uintptr uintptr
    
    type IntegerType int
    
    // ArbitraryType是int的一个别名,在 Go 中ArbitraryType有特殊的意义,意为任意类型
    type ArbitraryType int
    
    // 可以把任意指针类型转换成unsafe.Pointer类型,unsafe.Pointer(&a)
    // 虽然表现是也是整型数值的,但是不能进行整型的运算,需要先转换成 uintptr 再去运算
    type Pointer *ArbitraryType
    
    // Sizeof接受任意类型的值(表达式),返回其占用的字节数
    // 任意变量
    func Sizeof(x ArbitraryType) uintptr
    
    // 返回结构体成员在内存中的位置距离结构体起始处的字节数,所传参数必须是结构体的
    // 成员(结构体指针指向的地址就是结构体起始处的地址,即第一个成员的内存地址)
    // 任意变量
    // unsafe.Sizeof(b.Name)
    func Offsetof(x ArbitraryType) uintptr
    
    // 返回变量是按多少字节对齐的
    // 任意变量
    // unsafe.Alignof(wg.state1)
    func Alignof(x ArbitraryType) uintptr
    
    // 向 ptr 追加一定的长度
    // 等价于 Pointer(uintptr(ptr) + uintptr(len))
    func Add(ptr Pointer, len IntegerType) Pointer
    
    // 返回一个切片,切片的起始位置为 ptr,切片的长度为 len
    // 等价于 (*[len]ArbitraryType)(unsafe.Pointer(ptr))[:]
    func Slice(ptr *ArbitraryType, len IntegerType) []ArbitraryType
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    下面重点看看Pointer

    // 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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164

    unsafe包提供了一些跳过go语言类型安全限制的操作。

    1、任意类型的指针可以转换为一个Pointer类型值

    unsafe.Pointer(&a)
    
    • 1

    2、一个Pointer类型值可以转换为任意类型的指针

    // 此处将 float64 类型直接转换成 uint64 类型
    func Float64bits(f float64) uint64 {
    	return *(*uint64)(unsafe.Pointer(&f))
    }
    
    // 在标准库经常能看到这个用法,比如 src/runtime/select.go 中 selectgo() 方法里
    // cas1 := (*[1 << 16]scase)(unsafe.Pointer(cas0))
    // cas0 类型为 *scase,此表达式意为将 cas0 转换成 *[65536]scase
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    3、一个Pointer类型值可以转换为一个uintptr类型值

    uintptr(unsafe.Pointer(&a))
    
    • 1

    4、一个uintptr类型值可以转换为一个Pointer类型值

    uintptr 不能随意的转回 Pointer,可能会破坏类型系统,因为并不是所有的数字都是有效的内存地址。
    uintptr 并没有指针的语义,意思就是存储 uintptr 值的内存地址在Go发生GC时会被回收。而 unsafe.Pointer 有指针语义,可以保护它不会被垃圾回收。
    
    • 1
    • 2
  • 相关阅读:
    git的安装与使用
    uniapp踩坑小伎俩记录
    python3-算法刷题-数组-记录累加和差值-更新中
    软件工程概述----- Scrum敏捷开发
    欢迎初识MongoDB
    uniapp 轮播列表左右滑动,滑动到中间放大
    【C++11并发】thread 笔记
    JDK 自带的服务发现框架 ServiceLoader 好用吗?
    Linux编译链接选项静态库--whole-archive,--no-whole-archive
    Linux进程终止
  • 原文地址:https://blog.csdn.net/raoxiaoya/article/details/126116493