blob: ab0d6eed0937114ae64a346b9fdf28a459bc9d6b [file] [log] [blame]
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001// Copyright 2018 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
Joe Tsaibc534a92018-12-18 16:07:48 -08005// +build !purego,!appengine
Joe Tsaifa02f4e2018-09-12 16:20:37 -07006
7package impl
8
9import (
10 "reflect"
Joe Tsai82760ce2019-06-20 03:09:57 -070011 "sync/atomic"
Joe Tsaifa02f4e2018-09-12 16:20:37 -070012 "unsafe"
13)
14
Joe Tsaic0e4bb22019-07-06 13:05:11 -070015const UnsafeEnabled = true
16
Joe Tsai82760ce2019-06-20 03:09:57 -070017// Pointer is an opaque pointer type.
18type Pointer unsafe.Pointer
19
Joe Tsaifa02f4e2018-09-12 16:20:37 -070020// offset represents the offset to a struct field, accessible from a pointer.
21// The offset is the byte offset to the field from the start of the struct.
22type offset uintptr
23
24// offsetOf returns a field offset for the struct field.
Joe Tsaic0e4bb22019-07-06 13:05:11 -070025func offsetOf(f reflect.StructField, x exporter) offset {
Joe Tsaifa02f4e2018-09-12 16:20:37 -070026 return offset(f.Offset)
27}
28
Damien Neilc37adef2019-04-01 13:49:56 -070029// IsValid reports whether the offset is valid.
30func (f offset) IsValid() bool { return f != invalidOffset }
31
32// invalidOffset is an invalid field offset.
33var invalidOffset = ^offset(0)
34
35// zeroOffset is a noop when calling pointer.Apply.
36var zeroOffset = offset(0)
37
Joe Tsaifa02f4e2018-09-12 16:20:37 -070038// pointer is a pointer to a message struct or field.
39type pointer struct{ p unsafe.Pointer }
40
Joe Tsai82760ce2019-06-20 03:09:57 -070041// pointerOf returns p as a pointer.
42func pointerOf(p Pointer) pointer {
43 return pointer{p: unsafe.Pointer(p)}
44}
45
Joe Tsaifa02f4e2018-09-12 16:20:37 -070046// pointerOfValue returns v as a pointer.
47func pointerOfValue(v reflect.Value) pointer {
48 return pointer{p: unsafe.Pointer(v.Pointer())}
49}
50
Joe Tsaic6b75612018-09-13 14:24:37 -070051// pointerOfIface returns the pointer portion of an interface.
Joe Tsai6cf80c42018-12-01 04:57:09 -080052func pointerOfIface(v interface{}) pointer {
Joe Tsaic6b75612018-09-13 14:24:37 -070053 type ifaceHeader struct {
54 Type unsafe.Pointer
55 Data unsafe.Pointer
56 }
Joe Tsai6cf80c42018-12-01 04:57:09 -080057 return pointer{p: (*ifaceHeader)(unsafe.Pointer(&v)).Data}
Joe Tsaic6b75612018-09-13 14:24:37 -070058}
59
Joe Tsai6cf80c42018-12-01 04:57:09 -080060// IsNil reports whether the pointer is nil.
61func (p pointer) IsNil() bool {
62 return p.p == nil
63}
64
65// Apply adds an offset to the pointer to derive a new pointer
66// to a specified field. The pointer must be valid and pointing at a struct.
67func (p pointer) Apply(f offset) pointer {
68 if p.IsNil() {
69 panic("invalid nil pointer")
70 }
Joe Tsaifa02f4e2018-09-12 16:20:37 -070071 return pointer{p: unsafe.Pointer(uintptr(p.p) + uintptr(f))}
72}
73
Joe Tsai6cf80c42018-12-01 04:57:09 -080074// AsValueOf treats p as a pointer to an object of type t and returns the value.
75// It is equivalent to reflect.ValueOf(p.AsIfaceOf(t))
76func (p pointer) AsValueOf(t reflect.Type) reflect.Value {
Joe Tsaifa02f4e2018-09-12 16:20:37 -070077 return reflect.NewAt(t, p.p)
78}
Joe Tsai6cf80c42018-12-01 04:57:09 -080079
80// AsIfaceOf treats p as a pointer to an object of type t and returns the value.
81// It is equivalent to p.AsValueOf(t).Interface()
82func (p pointer) AsIfaceOf(t reflect.Type) interface{} {
83 // TODO: Use tricky unsafe magic to directly create ifaceHeader.
84 return p.AsValueOf(t).Interface()
85}
Damien Neilc37adef2019-04-01 13:49:56 -070086
Joe Tsai89d49632019-06-04 16:20:00 -070087func (p pointer) Bool() *bool { return (*bool)(p.p) }
88func (p pointer) BoolPtr() **bool { return (**bool)(p.p) }
89func (p pointer) BoolSlice() *[]bool { return (*[]bool)(p.p) }
90func (p pointer) Int32() *int32 { return (*int32)(p.p) }
91func (p pointer) Int32Ptr() **int32 { return (**int32)(p.p) }
92func (p pointer) Int32Slice() *[]int32 { return (*[]int32)(p.p) }
93func (p pointer) Int64() *int64 { return (*int64)(p.p) }
94func (p pointer) Int64Ptr() **int64 { return (**int64)(p.p) }
95func (p pointer) Int64Slice() *[]int64 { return (*[]int64)(p.p) }
96func (p pointer) Uint32() *uint32 { return (*uint32)(p.p) }
97func (p pointer) Uint32Ptr() **uint32 { return (**uint32)(p.p) }
98func (p pointer) Uint32Slice() *[]uint32 { return (*[]uint32)(p.p) }
99func (p pointer) Uint64() *uint64 { return (*uint64)(p.p) }
100func (p pointer) Uint64Ptr() **uint64 { return (**uint64)(p.p) }
101func (p pointer) Uint64Slice() *[]uint64 { return (*[]uint64)(p.p) }
102func (p pointer) Float32() *float32 { return (*float32)(p.p) }
103func (p pointer) Float32Ptr() **float32 { return (**float32)(p.p) }
104func (p pointer) Float32Slice() *[]float32 { return (*[]float32)(p.p) }
105func (p pointer) Float64() *float64 { return (*float64)(p.p) }
106func (p pointer) Float64Ptr() **float64 { return (**float64)(p.p) }
107func (p pointer) Float64Slice() *[]float64 { return (*[]float64)(p.p) }
108func (p pointer) String() *string { return (*string)(p.p) }
109func (p pointer) StringPtr() **string { return (**string)(p.p) }
110func (p pointer) StringSlice() *[]string { return (*[]string)(p.p) }
111func (p pointer) Bytes() *[]byte { return (*[]byte)(p.p) }
112func (p pointer) BytesSlice() *[][]byte { return (*[][]byte)(p.p) }
113func (p pointer) Extensions() *map[int32]ExtensionField { return (*map[int32]ExtensionField)(p.p) }
Damien Neilc37adef2019-04-01 13:49:56 -0700114
115func (p pointer) Elem() pointer {
116 return pointer{p: *(*unsafe.Pointer)(p.p)}
117}
118
119// PointerSlice loads []*T from p as a []pointer.
120// The value returned is aliased with the original slice.
121// This behavior differs from the implementation in pointer_reflect.go.
122func (p pointer) PointerSlice() []pointer {
123 // Super-tricky - p should point to a []*T where T is a
124 // message type. We load it as []pointer.
125 return *(*[]pointer)(p.p)
126}
Damien Neile91877d2019-06-27 10:54:42 -0700127
128// AppendPointerSlice appends v to p, which must be a []*T.
129func (p pointer) AppendPointerSlice(v pointer) {
130 *(*[]pointer)(p.p) = append(*(*[]pointer)(p.p), v)
131}
132
133// SetPointer sets *p to v.
134func (p pointer) SetPointer(v pointer) {
135 *(*unsafe.Pointer)(p.p) = (unsafe.Pointer)(v.p)
136}
Joe Tsai82760ce2019-06-20 03:09:57 -0700137
138// Static check that MessageState does not exceed the size of a pointer.
139const _ = uint(unsafe.Sizeof(unsafe.Pointer(nil)) - unsafe.Sizeof(MessageState{}))
140
141func (Export) MessageStateOf(p Pointer) *messageState {
142 // Super-tricky - see documentation on MessageState.
143 return (*messageState)(unsafe.Pointer(p))
144}
145func (ms *messageState) pointer() pointer {
146 // Super-tricky - see documentation on MessageState.
147 return pointer{p: unsafe.Pointer(ms)}
148}
149func (ms *messageState) LoadMessageInfo() *MessageInfo {
150 return (*MessageInfo)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&ms.mi))))
151}
152func (ms *messageState) StoreMessageInfo(mi *MessageInfo) {
153 atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&ms.mi)), unsafe.Pointer(mi))
154}
155
156type atomicNilMessage struct{ m messageReflectWrapper }
157
158func (m *atomicNilMessage) Init(mi *MessageInfo) *messageReflectWrapper {
159 if (*messageReflectWrapper)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&m.m.mi)))) == nil {
160 atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&m.m.mi)), unsafe.Pointer(mi))
161 }
162 return &m.m
163}