1
// Go support for Protocol Buffers - Google's data interchange format
3
// Copyright 2012 The Go Authors. All rights reserved.
4
// https://github.com/golang/protobuf
6
// Redistribution and use in source and binary forms, with or without
7
// modification, are permitted provided that the following conditions are
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
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.
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.
32
// +build !purego,!appengine,!js
34
// This file contains the implementation of the proto field accesses using package unsafe.
44
const unsafeAllowed = true
46
// A field identifies a field in a struct, accessible from a pointer.
47
// In this implementation, a field is identified by its byte offset from the start of the struct.
50
// toField returns a field equivalent to the given reflect field.
51
func toField(f *reflect.StructField) field {
52
return field(f.Offset)
55
// invalidField is an invalid field identifier.
56
const invalidField = ^field(0)
58
// zeroField is a noop when calling pointer.offset.
59
const zeroField = field(0)
61
// IsValid reports whether the field identifier is valid.
62
func (f field) IsValid() bool {
63
return f != invalidField
66
// The pointer type below is for the new table-driven encoder/decoder.
67
// The implementation here uses unsafe.Pointer to create a generic pointer.
68
// In pointer_reflect.go we use reflect instead of unsafe to implement
69
// the same (but slower) interface.
75
var ptrSize = unsafe.Sizeof(uintptr(0))
77
// toPointer converts an interface of pointer type to a pointer
78
// that points to the same target.
79
func toPointer(i *Message) pointer {
80
// Super-tricky - read pointer out of data word of interface value.
81
// Saves ~25ns over the equivalent:
82
// return valToPointer(reflect.ValueOf(*i))
83
return pointer{p: (*[2]unsafe.Pointer)(unsafe.Pointer(i))[1]}
86
// toAddrPointer converts an interface to a pointer that points to
88
func toAddrPointer(i *interface{}, isptr bool) pointer {
89
// Super-tricky - read or get the address of data word of interface value.
91
// The interface is of pointer type, thus it is a direct interface.
92
// The data word is the pointer data itself. We take its address.
93
return pointer{p: unsafe.Pointer(uintptr(unsafe.Pointer(i)) + ptrSize)}
95
// The interface is not of pointer type. The data word is the pointer
97
return pointer{p: (*[2]unsafe.Pointer)(unsafe.Pointer(i))[1]}
100
// valToPointer converts v to a pointer. v must be of pointer type.
101
func valToPointer(v reflect.Value) pointer {
102
return pointer{p: unsafe.Pointer(v.Pointer())}
105
// offset converts from a pointer to a structure to a pointer to
107
func (p pointer) offset(f field) pointer {
108
// For safety, we should panic if !f.IsValid, however calling panic causes
109
// this to no longer be inlineable, which is a serious performance cost.
112
panic("invalid field")
115
return pointer{p: unsafe.Pointer(uintptr(p.p) + uintptr(f))}
118
func (p pointer) isNil() bool {
122
func (p pointer) toInt64() *int64 {
125
func (p pointer) toInt64Ptr() **int64 {
126
return (**int64)(p.p)
128
func (p pointer) toInt64Slice() *[]int64 {
129
return (*[]int64)(p.p)
131
func (p pointer) toInt32() *int32 {
135
// See pointer_reflect.go for why toInt32Ptr/Slice doesn't exist.
137
func (p pointer) toInt32Ptr() **int32 {
138
return (**int32)(p.p)
140
func (p pointer) toInt32Slice() *[]int32 {
141
return (*[]int32)(p.p)
144
func (p pointer) getInt32Ptr() *int32 {
145
return *(**int32)(p.p)
147
func (p pointer) setInt32Ptr(v int32) {
151
// getInt32Slice loads a []int32 from p.
152
// The value returned is aliased with the original slice.
153
// This behavior differs from the implementation in pointer_reflect.go.
154
func (p pointer) getInt32Slice() []int32 {
155
return *(*[]int32)(p.p)
158
// setInt32Slice stores a []int32 to p.
159
// The value set is aliased with the input slice.
160
// This behavior differs from the implementation in pointer_reflect.go.
161
func (p pointer) setInt32Slice(v []int32) {
165
// TODO: Can we get rid of appendInt32Slice and use setInt32Slice instead?
166
func (p pointer) appendInt32Slice(v int32) {
171
func (p pointer) toUint64() *uint64 {
172
return (*uint64)(p.p)
174
func (p pointer) toUint64Ptr() **uint64 {
175
return (**uint64)(p.p)
177
func (p pointer) toUint64Slice() *[]uint64 {
178
return (*[]uint64)(p.p)
180
func (p pointer) toUint32() *uint32 {
181
return (*uint32)(p.p)
183
func (p pointer) toUint32Ptr() **uint32 {
184
return (**uint32)(p.p)
186
func (p pointer) toUint32Slice() *[]uint32 {
187
return (*[]uint32)(p.p)
189
func (p pointer) toBool() *bool {
192
func (p pointer) toBoolPtr() **bool {
195
func (p pointer) toBoolSlice() *[]bool {
196
return (*[]bool)(p.p)
198
func (p pointer) toFloat64() *float64 {
199
return (*float64)(p.p)
201
func (p pointer) toFloat64Ptr() **float64 {
202
return (**float64)(p.p)
204
func (p pointer) toFloat64Slice() *[]float64 {
205
return (*[]float64)(p.p)
207
func (p pointer) toFloat32() *float32 {
208
return (*float32)(p.p)
210
func (p pointer) toFloat32Ptr() **float32 {
211
return (**float32)(p.p)
213
func (p pointer) toFloat32Slice() *[]float32 {
214
return (*[]float32)(p.p)
216
func (p pointer) toString() *string {
217
return (*string)(p.p)
219
func (p pointer) toStringPtr() **string {
220
return (**string)(p.p)
222
func (p pointer) toStringSlice() *[]string {
223
return (*[]string)(p.p)
225
func (p pointer) toBytes() *[]byte {
226
return (*[]byte)(p.p)
228
func (p pointer) toBytesSlice() *[][]byte {
229
return (*[][]byte)(p.p)
231
func (p pointer) toExtensions() *XXX_InternalExtensions {
232
return (*XXX_InternalExtensions)(p.p)
234
func (p pointer) toOldExtensions() *map[int32]Extension {
235
return (*map[int32]Extension)(p.p)
238
// getPointerSlice loads []*T from p as a []pointer.
239
// The value returned is aliased with the original slice.
240
// This behavior differs from the implementation in pointer_reflect.go.
241
func (p pointer) getPointerSlice() []pointer {
242
// Super-tricky - p should point to a []*T where T is a
243
// message type. We load it as []pointer.
244
return *(*[]pointer)(p.p)
247
// setPointerSlice stores []pointer into p as a []*T.
248
// The value set is aliased with the input slice.
249
// This behavior differs from the implementation in pointer_reflect.go.
250
func (p pointer) setPointerSlice(v []pointer) {
251
// Super-tricky - p should point to a []*T where T is a
252
// message type. We store it as []pointer.
253
*(*[]pointer)(p.p) = v
256
// getPointer loads the pointer at p and returns it.
257
func (p pointer) getPointer() pointer {
258
return pointer{p: *(*unsafe.Pointer)(p.p)}
261
// setPointer stores the pointer q at p.
262
func (p pointer) setPointer(q pointer) {
263
*(*unsafe.Pointer)(p.p) = q.p
266
// append q to the slice pointed to by p.
267
func (p pointer) appendPointer(q pointer) {
268
s := (*[]unsafe.Pointer)(p.p)
272
// getInterfacePointer returns a pointer that points to the
273
// interface data of the interface pointed by p.
274
func (p pointer) getInterfacePointer() pointer {
275
// Super-tricky - read pointer out of data word of interface value.
276
return pointer{p: (*(*[2]unsafe.Pointer)(p.p))[1]}
279
// asPointerTo returns a reflect.Value that is a pointer to an
280
// object of type t stored at p.
281
func (p pointer) asPointerTo(t reflect.Type) reflect.Value {
282
return reflect.NewAt(t, p.p)
285
func atomicLoadUnmarshalInfo(p **unmarshalInfo) *unmarshalInfo {
286
return (*unmarshalInfo)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(p))))
288
func atomicStoreUnmarshalInfo(p **unmarshalInfo, v *unmarshalInfo) {
289
atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(p)), unsafe.Pointer(v))
291
func atomicLoadMarshalInfo(p **marshalInfo) *marshalInfo {
292
return (*marshalInfo)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(p))))
294
func atomicStoreMarshalInfo(p **marshalInfo, v *marshalInfo) {
295
atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(p)), unsafe.Pointer(v))
297
func atomicLoadMergeInfo(p **mergeInfo) *mergeInfo {
298
return (*mergeInfo)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(p))))
300
func atomicStoreMergeInfo(p **mergeInfo, v *mergeInfo) {
301
atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(p)), unsafe.Pointer(v))
303
func atomicLoadDiscardInfo(p **discardInfo) *discardInfo {
304
return (*discardInfo)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(p))))
306
func atomicStoreDiscardInfo(p **discardInfo, v *discardInfo) {
307
atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(p)), unsafe.Pointer(v))