podman

Форк
0
215 строк · 7.6 Кб
1
// 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

5
//go:build !purego && !appengine
6
// +build !purego,!appengine
7

8
package impl
9

10
import (
11
	"reflect"
12
	"sync/atomic"
13
	"unsafe"
14
)
15

16
const UnsafeEnabled = true
17

18
// Pointer is an opaque pointer type.
19
type Pointer unsafe.Pointer
20

21
// offset represents the offset to a struct field, accessible from a pointer.
22
// The offset is the byte offset to the field from the start of the struct.
23
type offset uintptr
24

25
// offsetOf returns a field offset for the struct field.
26
func offsetOf(f reflect.StructField, x exporter) offset {
27
	return offset(f.Offset)
28
}
29

30
// IsValid reports whether the offset is valid.
31
func (f offset) IsValid() bool { return f != invalidOffset }
32

33
// invalidOffset is an invalid field offset.
34
var invalidOffset = ^offset(0)
35

36
// zeroOffset is a noop when calling pointer.Apply.
37
var zeroOffset = offset(0)
38

39
// pointer is a pointer to a message struct or field.
40
type pointer struct{ p unsafe.Pointer }
41

42
// pointerOf returns p as a pointer.
43
func pointerOf(p Pointer) pointer {
44
	return pointer{p: unsafe.Pointer(p)}
45
}
46

47
// pointerOfValue returns v as a pointer.
48
func pointerOfValue(v reflect.Value) pointer {
49
	return pointer{p: unsafe.Pointer(v.Pointer())}
50
}
51

52
// pointerOfIface returns the pointer portion of an interface.
53
func pointerOfIface(v interface{}) pointer {
54
	type ifaceHeader struct {
55
		Type unsafe.Pointer
56
		Data unsafe.Pointer
57
	}
58
	return pointer{p: (*ifaceHeader)(unsafe.Pointer(&v)).Data}
59
}
60

61
// IsNil reports whether the pointer is nil.
62
func (p pointer) IsNil() bool {
63
	return p.p == nil
64
}
65

66
// Apply adds an offset to the pointer to derive a new pointer
67
// to a specified field. The pointer must be valid and pointing at a struct.
68
func (p pointer) Apply(f offset) pointer {
69
	if p.IsNil() {
70
		panic("invalid nil pointer")
71
	}
72
	return pointer{p: unsafe.Pointer(uintptr(p.p) + uintptr(f))}
73
}
74

75
// AsValueOf treats p as a pointer to an object of type t and returns the value.
76
// It is equivalent to reflect.ValueOf(p.AsIfaceOf(t))
77
func (p pointer) AsValueOf(t reflect.Type) reflect.Value {
78
	return reflect.NewAt(t, p.p)
79
}
80

81
// AsIfaceOf treats p as a pointer to an object of type t and returns the value.
82
// It is equivalent to p.AsValueOf(t).Interface()
83
func (p pointer) AsIfaceOf(t reflect.Type) interface{} {
84
	// TODO: Use tricky unsafe magic to directly create ifaceHeader.
85
	return p.AsValueOf(t).Interface()
86
}
87

88
func (p pointer) Bool() *bool                           { return (*bool)(p.p) }
89
func (p pointer) BoolPtr() **bool                       { return (**bool)(p.p) }
90
func (p pointer) BoolSlice() *[]bool                    { return (*[]bool)(p.p) }
91
func (p pointer) Int32() *int32                         { return (*int32)(p.p) }
92
func (p pointer) Int32Ptr() **int32                     { return (**int32)(p.p) }
93
func (p pointer) Int32Slice() *[]int32                  { return (*[]int32)(p.p) }
94
func (p pointer) Int64() *int64                         { return (*int64)(p.p) }
95
func (p pointer) Int64Ptr() **int64                     { return (**int64)(p.p) }
96
func (p pointer) Int64Slice() *[]int64                  { return (*[]int64)(p.p) }
97
func (p pointer) Uint32() *uint32                       { return (*uint32)(p.p) }
98
func (p pointer) Uint32Ptr() **uint32                   { return (**uint32)(p.p) }
99
func (p pointer) Uint32Slice() *[]uint32                { return (*[]uint32)(p.p) }
100
func (p pointer) Uint64() *uint64                       { return (*uint64)(p.p) }
101
func (p pointer) Uint64Ptr() **uint64                   { return (**uint64)(p.p) }
102
func (p pointer) Uint64Slice() *[]uint64                { return (*[]uint64)(p.p) }
103
func (p pointer) Float32() *float32                     { return (*float32)(p.p) }
104
func (p pointer) Float32Ptr() **float32                 { return (**float32)(p.p) }
105
func (p pointer) Float32Slice() *[]float32              { return (*[]float32)(p.p) }
106
func (p pointer) Float64() *float64                     { return (*float64)(p.p) }
107
func (p pointer) Float64Ptr() **float64                 { return (**float64)(p.p) }
108
func (p pointer) Float64Slice() *[]float64              { return (*[]float64)(p.p) }
109
func (p pointer) String() *string                       { return (*string)(p.p) }
110
func (p pointer) StringPtr() **string                   { return (**string)(p.p) }
111
func (p pointer) StringSlice() *[]string                { return (*[]string)(p.p) }
112
func (p pointer) Bytes() *[]byte                        { return (*[]byte)(p.p) }
113
func (p pointer) BytesPtr() **[]byte                    { return (**[]byte)(p.p) }
114
func (p pointer) BytesSlice() *[][]byte                 { return (*[][]byte)(p.p) }
115
func (p pointer) WeakFields() *weakFields               { return (*weakFields)(p.p) }
116
func (p pointer) Extensions() *map[int32]ExtensionField { return (*map[int32]ExtensionField)(p.p) }
117

118
func (p pointer) Elem() pointer {
119
	return pointer{p: *(*unsafe.Pointer)(p.p)}
120
}
121

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

131
// AppendPointerSlice appends v to p, which must be a []*T.
132
func (p pointer) AppendPointerSlice(v pointer) {
133
	*(*[]pointer)(p.p) = append(*(*[]pointer)(p.p), v)
134
}
135

136
// SetPointer sets *p to v.
137
func (p pointer) SetPointer(v pointer) {
138
	*(*unsafe.Pointer)(p.p) = (unsafe.Pointer)(v.p)
139
}
140

141
func (p pointer) growBoolSlice(addCap int) {
142
	sp := p.BoolSlice()
143
	s := make([]bool, 0, addCap+len(*sp))
144
	s = s[:len(*sp)]
145
	copy(s, *sp)
146
	*sp = s
147
}
148

149
func (p pointer) growInt32Slice(addCap int) {
150
	sp := p.Int32Slice()
151
	s := make([]int32, 0, addCap+len(*sp))
152
	s = s[:len(*sp)]
153
	copy(s, *sp)
154
	*sp = s
155
}
156

157
func (p pointer) growUint32Slice(addCap int) {
158
	p.growInt32Slice(addCap)
159
}
160

161
func (p pointer) growFloat32Slice(addCap int) {
162
	p.growInt32Slice(addCap)
163
}
164

165
func (p pointer) growInt64Slice(addCap int) {
166
	sp := p.Int64Slice()
167
	s := make([]int64, 0, addCap+len(*sp))
168
	s = s[:len(*sp)]
169
	copy(s, *sp)
170
	*sp = s
171
}
172

173
func (p pointer) growUint64Slice(addCap int) {
174
	p.growInt64Slice(addCap)
175
}
176

177
func (p pointer) growFloat64Slice(addCap int) {
178
	p.growInt64Slice(addCap)
179
}
180

181
// Static check that MessageState does not exceed the size of a pointer.
182
const _ = uint(unsafe.Sizeof(unsafe.Pointer(nil)) - unsafe.Sizeof(MessageState{}))
183

184
func (Export) MessageStateOf(p Pointer) *messageState {
185
	// Super-tricky - see documentation on MessageState.
186
	return (*messageState)(unsafe.Pointer(p))
187
}
188
func (ms *messageState) pointer() pointer {
189
	// Super-tricky - see documentation on MessageState.
190
	return pointer{p: unsafe.Pointer(ms)}
191
}
192
func (ms *messageState) messageInfo() *MessageInfo {
193
	mi := ms.LoadMessageInfo()
194
	if mi == nil {
195
		panic("invalid nil message info; this suggests memory corruption due to a race or shallow copy on the message struct")
196
	}
197
	return mi
198
}
199
func (ms *messageState) LoadMessageInfo() *MessageInfo {
200
	return (*MessageInfo)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&ms.atomicMessageInfo))))
201
}
202
func (ms *messageState) StoreMessageInfo(mi *MessageInfo) {
203
	atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&ms.atomicMessageInfo)), unsafe.Pointer(mi))
204
}
205

206
type atomicNilMessage struct{ p unsafe.Pointer } // p is a *messageReflectWrapper
207

208
func (m *atomicNilMessage) Init(mi *MessageInfo) *messageReflectWrapper {
209
	if p := atomic.LoadPointer(&m.p); p != nil {
210
		return (*messageReflectWrapper)(p)
211
	}
212
	w := &messageReflectWrapper{mi: mi}
213
	atomic.CompareAndSwapPointer(&m.p, nil, (unsafe.Pointer)(w))
214
	return (*messageReflectWrapper)(atomic.LoadPointer(&m.p))
215
}
216

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.