aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/golang/protobuf/proto/pointer_unsafe.go
blob: d55a335d9453204ae01c7c21d474efdbefa7d120 (plain)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Go support for Protocol Buffers - Google's data interchange format
//
// Copyright 2012 The Go Authors.  All rights reserved.
// https://github.com/golang/protobuf
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// +build !purego,!appengine,!js

// This file contains the implementation of the proto field accesses using package unsafe.

package proto

import (
	"reflect"
	"sync/atomic"
	"unsafe"
)

const unsafeAllowed = true

// A field identifies a field in a struct, accessible from a pointer.
// In this implementation, a field is identified by its byte offset from the start of the struct.
type field uintptr

// toField returns a field equivalent to the given reflect field.
func toField(f *reflect.StructField) field {
	return field(f.Offset)
}

// invalidField is an invalid field identifier.
const invalidField = ^field(0)

// zeroField is a noop when calling pointer.offset.
const zeroField = field(0)

// IsValid reports whether the field identifier is valid.
func (f field) IsValid() bool {
	return f != invalidField
}

// The pointer type below is for the new table-driven encoder/decoder.
// The implementation here uses unsafe.Pointer to create a generic pointer.
// In pointer_reflect.go we use reflect instead of unsafe to implement
// the same (but slower) interface.
type pointer struct {
	p unsafe.Pointer
}

// size of pointer
var ptrSize = unsafe.Sizeof(uintptr(0))

// toPointer converts an interface of pointer type to a pointer
// that points to the same target.
func toPointer(i *Message) pointer {
	// Super-tricky - read pointer out of data word of interface value.
	// Saves ~25ns over the equivalent:
	// return valToPointer(reflect.ValueOf(*i))
	return pointer{p: (*[2]unsafe.Pointer)(unsafe.Pointer(i))[1]}
}

// toAddrPointer converts an interface to a pointer that points to
// the interface data.
func toAddrPointer(i *interface{}, isptr bool) pointer {
	// Super-tricky - read or get the address of data word of interface value.
	if isptr {
		// The interface is of pointer type, thus it is a direct interface.
		// The data word is the pointer data itself. We take its address.
		return pointer{p: unsafe.Pointer(uintptr(unsafe.Pointer(i)) + ptrSize)}
	}
	// The interface is not of pointer type. The data word is the pointer
	// to the data.
	return pointer{p: (*[2]unsafe.Pointer)(unsafe.Pointer(i))[1]}
}

// valToPointer converts v to a pointer. v must be of pointer type.
func valToPointer(v reflect.Value) pointer {
	return pointer{p: unsafe.Pointer(v.Pointer())}
}

// offset converts from a pointer to a structure to a pointer to
// one of its fields.
func (p pointer) offset(f field) pointer {
	// For safety, we should panic if !f.IsValid, however calling panic causes
	// this to no longer be inlineable, which is a serious performance cost.
	/*
		if !f.IsValid() {
			panic("invalid field")
		}
	*/
	return pointer{p: unsafe.Pointer(uintptr(p.p) + uintptr(f))}
}

func (p pointer) isNil() bool {
	return p.p == nil
}

func (p pointer) toInt64() *int64 {
	return (*int64)(p.p)
}
func (p pointer) toInt64Ptr() **int64 {
	return (**int64)(p.p)
}
func (p pointer) toInt64Slice() *[]int64 {
	return (*[]int64)(p.p)
}
func (p pointer) toInt32() *int32 {
	return (*int32)(p.p)
}

// See pointer_reflect.go for why toInt32Ptr/Slice doesn't exist.
/*
	func (p pointer) toInt32Ptr() **int32 {
		return (**int32)(p.p)
	}
	func (p pointer) toInt32Slice() *[]int32 {
		return (*[]int32)(p.p)
	}
*/
func (p pointer) getInt32Ptr() *int32 {
	return *(**int32)(p.p)
}
func (p pointer) setInt32Ptr(v int32) {
	*(**int32)(p.p) = &v
}

// getInt32Slice loads a []int32 from p.
// The value returned is aliased with the original slice.
// This behavior differs from the implementation in pointer_reflect.go.
func (p pointer) getInt32Slice() []int32 {
	return *(*[]int32)(p.p)
}

// setInt32Slice stores a []int32 to p.
// The value set is aliased with the input slice.
// This behavior differs from the implementation in pointer_reflect.go.
func (p pointer) setInt32Slice(v []int32) {
	*(*[]int32)(p.p) = v
}

// TODO: Can we get rid of appendInt32Slice and use setInt32Slice instead?
func (p pointer) appendInt32Slice(v int32) {
	s := (*[]int32)(p.p)
	*s = append(*s, v)
}

func (p pointer) toUint64() *uint64 {
	return (*uint64)(p.p)
}
func (p pointer) toUint64Ptr() **uint64 {
	return (**uint64)(p.p)
}
func (p pointer) toUint64Slice() *[]uint64 {
	return (*[]uint64)(p.p)
}
func (p pointer) toUint32() *uint32 {
	return (*uint32)(p.p)
}
func (p pointer) toUint32Ptr() **uint32 {
	return (**uint32)(p.p)
}
func (p pointer) toUint32Slice() *[]uint32 {
	return (*[]uint32)(p.p)
}
func (p pointer) toBool() *bool {
	return (*bool)(p.p)
}
func (p pointer) toBoolPtr() **bool {
	return (**bool)(p.p)
}
func (p pointer) toBoolSlice() *[]bool {
	return (*[]bool)(p.p)
}
func (p pointer) toFloat64() *float64 {
	return (*float64)(p.p)
}
func (p pointer) toFloat64Ptr() **float64 {
	return (**float64)(p.p)
}
func (p pointer) toFloat64Slice() *[]float64 {
	return (*[]float64)(p.p)
}
func (p pointer) toFloat32() *float32 {
	return (*float32)(p.p)
}
func (p pointer) toFloat32Ptr() **float32 {
	return (**float32)(p.p)
}
func (p pointer) toFloat32Slice() *[]float32 {
	return (*[]float32)(p.p)
}
func (p pointer) toString() *string {
	return (*string)(p.p)
}
func (p pointer) toStringPtr() **string {
	return (**string)(p.p)
}
func (p pointer) toStringSlice() *[]string {
	return (*[]string)(p.p)
}
func (p pointer) toBytes() *[]byte {
	return (*[]byte)(p.p)
}
func (p pointer) toBytesSlice() *[][]byte {
	return (*[][]byte)(p.p)
}
func (p pointer) toExtensions() *XXX_InternalExtensions {
	return (*XXX_InternalExtensions)(p.p)
}
func (p pointer) toOldExtensions() *map[int32]Extension {
	return (*map[int32]Extension)(p.p)
}

// getPointerSlice loads []*T from p as a []pointer.
// The value returned is aliased with the original slice.
// This behavior differs from the implementation in pointer_reflect.go.
func (p pointer) getPointerSlice() []pointer {
	// Super-tricky - p should point to a []*T where T is a
	// message type. We load it as []pointer.
	return *(*[]pointer)(p.p)
}

// setPointerSlice stores []pointer into p as a []*T.
// The value set is aliased with the input slice.
// This behavior differs from the implementation in pointer_reflect.go.
func (p pointer) setPointerSlice(v []pointer) {
	// Super-tricky - p should point to a []*T where T is a
	// message type. We store it as []pointer.
	*(*[]pointer)(p.p) = v
}

// getPointer loads the pointer at p and returns it.
func (p pointer) getPointer() pointer {
	return pointer{p: *(*unsafe.Pointer)(p.p)}
}

// setPointer stores the pointer q at p.
func (p pointer) setPointer(q pointer) {
	*(*unsafe.Pointer)(p.p) = q.p
}

// append q to the slice pointed to by p.
func (p pointer) appendPointer(q pointer) {
	s := (*[]unsafe.Pointer)(p.p)
	*s = append(*s, q.p)
}

// getInterfacePointer returns a pointer that points to the
// interface data of the interface pointed by p.
func (p pointer) getInterfacePointer() pointer {
	// Super-tricky - read pointer out of data word of interface value.
	return pointer{p: (*(*[2]unsafe.Pointer)(p.p))[1]}
}

// asPointerTo returns a reflect.Value that is a pointer to an
// object of type t stored at p.
func (p pointer) asPointerTo(t reflect.Type) reflect.Value {
	return reflect.NewAt(t, p.p)
}

func atomicLoadUnmarshalInfo(p **unmarshalInfo) *unmarshalInfo {
	return (*unmarshalInfo)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(p))))
}
func atomicStoreUnmarshalInfo(p **unmarshalInfo, v *unmarshalInfo) {
	atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(p)), unsafe.Pointer(v))
}
func atomicLoadMarshalInfo(p **marshalInfo) *marshalInfo {
	return (*marshalInfo)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(p))))
}
func atomicStoreMarshalInfo(p **marshalInfo, v *marshalInfo) {
	atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(p)), unsafe.Pointer(v))
}
func atomicLoadMergeInfo(p **mergeInfo) *mergeInfo {
	return (*mergeInfo)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(p))))
}
func atomicStoreMergeInfo(p **mergeInfo, v *mergeInfo) {
	atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(p)), unsafe.Pointer(v))
}
func atomicLoadDiscardInfo(p **discardInfo) *discardInfo {
	return (*discardInfo)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(p))))
}
func atomicStoreDiscardInfo(p **discardInfo, v *discardInfo) {
	atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(p)), unsafe.Pointer(v))
}