http urls monitor.

helper_not_unsafe.go 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 atomicTypeInfoSlice struct { // expected to be 2 words
  82. v atomic.Value
  83. }
  84. func (x *atomicTypeInfoSlice) load() []rtid2ti {
  85. i := x.v.Load()
  86. if i == nil {
  87. return nil
  88. }
  89. return i.([]rtid2ti)
  90. }
  91. func (x *atomicTypeInfoSlice) store(p []rtid2ti) {
  92. x.v.Store(p)
  93. }
  94. // --------------------------
  95. func (d *Decoder) raw(f *codecFnInfo, rv reflect.Value) {
  96. rv.SetBytes(d.rawBytes())
  97. }
  98. func (d *Decoder) kString(f *codecFnInfo, rv reflect.Value) {
  99. rv.SetString(d.d.DecodeString())
  100. }
  101. func (d *Decoder) kBool(f *codecFnInfo, rv reflect.Value) {
  102. rv.SetBool(d.d.DecodeBool())
  103. }
  104. func (d *Decoder) kTime(f *codecFnInfo, rv reflect.Value) {
  105. rv.Set(reflect.ValueOf(d.d.DecodeTime()))
  106. }
  107. func (d *Decoder) kFloat32(f *codecFnInfo, rv reflect.Value) {
  108. fv := d.d.DecodeFloat64()
  109. if chkOvf.Float32(fv) {
  110. d.errorf("float32 overflow: %v", fv)
  111. }
  112. rv.SetFloat(fv)
  113. }
  114. func (d *Decoder) kFloat64(f *codecFnInfo, rv reflect.Value) {
  115. rv.SetFloat(d.d.DecodeFloat64())
  116. }
  117. func (d *Decoder) kInt(f *codecFnInfo, rv reflect.Value) {
  118. rv.SetInt(chkOvf.IntV(d.d.DecodeInt64(), intBitsize))
  119. }
  120. func (d *Decoder) kInt8(f *codecFnInfo, rv reflect.Value) {
  121. rv.SetInt(chkOvf.IntV(d.d.DecodeInt64(), 8))
  122. }
  123. func (d *Decoder) kInt16(f *codecFnInfo, rv reflect.Value) {
  124. rv.SetInt(chkOvf.IntV(d.d.DecodeInt64(), 16))
  125. }
  126. func (d *Decoder) kInt32(f *codecFnInfo, rv reflect.Value) {
  127. rv.SetInt(chkOvf.IntV(d.d.DecodeInt64(), 32))
  128. }
  129. func (d *Decoder) kInt64(f *codecFnInfo, rv reflect.Value) {
  130. rv.SetInt(d.d.DecodeInt64())
  131. }
  132. func (d *Decoder) kUint(f *codecFnInfo, rv reflect.Value) {
  133. rv.SetUint(chkOvf.UintV(d.d.DecodeUint64(), uintBitsize))
  134. }
  135. func (d *Decoder) kUintptr(f *codecFnInfo, rv reflect.Value) {
  136. rv.SetUint(chkOvf.UintV(d.d.DecodeUint64(), uintBitsize))
  137. }
  138. func (d *Decoder) kUint8(f *codecFnInfo, rv reflect.Value) {
  139. rv.SetUint(chkOvf.UintV(d.d.DecodeUint64(), 8))
  140. }
  141. func (d *Decoder) kUint16(f *codecFnInfo, rv reflect.Value) {
  142. rv.SetUint(chkOvf.UintV(d.d.DecodeUint64(), 16))
  143. }
  144. func (d *Decoder) kUint32(f *codecFnInfo, rv reflect.Value) {
  145. rv.SetUint(chkOvf.UintV(d.d.DecodeUint64(), 32))
  146. }
  147. func (d *Decoder) kUint64(f *codecFnInfo, rv reflect.Value) {
  148. rv.SetUint(d.d.DecodeUint64())
  149. }
  150. // ----------------
  151. func (e *Encoder) kBool(f *codecFnInfo, rv reflect.Value) {
  152. e.e.EncodeBool(rv.Bool())
  153. }
  154. func (e *Encoder) kTime(f *codecFnInfo, rv reflect.Value) {
  155. e.e.EncodeTime(rv2i(rv).(time.Time))
  156. }
  157. func (e *Encoder) kString(f *codecFnInfo, rv reflect.Value) {
  158. e.e.EncodeString(cUTF8, rv.String())
  159. }
  160. func (e *Encoder) kFloat64(f *codecFnInfo, rv reflect.Value) {
  161. e.e.EncodeFloat64(rv.Float())
  162. }
  163. func (e *Encoder) kFloat32(f *codecFnInfo, rv reflect.Value) {
  164. e.e.EncodeFloat32(float32(rv.Float()))
  165. }
  166. func (e *Encoder) kInt(f *codecFnInfo, rv reflect.Value) {
  167. e.e.EncodeInt(rv.Int())
  168. }
  169. func (e *Encoder) kInt8(f *codecFnInfo, rv reflect.Value) {
  170. e.e.EncodeInt(rv.Int())
  171. }
  172. func (e *Encoder) kInt16(f *codecFnInfo, rv reflect.Value) {
  173. e.e.EncodeInt(rv.Int())
  174. }
  175. func (e *Encoder) kInt32(f *codecFnInfo, rv reflect.Value) {
  176. e.e.EncodeInt(rv.Int())
  177. }
  178. func (e *Encoder) kInt64(f *codecFnInfo, rv reflect.Value) {
  179. e.e.EncodeInt(rv.Int())
  180. }
  181. func (e *Encoder) kUint(f *codecFnInfo, rv reflect.Value) {
  182. e.e.EncodeUint(rv.Uint())
  183. }
  184. func (e *Encoder) kUint8(f *codecFnInfo, rv reflect.Value) {
  185. e.e.EncodeUint(rv.Uint())
  186. }
  187. func (e *Encoder) kUint16(f *codecFnInfo, rv reflect.Value) {
  188. e.e.EncodeUint(rv.Uint())
  189. }
  190. func (e *Encoder) kUint32(f *codecFnInfo, rv reflect.Value) {
  191. e.e.EncodeUint(rv.Uint())
  192. }
  193. func (e *Encoder) kUint64(f *codecFnInfo, rv reflect.Value) {
  194. e.e.EncodeUint(rv.Uint())
  195. }
  196. func (e *Encoder) kUintptr(f *codecFnInfo, rv reflect.Value) {
  197. e.e.EncodeUint(rv.Uint())
  198. }
  199. // // keepAlive4BytesView maintains a reference to the input parameter for bytesView.
  200. // //
  201. // // Usage: call this at point where done with the bytes view.
  202. // func keepAlive4BytesView(v string) {}
  203. // // keepAlive4BytesView maintains a reference to the input parameter for stringView.
  204. // //
  205. // // Usage: call this at point where done with the string view.
  206. // func keepAlive4StringView(v []byte) {}
  207. // func definitelyNil(v interface{}) bool {
  208. // rv := reflect.ValueOf(v)
  209. // switch rv.Kind() {
  210. // case reflect.Invalid:
  211. // return true
  212. // case reflect.Ptr, reflect.Interface, reflect.Chan, reflect.Slice, reflect.Map, reflect.Func:
  213. // return rv.IsNil()
  214. // default:
  215. // return false
  216. // }
  217. // }