...
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 package proto
37
38 import (
39 "reflect"
40 "sync/atomic"
41 "unsafe"
42 )
43
44 const unsafeAllowed = true
45
46
47
48 type field uintptr
49
50
51 func toField(f *reflect.StructField) field {
52 return field(f.Offset)
53 }
54
55
56 const invalidField = ^field(0)
57
58
59 const zeroField = field(0)
60
61
62 func (f field) IsValid() bool {
63 return f != invalidField
64 }
65
66
67
68
69
70 type pointer struct {
71 p unsafe.Pointer
72 }
73
74
75 var ptrSize = unsafe.Sizeof(uintptr(0))
76
77
78
79 func toPointer(i *Message) pointer {
80
81
82
83 return pointer{p: (*[2]unsafe.Pointer)(unsafe.Pointer(i))[1]}
84 }
85
86
87
88 func toAddrPointer(i *interface{}, isptr bool) pointer {
89
90 if isptr {
91
92
93 return pointer{p: unsafe.Pointer(uintptr(unsafe.Pointer(i)) + ptrSize)}
94 }
95
96
97 return pointer{p: (*[2]unsafe.Pointer)(unsafe.Pointer(i))[1]}
98 }
99
100
101 func valToPointer(v reflect.Value) pointer {
102 return pointer{p: unsafe.Pointer(v.Pointer())}
103 }
104
105
106
107 func (p pointer) offset(f field) pointer {
108
109
110
115 return pointer{p: unsafe.Pointer(uintptr(p.p) + uintptr(f))}
116 }
117
118 func (p pointer) isNil() bool {
119 return p.p == nil
120 }
121
122 func (p pointer) toInt64() *int64 {
123 return (*int64)(p.p)
124 }
125 func (p pointer) toInt64Ptr() **int64 {
126 return (**int64)(p.p)
127 }
128 func (p pointer) toInt64Slice() *[]int64 {
129 return (*[]int64)(p.p)
130 }
131 func (p pointer) toInt32() *int32 {
132 return (*int32)(p.p)
133 }
134
135
136
144 func (p pointer) getInt32Ptr() *int32 {
145 return *(**int32)(p.p)
146 }
147 func (p pointer) setInt32Ptr(v int32) {
148 *(**int32)(p.p) = &v
149 }
150
151
152
153
154 func (p pointer) getInt32Slice() []int32 {
155 return *(*[]int32)(p.p)
156 }
157
158
159
160
161 func (p pointer) setInt32Slice(v []int32) {
162 *(*[]int32)(p.p) = v
163 }
164
165
166 func (p pointer) appendInt32Slice(v int32) {
167 s := (*[]int32)(p.p)
168 *s = append(*s, v)
169 }
170
171 func (p pointer) toUint64() *uint64 {
172 return (*uint64)(p.p)
173 }
174 func (p pointer) toUint64Ptr() **uint64 {
175 return (**uint64)(p.p)
176 }
177 func (p pointer) toUint64Slice() *[]uint64 {
178 return (*[]uint64)(p.p)
179 }
180 func (p pointer) toUint32() *uint32 {
181 return (*uint32)(p.p)
182 }
183 func (p pointer) toUint32Ptr() **uint32 {
184 return (**uint32)(p.p)
185 }
186 func (p pointer) toUint32Slice() *[]uint32 {
187 return (*[]uint32)(p.p)
188 }
189 func (p pointer) toBool() *bool {
190 return (*bool)(p.p)
191 }
192 func (p pointer) toBoolPtr() **bool {
193 return (**bool)(p.p)
194 }
195 func (p pointer) toBoolSlice() *[]bool {
196 return (*[]bool)(p.p)
197 }
198 func (p pointer) toFloat64() *float64 {
199 return (*float64)(p.p)
200 }
201 func (p pointer) toFloat64Ptr() **float64 {
202 return (**float64)(p.p)
203 }
204 func (p pointer) toFloat64Slice() *[]float64 {
205 return (*[]float64)(p.p)
206 }
207 func (p pointer) toFloat32() *float32 {
208 return (*float32)(p.p)
209 }
210 func (p pointer) toFloat32Ptr() **float32 {
211 return (**float32)(p.p)
212 }
213 func (p pointer) toFloat32Slice() *[]float32 {
214 return (*[]float32)(p.p)
215 }
216 func (p pointer) toString() *string {
217 return (*string)(p.p)
218 }
219 func (p pointer) toStringPtr() **string {
220 return (**string)(p.p)
221 }
222 func (p pointer) toStringSlice() *[]string {
223 return (*[]string)(p.p)
224 }
225 func (p pointer) toBytes() *[]byte {
226 return (*[]byte)(p.p)
227 }
228 func (p pointer) toBytesSlice() *[][]byte {
229 return (*[][]byte)(p.p)
230 }
231 func (p pointer) toExtensions() *XXX_InternalExtensions {
232 return (*XXX_InternalExtensions)(p.p)
233 }
234 func (p pointer) toOldExtensions() *map[int32]Extension {
235 return (*map[int32]Extension)(p.p)
236 }
237
238
239
240
241 func (p pointer) getPointerSlice() []pointer {
242
243
244 return *(*[]pointer)(p.p)
245 }
246
247
248
249
250 func (p pointer) setPointerSlice(v []pointer) {
251
252
253 *(*[]pointer)(p.p) = v
254 }
255
256
257 func (p pointer) getPointer() pointer {
258 return pointer{p: *(*unsafe.Pointer)(p.p)}
259 }
260
261
262 func (p pointer) setPointer(q pointer) {
263 *(*unsafe.Pointer)(p.p) = q.p
264 }
265
266
267 func (p pointer) appendPointer(q pointer) {
268 s := (*[]unsafe.Pointer)(p.p)
269 *s = append(*s, q.p)
270 }
271
272
273
274 func (p pointer) getInterfacePointer() pointer {
275
276 return pointer{p: (*(*[2]unsafe.Pointer)(p.p))[1]}
277 }
278
279
280
281 func (p pointer) asPointerTo(t reflect.Type) reflect.Value {
282 return reflect.NewAt(t, p.p)
283 }
284
285 func atomicLoadUnmarshalInfo(p **unmarshalInfo) *unmarshalInfo {
286 return (*unmarshalInfo)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(p))))
287 }
288 func atomicStoreUnmarshalInfo(p **unmarshalInfo, v *unmarshalInfo) {
289 atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(p)), unsafe.Pointer(v))
290 }
291 func atomicLoadMarshalInfo(p **marshalInfo) *marshalInfo {
292 return (*marshalInfo)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(p))))
293 }
294 func atomicStoreMarshalInfo(p **marshalInfo, v *marshalInfo) {
295 atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(p)), unsafe.Pointer(v))
296 }
297 func atomicLoadMergeInfo(p **mergeInfo) *mergeInfo {
298 return (*mergeInfo)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(p))))
299 }
300 func atomicStoreMergeInfo(p **mergeInfo, v *mergeInfo) {
301 atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(p)), unsafe.Pointer(v))
302 }
303 func atomicLoadDiscardInfo(p **discardInfo) *discardInfo {
304 return (*discardInfo)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(p))))
305 }
306 func atomicStoreDiscardInfo(p **discardInfo, v *discardInfo) {
307 atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(p)), unsafe.Pointer(v))
308 }
309
View as plain text