helper_not_unsafe.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // +build !go1.7 safe appengine
  2. // Copyright (c) 2012-2015 Ugorji Nwoke. All rights reserved.
  3. // Use of this source code is governed by a MIT license found in the LICENSE file.
  4. package codec
  5. import (
  6. "reflect"
  7. "sync/atomic"
  8. )
  9. // stringView returns a view of the []byte as a string.
  10. // In unsafe mode, it doesn't incur allocation and copying caused by conversion.
  11. // In regular safe mode, it is an allocation and copy.
  12. //
  13. // Usage: Always maintain a reference to v while result of this call is in use,
  14. // and call keepAlive4BytesView(v) at point where done with view.
  15. func stringView(v []byte) string {
  16. return string(v)
  17. }
  18. // bytesView returns a view of the string as a []byte.
  19. // In unsafe mode, it doesn't incur allocation and copying caused by conversion.
  20. // In regular safe mode, it is an allocation and copy.
  21. //
  22. // Usage: Always maintain a reference to v while result of this call is in use,
  23. // and call keepAlive4BytesView(v) at point where done with view.
  24. func bytesView(v string) []byte {
  25. return []byte(v)
  26. }
  27. func definitelyNil(v interface{}) bool {
  28. return false
  29. // rv := reflect.ValueOf(v)
  30. // switch rv.Kind() {
  31. // case reflect.Invalid:
  32. // return true
  33. // case reflect.Ptr, reflect.Interface, reflect.Chan, reflect.Slice, reflect.Map, reflect.Func:
  34. // return rv.IsNil()
  35. // default:
  36. // return false
  37. // }
  38. }
  39. // // keepAlive4BytesView maintains a reference to the input parameter for bytesView.
  40. // //
  41. // // Usage: call this at point where done with the bytes view.
  42. // func keepAlive4BytesView(v string) {}
  43. // // keepAlive4BytesView maintains a reference to the input parameter for stringView.
  44. // //
  45. // // Usage: call this at point where done with the string view.
  46. // func keepAlive4StringView(v []byte) {}
  47. func rv2i(rv reflect.Value) interface{} {
  48. return rv.Interface()
  49. }
  50. func rt2id(rt reflect.Type) uintptr {
  51. return reflect.ValueOf(rt).Pointer()
  52. }
  53. func rv2rtid(rv reflect.Value) uintptr {
  54. return reflect.ValueOf(rv.Type()).Pointer()
  55. }
  56. // --------------------------
  57. // type ptrToRvMap struct{}
  58. // func (_ *ptrToRvMap) init() {}
  59. // func (_ *ptrToRvMap) get(i interface{}) reflect.Value {
  60. // return reflect.ValueOf(i).Elem()
  61. // }
  62. // --------------------------
  63. type atomicTypeInfoSlice struct {
  64. v atomic.Value
  65. }
  66. func (x *atomicTypeInfoSlice) load() *[]rtid2ti {
  67. i := x.v.Load()
  68. if i == nil {
  69. return nil
  70. }
  71. return i.(*[]rtid2ti)
  72. }
  73. func (x *atomicTypeInfoSlice) store(p *[]rtid2ti) {
  74. x.v.Store(p)
  75. }
  76. // --------------------------
  77. func (d *Decoder) raw(f *codecFnInfo, rv reflect.Value) {
  78. rv.SetBytes(d.rawBytes())
  79. }
  80. func (d *Decoder) kString(f *codecFnInfo, rv reflect.Value) {
  81. rv.SetString(d.d.DecodeString())
  82. }
  83. func (d *Decoder) kBool(f *codecFnInfo, rv reflect.Value) {
  84. rv.SetBool(d.d.DecodeBool())
  85. }
  86. func (d *Decoder) kFloat32(f *codecFnInfo, rv reflect.Value) {
  87. rv.SetFloat(d.d.DecodeFloat(true))
  88. }
  89. func (d *Decoder) kFloat64(f *codecFnInfo, rv reflect.Value) {
  90. rv.SetFloat(d.d.DecodeFloat(false))
  91. }
  92. func (d *Decoder) kInt(f *codecFnInfo, rv reflect.Value) {
  93. rv.SetInt(d.d.DecodeInt(intBitsize))
  94. }
  95. func (d *Decoder) kInt8(f *codecFnInfo, rv reflect.Value) {
  96. rv.SetInt(d.d.DecodeInt(8))
  97. }
  98. func (d *Decoder) kInt16(f *codecFnInfo, rv reflect.Value) {
  99. rv.SetInt(d.d.DecodeInt(16))
  100. }
  101. func (d *Decoder) kInt32(f *codecFnInfo, rv reflect.Value) {
  102. rv.SetInt(d.d.DecodeInt(32))
  103. }
  104. func (d *Decoder) kInt64(f *codecFnInfo, rv reflect.Value) {
  105. rv.SetInt(d.d.DecodeInt(64))
  106. }
  107. func (d *Decoder) kUint(f *codecFnInfo, rv reflect.Value) {
  108. rv.SetUint(d.d.DecodeUint(uintBitsize))
  109. }
  110. func (d *Decoder) kUintptr(f *codecFnInfo, rv reflect.Value) {
  111. rv.SetUint(d.d.DecodeUint(uintBitsize))
  112. }
  113. func (d *Decoder) kUint8(f *codecFnInfo, rv reflect.Value) {
  114. rv.SetUint(d.d.DecodeUint(8))
  115. }
  116. func (d *Decoder) kUint16(f *codecFnInfo, rv reflect.Value) {
  117. rv.SetUint(d.d.DecodeUint(16))
  118. }
  119. func (d *Decoder) kUint32(f *codecFnInfo, rv reflect.Value) {
  120. rv.SetUint(d.d.DecodeUint(32))
  121. }
  122. func (d *Decoder) kUint64(f *codecFnInfo, rv reflect.Value) {
  123. rv.SetUint(d.d.DecodeUint(64))
  124. }