gitee.com/quant1x/gox@v1.7.6/encoding/binary/cstruct/pointer_unsafe.go (about) 1 // Go support for Protocol Buffers - Google's data interchange format 2 // 3 // Copyright 2012 The Go Authors. All rights reserved. 4 // https://github.com/golang/protobuf 5 // 6 // Redistribution and use in source and binary forms, with or without 7 // modification, are permitted provided that the following conditions are 8 // met: 9 // 10 // * Redistributions of source code must retain the above copyright 11 // notice, this list of conditions and the following disclaimer. 12 // * Redistributions in binary form must reproduce the above 13 // copyright notice, this list of conditions and the following disclaimer 14 // in the documentation and/or other materials provided with the 15 // distribution. 16 // * Neither the name of Google Inc. nor the names of its 17 // contributors may be used to endorse or promote products derived from 18 // this software without specific prior written permission. 19 // 20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 32 //go:build !appengine && !js 33 34 // This file contains the implementation of the proto field accesses using package unsafe. 35 36 // package proto 37 package cstruct 38 39 import ( 40 "reflect" 41 "unsafe" 42 ) 43 44 // NOTE: These type_Foo functions would more idiomatically be methods, 45 // but Go does not allow methods on pointer types, and we must preserve 46 // some pointer type for the garbage collector. We use these 47 // funcs with clunky names as our poor approximation to methods. 48 // 49 // An alternative would be 50 // type structPointer cstruct { p unsafe.Pointer } 51 // but that does not registerize as well. 52 53 // A structPointer is a pointer to a cstruct. 54 type structPointer unsafe.Pointer 55 56 // toStructPointer returns a structPointer equivalent to the given reflect value. 57 func toStructPointer(v reflect.Value) structPointer { 58 return structPointer(unsafe.Pointer(v.Pointer())) 59 } 60 61 // IsNil reports whether p is nil. 62 func structPointer_IsNil(p structPointer) bool { 63 return p == nil 64 } 65 66 // Interface returns the cstruct pointer, assumed to have element type t, 67 // as an interface value. 68 func structPointer_Interface(p structPointer, t reflect.Type) interface{} { 69 return reflect.NewAt(t, unsafe.Pointer(p)).Interface() 70 } 71 72 // A field identifies a field in a cstruct, accessible from a structPointer. 73 // In this implementation, a field is identified by its byte offset from the start of the cstruct. 74 type field uintptr 75 76 // toField returns a field equivalent to the given reflect field. 77 func toField(f *reflect.StructField) field { 78 return field(f.Offset) 79 } 80 81 // invalidField is an invalid field identifier. 82 const invalidField = ^field(0) 83 84 // IsValid reports whether the field identifier is valid. 85 func (f field) IsValid() bool { 86 return f != ^field(0) 87 } 88 89 // Bytes returns the address of a []byte field in the cstruct. 90 func structPointer_Bytes(p structPointer, f field) *[]byte { 91 return (*[]byte)(unsafe.Pointer(uintptr(p) + uintptr(f))) 92 } 93 94 // BytesSlice returns the address of a [][]byte field in the cstruct. 95 func structPointer_BytesSlice(p structPointer, f field) *[][]byte { 96 return (*[][]byte)(unsafe.Pointer(uintptr(p) + uintptr(f))) 97 } 98 99 // Bool returns the address of a *bool field in the cstruct. 100 func structPointer_Bool(p structPointer, f field) **bool { 101 return (**bool)(unsafe.Pointer(uintptr(p) + uintptr(f))) 102 } 103 104 // BoolVal returns the address of a bool field in the cstruct. 105 func structPointer_BoolVal(p structPointer, f field) *bool { 106 return (*bool)(unsafe.Pointer(uintptr(p) + uintptr(f))) 107 } 108 109 // BoolSlice returns the address of a []bool field in the cstruct. 110 func structPointer_BoolSlice(p structPointer, f field) *[]bool { 111 return (*[]bool)(unsafe.Pointer(uintptr(p) + uintptr(f))) 112 } 113 114 // String returns the address of a *string field in the cstruct. 115 func structPointer_String(p structPointer, f field) **string { 116 return (**string)(unsafe.Pointer(uintptr(p) + uintptr(f))) 117 } 118 119 // StringVal returns the address of a string field in the cstruct. 120 func structPointer_StringVal(p structPointer, f field) *string { 121 return (*string)(unsafe.Pointer(uintptr(p) + uintptr(f))) 122 } 123 124 // StringSlice returns the address of a []string field in the cstruct. 125 func structPointer_StringSlice(p structPointer, f field) *[]string { 126 return (*[]string)(unsafe.Pointer(uintptr(p) + uintptr(f))) 127 } 128 129 // NewAt returns the reflect.Value for a pointer to a field in the cstruct. 130 func structPointer_NewAt(p structPointer, f field, typ reflect.Type) reflect.Value { 131 return reflect.NewAt(typ, unsafe.Pointer(uintptr(p)+uintptr(f))) 132 } 133 134 // SetStructPointer writes a *cstruct field in the cstruct. 135 func structPointer_SetStructPointer(p structPointer, f field, q structPointer) { 136 *(*structPointer)(unsafe.Pointer(uintptr(p) + uintptr(f))) = q 137 } 138 139 // GetStructPointer reads a *cstruct field in the cstruct. 140 func structPointer_GetStructPointer(p structPointer, f field) structPointer { 141 return *(*structPointer)(unsafe.Pointer(uintptr(p) + uintptr(f))) 142 } 143 144 // StructPointerSlice the address of a []*cstruct field in the cstruct. 145 func structPointer_StructPointerSlice(p structPointer, f field) *structPointerSlice { 146 return (*structPointerSlice)(unsafe.Pointer(uintptr(p) + uintptr(f))) 147 } 148 149 // A structPointerSlice represents a slice of pointers to structs (themselves submessages or groups). 150 type structPointerSlice []structPointer 151 152 func (v *structPointerSlice) Len() int { return len(*v) } 153 func (v *structPointerSlice) Index(i int) structPointer { return (*v)[i] } 154 func (v *structPointerSlice) Append(p structPointer) { *v = append(*v, p) } 155 156 // A word32 is the address of a "pointer to 32-bit value" field. 157 type word32 **uint32 158 159 // IsNil reports whether *v is nil. 160 func word32_IsNil(p word32) bool { 161 return *p == nil 162 } 163 164 // Get gets the value pointed at by *v. 165 func word32_Get(p word32) uint32 { 166 return **p 167 } 168 169 // Word32 returns the address of a *int32, *uint32, *float32, or *enum field in the cstruct. 170 func structPointer_Word32(p structPointer, f field) word32 { 171 return word32((**uint32)(unsafe.Pointer(uintptr(p) + uintptr(f)))) 172 } 173 174 // A word32Val is the address of a 32-bit value field. 175 type word32Val *uint32 176 177 // Set sets *p to x. 178 func word32Val_Set(p word32Val, x uint32) { 179 *p = x 180 } 181 182 // Get gets the value pointed at by p. 183 func word32Val_Get(p word32Val) uint32 { 184 return *p 185 } 186 187 // Word32Val returns the address of a *int32, *uint32, *float32, or *enum field in the cstruct. 188 func structPointer_Word32Val(p structPointer, f field) word32Val { 189 return word32Val((*uint32)(unsafe.Pointer(uintptr(p) + uintptr(f)))) 190 } 191 192 // A word32Slice is a slice of 32-bit values. 193 type word32Slice []uint32 194 195 func (v *word32Slice) Append(x uint32) { *v = append(*v, x) } 196 func (v *word32Slice) Len() int { return len(*v) } 197 func (v *word32Slice) Index(i int) uint32 { return (*v)[i] } 198 199 // Word32Slice returns the address of a []int32, []uint32, []float32, or []enum field in the cstruct. 200 func structPointer_Word32Slice(p structPointer, f field) *word32Slice { 201 return (*word32Slice)(unsafe.Pointer(uintptr(p) + uintptr(f))) 202 } 203 204 // word64 is like word32 but for 64-bit values. 205 type word64 **uint64 206 207 func word64_IsNil(p word64) bool { 208 return *p == nil 209 } 210 211 func word64_Get(p word64) uint64 { 212 return **p 213 } 214 215 func structPointer_Word64(p structPointer, f field) word64 { 216 return word64((**uint64)(unsafe.Pointer(uintptr(p) + uintptr(f)))) 217 } 218 219 // word64Val is like word32Val but for 64-bit values. 220 type word64Val *uint64 221 222 func word64Val_Set(p word64Val, o *Buffer, x uint64) { 223 *p = x 224 } 225 226 func word64Val_Get(p word64Val) uint64 { 227 return *p 228 } 229 230 func structPointer_Word64Val(p structPointer, f field) word64Val { 231 return word64Val((*uint64)(unsafe.Pointer(uintptr(p) + uintptr(f)))) 232 } 233 234 // word64Slice is like word32Slice but for 64-bit values. 235 type word64Slice []uint64 236 237 func (v *word64Slice) Append(x uint64) { *v = append(*v, x) } 238 func (v *word64Slice) Len() int { return len(*v) } 239 func (v *word64Slice) Index(i int) uint64 { return (*v)[i] } 240 241 func structPointer_Word64Slice(p structPointer, f field) *word64Slice { 242 return (*word64Slice)(unsafe.Pointer(uintptr(p) + uintptr(f))) 243 }