grpc 1.1.2 → 1.2.0.pre1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (255) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +1257 -404
  3. data/etc/roots.pem +189 -102
  4. data/include/grpc/census.h +7 -7
  5. data/include/grpc/compression.h +4 -4
  6. data/include/grpc/grpc.h +13 -7
  7. data/include/grpc/impl/codegen/atm_gcc_atomic.h +26 -9
  8. data/include/grpc/impl/codegen/grpc_types.h +39 -30
  9. data/include/grpc/impl/codegen/slice.h +24 -6
  10. data/include/grpc/impl/codegen/sync.h +8 -0
  11. data/include/grpc/load_reporting.h +63 -0
  12. data/include/grpc/slice.h +37 -1
  13. data/include/grpc/slice_buffer.h +7 -0
  14. data/include/grpc/support/alloc.h +3 -0
  15. data/include/grpc/support/useful.h +3 -0
  16. data/src/core/ext/census/gen/census.pb.h +1 -1
  17. data/src/core/ext/census/gen/trace_context.pb.c +9 -36
  18. data/src/core/ext/census/gen/trace_context.pb.h +20 -26
  19. data/src/core/ext/census/grpc_filter.c +3 -5
  20. data/src/core/ext/census/trace_context.c +1 -1
  21. data/src/core/ext/census/trace_context.h +3 -0
  22. data/src/core/ext/census/trace_label.h +61 -0
  23. data/src/core/ext/census/trace_propagation.h +63 -0
  24. data/src/core/ext/census/trace_status.h +45 -0
  25. data/src/core/ext/census/trace_string.h +50 -0
  26. data/src/core/ext/census/tracing.c +31 -11
  27. data/src/core/ext/census/tracing.h +124 -0
  28. data/src/core/ext/client_channel/client_channel.c +456 -368
  29. data/src/core/ext/client_channel/client_channel.h +4 -0
  30. data/src/core/ext/client_channel/client_channel_plugin.c +6 -1
  31. data/src/core/ext/client_channel/connector.c +3 -3
  32. data/src/core/ext/client_channel/connector.h +4 -3
  33. data/src/core/ext/client_channel/http_connect_handshaker.c +62 -72
  34. data/src/core/ext/client_channel/http_connect_handshaker.h +7 -10
  35. data/src/core/ext/client_channel/http_proxy.c +125 -0
  36. data/src/core/ext/client_channel/http_proxy.h +39 -0
  37. data/src/core/ext/client_channel/lb_policy.c +56 -35
  38. data/src/core/ext/client_channel/lb_policy.h +46 -39
  39. data/src/core/ext/client_channel/lb_policy_factory.h +1 -0
  40. data/src/core/ext/client_channel/parse_address.c +32 -6
  41. data/src/core/ext/client_channel/proxy_mapper.c +63 -0
  42. data/src/core/ext/client_channel/proxy_mapper.h +89 -0
  43. data/src/core/ext/client_channel/proxy_mapper_registry.c +133 -0
  44. data/src/core/ext/client_channel/proxy_mapper_registry.h +59 -0
  45. data/src/core/ext/client_channel/resolver.c +16 -9
  46. data/src/core/ext/client_channel/resolver.h +23 -12
  47. data/src/core/ext/client_channel/resolver_factory.h +1 -0
  48. data/src/core/ext/client_channel/resolver_registry.c +15 -11
  49. data/src/core/ext/client_channel/resolver_registry.h +5 -3
  50. data/src/core/ext/client_channel/subchannel.c +44 -27
  51. data/src/core/ext/client_channel/subchannel.h +6 -2
  52. data/src/core/ext/client_channel/uri_parser.c +26 -14
  53. data/src/core/ext/client_channel/uri_parser.h +3 -1
  54. data/src/core/ext/lb_policy/grpclb/grpclb.c +220 -209
  55. data/src/core/ext/lb_policy/grpclb/grpclb_channel.h +56 -0
  56. data/src/core/ext/lb_policy/grpclb/grpclb_channel_secure.c +107 -0
  57. data/src/core/ext/lb_policy/grpclb/load_balancer_api.c +3 -6
  58. data/src/core/ext/lb_policy/pick_first/pick_first.c +71 -116
  59. data/src/core/ext/lb_policy/round_robin/round_robin.c +52 -67
  60. data/src/core/ext/load_reporting/load_reporting.c +20 -0
  61. data/src/core/ext/load_reporting/load_reporting.h +1 -16
  62. data/src/core/ext/load_reporting/load_reporting_filter.c +28 -54
  63. data/src/core/ext/resolver/dns/native/dns_resolver.c +31 -45
  64. data/src/core/ext/resolver/sockaddr/sockaddr_resolver.c +20 -29
  65. data/src/core/ext/transport/chttp2/client/chttp2_connector.c +11 -8
  66. data/src/core/ext/transport/chttp2/client/insecure/channel_create.c +11 -2
  67. data/src/core/ext/transport/chttp2/client/secure/secure_channel_create.c +143 -46
  68. data/src/core/ext/transport/chttp2/server/chttp2_server.c +12 -50
  69. data/src/core/ext/transport/chttp2/server/insecure/server_chttp2.c +1 -1
  70. data/src/core/ext/transport/chttp2/server/secure/server_secure_chttp2.c +1 -1
  71. data/src/core/ext/transport/chttp2/transport/bin_decoder.c +7 -7
  72. data/src/core/ext/transport/chttp2/transport/bin_encoder.c +1 -2
  73. data/src/core/ext/transport/chttp2/transport/bin_encoder.h +1 -2
  74. data/src/core/ext/transport/chttp2/transport/chttp2_plugin.c +0 -3
  75. data/src/core/ext/transport/chttp2/transport/chttp2_transport.c +606 -374
  76. data/src/core/ext/transport/chttp2/transport/frame_ping.c +17 -5
  77. data/src/core/ext/transport/chttp2/transport/frame_ping.h +2 -2
  78. data/src/core/ext/transport/chttp2/transport/frame_rst_stream.c +9 -13
  79. data/src/core/ext/transport/chttp2/transport/frame_settings.c +12 -11
  80. data/src/core/ext/transport/chttp2/transport/frame_settings.h +1 -1
  81. data/src/core/ext/transport/chttp2/transport/frame_window_update.c +5 -6
  82. data/src/core/ext/transport/chttp2/transport/hpack_encoder.c +100 -53
  83. data/src/core/ext/transport/chttp2/transport/hpack_encoder.h +2 -2
  84. data/src/core/ext/transport/chttp2/transport/hpack_parser.c +126 -70
  85. data/src/core/ext/transport/chttp2/transport/hpack_parser.h +13 -7
  86. data/src/core/ext/transport/chttp2/transport/hpack_table.c +22 -19
  87. data/src/core/ext/transport/chttp2/transport/hpack_table.h +6 -6
  88. data/src/core/ext/transport/chttp2/transport/incoming_metadata.c +23 -11
  89. data/src/core/ext/transport/chttp2/transport/incoming_metadata.h +6 -2
  90. data/src/core/ext/transport/chttp2/transport/internal.h +169 -42
  91. data/src/core/ext/transport/chttp2/transport/parsing.c +98 -41
  92. data/src/core/ext/transport/chttp2/transport/stream_lists.c +29 -14
  93. data/src/core/ext/transport/chttp2/transport/writing.c +137 -15
  94. data/src/core/lib/channel/channel_stack.c +14 -44
  95. data/src/core/lib/channel/channel_stack.h +10 -17
  96. data/src/core/lib/channel/channel_stack_builder.c +2 -3
  97. data/src/core/lib/channel/compress_filter.c +54 -46
  98. data/src/core/lib/channel/connected_channel.c +4 -4
  99. data/src/core/lib/channel/connected_channel.h +5 -0
  100. data/src/core/lib/channel/context.h +3 -0
  101. data/src/core/lib/channel/deadline_filter.c +61 -61
  102. data/src/core/lib/channel/deadline_filter.h +8 -5
  103. data/src/core/lib/channel/handshaker.c +47 -7
  104. data/src/core/lib/channel/handshaker.h +21 -3
  105. data/src/core/lib/channel/http_client_filter.c +149 -99
  106. data/src/core/lib/channel/http_server_filter.c +163 -147
  107. data/src/core/lib/channel/message_size_filter.c +15 -10
  108. data/src/core/lib/compression/algorithm_metadata.h +4 -4
  109. data/src/core/lib/compression/compression.c +17 -23
  110. data/src/core/lib/http/httpcli.c +3 -2
  111. data/src/core/lib/http/httpcli.h +2 -1
  112. data/src/core/lib/http/httpcli_security_connector.c +2 -3
  113. data/src/core/lib/http/parser.c +2 -2
  114. data/src/core/lib/iomgr/closure.c +6 -3
  115. data/src/core/lib/iomgr/closure.h +4 -2
  116. data/src/core/lib/iomgr/combiner.c +35 -5
  117. data/src/core/lib/iomgr/combiner.h +21 -2
  118. data/src/core/lib/iomgr/endpoint.c +3 -2
  119. data/src/core/lib/iomgr/endpoint.h +3 -2
  120. data/src/core/lib/iomgr/error.c +60 -94
  121. data/src/core/lib/iomgr/error.h +7 -10
  122. data/src/core/lib/iomgr/error_internal.h +54 -0
  123. data/src/core/lib/iomgr/ev_epoll_linux.c +253 -109
  124. data/src/core/lib/iomgr/ev_poll_posix.c +61 -29
  125. data/src/core/lib/iomgr/ev_posix.c +7 -8
  126. data/src/core/lib/iomgr/ev_posix.h +4 -4
  127. data/src/core/lib/iomgr/exec_ctx.c +11 -6
  128. data/src/core/lib/iomgr/exec_ctx.h +11 -14
  129. data/src/core/lib/iomgr/executor.c +2 -2
  130. data/src/core/lib/iomgr/load_file.c +1 -1
  131. data/src/core/lib/iomgr/network_status_tracker.c +5 -81
  132. data/src/core/lib/iomgr/pollset.h +1 -3
  133. data/src/core/lib/iomgr/pollset_set.h +2 -1
  134. data/src/core/lib/iomgr/pollset_set_uv.c +2 -1
  135. data/src/core/lib/iomgr/pollset_set_windows.c +2 -1
  136. data/src/core/lib/iomgr/pollset_uv.c +25 -11
  137. data/src/core/lib/iomgr/pollset_windows.c +0 -11
  138. data/src/core/lib/iomgr/resolve_address_uv.c +50 -2
  139. data/src/core/lib/iomgr/resource_quota.c +41 -11
  140. data/src/core/lib/iomgr/resource_quota.h +6 -0
  141. data/src/core/lib/iomgr/sockaddr_utils.c +33 -17
  142. data/src/core/lib/iomgr/sockaddr_utils.h +4 -0
  143. data/src/core/lib/iomgr/tcp_client_posix.c +2 -3
  144. data/src/core/lib/iomgr/tcp_client_uv.c +1 -3
  145. data/src/core/lib/iomgr/tcp_client_windows.c +21 -6
  146. data/src/core/lib/iomgr/tcp_posix.c +4 -5
  147. data/src/core/lib/iomgr/tcp_server_posix.c +269 -94
  148. data/src/core/lib/iomgr/tcp_server_windows.c +1 -1
  149. data/src/core/lib/iomgr/tcp_uv.c +11 -5
  150. data/src/core/lib/iomgr/tcp_windows.c +20 -7
  151. data/src/core/lib/iomgr/timer_generic.c +15 -22
  152. data/src/core/lib/iomgr/timer_generic.h +1 -1
  153. data/src/core/lib/iomgr/timer_uv.c +10 -6
  154. data/src/core/lib/iomgr/timer_uv.h +1 -1
  155. data/src/core/lib/iomgr/udp_server.c +45 -6
  156. data/src/core/lib/iomgr/udp_server.h +7 -1
  157. data/src/core/lib/iomgr/unix_sockets_posix.c +11 -1
  158. data/src/core/lib/json/json.c +1 -2
  159. data/src/core/lib/profiling/basic_timers.c +17 -3
  160. data/src/core/lib/security/context/security_context.c +3 -10
  161. data/src/core/lib/security/credentials/composite/composite_credentials.c +4 -8
  162. data/src/core/lib/security/credentials/credentials.c +48 -2
  163. data/src/core/lib/security/credentials/credentials.h +13 -0
  164. data/src/core/lib/security/credentials/credentials_metadata.c +1 -2
  165. data/src/core/lib/security/credentials/fake/fake_credentials.c +6 -8
  166. data/src/core/lib/security/credentials/fake/fake_credentials.h +15 -0
  167. data/src/core/lib/security/credentials/google_default/google_default_credentials.c +3 -3
  168. data/src/core/lib/security/credentials/iam/iam_credentials.c +1 -2
  169. data/src/core/lib/security/credentials/jwt/jwt_credentials.c +1 -2
  170. data/src/core/lib/security/credentials/jwt/jwt_verifier.c +5 -8
  171. data/src/core/lib/security/credentials/jwt/jwt_verifier.h +2 -1
  172. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.c +3 -5
  173. data/src/core/lib/security/credentials/plugin/plugin_credentials.c +15 -13
  174. data/src/core/lib/security/credentials/ssl/ssl_credentials.c +2 -4
  175. data/src/core/lib/security/transport/client_auth_filter.c +72 -47
  176. data/src/core/lib/security/transport/lb_targets_info.c +70 -0
  177. data/src/core/lib/security/transport/lb_targets_info.h +47 -0
  178. data/src/core/lib/security/transport/secure_endpoint.c +3 -3
  179. data/src/core/lib/security/transport/security_connector.c +125 -28
  180. data/src/core/lib/security/transport/security_connector.h +4 -3
  181. data/src/core/lib/security/transport/security_handshaker.c +13 -9
  182. data/src/core/lib/security/transport/server_auth_filter.c +31 -40
  183. data/src/core/lib/security/util/b64.c +1 -1
  184. data/src/core/lib/slice/slice.c +110 -20
  185. data/src/core/lib/slice/slice_buffer.c +92 -39
  186. data/src/core/lib/{transport/mdstr_hash_table.c → slice/slice_hash_table.c} +40 -33
  187. data/src/core/lib/{transport/mdstr_hash_table.h → slice/slice_hash_table.h} +21 -21
  188. data/src/core/lib/slice/slice_intern.c +346 -0
  189. data/src/core/lib/slice/slice_internal.h +15 -0
  190. data/src/core/lib/slice/slice_string_helpers.c +5 -0
  191. data/src/core/lib/slice/slice_string_helpers.h +5 -0
  192. data/src/core/lib/support/alloc.c +26 -1
  193. data/src/core/lib/support/cmdline.c +2 -4
  194. data/src/core/lib/support/cpu_posix.c +2 -7
  195. data/src/core/lib/support/histogram.c +1 -2
  196. data/src/core/lib/support/log_posix.c +8 -4
  197. data/src/core/lib/support/spinlock.h +52 -0
  198. data/src/core/lib/support/subprocess_posix.c +1 -2
  199. data/src/core/lib/support/sync.c +7 -1
  200. data/src/core/lib/support/sync_posix.c +9 -0
  201. data/src/core/lib/support/time_windows.c +7 -1
  202. data/src/core/lib/surface/call.c +647 -629
  203. data/src/core/lib/surface/call.h +4 -1
  204. data/src/core/lib/surface/call_details.c +8 -2
  205. data/src/core/lib/surface/call_log_batch.c +17 -6
  206. data/src/core/lib/surface/channel.c +49 -59
  207. data/src/core/lib/surface/channel.h +5 -6
  208. data/src/core/lib/surface/completion_queue.c +16 -45
  209. data/src/core/lib/surface/completion_queue.h +0 -3
  210. data/src/core/lib/surface/init.c +6 -2
  211. data/src/core/lib/surface/init_secure.c +1 -1
  212. data/src/core/lib/surface/lame_client.c +14 -4
  213. data/src/core/lib/surface/server.c +79 -82
  214. data/src/core/lib/surface/validate_metadata.c +46 -15
  215. data/src/core/lib/surface/validate_metadata.h +43 -0
  216. data/src/core/lib/surface/version.c +2 -2
  217. data/src/core/lib/transport/bdp_estimator.c +104 -0
  218. data/src/core/lib/transport/bdp_estimator.h +76 -0
  219. data/src/core/lib/transport/connectivity_state.c +33 -13
  220. data/src/core/lib/transport/connectivity_state.h +15 -5
  221. data/src/core/lib/transport/error_utils.c +124 -0
  222. data/src/core/lib/transport/error_utils.h +56 -0
  223. data/src/core/{ext/transport/chttp2 → lib}/transport/http2_errors.h +18 -18
  224. data/src/core/lib/transport/metadata.c +259 -503
  225. data/src/core/lib/transport/metadata.h +69 -68
  226. data/src/core/lib/transport/metadata_batch.c +183 -63
  227. data/src/core/lib/transport/metadata_batch.h +50 -26
  228. data/src/core/lib/transport/pid_controller.c +28 -8
  229. data/src/core/lib/transport/pid_controller.h +15 -2
  230. data/src/core/lib/transport/service_config.c +21 -18
  231. data/src/core/lib/transport/service_config.h +5 -5
  232. data/src/core/lib/transport/static_metadata.c +753 -112
  233. data/src/core/lib/transport/static_metadata.h +403 -264
  234. data/src/core/{ext/transport/chttp2 → lib}/transport/status_conversion.c +18 -20
  235. data/src/core/{ext/transport/chttp2 → lib}/transport/status_conversion.h +9 -10
  236. data/src/core/lib/transport/timeout_encoding.c +11 -9
  237. data/src/core/lib/transport/timeout_encoding.h +3 -1
  238. data/src/core/lib/transport/transport.c +47 -87
  239. data/src/core/lib/transport/transport.h +20 -25
  240. data/src/core/lib/transport/transport_op_string.c +7 -19
  241. data/src/core/lib/tsi/fake_transport_security.c +2 -4
  242. data/src/core/lib/tsi/ssl_transport_security.c +7 -16
  243. data/src/core/lib/tsi/transport_security.c +2 -4
  244. data/src/ruby/ext/grpc/extconf.rb +4 -1
  245. data/src/ruby/ext/grpc/rb_byte_buffer.c +7 -0
  246. data/src/ruby/ext/grpc/rb_byte_buffer.h +3 -0
  247. data/src/ruby/ext/grpc/rb_call.c +47 -46
  248. data/src/ruby/ext/grpc/rb_channel.c +21 -6
  249. data/src/ruby/ext/grpc/rb_compression_options.c +9 -6
  250. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +36 -2
  251. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +59 -8
  252. data/src/ruby/ext/grpc/rb_server.c +6 -4
  253. data/src/ruby/lib/grpc/generic/client_stub.rb +1 -1
  254. data/src/ruby/lib/grpc/version.rb +1 -1
  255. metadata +33 -9
@@ -232,5 +232,5 @@ grpc_slice grpc_base64_decode_with_len(grpc_exec_ctx *exec_ctx, const char *b64,
232
232
 
233
233
  fail:
234
234
  grpc_slice_unref_internal(exec_ctx, result);
235
- return gpr_empty_slice();
235
+ return grpc_empty_slice();
236
236
  }
@@ -41,23 +41,30 @@
41
41
 
42
42
  #include "src/core/lib/iomgr/exec_ctx.h"
43
43
 
44
- grpc_slice gpr_empty_slice(void) {
44
+ char *grpc_slice_to_c_string(grpc_slice slice) {
45
+ char *out = gpr_malloc(GRPC_SLICE_LENGTH(slice) + 1);
46
+ memcpy(out, GRPC_SLICE_START_PTR(slice), GRPC_SLICE_LENGTH(slice));
47
+ out[GRPC_SLICE_LENGTH(slice)] = 0;
48
+ return out;
49
+ }
50
+
51
+ grpc_slice grpc_empty_slice(void) {
45
52
  grpc_slice out;
46
- out.refcount = 0;
53
+ out.refcount = NULL;
47
54
  out.data.inlined.length = 0;
48
55
  return out;
49
56
  }
50
57
 
51
58
  grpc_slice grpc_slice_ref_internal(grpc_slice slice) {
52
59
  if (slice.refcount) {
53
- slice.refcount->ref(slice.refcount);
60
+ slice.refcount->vtable->ref(slice.refcount);
54
61
  }
55
62
  return slice;
56
63
  }
57
64
 
58
65
  void grpc_slice_unref_internal(grpc_exec_ctx *exec_ctx, grpc_slice slice) {
59
66
  if (slice.refcount) {
60
- slice.refcount->unref(exec_ctx, slice.refcount);
67
+ slice.refcount->vtable->unref(exec_ctx, slice.refcount);
61
68
  }
62
69
  }
63
70
 
@@ -78,16 +85,24 @@ void grpc_slice_unref(grpc_slice slice) {
78
85
  static void noop_ref(void *unused) {}
79
86
  static void noop_unref(grpc_exec_ctx *exec_ctx, void *unused) {}
80
87
 
81
- static grpc_slice_refcount noop_refcount = {noop_ref, noop_unref};
88
+ static const grpc_slice_refcount_vtable noop_refcount_vtable = {
89
+ noop_ref, noop_unref, grpc_slice_default_eq_impl,
90
+ grpc_slice_default_hash_impl};
91
+ static grpc_slice_refcount noop_refcount = {&noop_refcount_vtable,
92
+ &noop_refcount};
82
93
 
83
- grpc_slice grpc_slice_from_static_string(const char *s) {
94
+ grpc_slice grpc_slice_from_static_buffer(const void *s, size_t len) {
84
95
  grpc_slice slice;
85
96
  slice.refcount = &noop_refcount;
86
97
  slice.data.refcounted.bytes = (uint8_t *)s;
87
- slice.data.refcounted.length = strlen(s);
98
+ slice.data.refcounted.length = len;
88
99
  return slice;
89
100
  }
90
101
 
102
+ grpc_slice grpc_slice_from_static_string(const char *s) {
103
+ return grpc_slice_from_static_buffer(s, strlen(s));
104
+ }
105
+
91
106
  /* grpc_slice_new support structures - we create a refcount object extended
92
107
  with the user provided data pointer & destroy function */
93
108
  typedef struct new_slice_refcount {
@@ -110,14 +125,18 @@ static void new_slice_unref(grpc_exec_ctx *exec_ctx, void *p) {
110
125
  }
111
126
  }
112
127
 
128
+ static const grpc_slice_refcount_vtable new_slice_vtable = {
129
+ new_slice_ref, new_slice_unref, grpc_slice_default_eq_impl,
130
+ grpc_slice_default_hash_impl};
131
+
113
132
  grpc_slice grpc_slice_new_with_user_data(void *p, size_t len,
114
133
  void (*destroy)(void *),
115
134
  void *user_data) {
116
135
  grpc_slice slice;
117
136
  new_slice_refcount *rc = gpr_malloc(sizeof(new_slice_refcount));
118
137
  gpr_ref_init(&rc->refs, 1);
119
- rc->rc.ref = new_slice_ref;
120
- rc->rc.unref = new_slice_unref;
138
+ rc->rc.vtable = &new_slice_vtable;
139
+ rc->rc.sub_refcount = &rc->rc;
121
140
  rc->user_destroy = destroy;
122
141
  rc->user_data = user_data;
123
142
 
@@ -155,14 +174,18 @@ static void new_with_len_unref(grpc_exec_ctx *exec_ctx, void *p) {
155
174
  }
156
175
  }
157
176
 
177
+ static const grpc_slice_refcount_vtable new_with_len_vtable = {
178
+ new_with_len_ref, new_with_len_unref, grpc_slice_default_eq_impl,
179
+ grpc_slice_default_hash_impl};
180
+
158
181
  grpc_slice grpc_slice_new_with_len(void *p, size_t len,
159
182
  void (*destroy)(void *, size_t)) {
160
183
  grpc_slice slice;
161
184
  new_with_len_slice_refcount *rc =
162
185
  gpr_malloc(sizeof(new_with_len_slice_refcount));
163
186
  gpr_ref_init(&rc->refs, 1);
164
- rc->rc.ref = new_with_len_ref;
165
- rc->rc.unref = new_with_len_unref;
187
+ rc->rc.vtable = &new_with_len_vtable;
188
+ rc->rc.sub_refcount = &rc->rc;
166
189
  rc->user_destroy = destroy;
167
190
  rc->user_data = p;
168
191
  rc->user_length = len;
@@ -200,6 +223,10 @@ static void malloc_unref(grpc_exec_ctx *exec_ctx, void *p) {
200
223
  }
201
224
  }
202
225
 
226
+ static const grpc_slice_refcount_vtable malloc_vtable = {
227
+ malloc_ref, malloc_unref, grpc_slice_default_eq_impl,
228
+ grpc_slice_default_hash_impl};
229
+
203
230
  grpc_slice grpc_slice_malloc(size_t length) {
204
231
  grpc_slice slice;
205
232
 
@@ -219,8 +246,8 @@ grpc_slice grpc_slice_malloc(size_t length) {
219
246
  this reference. */
220
247
  gpr_ref_init(&rc->refs, 1);
221
248
 
222
- rc->base.ref = malloc_ref;
223
- rc->base.unref = malloc_unref;
249
+ rc->base.vtable = &malloc_vtable;
250
+ rc->base.sub_refcount = &rc->base;
224
251
 
225
252
  /* Build up the slice to be returned. */
226
253
  /* The slices refcount points back to the allocated block. */
@@ -247,7 +274,7 @@ grpc_slice grpc_slice_sub_no_ref(grpc_slice source, size_t begin, size_t end) {
247
274
  GPR_ASSERT(source.data.refcounted.length >= end);
248
275
 
249
276
  /* Build the result */
250
- subset.refcount = source.refcount;
277
+ subset.refcount = source.refcount->sub_refcount;
251
278
  /* Point into the source array */
252
279
  subset.data.refcounted.bytes = source.data.refcounted.bytes + begin;
253
280
  subset.data.refcounted.length = end - begin;
@@ -273,7 +300,7 @@ grpc_slice grpc_slice_sub(grpc_slice source, size_t begin, size_t end) {
273
300
  } else {
274
301
  subset = grpc_slice_sub_no_ref(source, begin, end);
275
302
  /* Bump the refcount */
276
- subset.refcount->ref(subset.refcount);
303
+ subset.refcount->vtable->ref(subset.refcount);
277
304
  }
278
305
  return subset;
279
306
  }
@@ -300,13 +327,14 @@ grpc_slice grpc_slice_split_tail(grpc_slice *source, size_t split) {
300
327
  tail_length);
301
328
  } else {
302
329
  /* Build the result */
303
- tail.refcount = source->refcount;
330
+ tail.refcount = source->refcount->sub_refcount;
304
331
  /* Bump the refcount */
305
- tail.refcount->ref(tail.refcount);
332
+ tail.refcount->vtable->ref(tail.refcount);
306
333
  /* Point into the source array */
307
334
  tail.data.refcounted.bytes = source->data.refcounted.bytes + split;
308
335
  tail.data.refcounted.length = tail_length;
309
336
  }
337
+ source->refcount = source->refcount->sub_refcount;
310
338
  source->data.refcounted.length = split;
311
339
  }
312
340
 
@@ -332,18 +360,20 @@ grpc_slice grpc_slice_split_head(grpc_slice *source, size_t split) {
332
360
  head.refcount = NULL;
333
361
  head.data.inlined.length = (uint8_t)split;
334
362
  memcpy(head.data.inlined.bytes, source->data.refcounted.bytes, split);
363
+ source->refcount = source->refcount->sub_refcount;
335
364
  source->data.refcounted.bytes += split;
336
365
  source->data.refcounted.length -= split;
337
366
  } else {
338
367
  GPR_ASSERT(source->data.refcounted.length >= split);
339
368
 
340
369
  /* Build the result */
341
- head.refcount = source->refcount;
370
+ head.refcount = source->refcount->sub_refcount;
342
371
  /* Bump the refcount */
343
- head.refcount->ref(head.refcount);
372
+ head.refcount->vtable->ref(head.refcount);
344
373
  /* Point into the source array */
345
374
  head.data.refcounted.bytes = source->data.refcounted.bytes;
346
375
  head.data.refcounted.length = split;
376
+ source->refcount = source->refcount->sub_refcount;
347
377
  source->data.refcounted.bytes += split;
348
378
  source->data.refcounted.length -= split;
349
379
  }
@@ -351,6 +381,19 @@ grpc_slice grpc_slice_split_head(grpc_slice *source, size_t split) {
351
381
  return head;
352
382
  }
353
383
 
384
+ int grpc_slice_default_eq_impl(grpc_slice a, grpc_slice b) {
385
+ return GRPC_SLICE_LENGTH(a) == GRPC_SLICE_LENGTH(b) &&
386
+ 0 == memcmp(GRPC_SLICE_START_PTR(a), GRPC_SLICE_START_PTR(b),
387
+ GRPC_SLICE_LENGTH(a));
388
+ }
389
+
390
+ int grpc_slice_eq(grpc_slice a, grpc_slice b) {
391
+ if (a.refcount && b.refcount && a.refcount->vtable == b.refcount->vtable) {
392
+ return a.refcount->vtable->eq(a, b);
393
+ }
394
+ return grpc_slice_default_eq_impl(a, b);
395
+ }
396
+
354
397
  int grpc_slice_cmp(grpc_slice a, grpc_slice b) {
355
398
  int d = (int)(GRPC_SLICE_LENGTH(a) - GRPC_SLICE_LENGTH(b));
356
399
  if (d != 0) return d;
@@ -367,8 +410,55 @@ int grpc_slice_str_cmp(grpc_slice a, const char *b) {
367
410
 
368
411
  int grpc_slice_is_equivalent(grpc_slice a, grpc_slice b) {
369
412
  if (a.refcount == NULL || b.refcount == NULL) {
370
- return grpc_slice_cmp(a, b) == 0;
413
+ return grpc_slice_eq(a, b);
371
414
  }
372
415
  return a.data.refcounted.length == b.data.refcounted.length &&
373
416
  a.data.refcounted.bytes == b.data.refcounted.bytes;
374
417
  }
418
+
419
+ int grpc_slice_buf_start_eq(grpc_slice a, const void *b, size_t len) {
420
+ if (GRPC_SLICE_LENGTH(a) < len) return 0;
421
+ return 0 == memcmp(GRPC_SLICE_START_PTR(a), b, len);
422
+ }
423
+
424
+ int grpc_slice_rchr(grpc_slice s, char c) {
425
+ const char *b = (const char *)GRPC_SLICE_START_PTR(s);
426
+ int i;
427
+ for (i = (int)GRPC_SLICE_LENGTH(s) - 1; i != -1 && b[i] != c; i--)
428
+ ;
429
+ return i;
430
+ }
431
+
432
+ int grpc_slice_chr(grpc_slice s, char c) {
433
+ const char *b = (const char *)GRPC_SLICE_START_PTR(s);
434
+ const char *p = memchr(b, c, GRPC_SLICE_LENGTH(s));
435
+ return p == NULL ? -1 : (int)(p - b);
436
+ }
437
+
438
+ int grpc_slice_slice(grpc_slice haystack, grpc_slice needle) {
439
+ size_t haystack_len = GRPC_SLICE_LENGTH(haystack);
440
+ const uint8_t *haystack_bytes = GRPC_SLICE_START_PTR(haystack);
441
+ size_t needle_len = GRPC_SLICE_LENGTH(needle);
442
+ const uint8_t *needle_bytes = GRPC_SLICE_START_PTR(needle);
443
+
444
+ if (haystack_len == 0 || needle_len == 0) return -1;
445
+ if (haystack_len < needle_len) return -1;
446
+ if (haystack_len == needle_len)
447
+ return grpc_slice_eq(haystack, needle) ? 0 : -1;
448
+ if (needle_len == 1) return grpc_slice_chr(haystack, (char)*needle_bytes);
449
+
450
+ const uint8_t *last = haystack_bytes + haystack_len - needle_len;
451
+ for (const uint8_t *cur = haystack_bytes; cur != last; ++cur) {
452
+ if (0 == memcmp(cur, needle_bytes, needle_len)) {
453
+ return (int)(cur - haystack_bytes);
454
+ }
455
+ }
456
+ return -1;
457
+ }
458
+
459
+ grpc_slice grpc_slice_dup(grpc_slice a) {
460
+ grpc_slice copy = grpc_slice_malloc(GRPC_SLICE_LENGTH(a));
461
+ memcpy(GRPC_SLICE_START_PTR(copy), GRPC_SLICE_START_PTR(a),
462
+ GRPC_SLICE_LENGTH(a));
463
+ return copy;
464
+ }
@@ -46,15 +46,27 @@
46
46
  #define GROW(x) (3 * (x) / 2)
47
47
 
48
48
  static void maybe_embiggen(grpc_slice_buffer *sb) {
49
- if (sb->count == sb->capacity) {
49
+ if (sb->base_slices != sb->slices) {
50
+ memmove(sb->base_slices, sb->slices, sb->count * sizeof(grpc_slice));
51
+ sb->slices = sb->base_slices;
52
+ }
53
+
54
+ /* How far away from sb->base_slices is sb->slices pointer */
55
+ size_t slice_offset = (size_t)(sb->slices - sb->base_slices);
56
+ size_t slice_count = sb->count + slice_offset;
57
+
58
+ if (slice_count == sb->capacity) {
50
59
  sb->capacity = GROW(sb->capacity);
51
- GPR_ASSERT(sb->capacity > sb->count);
52
- if (sb->slices == sb->inlined) {
53
- sb->slices = gpr_malloc(sb->capacity * sizeof(grpc_slice));
54
- memcpy(sb->slices, sb->inlined, sb->count * sizeof(grpc_slice));
60
+ GPR_ASSERT(sb->capacity > slice_count);
61
+ if (sb->base_slices == sb->inlined) {
62
+ sb->base_slices = gpr_malloc(sb->capacity * sizeof(grpc_slice));
63
+ memcpy(sb->base_slices, sb->inlined, slice_count * sizeof(grpc_slice));
55
64
  } else {
56
- sb->slices = gpr_realloc(sb->slices, sb->capacity * sizeof(grpc_slice));
65
+ sb->base_slices =
66
+ gpr_realloc(sb->base_slices, sb->capacity * sizeof(grpc_slice));
57
67
  }
68
+
69
+ sb->slices = sb->base_slices + slice_offset;
58
70
  }
59
71
  }
60
72
 
@@ -62,14 +74,14 @@ void grpc_slice_buffer_init(grpc_slice_buffer *sb) {
62
74
  sb->count = 0;
63
75
  sb->length = 0;
64
76
  sb->capacity = GRPC_SLICE_BUFFER_INLINE_ELEMENTS;
65
- sb->slices = sb->inlined;
77
+ sb->base_slices = sb->slices = sb->inlined;
66
78
  }
67
79
 
68
80
  void grpc_slice_buffer_destroy_internal(grpc_exec_ctx *exec_ctx,
69
81
  grpc_slice_buffer *sb) {
70
82
  grpc_slice_buffer_reset_and_unref_internal(exec_ctx, sb);
71
- if (sb->slices != sb->inlined) {
72
- gpr_free(sb->slices);
83
+ if (sb->base_slices != sb->inlined) {
84
+ gpr_free(sb->base_slices);
73
85
  }
74
86
  }
75
87
 
@@ -166,7 +178,6 @@ void grpc_slice_buffer_pop(grpc_slice_buffer *sb) {
166
178
  void grpc_slice_buffer_reset_and_unref_internal(grpc_exec_ctx *exec_ctx,
167
179
  grpc_slice_buffer *sb) {
168
180
  size_t i;
169
-
170
181
  for (i = 0; i < sb->count; i++) {
171
182
  grpc_slice_unref_internal(exec_ctx, sb->slices[i]);
172
183
  }
@@ -182,32 +193,45 @@ void grpc_slice_buffer_reset_and_unref(grpc_slice_buffer *sb) {
182
193
  }
183
194
 
184
195
  void grpc_slice_buffer_swap(grpc_slice_buffer *a, grpc_slice_buffer *b) {
185
- GPR_SWAP(size_t, a->count, b->count);
186
- GPR_SWAP(size_t, a->capacity, b->capacity);
187
- GPR_SWAP(size_t, a->length, b->length);
196
+ size_t a_offset = (size_t)(a->slices - a->base_slices);
197
+ size_t b_offset = (size_t)(b->slices - b->base_slices);
188
198
 
189
- if (a->slices == a->inlined) {
190
- if (b->slices == b->inlined) {
199
+ size_t a_count = a->count + a_offset;
200
+ size_t b_count = b->count + b_offset;
201
+
202
+ if (a->base_slices == a->inlined) {
203
+ if (b->base_slices == b->inlined) {
191
204
  /* swap contents of inlined buffer */
192
205
  grpc_slice temp[GRPC_SLICE_BUFFER_INLINE_ELEMENTS];
193
- memcpy(temp, a->slices, b->count * sizeof(grpc_slice));
194
- memcpy(a->slices, b->slices, a->count * sizeof(grpc_slice));
195
- memcpy(b->slices, temp, b->count * sizeof(grpc_slice));
206
+ memcpy(temp, a->base_slices, a_count * sizeof(grpc_slice));
207
+ memcpy(a->base_slices, b->base_slices, b_count * sizeof(grpc_slice));
208
+ memcpy(b->base_slices, temp, a_count * sizeof(grpc_slice));
196
209
  } else {
197
210
  /* a is inlined, b is not - copy a inlined into b, fix pointers */
198
- a->slices = b->slices;
199
- b->slices = b->inlined;
200
- memcpy(b->slices, a->inlined, b->count * sizeof(grpc_slice));
211
+ a->base_slices = b->base_slices;
212
+ b->base_slices = b->inlined;
213
+ memcpy(b->base_slices, a->inlined, a_count * sizeof(grpc_slice));
201
214
  }
202
- } else if (b->slices == b->inlined) {
215
+ } else if (b->base_slices == b->inlined) {
203
216
  /* b is inlined, a is not - copy b inlined int a, fix pointers */
204
- b->slices = a->slices;
205
- a->slices = a->inlined;
206
- memcpy(a->slices, b->inlined, a->count * sizeof(grpc_slice));
217
+ b->base_slices = a->base_slices;
218
+ a->base_slices = a->inlined;
219
+ memcpy(a->base_slices, b->inlined, b_count * sizeof(grpc_slice));
207
220
  } else {
208
221
  /* no inlining: easy swap */
209
- GPR_SWAP(grpc_slice *, a->slices, b->slices);
222
+ GPR_SWAP(grpc_slice *, a->base_slices, b->base_slices);
210
223
  }
224
+
225
+ /* Update the slices pointers (cannot do a GPR_SWAP on slices fields here).
226
+ * Also note that since the base_slices pointers are already swapped we need
227
+ * use 'b_offset' for 'a->base_slices' and vice versa */
228
+ a->slices = a->base_slices + b_offset;
229
+ b->slices = b->base_slices + a_offset;
230
+
231
+ /* base_slices and slices fields are correctly set. Swap all other fields */
232
+ GPR_SWAP(size_t, a->count, b->count);
233
+ GPR_SWAP(size_t, a->capacity, b->capacity);
234
+ GPR_SWAP(size_t, a->length, b->length);
211
235
  }
212
236
 
213
237
  void grpc_slice_buffer_move_into(grpc_slice_buffer *src,
@@ -229,7 +253,6 @@ void grpc_slice_buffer_move_into(grpc_slice_buffer *src,
229
253
 
230
254
  void grpc_slice_buffer_move_first(grpc_slice_buffer *src, size_t n,
231
255
  grpc_slice_buffer *dst) {
232
- size_t src_idx;
233
256
  size_t output_len = dst->length + n;
234
257
  size_t new_input_len = src->length - n;
235
258
  GPR_ASSERT(src->length >= n);
@@ -237,34 +260,55 @@ void grpc_slice_buffer_move_first(grpc_slice_buffer *src, size_t n,
237
260
  grpc_slice_buffer_move_into(src, dst);
238
261
  return;
239
262
  }
240
- src_idx = 0;
241
- while (src_idx < src->capacity) {
242
- grpc_slice slice = src->slices[src_idx];
263
+
264
+ while (src->count > 0) {
265
+ grpc_slice slice = grpc_slice_buffer_take_first(src);
243
266
  size_t slice_len = GRPC_SLICE_LENGTH(slice);
244
267
  if (n > slice_len) {
245
268
  grpc_slice_buffer_add(dst, slice);
246
269
  n -= slice_len;
247
- src_idx++;
248
270
  } else if (n == slice_len) {
249
271
  grpc_slice_buffer_add(dst, slice);
250
- src_idx++;
251
272
  break;
252
273
  } else { /* n < slice_len */
253
- src->slices[src_idx] = grpc_slice_split_tail(&slice, n);
274
+ grpc_slice_buffer_undo_take_first(src, grpc_slice_split_tail(&slice, n));
254
275
  GPR_ASSERT(GRPC_SLICE_LENGTH(slice) == n);
255
- GPR_ASSERT(GRPC_SLICE_LENGTH(src->slices[src_idx]) == slice_len - n);
256
276
  grpc_slice_buffer_add(dst, slice);
257
277
  break;
258
278
  }
259
279
  }
260
280
  GPR_ASSERT(dst->length == output_len);
261
- memmove(src->slices, src->slices + src_idx,
262
- sizeof(grpc_slice) * (src->count - src_idx));
263
- src->count -= src_idx;
264
- src->length = new_input_len;
281
+ GPR_ASSERT(src->length == new_input_len);
265
282
  GPR_ASSERT(src->count > 0);
266
283
  }
267
284
 
285
+ void grpc_slice_buffer_move_first_into_buffer(grpc_exec_ctx *exec_ctx,
286
+ grpc_slice_buffer *src, size_t n,
287
+ void *dst) {
288
+ char *dstp = dst;
289
+ GPR_ASSERT(src->length >= n);
290
+
291
+ while (n > 0) {
292
+ grpc_slice slice = grpc_slice_buffer_take_first(src);
293
+ size_t slice_len = GRPC_SLICE_LENGTH(slice);
294
+ if (slice_len > n) {
295
+ memcpy(dstp, GRPC_SLICE_START_PTR(slice), n);
296
+ grpc_slice_buffer_undo_take_first(
297
+ src, grpc_slice_sub_no_ref(slice, n, slice_len));
298
+ n = 0;
299
+ } else if (slice_len == n) {
300
+ memcpy(dstp, GRPC_SLICE_START_PTR(slice), n);
301
+ grpc_slice_unref_internal(exec_ctx, slice);
302
+ n = 0;
303
+ } else {
304
+ memcpy(dstp, GRPC_SLICE_START_PTR(slice), slice_len);
305
+ dstp += slice_len;
306
+ n -= slice_len;
307
+ grpc_slice_unref_internal(exec_ctx, slice);
308
+ }
309
+ }
310
+ }
311
+
268
312
  void grpc_slice_buffer_trim_end(grpc_slice_buffer *sb, size_t n,
269
313
  grpc_slice_buffer *garbage) {
270
314
  GPR_ASSERT(n <= sb->length);
@@ -293,8 +337,17 @@ grpc_slice grpc_slice_buffer_take_first(grpc_slice_buffer *sb) {
293
337
  grpc_slice slice;
294
338
  GPR_ASSERT(sb->count > 0);
295
339
  slice = sb->slices[0];
296
- memmove(&sb->slices[0], &sb->slices[1], (sb->count - 1) * sizeof(grpc_slice));
340
+ sb->slices++;
297
341
  sb->count--;
298
342
  sb->length -= GRPC_SLICE_LENGTH(slice);
343
+
299
344
  return slice;
300
345
  }
346
+
347
+ void grpc_slice_buffer_undo_take_first(grpc_slice_buffer *sb,
348
+ grpc_slice slice) {
349
+ sb->slices--;
350
+ sb->slices[0] = slice;
351
+ sb->count++;
352
+ sb->length += GRPC_SLICE_LENGTH(slice);
353
+ }