slice_splitter.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 "test/core/util/slice_splitter.h"
  19. #include <string.h>
  20. #include <algorithm>
  21. #include <grpc/support/alloc.h>
  22. #include "src/core/lib/gpr/useful.h"
  23. const char* grpc_slice_split_mode_name(grpc_slice_split_mode mode) {
  24. switch (mode) {
  25. case GRPC_SLICE_SPLIT_IDENTITY:
  26. return "identity";
  27. case GRPC_SLICE_SPLIT_MERGE_ALL:
  28. return "merge_all";
  29. case GRPC_SLICE_SPLIT_ONE_BYTE:
  30. return "one_byte";
  31. }
  32. return "error";
  33. }
  34. void grpc_split_slices(grpc_slice_split_mode mode, grpc_slice* src_slices,
  35. size_t src_slice_count, grpc_slice** dst_slices,
  36. size_t* dst_slice_count) {
  37. size_t i, j;
  38. size_t length;
  39. switch (mode) {
  40. case GRPC_SLICE_SPLIT_IDENTITY:
  41. *dst_slice_count = src_slice_count;
  42. *dst_slices = static_cast<grpc_slice*>(
  43. gpr_malloc(sizeof(grpc_slice) * src_slice_count));
  44. for (i = 0; i < src_slice_count; i++) {
  45. (*dst_slices)[i] = src_slices[i];
  46. grpc_slice_ref((*dst_slices)[i]);
  47. }
  48. break;
  49. case GRPC_SLICE_SPLIT_MERGE_ALL:
  50. *dst_slice_count = 1;
  51. length = 0;
  52. for (i = 0; i < src_slice_count; i++) {
  53. length += GRPC_SLICE_LENGTH(src_slices[i]);
  54. }
  55. *dst_slices = static_cast<grpc_slice*>(gpr_malloc(sizeof(grpc_slice)));
  56. **dst_slices = grpc_slice_malloc(length);
  57. length = 0;
  58. for (i = 0; i < src_slice_count; i++) {
  59. memcpy(GRPC_SLICE_START_PTR(**dst_slices) + length,
  60. GRPC_SLICE_START_PTR(src_slices[i]),
  61. GRPC_SLICE_LENGTH(src_slices[i]));
  62. length += GRPC_SLICE_LENGTH(src_slices[i]);
  63. }
  64. break;
  65. case GRPC_SLICE_SPLIT_ONE_BYTE:
  66. length = 0;
  67. for (i = 0; i < src_slice_count; i++) {
  68. length += GRPC_SLICE_LENGTH(src_slices[i]);
  69. }
  70. *dst_slice_count = length;
  71. *dst_slices =
  72. static_cast<grpc_slice*>(gpr_malloc(sizeof(grpc_slice) * length));
  73. length = 0;
  74. for (i = 0; i < src_slice_count; i++) {
  75. for (j = 0; j < GRPC_SLICE_LENGTH(src_slices[i]); j++) {
  76. (*dst_slices)[length] = grpc_slice_sub(src_slices[i], j, j + 1);
  77. length++;
  78. }
  79. }
  80. break;
  81. }
  82. }
  83. void grpc_split_slices_to_buffer(grpc_slice_split_mode mode,
  84. grpc_slice* src_slices, size_t src_slice_count,
  85. grpc_slice_buffer* dst) {
  86. grpc_slice* slices;
  87. size_t nslices;
  88. size_t i;
  89. grpc_split_slices(mode, src_slices, src_slice_count, &slices, &nslices);
  90. for (i = 0; i < nslices; i++) {
  91. /* add indexed to avoid re-merging split slices */
  92. grpc_slice_buffer_add_indexed(dst, slices[i]);
  93. }
  94. gpr_free(slices);
  95. }
  96. void grpc_split_slice_buffer(grpc_slice_split_mode mode, grpc_slice_buffer* src,
  97. grpc_slice_buffer* dst) {
  98. grpc_split_slices_to_buffer(mode, src->slices, src->count, dst);
  99. }
  100. grpc_slice grpc_slice_merge(grpc_slice* slices, size_t nslices) {
  101. uint8_t* out = nullptr;
  102. size_t length = 0;
  103. size_t capacity = 0;
  104. size_t i;
  105. for (i = 0; i < nslices; i++) {
  106. if (GRPC_SLICE_LENGTH(slices[i]) + length > capacity) {
  107. capacity = std::max(capacity * 2, GRPC_SLICE_LENGTH(slices[i]) + length);
  108. out = static_cast<uint8_t*>(gpr_realloc(out, capacity));
  109. }
  110. memcpy(out + length, GRPC_SLICE_START_PTR(slices[i]),
  111. GRPC_SLICE_LENGTH(slices[i]));
  112. length += GRPC_SLICE_LENGTH(slices[i]);
  113. }
  114. return grpc_slice_new(out, length, gpr_free);
  115. }