From 180b32380c30c2562dca1e158555f024b076f13c Mon Sep 17 00:00:00 2001
From: Muxi Yan <mxyan@google.com>
Date: Fri, 13 Jan 2017 17:42:53 -0800
Subject: [PATCH] Use grpc_slice at core boudary

---
 .../GRPCClient/private/GRPCChannel.m           |  2 +-
 .../GRPCClient/private/GRPCWrappedCall.m       |  7 +++----
 .../GRPCClient/private/NSDictionary+GRPC.m     | 18 +++++++++---------
 3 files changed, 13 insertions(+), 14 deletions(-)

diff --git a/src/objective-c/GRPCClient/private/GRPCChannel.m b/src/objective-c/GRPCClient/private/GRPCChannel.m
index e49aceefe1..b4a7c247ef 100644
--- a/src/objective-c/GRPCClient/private/GRPCChannel.m
+++ b/src/objective-c/GRPCClient/private/GRPCChannel.m
@@ -200,7 +200,7 @@ static grpc_channel_args *BuildChannelArgs(NSDictionary *dictionary) {
   return grpc_channel_create_call(_unmanagedChannel,
                                   NULL, GRPC_PROPAGATE_DEFAULTS,
                                   queue.unmanagedQueue,
-                                  path.UTF8String,
+                                  grpc_slice_from_copied_string(path.UTF8String),
                                   NULL, // Passing NULL for host
                                   gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
 }
diff --git a/src/objective-c/GRPCClient/private/GRPCWrappedCall.m b/src/objective-c/GRPCClient/private/GRPCWrappedCall.m
index 38fcae0299..7e73495043 100644
--- a/src/objective-c/GRPCClient/private/GRPCWrappedCall.m
+++ b/src/objective-c/GRPCClient/private/GRPCWrappedCall.m
@@ -194,7 +194,7 @@
 
 @implementation GRPCOpRecvStatus{
   grpc_status_code _statusCode;
-  char *_details;
+  grpc_slice _details;
   size_t _detailsCapacity;
   grpc_metadata_array _trailers;
 }
@@ -208,7 +208,6 @@
     _op.op = GRPC_OP_RECV_STATUS_ON_CLIENT;
     _op.data.recv_status_on_client.status = &_statusCode;
     _op.data.recv_status_on_client.status_details = &_details;
-    _op.data.recv_status_on_client.status_details_capacity = &_detailsCapacity;
     grpc_metadata_array_init(&_trailers);
     _op.data.recv_status_on_client.trailing_metadata = &_trailers;
     if (handler) {
@@ -217,7 +216,7 @@
       _handler = ^{
         __strong typeof(self) strongSelf = weakSelf;
         NSError *error = [NSError grpc_errorFromStatusCode:strongSelf->_statusCode
-                                                   details:strongSelf->_details];
+                                                   details:(char*)GRPC_SLICE_START_PTR(strongSelf->_details)];
         NSDictionary *trailers = [NSDictionary
                                   grpc_dictionaryFromMetadataArray:strongSelf->_trailers];
         handler(error, trailers);
@@ -229,7 +228,7 @@
 
 - (void)dealloc {
   grpc_metadata_array_destroy(&_trailers);
-  gpr_free(_details);
+  grpc_slice_unref(_details);
 }
 
 @end
diff --git a/src/objective-c/GRPCClient/private/NSDictionary+GRPC.m b/src/objective-c/GRPCClient/private/NSDictionary+GRPC.m
index 7477da7619..f834fef5e9 100644
--- a/src/objective-c/GRPCClient/private/NSDictionary+GRPC.m
+++ b/src/objective-c/GRPCClient/private/NSDictionary+GRPC.m
@@ -47,12 +47,12 @@
 @implementation NSData (GRPCMetadata)
 + (instancetype)grpc_dataFromMetadataValue:(grpc_metadata *)metadata {
   // TODO(jcanizales): Should we use a non-copy constructor?
-  return [self dataWithBytes:metadata->value length:metadata->value_length];
+  return [self dataWithBytes:GRPC_SLICE_START_PTR(metadata->value)
+                      length:GRPC_SLICE_LENGTH(metadata->value)];
 }
 
 - (void)grpc_initMetadata:(grpc_metadata *)metadata {
-  metadata->value = self.bytes;
-  metadata->value_length = self.length;
+  metadata->value = grpc_slice_from_copied_buffer(self.bytes, self.length);
 }
 @end
 
@@ -67,15 +67,14 @@
 
 @implementation NSString (GRPCMetadata)
 + (instancetype)grpc_stringFromMetadataValue:(grpc_metadata *)metadata {
-  return [[self alloc] initWithBytes:metadata->value
-                              length:metadata->value_length
+  return [[self alloc] initWithBytes:GRPC_SLICE_START_PTR(metadata->value)
+                              length:GRPC_SLICE_LENGTH(metadata->value)
                             encoding:NSASCIIStringEncoding];
 }
 
 // Precondition: This object contains only ASCII characters.
 - (void)grpc_initMetadata:(grpc_metadata *)metadata {
-  metadata->value = self.UTF8String;
-  metadata->value_length = self.length;
+  metadata->value = grpc_slice_from_copied_string(self.UTF8String);
 }
 @end
 
@@ -89,7 +88,8 @@
 + (instancetype)grpc_dictionaryFromMetadata:(grpc_metadata *)entries count:(size_t)count {
   NSMutableDictionary *metadata = [NSMutableDictionary dictionaryWithCapacity:count];
   for (grpc_metadata *entry = entries; entry < entries + count; entry++) {
-    NSString *name = [NSString stringWithCString:entry->key encoding:NSASCIIStringEncoding];
+    NSString *name = [NSString stringWithCString:(char*)GRPC_SLICE_START_PTR(entry->key)
+                                        encoding:NSASCIIStringEncoding];
     if (!name || metadata[name]) {
       // Log if name is nil?
       continue;
@@ -112,7 +112,7 @@
   grpc_metadata *current = metadata;
   for (NSString* key in self) {
     id value = self[key];
-    current->key = key.UTF8String;
+    current->key = grpc_slice_from_copied_string(key.UTF8String);
     if ([value respondsToSelector:@selector(grpc_initMetadata:)]) {
       [value grpc_initMetadata:current];
     } else {
-- 
GitLab