helper_not_unsafe.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. fv := d.d.DecodeFloat64()
  156. if chkOvf.Float32(fv) {
  157. d.errorf("float32 overflow: %v", fv)
  158. }
  159. rv.SetFloat(fv)
  160. }
  161. func (d *Decoder) kFloat64(f *codecFnInfo, rv reflect.Value) {
  162. rv.SetFloat(d.d.DecodeFloat64())
  163. }
  164. func (d *Decoder) kInt(f *codecFnInfo, rv reflect.Value) {
  165. rv.SetInt(chkOvf.IntV(d.d.DecodeInt64(), intBitsize))
  166. }
  167. func (d *Decoder) kInt8(f *codecFnInfo, rv reflect.Value) {
  168. rv.SetInt(chkOvf.IntV(d.d.DecodeInt64(), 8))
  169. }
  170. func (d *Decoder) kInt16(f *codecFnInfo, rv reflect.Value) {
  171. rv.SetInt(chkOvf.IntV(d.d.DecodeInt64(), 16))
  172. }
  173. func (d *Decoder) kInt32(f *codecFnInfo, rv reflect.Value) {
  174. rv.SetInt(chkOvf.IntV(d.d.DecodeInt64(), 32))
  175. }
  176. func (d *Decoder) kInt64(f *codecFnInfo, rv reflect.Value) {
  177. rv.SetInt(d.d.DecodeInt64())
  178. }
  179. func (d *Decoder) kUint(f *codecFnInfo, rv reflect.Value) {
  180. rv.SetUint(chkOvf.UintV(d.d.DecodeUint64(), uintBitsize))
  181. }
  182. func (d *Decoder) kUintptr(f *codecFnInfo, rv reflect.Value) {
  183. rv.SetUint(chkOvf.UintV(d.d.DecodeUint64(), uintBitsize))
  184. }
  185. func (d *Decoder) kUint8(f *codecFnInfo, rv reflect.Value) {
  186. rv.SetUint(chkOvf.UintV(d.d.DecodeUint64(), 8))
  187. }
  188. func (d *Decoder) kUint16(f *codecFnInfo, rv reflect.Value) {
  189. rv.SetUint(chkOvf.UintV(d.d.DecodeUint64(), 16))
  190. }
  191. func (d *Decoder) kUint32(f *codecFnInfo, rv reflect.Value) {
  192. rv.SetUint(chkOvf.UintV(d.d.DecodeUint64(), 32))
  193. }
  194. func (d *Decoder) kUint64(f *codecFnInfo, rv reflect.Value) {
  195. rv.SetUint(d.d.DecodeUint64())
  196. }
  197. // ----------------
  198. func (e *Encoder) kBool(f *codecFnInfo, rv reflect.Value) {
  199. e.e.EncodeBool(rv.Bool())
  200. }
  201. func (e *Encoder) kTime(f *codecFnInfo, rv reflect.Value) {
  202. e.e.EncodeTime(rv2i(rv).(time.Time))
  203. }
  204. func (e *Encoder) kString(f *codecFnInfo, rv reflect.Value) {
  205. s := rv.String()
  206. if e.h.StringToRaw {
  207. e.e.EncodeStringBytesRaw(bytesView(s))
  208. } else {
  209. e.e.EncodeStringEnc(cUTF8, s)
  210. }
  211. }
  212. func (e *Encoder) kFloat64(f *codecFnInfo, rv reflect.Value) {
  213. e.e.EncodeFloat64(rv.Float())
  214. }
  215. func (e *Encoder) kFloat32(f *codecFnInfo, rv reflect.Value) {
  216. e.e.EncodeFloat32(float32(rv.Float()))
  217. }
  218. func (e *Encoder) kInt(f *codecFnInfo, rv reflect.Value) {
  219. e.e.EncodeInt(rv.Int())
  220. }
  221. func (e *Encoder) kInt8(f *codecFnInfo, rv reflect.Value) {
  222. e.e.EncodeInt(rv.Int())
  223. }
  224. func (e *Encoder) kInt16(f *codecFnInfo, rv reflect.Value) {
  225. e.e.EncodeInt(rv.Int())
  226. }
  227. func (e *Encoder) kInt32(f *codecFnInfo, rv reflect.Value) {
  228. e.e.EncodeInt(rv.Int())
  229. }
  230. func (e *Encoder) kInt64(f *codecFnInfo, rv reflect.Value) {
  231. e.e.EncodeInt(rv.Int())
  232. }
  233. func (e *Encoder) kUint(f *codecFnInfo, rv reflect.Value) {
  234. e.e.EncodeUint(rv.Uint())
  235. }
  236. func (e *Encoder) kUint8(f *codecFnInfo, rv reflect.Value) {
  237. e.e.EncodeUint(rv.Uint())
  238. }
  239. func (e *Encoder) kUint16(f *codecFnInfo, rv reflect.Value) {
  240. e.e.EncodeUint(rv.Uint())
  241. }
  242. func (e *Encoder) kUint32(f *codecFnInfo, rv reflect.Value) {
  243. e.e.EncodeUint(rv.Uint())
  244. }
  245. func (e *Encoder) kUint64(f *codecFnInfo, rv reflect.Value) {
  246. e.e.EncodeUint(rv.Uint())
  247. }
  248. func (e *Encoder) kUintptr(f *codecFnInfo, rv reflect.Value) {
  249. e.e.EncodeUint(rv.Uint())
  250. }
  251. // // keepAlive4BytesView maintains a reference to the input parameter for bytesView.
  252. // //
  253. // // Usage: call this at point where done with the bytes view.
  254. // func keepAlive4BytesView(v string) {}
  255. // // keepAlive4BytesView maintains a reference to the input parameter for stringView.
  256. // //
  257. // // Usage: call this at point where done with the string view.
  258. // func keepAlive4StringView(v []byte) {}
  259. // func definitelyNil(v interface{}) bool {
  260. // rv := reflect.ValueOf(v)
  261. // switch rv.Kind() {
  262. // case reflect.Invalid:
  263. // return true
  264. // case reflect.Ptr, reflect.Interface, reflect.Chan, reflect.Slice, reflect.Map, reflect.Func:
  265. // return rv.IsNil()
  266. // default:
  267. // return false
  268. // }
  269. // }