byte_buffer.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #include <grpc/byte_buffer.h>
  19. #include <grpc/support/alloc.h>
  20. #include <grpc/support/log.h>
  21. #include "src/core/lib/slice/slice_internal.h"
  22. grpc_byte_buffer *grpc_raw_byte_buffer_create(grpc_slice *slices,
  23. size_t nslices) {
  24. return grpc_raw_compressed_byte_buffer_create(slices, nslices,
  25. GRPC_COMPRESS_NONE);
  26. }
  27. grpc_byte_buffer *grpc_raw_compressed_byte_buffer_create(
  28. grpc_slice *slices, size_t nslices,
  29. grpc_compression_algorithm compression) {
  30. size_t i;
  31. grpc_byte_buffer *bb =
  32. (grpc_byte_buffer *)gpr_malloc(sizeof(grpc_byte_buffer));
  33. bb->type = GRPC_BB_RAW;
  34. bb->data.raw.compression = compression;
  35. grpc_slice_buffer_init(&bb->data.raw.slice_buffer);
  36. for (i = 0; i < nslices; i++) {
  37. grpc_slice_ref_internal(slices[i]);
  38. grpc_slice_buffer_add(&bb->data.raw.slice_buffer, slices[i]);
  39. }
  40. return bb;
  41. }
  42. grpc_byte_buffer *grpc_raw_byte_buffer_from_reader(
  43. grpc_byte_buffer_reader *reader) {
  44. grpc_byte_buffer *bb =
  45. (grpc_byte_buffer *)gpr_malloc(sizeof(grpc_byte_buffer));
  46. grpc_slice slice;
  47. bb->type = GRPC_BB_RAW;
  48. bb->data.raw.compression = GRPC_COMPRESS_NONE;
  49. grpc_slice_buffer_init(&bb->data.raw.slice_buffer);
  50. while (grpc_byte_buffer_reader_next(reader, &slice)) {
  51. grpc_slice_buffer_add(&bb->data.raw.slice_buffer, slice);
  52. }
  53. return bb;
  54. }
  55. grpc_byte_buffer *grpc_byte_buffer_copy(grpc_byte_buffer *bb) {
  56. switch (bb->type) {
  57. case GRPC_BB_RAW:
  58. return grpc_raw_compressed_byte_buffer_create(
  59. bb->data.raw.slice_buffer.slices, bb->data.raw.slice_buffer.count,
  60. bb->data.raw.compression);
  61. }
  62. GPR_UNREACHABLE_CODE(return NULL);
  63. }
  64. void grpc_byte_buffer_destroy(grpc_byte_buffer *bb) {
  65. if (!bb) return;
  66. grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  67. switch (bb->type) {
  68. case GRPC_BB_RAW:
  69. grpc_slice_buffer_destroy_internal(&exec_ctx, &bb->data.raw.slice_buffer);
  70. break;
  71. }
  72. gpr_free(bb);
  73. grpc_exec_ctx_finish(&exec_ctx);
  74. }
  75. size_t grpc_byte_buffer_length(grpc_byte_buffer *bb) {
  76. switch (bb->type) {
  77. case GRPC_BB_RAW:
  78. return bb->data.raw.slice_buffer.length;
  79. }
  80. GPR_UNREACHABLE_CODE(return 0);
  81. }