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 #include <grpc/byte_buffer.h> |
| 35 #include <grpc/support/alloc.h> |
| 36 #include <grpc/support/log.h> |
| 37 |
| 38 grpc_byte_buffer *grpc_raw_byte_buffer_create(gpr_slice *slices, |
| 39 size_t nslices) { |
| 40 return grpc_raw_compressed_byte_buffer_create(slices, nslices, |
| 41 GRPC_COMPRESS_NONE); |
| 42 } |
| 43 |
| 44 grpc_byte_buffer *grpc_raw_compressed_byte_buffer_create( |
| 45 gpr_slice *slices, size_t nslices, grpc_compression_algorithm compression) { |
| 46 size_t i; |
| 47 grpc_byte_buffer *bb = malloc(sizeof(grpc_byte_buffer)); |
| 48 bb->type = GRPC_BB_RAW; |
| 49 bb->data.raw.compression = compression; |
| 50 gpr_slice_buffer_init(&bb->data.raw.slice_buffer); |
| 51 for (i = 0; i < nslices; i++) { |
| 52 gpr_slice_ref(slices[i]); |
| 53 gpr_slice_buffer_add(&bb->data.raw.slice_buffer, slices[i]); |
| 54 } |
| 55 return bb; |
| 56 } |
| 57 |
| 58 grpc_byte_buffer *grpc_raw_byte_buffer_from_reader( |
| 59 grpc_byte_buffer_reader *reader) { |
| 60 grpc_byte_buffer *bb = malloc(sizeof(grpc_byte_buffer)); |
| 61 gpr_slice slice; |
| 62 bb->type = GRPC_BB_RAW; |
| 63 bb->data.raw.compression = GRPC_COMPRESS_NONE; |
| 64 gpr_slice_buffer_init(&bb->data.raw.slice_buffer); |
| 65 |
| 66 while (grpc_byte_buffer_reader_next(reader, &slice)) { |
| 67 gpr_slice_buffer_add(&bb->data.raw.slice_buffer, slice); |
| 68 } |
| 69 return bb; |
| 70 } |
| 71 |
| 72 grpc_byte_buffer *grpc_byte_buffer_copy(grpc_byte_buffer *bb) { |
| 73 switch (bb->type) { |
| 74 case GRPC_BB_RAW: |
| 75 return grpc_raw_byte_buffer_create(bb->data.raw.slice_buffer.slices, |
| 76 bb->data.raw.slice_buffer.count); |
| 77 } |
| 78 GPR_UNREACHABLE_CODE(return NULL); |
| 79 } |
| 80 |
| 81 void grpc_byte_buffer_destroy(grpc_byte_buffer *bb) { |
| 82 if (!bb) return; |
| 83 switch (bb->type) { |
| 84 case GRPC_BB_RAW: |
| 85 gpr_slice_buffer_destroy(&bb->data.raw.slice_buffer); |
| 86 break; |
| 87 } |
| 88 free(bb); |
| 89 } |
| 90 |
| 91 size_t grpc_byte_buffer_length(grpc_byte_buffer *bb) { |
| 92 switch (bb->type) { |
| 93 case GRPC_BB_RAW: |
| 94 return bb->data.raw.slice_buffer.length; |
| 95 } |
| 96 GPR_UNREACHABLE_CODE(return 0); |
| 97 } |
OLD | NEW |