Line data Source code
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 7710028 : grpc_byte_buffer *grpc_raw_byte_buffer_create(gpr_slice *slices,
39 : size_t nslices) {
40 7710028 : return grpc_raw_compressed_byte_buffer_create(slices, nslices,
41 : GRPC_COMPRESS_NONE);
42 : }
43 :
44 7708546 : 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 7708546 : grpc_byte_buffer *bb = malloc(sizeof(grpc_byte_buffer));
48 7708546 : bb->type = GRPC_BB_RAW;
49 7708546 : bb->data.raw.compression = compression;
50 7708546 : gpr_slice_buffer_init(&bb->data.raw.slice_buffer);
51 11467639 : for (i = 0; i < nslices; i++) {
52 3760449 : gpr_slice_ref(slices[i]);
53 3760344 : gpr_slice_buffer_add(&bb->data.raw.slice_buffer, slices[i]);
54 : }
55 7707190 : return bb;
56 : }
57 :
58 901 : grpc_byte_buffer *grpc_raw_byte_buffer_from_reader(
59 : grpc_byte_buffer_reader *reader) {
60 901 : grpc_byte_buffer *bb = malloc(sizeof(grpc_byte_buffer));
61 : gpr_slice slice;
62 901 : bb->type = GRPC_BB_RAW;
63 901 : bb->data.raw.compression = GRPC_COMPRESS_NONE;
64 901 : gpr_slice_buffer_init(&bb->data.raw.slice_buffer);
65 :
66 3111 : while (grpc_byte_buffer_reader_next(reader, &slice)) {
67 1309 : gpr_slice_buffer_add(&bb->data.raw.slice_buffer, slice);
68 : }
69 901 : return bb;
70 : }
71 :
72 1 : grpc_byte_buffer *grpc_byte_buffer_copy(grpc_byte_buffer *bb) {
73 1 : switch (bb->type) {
74 : case GRPC_BB_RAW:
75 2 : return grpc_raw_byte_buffer_create(bb->data.raw.slice_buffer.slices,
76 : bb->data.raw.slice_buffer.count);
77 : }
78 0 : GPR_UNREACHABLE_CODE(return NULL);
79 0 : }
80 :
81 7709737 : void grpc_byte_buffer_destroy(grpc_byte_buffer *bb) {
82 15431513 : if (!bb) return;
83 7708082 : switch (bb->type) {
84 : case GRPC_BB_RAW:
85 7703814 : gpr_slice_buffer_destroy(&bb->data.raw.slice_buffer);
86 7715853 : break;
87 : }
88 7720121 : free(bb);
89 : }
90 :
91 367 : size_t grpc_byte_buffer_length(grpc_byte_buffer *bb) {
92 367 : switch (bb->type) {
93 : case GRPC_BB_RAW:
94 549 : return bb->data.raw.slice_buffer.length;
95 : }
96 0 : GPR_UNREACHABLE_CODE(return 0);
97 0 : }
|