helper_not_unsafe.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. // +build !go1.7 safe appengine
  2. // Copyright (c) 2012-2018 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. "time"
  9. )
  10. const safeMode = true
  11. // stringView returns a view of the []byte as a string.
  12. // In unsafe mode, it doesn't incur allocation and copying caused by conversion.
  13. // In regular safe mode, it is an allocation and copy.
  14. //
  15. // Usage: Always maintain a reference to v while result of this call is in use,
  16. // and call keepAlive4BytesView(v) at point where done with view.
  17. func stringView(v []byte) string {
  18. return string(v)
  19. }
  20. // bytesView returns a view of the string as a []byte.
  21. // In unsafe mode, it doesn't incur allocation and copying caused by conversion.
  22. // In regular safe mode, it is an allocation and copy.
  23. //
  24. // Usage: Always maintain a reference to v while result of this call is in use,
  25. // and call keepAlive4BytesView(v) at point where done with view.
  26. func bytesView(v string) []byte {
  27. return []byte(v)
  28. }
  29. func definitelyNil(v interface{}) bool {
  30. // this is a best-effort option.
  31. // We just return false, so we don't unnecessarily incur the cost of reflection this early.
  32. return false
  33. }
  34. func rv2i(rv reflect.Value) interface{} {
  35. return rv.Interface()
  36. }
  37. func rt2id(rt reflect.Type) uintptr {
  38. return reflect.ValueOf(rt).Pointer()
  39. }
  40. // func rv2rtid(rv reflect.Value) uintptr {
  41. // return reflect.ValueOf(rv.Type()).Pointer()
  42. // }
  43. func i2rtid(i interface{}) uintptr {
  44. return reflect.ValueOf(reflect.TypeOf(i)).Pointer()
  45. }
  46. // --------------------------
  47. func isEmptyValue(v reflect.Value, tinfos *TypeInfos, deref, checkStruct bool) bool {
  48. switch v.Kind() {
  49. case reflect.Invalid:
  50. return true
  51. case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
  52. return v.Len() == 0
  53. case reflect.Bool:
  54. return !v.Bool()
  55. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  56. return v.Int() == 0
  57. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  58. return v.Uint() == 0
  59. case reflect.Float32, reflect.Float64:
  60. return v.Float() == 0
  61. case reflect.Interface, reflect.Ptr:
  62. if deref {
  63. if v.IsNil() {
  64. return true
  65. }
  66. return isEmptyValue(v.Elem(), tinfos, deref, checkStruct)
  67. }
  68. return v.IsNil()
  69. case reflect.Struct:
  70. return isEmptyStruct(v, tinfos, deref, checkStruct)
  71. }
  72. return false
  73. }
  74. // --------------------------
  75. // type ptrToRvMap struct{}
  76. // func (*ptrToRvMap) init() {}
  77. // func (*ptrToRvMap) get(i interface{}) reflect.Value {
  78. // return reflect.ValueOf(i).Elem()
  79. // }
  80. // --------------------------
  81. type atomicClsErr struct {
  82. v atomic.Value
  83. }
  84. func (x *atomicClsErr) load() (e clsErr) {
  85. if i := x.v.Load(); i != nil {
  86. e = i.(clsErr)
  87. }
  88. return
  89. }
  90. func (x *atomicClsErr) store(p clsErr) {
  91. x.v.Store(p)
  92. }
  93. // --------------------------
  94. type atomicTypeInfoSlice struct { // expected to be 2 words
  95. v atomic.Value
  96. }
  97. func (x *atomicTypeInfoSlice) load() (e []rtid2ti) {
  98. if i := x.v.Load(); i != nil {
  99. e = i.([]rtid2ti)
  100. }
  101. return
  102. }
  103. func (x *atomicTypeInfoSlice) store(p []rtid2ti) {
  104. x.v.Store(p)
  105. }
  106. // --------------------------
  107. type atomicRtidFnSlice struct { // expected to be 2 words
  108. v atomic.Value
  109. }
  110. func (x *atomicRtidFnSlice) load() (e []codecRtidFn) {
  111. if i := x.v.Load(); i != nil {
  112. e = i.([]codecRtidFn)
  113. }
  114. return
  115. }
  116. func (x *atomicRtidFnSlice) store(p []codecRtidFn) {
  117. x.v.Store(p)
  118. }
  119. // --------------------------
  120. func (n *decNaked) ru() reflect.Value {
  121. return reflect.ValueOf(&n.u).Elem()
  122. }
  123. func (n *decNaked) ri() reflect.Value {
  124. return reflect.ValueOf(&n.i).Elem()
  125. }
  126. func (n *decNaked) rf() reflect.Value {
  127. return reflect.ValueOf(&n.f).Elem()
  128. }
  129. func (n *decNaked) rl() reflect.Value {
  130. return reflect.ValueOf(&n.l).Elem()
  131. }
  132. func (n *decNaked) rs() reflect.Value {
  133. return reflect.ValueOf(&n.s).Elem()
  134. }
  135. func (n *decNaked) rt() reflect.Value {
  136. return reflect.ValueOf(&n.t).Elem()
  137. }
  138. func (n *decNaked) rb() reflect.Value {
  139. return reflect.ValueOf(&n.b).Elem()
  140. }
  141. // --------------------------
  142. func (d *Decoder) raw(f *codecFnInfo, rv reflect.Value) {
  143. rv.SetBytes(d.rawBytes())
  144. }
  145. func (d *Decoder) kString(f *codecFnInfo, rv reflect.Value) {
  146. rv.SetString(d.d.DecodeString())
  147. }
  148. func (d *Decoder) kBool(f *codecFnInfo, rv reflect.Value) {
  149. rv.SetBool(d.d.DecodeBool())
  150. }
  151. func (d *Decoder) kTime(f *codecFnInfo, rv reflect.Value) {
  152. rv.Set(reflect.ValueOf(d.d.DecodeTime()))
  153. }
  154. func (d *Decoder) kFloat32(f *codecFnInfo, rv reflect.Value) {
  155. rv.SetFloat(d.decodeFloat32())
  156. }
  157. func (d *Decoder) kFloat64(f *codecFnInfo, rv reflect.Value) {
  158. rv.SetFloat(d.d.DecodeFloat64())
  159. }
  160. func (d *Decoder) kInt(f *codecFnInfo, rv reflect.Value) {
  161. rv.SetInt(chkOvf.IntV(d.d.DecodeInt64(), intBitsize))
  162. }
  163. func (d *Decoder) kInt8(f *codecFnInfo, rv reflect.Value) {
  164. rv.SetInt(chkOvf.IntV(d.d.DecodeInt64(), 8))
  165. }
  166. func (d *Decoder) kInt16(f *codecFnInfo, rv reflect.Value) {
  167. rv.SetInt(chkOvf.IntV(d.d.DecodeInt64(), 16))
  168. }
  169. func (d *Decoder) kInt32(f *codecFnInfo, rv reflect.Value) {
  170. rv.SetInt(chkOvf.IntV(d.d.DecodeInt64(), 32))
  171. }
  172. func (d *Decoder) kInt64(f *codecFnInfo, rv reflect.Value) {
  173. rv.SetInt(d.d.DecodeInt64())
  174. }
  175. func (d *Decoder) kUint(f *codecFnInfo, rv reflect.Value) {
  176. rv.SetUint(chkOvf.UintV(d.d.DecodeUint64(), uintBitsize))
  177. }
  178. func (d *Decoder) kUintptr(f *codecFnInfo, rv reflect.Value) {
  179. rv.SetUint(chkOvf.UintV(d.d.DecodeUint64(), uintBitsize))
  180. }
  181. func (d *Decoder) kUint8(f *codecFnInfo, rv reflect.Value) {
  182. rv.SetUint(chkOvf.UintV(d.d.DecodeUint64(), 8))
  183. }
  184. func (d *Decoder) kUint16(f *codecFnInfo, rv reflect.Value) {
  185. rv.SetUint(chkOvf.UintV(d.d.DecodeUint64(), 16))
  186. }
  187. func (d *Decoder) kUint32(f *codecFnInfo, rv reflect.Value) {
  188. rv.SetUint(chkOvf.UintV(d.d.DecodeUint64(), 32))
  189. }
  190. func (d *Decoder) kUint64(f *codecFnInfo, rv reflect.Value) {
  191. rv.SetUint(d.d.DecodeUint64())
  192. }
  193. // ----------------
  194. func (e *Encoder) kBool(f *codecFnInfo, rv reflect.Value) {
  195. e.e.EncodeBool(rv.Bool())
  196. }
  197. func (e *Encoder) kTime(f *codecFnInfo, rv reflect.Value) {
  198. e.e.EncodeTime(rv2i(rv).(time.Time))
  199. }
  200. func (e *Encoder) kString(f *codecFnInfo, rv reflect.Value) {
  201. s := rv.String()
  202. if e.h.StringToRaw {
  203. e.e.EncodeStringBytesRaw(bytesView(s))
  204. } else {
  205. e.e.EncodeStringEnc(cUTF8, s)
  206. }
  207. }
  208. func (e *Encoder) kFloat64(f *codecFnInfo, rv reflect.Value) {
  209. e.e.EncodeFloat64(rv.Float())
  210. }
  211. func (e *Encoder) kFloat32(f *codecFnInfo, rv reflect.Value) {
  212. e.e.EncodeFloat32(float32(rv.Float()))
  213. }
  214. func (e *Encoder) kInt(f *codecFnInfo, rv reflect.Value) {
  215. e.e.EncodeInt(rv.Int())
  216. }
  217. func (e *Encoder) kInt8(f *codecFnInfo, rv reflect.Value) {
  218. e.e.EncodeInt(rv.Int())
  219. }
  220. func (e *Encoder) kInt16(f *codecFnInfo, rv reflect.Value) {
  221. e.e.EncodeInt(rv.Int())
  222. }
  223. func (e *Encoder) kInt32(f *codecFnInfo, rv reflect.Value) {
  224. e.e.EncodeInt(rv.Int())
  225. }
  226. func (e *Encoder) kInt64(f *codecFnInfo, rv reflect.Value) {
  227. e.e.EncodeInt(rv.Int())
  228. }
  229. func (e *Encoder) kUint(f *codecFnInfo, rv reflect.Value) {
  230. e.e.EncodeUint(rv.Uint())
  231. }
  232. func (e *Encoder) kUint8(f *codecFnInfo, rv reflect.Value) {
  233. e.e.EncodeUint(rv.Uint())
  234. }
  235. func (e *Encoder) kUint16(f *codecFnInfo, rv reflect.Value) {
  236. e.e.EncodeUint(rv.Uint())
  237. }
  238. func (e *Encoder) kUint32(f *codecFnInfo, rv reflect.Value) {
  239. e.e.EncodeUint(rv.Uint())
  240. }
  241. func (e *Encoder) kUint64(f *codecFnInfo, rv reflect.Value) {
  242. e.e.EncodeUint(rv.Uint())
  243. }
  244. func (e *Encoder) kUintptr(f *codecFnInfo, rv reflect.Value) {
  245. e.e.EncodeUint(rv.Uint())
  246. }
  247. // // keepAlive4BytesView maintains a reference to the input parameter for bytesView.
  248. // //
  249. // // Usage: call this at point where done with the bytes view.
  250. // func keepAlive4BytesView(v string) {}
  251. // // keepAlive4BytesView maintains a reference to the input parameter for stringView.
  252. // //
  253. // // Usage: call this at point where done with the string view.
  254. // func keepAlive4StringView(v []byte) {}
  255. // func definitelyNil(v interface{}) bool {
  256. // rv := reflect.ValueOf(v)
  257. // switch rv.Kind() {
  258. // case reflect.Invalid:
  259. // return true
  260. // case reflect.Ptr, reflect.Interface, reflect.Chan, reflect.Slice, reflect.Map, reflect.Func:
  261. // return rv.IsNil()
  262. // default:
  263. // return false
  264. // }
  265. // }