OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015, Google Inc. |
| 4 * All rights reserved. |
| 5 * |
| 6 * Redistribution and use in source and binary forms, with or without |
| 7 * modification, are permitted provided that the following conditions are |
| 8 * met: |
| 9 * |
| 10 * * Redistributions of source code must retain the above copyright |
| 11 * notice, this list of conditions and the following disclaimer. |
| 12 * * Redistributions in binary form must reproduce the above |
| 13 * copyright notice, this list of conditions and the following disclaimer |
| 14 * in the documentation and/or other materials provided with the |
| 15 * distribution. |
| 16 * * Neither the name of Google Inc. nor the names of its |
| 17 * contributors may be used to endorse or promote products derived from |
| 18 * this software without specific prior written permission. |
| 19 * |
| 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 * |
| 32 */ |
| 33 |
| 34 #import "NSData+GRPC.h" |
| 35 |
| 36 #include <grpc/byte_buffer.h> |
| 37 #include <grpc/byte_buffer_reader.h> |
| 38 #include <string.h> |
| 39 |
| 40 // TODO(jcanizales): Move these two incantations to the C library. |
| 41 |
| 42 static void CopyByteBufferToCharArray(grpc_byte_buffer *buffer, char *array) { |
| 43 size_t offset = 0; |
| 44 grpc_byte_buffer_reader reader; |
| 45 grpc_byte_buffer_reader_init(&reader, buffer); |
| 46 gpr_slice next; |
| 47 while (grpc_byte_buffer_reader_next(&reader, &next) != 0){ |
| 48 memcpy(array + offset, GPR_SLICE_START_PTR(next), |
| 49 (size_t)GPR_SLICE_LENGTH(next)); |
| 50 offset += GPR_SLICE_LENGTH(next); |
| 51 gpr_slice_unref(next); |
| 52 } |
| 53 } |
| 54 |
| 55 static grpc_byte_buffer *CopyCharArrayToNewByteBuffer(const char *array, |
| 56 size_t length) { |
| 57 gpr_slice slice = gpr_slice_from_copied_buffer(array, length); |
| 58 grpc_byte_buffer *buffer = grpc_raw_byte_buffer_create(&slice, 1); |
| 59 gpr_slice_unref(slice); |
| 60 return buffer; |
| 61 } |
| 62 |
| 63 @implementation NSData (GRPC) |
| 64 + (instancetype)grpc_dataWithByteBuffer:(grpc_byte_buffer *)buffer { |
| 65 if (buffer == NULL) { |
| 66 return nil; |
| 67 } |
| 68 NSUInteger length = grpc_byte_buffer_length(buffer); |
| 69 char *array = malloc(length * sizeof(*array)); |
| 70 if (!array) { |
| 71 // TODO(jcanizales): grpc_byte_buffer is reference-counted, so we can |
| 72 // prevent this memory problem by implementing a subclass of NSData |
| 73 // that wraps the grpc_byte_buffer. Then enumerateByteRangesUsingBlock: |
| 74 // can be implemented using a grpc_byte_buffer_reader. |
| 75 return nil; |
| 76 } |
| 77 CopyByteBufferToCharArray(buffer, array); |
| 78 return [self dataWithBytesNoCopy:array length:length freeWhenDone:YES]; |
| 79 } |
| 80 |
| 81 - (grpc_byte_buffer *)grpc_byteBuffer { |
| 82 // Some implementations of NSData, as well as grpc_byte_buffer, support O(1) |
| 83 // appending of byte arrays by not using internally a single contiguous memory |
| 84 // block for representation. |
| 85 // The following implementation is thus not optimal, sometimes requiring two |
| 86 // copies (one by self.bytes and another by gpr_slice_from_copied_buffer). |
| 87 // If it turns out to be an issue, we can use enumerateByteRangesUsingblock: |
| 88 // to create an array of gpr_slice objects to pass to grpc_raw_byte_buffer_cre
ate. |
| 89 // That would make it do exactly one copy, always. |
| 90 return CopyCharArrayToNewByteBuffer((const char *)self.bytes, (size_t)self.len
gth); |
| 91 } |
| 92 @end |
OLD | NEW |