| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2015 gRPC authors. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <grpc/support/port_platform.h> |
| 20 |
|
| 21 |
#include <stddef.h> |
| 22 |
|
| 23 |
#include <grpc/byte_buffer.h> |
| 24 |
#include <grpc/impl/codegen/compression_types.h> |
| 25 |
#include <grpc/impl/codegen/grpc_types.h> |
| 26 |
#include <grpc/slice.h> |
| 27 |
#include <grpc/slice_buffer.h> |
| 28 |
#include <grpc/support/alloc.h> |
| 29 |
|
| 30 |
#include "src/core/lib/iomgr/exec_ctx.h" |
| 31 |
#include "src/core/lib/slice/slice.h" |
| 32 |
|
| 33 |
grpc_byte_buffer* grpc_raw_byte_buffer_create(grpc_slice* slices, |
| 34 |
size_t nslices) { |
| 35 |
return grpc_raw_compressed_byte_buffer_create(slices, nslices, |
| 36 |
GRPC_COMPRESS_NONE); |
| 37 |
} |
| 38 |
|
| 39 |
grpc_byte_buffer* grpc_raw_compressed_byte_buffer_create( |
| 40 |
grpc_slice* slices, size_t nslices, |
| 41 |
grpc_compression_algorithm compression) { |
| 42 |
size_t i; |
| 43 |
grpc_byte_buffer* bb = |
| 44 |
static_cast<grpc_byte_buffer*>(gpr_malloc(sizeof(grpc_byte_buffer))); |
| 45 |
bb->type = GRPC_BB_RAW; |
| 46 |
bb->data.raw.compression = compression; |
| 47 |
grpc_slice_buffer_init(&bb->data.raw.slice_buffer); |
| 48 |
for (i = 0; i < nslices; i++) { |
| 49 |
grpc_core::CSliceRef(slices[i]); |
| 50 |
grpc_slice_buffer_add(&bb->data.raw.slice_buffer, slices[i]); |
| 51 |
} |
| 52 |
return bb; |
| 53 |
} |
| 54 |
|
| 55 |
grpc_byte_buffer* grpc_raw_byte_buffer_from_reader( |
| 56 |
grpc_byte_buffer_reader* reader) { |
| 57 |
grpc_byte_buffer* bb = |
| 58 |
static_cast<grpc_byte_buffer*>(gpr_malloc(sizeof(grpc_byte_buffer))); |
| 59 |
grpc_slice slice; |
| 60 |
bb->type = GRPC_BB_RAW; |
| 61 |
bb->data.raw.compression = GRPC_COMPRESS_NONE; |
| 62 |
grpc_slice_buffer_init(&bb->data.raw.slice_buffer); |
| 63 |
|
| 64 |
while (grpc_byte_buffer_reader_next(reader, &slice)) { |
| 65 |
grpc_slice_buffer_add(&bb->data.raw.slice_buffer, slice); |
| 66 |
} |
| 67 |
return bb; |
| 68 |
} |
| 69 |
|
| 70 |
grpc_byte_buffer* grpc_byte_buffer_copy(grpc_byte_buffer* bb) { |
| 71 |
switch (bb->type) { |
| 72 |
case GRPC_BB_RAW: |
| 73 |
return grpc_raw_compressed_byte_buffer_create( |
| 74 |
bb->data.raw.slice_buffer.slices, bb->data.raw.slice_buffer.count, |
| 75 |
bb->data.raw.compression); |
| 76 |
} |
| 77 |
GPR_UNREACHABLE_CODE(return nullptr); |
| 78 |
} |
| 79 |
|
| 80 |
void grpc_byte_buffer_destroy(grpc_byte_buffer* bb) { |
| 81 |
if (!bb) return; |
| 82 |
grpc_core::ExecCtx exec_ctx; |
| 83 |
switch (bb->type) { |
| 84 |
case GRPC_BB_RAW: |
| 85 |
grpc_slice_buffer_destroy(&bb->data.raw.slice_buffer); |
| 86 |
break; |
| 87 |
} |
| 88 |
gpr_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 |
} |