| 1 |
// +build !safe |
| 2 |
// +build !appengine |
| 3 |
// +build go1.7 |
| 4 |
|
| 5 |
// Copyright (c) 2012-2015 Ugorji Nwoke. All rights reserved. |
| 6 |
// Use of this source code is governed by a MIT license found in the LICENSE file. |
| 7 |
|
| 8 |
package codec |
| 9 |
|
| 10 |
import ( |
| 11 |
"reflect" |
| 12 |
"sync/atomic" |
| 13 |
"unsafe" |
| 14 |
) |
| 15 |
|
| 16 |
// This file has unsafe variants of some helper methods. |
| 17 |
// NOTE: See helper_not_unsafe.go for the usage information. |
| 18 |
|
| 19 |
// var zeroRTv [4]uintptr |
| 20 |
|
| 21 |
const unsafeFlagIndir = 1 << 7 // keep in sync with GO_ROOT/src/reflect/value.go |
| 22 |
|
| 23 |
type unsafeString struct { |
| 24 |
Data uintptr |
| 25 |
Len int |
| 26 |
} |
| 27 |
|
| 28 |
type unsafeSlice struct { |
| 29 |
Data uintptr |
| 30 |
Len int |
| 31 |
Cap int |
| 32 |
} |
| 33 |
|
| 34 |
type unsafeIntf struct { |
| 35 |
typ unsafe.Pointer |
| 36 |
word unsafe.Pointer |
| 37 |
} |
| 38 |
|
| 39 |
type unsafeReflectValue struct { |
| 40 |
typ unsafe.Pointer |
| 41 |
ptr unsafe.Pointer |
| 42 |
flag uintptr |
| 43 |
} |
| 44 |
|
| 45 |
func stringView(v []byte) string { |
| 46 |
if len(v) == 0 { |
| 47 |
return "" |
| 48 |
} |
| 49 |
|
| 50 |
bx := (*unsafeSlice)(unsafe.Pointer(&v)) |
| 51 |
sx := unsafeString{bx.Data, bx.Len} |
| 52 |
return *(*string)(unsafe.Pointer(&sx)) |
| 53 |
} |
| 54 |
|
| 55 |
func bytesView(v string) []byte { |
| 56 |
if len(v) == 0 { |
| 57 |
return zeroByteSlice |
| 58 |
} |
| 59 |
|
| 60 |
sx := (*unsafeString)(unsafe.Pointer(&v)) |
| 61 |
bx := unsafeSlice{sx.Data, sx.Len, sx.Len} |
| 62 |
return *(*[]byte)(unsafe.Pointer(&bx)) |
| 63 |
} |
| 64 |
|
| 65 |
func definitelyNil(v interface{}) bool { |
| 66 |
return (*unsafeIntf)(unsafe.Pointer(&v)).word == nil |
| 67 |
} |
| 68 |
|
| 69 |
// func keepAlive4BytesView(v string) { |
| 70 |
// runtime.KeepAlive(v) |
| 71 |
// } |
| 72 |
|
| 73 |
// func keepAlive4StringView(v []byte) { |
| 74 |
// runtime.KeepAlive(v) |
| 75 |
// } |
| 76 |
|
| 77 |
// TODO: consider a more generally-known optimization for reflect.Value ==> Interface |
| 78 |
// |
| 79 |
// Currently, we use this fragile method that taps into implememtation details from |
| 80 |
// the source go stdlib reflect/value.go, |
| 81 |
// and trims the implementation. |
| 82 |
func rv2i(rv reflect.Value) interface{} { |
| 83 |
if false { |
| 84 |
return rv.Interface() |
| 85 |
} |
| 86 |
urv := (*unsafeReflectValue)(unsafe.Pointer(&rv)) |
| 87 |
// references that are single-words (map, ptr) may be double-referenced as flagIndir |
| 88 |
kk := urv.flag & (1<<5 - 1) |
| 89 |
if (kk == uintptr(reflect.Map) || kk == uintptr(reflect.Ptr)) && urv.flag&unsafeFlagIndir != 0 { |
| 90 |
return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: *(*unsafe.Pointer)(urv.ptr), typ: urv.typ})) |
| 91 |
} |
| 92 |
return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: urv.ptr, typ: urv.typ})) |
| 93 |
} |
| 94 |
|
| 95 |
func rt2id(rt reflect.Type) uintptr { |
| 96 |
return uintptr(((*unsafeIntf)(unsafe.Pointer(&rt))).word) |
| 97 |
} |
| 98 |
|
| 99 |
func rv2rtid(rv reflect.Value) uintptr { |
| 100 |
return uintptr((*unsafeReflectValue)(unsafe.Pointer(&rv)).typ) |
| 101 |
} |
| 102 |
|
| 103 |
// func rv0t(rt reflect.Type) reflect.Value { |
| 104 |
// ut := (*unsafeIntf)(unsafe.Pointer(&rt)) |
| 105 |
// // we need to determine whether ifaceIndir, and then whether to just pass 0 as the ptr |
| 106 |
// uv := unsafeReflectValue{ut.word, &zeroRTv, flag(rt.Kind())} |
| 107 |
// return *(*reflect.Value)(unsafe.Pointer(&uv}) |
| 108 |
// } |
| 109 |
|
| 110 |
// -------------------------- |
| 111 |
type atomicTypeInfoSlice struct { |
| 112 |
v unsafe.Pointer |
| 113 |
} |
| 114 |
|
| 115 |
func (x *atomicTypeInfoSlice) load() *[]rtid2ti { |
| 116 |
return (*[]rtid2ti)(atomic.LoadPointer(&x.v)) |
| 117 |
} |
| 118 |
|
| 119 |
func (x *atomicTypeInfoSlice) store(p *[]rtid2ti) { |
| 120 |
atomic.StorePointer(&x.v, unsafe.Pointer(p)) |
| 121 |
} |
| 122 |
|
| 123 |
// -------------------------- |
| 124 |
func (d *Decoder) raw(f *codecFnInfo, rv reflect.Value) { |
| 125 |
urv := (*unsafeReflectValue)(unsafe.Pointer(&rv)) |
| 126 |
// if urv.flag&unsafeFlagIndir != 0 { |
| 127 |
// urv.ptr = *(*unsafe.Pointer)(urv.ptr) |
| 128 |
// } |
| 129 |
*(*[]byte)(urv.ptr) = d.rawBytes() |
| 130 |
} |
| 131 |
|
| 132 |
func (d *Decoder) kString(f *codecFnInfo, rv reflect.Value) { |
| 133 |
urv := (*unsafeReflectValue)(unsafe.Pointer(&rv)) |
| 134 |
*(*string)(urv.ptr) = d.d.DecodeString() |
| 135 |
} |
| 136 |
|
| 137 |
func (d *Decoder) kBool(f *codecFnInfo, rv reflect.Value) { |
| 138 |
urv := (*unsafeReflectValue)(unsafe.Pointer(&rv)) |
| 139 |
*(*bool)(urv.ptr) = d.d.DecodeBool() |
| 140 |
} |
| 141 |
|
| 142 |
func (d *Decoder) kFloat32(f *codecFnInfo, rv reflect.Value) { |
| 143 |
urv := (*unsafeReflectValue)(unsafe.Pointer(&rv)) |
| 144 |
*(*float32)(urv.ptr) = float32(d.d.DecodeFloat(true)) |
| 145 |
} |
| 146 |
|
| 147 |
func (d *Decoder) kFloat64(f *codecFnInfo, rv reflect.Value) { |
| 148 |
urv := (*unsafeReflectValue)(unsafe.Pointer(&rv)) |
| 149 |
*(*float64)(urv.ptr) = d.d.DecodeFloat(false) |
| 150 |
} |
| 151 |
|
| 152 |
func (d *Decoder) kInt(f *codecFnInfo, rv reflect.Value) { |
| 153 |
urv := (*unsafeReflectValue)(unsafe.Pointer(&rv)) |
| 154 |
*(*int)(urv.ptr) = int(d.d.DecodeInt(intBitsize)) |
| 155 |
} |
| 156 |
|
| 157 |
func (d *Decoder) kInt8(f *codecFnInfo, rv reflect.Value) { |
| 158 |
urv := (*unsafeReflectValue)(unsafe.Pointer(&rv)) |
| 159 |
*(*int8)(urv.ptr) = int8(d.d.DecodeInt(8)) |
| 160 |
} |
| 161 |
|
| 162 |
func (d *Decoder) kInt16(f *codecFnInfo, rv reflect.Value) { |
| 163 |
urv := (*unsafeReflectValue)(unsafe.Pointer(&rv)) |
| 164 |
*(*int16)(urv.ptr) = int16(d.d.DecodeInt(16)) |
| 165 |
} |
| 166 |
|
| 167 |
func (d *Decoder) kInt32(f *codecFnInfo, rv reflect.Value) { |
| 168 |
urv := (*unsafeReflectValue)(unsafe.Pointer(&rv)) |
| 169 |
*(*int32)(urv.ptr) = int32(d.d.DecodeInt(32)) |
| 170 |
} |
| 171 |
|
| 172 |
func (d *Decoder) kInt64(f *codecFnInfo, rv reflect.Value) { |
| 173 |
urv := (*unsafeReflectValue)(unsafe.Pointer(&rv)) |
| 174 |
*(*int64)(urv.ptr) = d.d.DecodeInt(64) |
| 175 |
} |
| 176 |
|
| 177 |
func (d *Decoder) kUint(f *codecFnInfo, rv reflect.Value) { |
| 178 |
urv := (*unsafeReflectValue)(unsafe.Pointer(&rv)) |
| 179 |
*(*uint)(urv.ptr) = uint(d.d.DecodeUint(uintBitsize)) |
| 180 |
} |
| 181 |
|
| 182 |
func (d *Decoder) kUintptr(f *codecFnInfo, rv reflect.Value) { |
| 183 |
urv := (*unsafeReflectValue)(unsafe.Pointer(&rv)) |
| 184 |
*(*uintptr)(urv.ptr) = uintptr(d.d.DecodeUint(uintBitsize)) |
| 185 |
} |
| 186 |
|
| 187 |
func (d *Decoder) kUint8(f *codecFnInfo, rv reflect.Value) { |
| 188 |
urv := (*unsafeReflectValue)(unsafe.Pointer(&rv)) |
| 189 |
*(*uint8)(urv.ptr) = uint8(d.d.DecodeUint(8)) |
| 190 |
} |
| 191 |
|
| 192 |
func (d *Decoder) kUint16(f *codecFnInfo, rv reflect.Value) { |
| 193 |
urv := (*unsafeReflectValue)(unsafe.Pointer(&rv)) |
| 194 |
*(*uint16)(urv.ptr) = uint16(d.d.DecodeUint(16)) |
| 195 |
} |
| 196 |
|
| 197 |
func (d *Decoder) kUint32(f *codecFnInfo, rv reflect.Value) { |
| 198 |
urv := (*unsafeReflectValue)(unsafe.Pointer(&rv)) |
| 199 |
*(*uint32)(urv.ptr) = uint32(d.d.DecodeUint(32)) |
| 200 |
} |
| 201 |
|
| 202 |
func (d *Decoder) kUint64(f *codecFnInfo, rv reflect.Value) { |
| 203 |
urv := (*unsafeReflectValue)(unsafe.Pointer(&rv)) |
| 204 |
*(*uint64)(urv.ptr) = d.d.DecodeUint(64) |
| 205 |
} |
| 206 |
|
| 207 |
// ------------ |
| 208 |
|
| 209 |
// func rt2id(rt reflect.Type) uintptr { |
| 210 |
// return uintptr(((*unsafeIntf)(unsafe.Pointer(&rt))).word) |
| 211 |
// // var i interface{} = rt |
| 212 |
// // // ui := (*unsafeIntf)(unsafe.Pointer(&i)) |
| 213 |
// // return ((*unsafeIntf)(unsafe.Pointer(&i))).word |
| 214 |
// } |
| 215 |
|
| 216 |
// func rv2i(rv reflect.Value) interface{} { |
| 217 |
// urv := (*unsafeReflectValue)(unsafe.Pointer(&rv)) |
| 218 |
// // non-reference type: already indir |
| 219 |
// // reference type: depend on flagIndir property ('cos maybe was double-referenced) |
| 220 |
// // const (unsafeRvFlagKindMask = 1<<5 - 1 , unsafeRvFlagIndir = 1 << 7 ) |
| 221 |
// // rvk := reflect.Kind(urv.flag & (1<<5 - 1)) |
| 222 |
// // if (rvk == reflect.Chan || |
| 223 |
// // rvk == reflect.Func || |
| 224 |
// // rvk == reflect.Interface || |
| 225 |
// // rvk == reflect.Map || |
| 226 |
// // rvk == reflect.Ptr || |
| 227 |
// // rvk == reflect.UnsafePointer) && urv.flag&(1<<8) != 0 { |
| 228 |
// // fmt.Printf(">>>>> ---- double indirect reference: %v, %v\n", rvk, rv.Type()) |
| 229 |
// // return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: *(*unsafe.Pointer)(urv.ptr), typ: urv.typ})) |
| 230 |
// // } |
| 231 |
// if urv.flag&(1<<5-1) == uintptr(reflect.Map) && urv.flag&(1<<7) != 0 { |
| 232 |
// // fmt.Printf(">>>>> ---- double indirect reference: %v, %v\n", rvk, rv.Type()) |
| 233 |
// return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: *(*unsafe.Pointer)(urv.ptr), typ: urv.typ})) |
| 234 |
// } |
| 235 |
// // fmt.Printf(">>>>> ++++ direct reference: %v, %v\n", rvk, rv.Type()) |
| 236 |
// return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: urv.ptr, typ: urv.typ})) |
| 237 |
// } |
| 238 |
|
| 239 |
// const ( |
| 240 |
// unsafeRvFlagKindMask = 1<<5 - 1 |
| 241 |
// unsafeRvKindDirectIface = 1 << 5 |
| 242 |
// unsafeRvFlagIndir = 1 << 7 |
| 243 |
// unsafeRvFlagAddr = 1 << 8 |
| 244 |
// unsafeRvFlagMethod = 1 << 9 |
| 245 |
|
| 246 |
// _USE_RV_INTERFACE bool = false |
| 247 |
// _UNSAFE_RV_DEBUG = true |
| 248 |
// ) |
| 249 |
|
| 250 |
// type unsafeRtype struct { |
| 251 |
// _ [2]uintptr |
| 252 |
// _ uint32 |
| 253 |
// _ uint8 |
| 254 |
// _ uint8 |
| 255 |
// _ uint8 |
| 256 |
// kind uint8 |
| 257 |
// _ [2]uintptr |
| 258 |
// _ int32 |
| 259 |
// } |
| 260 |
|
| 261 |
// func _rv2i(rv reflect.Value) interface{} { |
| 262 |
// // Note: From use, |
| 263 |
// // - it's never an interface |
| 264 |
// // - the only calls here are for ifaceIndir types. |
| 265 |
// // (though that conditional is wrong) |
| 266 |
// // To know for sure, we need the value of t.kind (which is not exposed). |
| 267 |
// // |
| 268 |
// // Need to validate the path: type is indirect ==> only value is indirect ==> default (value is direct) |
| 269 |
// // - Type indirect, Value indirect: ==> numbers, boolean, slice, struct, array, string |
| 270 |
// // - Type Direct, Value indirect: ==> map??? |
| 271 |
// // - Type Direct, Value direct: ==> pointers, unsafe.Pointer, func, chan, map |
| 272 |
// // |
| 273 |
// // TRANSLATES TO: |
| 274 |
// // if typeIndirect { } else if valueIndirect { } else { } |
| 275 |
// // |
| 276 |
// // Since we don't deal with funcs, then "flagNethod" is unset, and can be ignored. |
| 277 |
|
| 278 |
// if _USE_RV_INTERFACE { |
| 279 |
// return rv.Interface() |
| 280 |
// } |
| 281 |
// urv := (*unsafeReflectValue)(unsafe.Pointer(&rv)) |
| 282 |
|
| 283 |
// // if urv.flag&unsafeRvFlagMethod != 0 || urv.flag&unsafeRvFlagKindMask == uintptr(reflect.Interface) { |
| 284 |
// // println("***** IS flag method or interface: delegating to rv.Interface()") |
| 285 |
// // return rv.Interface() |
| 286 |
// // } |
| 287 |
|
| 288 |
// // if urv.flag&unsafeRvFlagKindMask == uintptr(reflect.Interface) { |
| 289 |
// // println("***** IS Interface: delegate to rv.Interface") |
| 290 |
// // return rv.Interface() |
| 291 |
// // } |
| 292 |
// // if urv.flag&unsafeRvFlagKindMask&unsafeRvKindDirectIface == 0 { |
| 293 |
// // if urv.flag&unsafeRvFlagAddr == 0 { |
| 294 |
// // println("***** IS ifaceIndir typ") |
| 295 |
// // // ui := unsafeIntf{word: urv.ptr, typ: urv.typ} |
| 296 |
// // // return *(*interface{})(unsafe.Pointer(&ui)) |
| 297 |
// // // return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: urv.ptr, typ: urv.typ})) |
| 298 |
// // } |
| 299 |
// // } else if urv.flag&unsafeRvFlagIndir != 0 { |
| 300 |
// // println("***** IS flagindir") |
| 301 |
// // // return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: *(*unsafe.Pointer)(urv.ptr), typ: urv.typ})) |
| 302 |
// // } else { |
| 303 |
// // println("***** NOT flagindir") |
| 304 |
// // return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: urv.ptr, typ: urv.typ})) |
| 305 |
// // } |
| 306 |
// // println("***** default: delegate to rv.Interface") |
| 307 |
|
| 308 |
// urt := (*unsafeRtype)(unsafe.Pointer(urv.typ)) |
| 309 |
// if _UNSAFE_RV_DEBUG { |
| 310 |
// fmt.Printf(">>>> start: %v: ", rv.Type()) |
| 311 |
// fmt.Printf("%v - %v\n", *urv, *urt) |
| 312 |
// } |
| 313 |
// if urt.kind&unsafeRvKindDirectIface == 0 { |
| 314 |
// if _UNSAFE_RV_DEBUG { |
| 315 |
// fmt.Printf("**** +ifaceIndir type: %v\n", rv.Type()) |
| 316 |
// } |
| 317 |
// // println("***** IS ifaceIndir typ") |
| 318 |
// // if true || urv.flag&unsafeRvFlagAddr == 0 { |
| 319 |
// // // println(" ***** IS NOT addr") |
| 320 |
// return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: urv.ptr, typ: urv.typ})) |
| 321 |
// // } |
| 322 |
// } else if urv.flag&unsafeRvFlagIndir != 0 { |
| 323 |
// if _UNSAFE_RV_DEBUG { |
| 324 |
// fmt.Printf("**** +flagIndir type: %v\n", rv.Type()) |
| 325 |
// } |
| 326 |
// // println("***** IS flagindir") |
| 327 |
// return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: *(*unsafe.Pointer)(urv.ptr), typ: urv.typ})) |
| 328 |
// } else { |
| 329 |
// if _UNSAFE_RV_DEBUG { |
| 330 |
// fmt.Printf("**** -flagIndir type: %v\n", rv.Type()) |
| 331 |
// } |
| 332 |
// // println("***** NOT flagindir") |
| 333 |
// return *(*interface{})(unsafe.Pointer(&unsafeIntf{word: urv.ptr, typ: urv.typ})) |
| 334 |
// } |
| 335 |
// // println("***** default: delegating to rv.Interface()") |
| 336 |
// // return rv.Interface() |
| 337 |
// } |
| 338 |
|
| 339 |
// var staticM0 = make(map[string]uint64) |
| 340 |
// var staticI0 = (int32)(-5) |
| 341 |
|
| 342 |
// func staticRv2iTest() { |
| 343 |
// i0 := (int32)(-5) |
| 344 |
// m0 := make(map[string]uint16) |
| 345 |
// m0["1"] = 1 |
| 346 |
// for _, i := range []interface{}{ |
| 347 |
// (int)(7), |
| 348 |
// (uint)(8), |
| 349 |
// (int16)(-9), |
| 350 |
// (uint16)(19), |
| 351 |
// (uintptr)(77), |
| 352 |
// (bool)(true), |
| 353 |
// float32(-32.7), |
| 354 |
// float64(64.9), |
| 355 |
// complex(float32(19), 5), |
| 356 |
// complex(float64(-32), 7), |
| 357 |
// [4]uint64{1, 2, 3, 4}, |
| 358 |
// (chan<- int)(nil), // chan, |
| 359 |
// rv2i, // func |
| 360 |
// io.Writer(ioutil.Discard), |
| 361 |
// make(map[string]uint), |
| 362 |
// (map[string]uint)(nil), |
| 363 |
// staticM0, |
| 364 |
// m0, |
| 365 |
// &m0, |
| 366 |
// i0, |
| 367 |
// &i0, |
| 368 |
// &staticI0, |
| 369 |
// &staticM0, |
| 370 |
// []uint32{6, 7, 8}, |
| 371 |
// "abc", |
| 372 |
// Raw{}, |
| 373 |
// RawExt{}, |
| 374 |
// &Raw{}, |
| 375 |
// &RawExt{}, |
| 376 |
// unsafe.Pointer(&i0), |
| 377 |
// } { |
| 378 |
// i2 := rv2i(reflect.ValueOf(i)) |
| 379 |
// eq := reflect.DeepEqual(i, i2) |
| 380 |
// fmt.Printf(">>>> %v == %v? %v\n", i, i2, eq) |
| 381 |
// } |
| 382 |
// // os.Exit(0) |
| 383 |
// } |
| 384 |
|
| 385 |
// func init() { |
| 386 |
// staticRv2iTest() |
| 387 |
// } |
| 388 |
|
| 389 |
// func rv2i(rv reflect.Value) interface{} { |
| 390 |
// if _USE_RV_INTERFACE || rv.Kind() == reflect.Interface || rv.CanAddr() { |
| 391 |
// return rv.Interface() |
| 392 |
// } |
| 393 |
// // var i interface{} |
| 394 |
// // ui := (*unsafeIntf)(unsafe.Pointer(&i)) |
| 395 |
// var ui unsafeIntf |
| 396 |
// urv := (*unsafeReflectValue)(unsafe.Pointer(&rv)) |
| 397 |
// // fmt.Printf("urv: flag: %b, typ: %b, ptr: %b\n", urv.flag, uintptr(urv.typ), uintptr(urv.ptr)) |
| 398 |
// if (urv.flag&unsafeRvFlagKindMask)&unsafeRvKindDirectIface == 0 { |
| 399 |
// if urv.flag&unsafeRvFlagAddr != 0 { |
| 400 |
// println("***** indirect and addressable! Needs typed move - delegate to rv.Interface()") |
| 401 |
// return rv.Interface() |
| 402 |
// } |
| 403 |
// println("****** indirect type/kind") |
| 404 |
// ui.word = urv.ptr |
| 405 |
// } else if urv.flag&unsafeRvFlagIndir != 0 { |
| 406 |
// println("****** unsafe rv flag indir") |
| 407 |
// ui.word = *(*unsafe.Pointer)(urv.ptr) |
| 408 |
// } else { |
| 409 |
// println("****** default: assign prt to word directly") |
| 410 |
// ui.word = urv.ptr |
| 411 |
// } |
| 412 |
// // ui.word = urv.ptr |
| 413 |
// ui.typ = urv.typ |
| 414 |
// // fmt.Printf("(pointers) ui.typ: %p, word: %p\n", ui.typ, ui.word) |
| 415 |
// // fmt.Printf("(binary) ui.typ: %b, word: %b\n", uintptr(ui.typ), uintptr(ui.word)) |
| 416 |
// return *(*interface{})(unsafe.Pointer(&ui)) |
| 417 |
// // return i |
| 418 |
// } |