slice.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #ifndef GRPC_IMPL_CODEGEN_SLICE_H
  19. #define GRPC_IMPL_CODEGEN_SLICE_H
  20. // IWYU pragma: private, include <grpc/slice.h>
  21. #include <grpc/impl/codegen/port_platform.h>
  22. #include <stddef.h>
  23. #include <grpc/impl/codegen/gpr_slice.h>
  24. typedef struct grpc_slice grpc_slice;
  25. /** Slice API
  26. A slice represents a contiguous reference counted array of bytes.
  27. It is cheap to take references to a slice, and it is cheap to create a
  28. slice pointing to a subset of another slice.
  29. The data-structure for slices is exposed here to allow non-gpr code to
  30. build slices from whatever data they have available.
  31. When defining interfaces that handle slices, care should be taken to define
  32. reference ownership semantics (who should call unref?) and mutability
  33. constraints (is the callee allowed to modify the slice?) */
  34. /* Inlined half of grpc_slice is allowed to expand the size of the overall type
  35. by this many bytes */
  36. #define GRPC_SLICE_INLINE_EXTRA_SIZE sizeof(void*)
  37. #define GRPC_SLICE_INLINED_SIZE \
  38. (sizeof(size_t) + sizeof(uint8_t*) - 1 + GRPC_SLICE_INLINE_EXTRA_SIZE)
  39. struct grpc_slice_refcount;
  40. /** A grpc_slice s, if initialized, represents the byte range
  41. s.bytes[0..s.length-1].
  42. It can have an associated ref count which has a destruction routine to be run
  43. when the ref count reaches zero (see grpc_slice_new() and grp_slice_unref()).
  44. Multiple grpc_slice values may share a ref count.
  45. If the slice does not have a refcount, it represents an inlined small piece
  46. of data that is copied by value.
  47. As a special case, a slice can be given refcount == uintptr_t(1), meaning
  48. that the slice represents external data that is not refcounted. */
  49. struct grpc_slice {
  50. struct grpc_slice_refcount* refcount;
  51. union grpc_slice_data {
  52. struct grpc_slice_refcounted {
  53. size_t length;
  54. uint8_t* bytes;
  55. } refcounted;
  56. struct grpc_slice_inlined {
  57. uint8_t length;
  58. uint8_t bytes[GRPC_SLICE_INLINED_SIZE];
  59. } inlined;
  60. } data;
  61. };
  62. #define GRPC_SLICE_BUFFER_INLINE_ELEMENTS 8
  63. /** Represents an expandable array of slices, to be interpreted as a
  64. single item. */
  65. typedef struct grpc_slice_buffer {
  66. /** This is for internal use only. External users (i.e any code outside grpc
  67. * core) MUST NOT use this field */
  68. grpc_slice* base_slices;
  69. /** slices in the array (Points to the first valid grpc_slice in the array) */
  70. grpc_slice* slices;
  71. /** the number of slices in the array */
  72. size_t count;
  73. /** the number of slices allocated in the array. External users (i.e any code
  74. * outside grpc core) MUST NOT use this field */
  75. size_t capacity;
  76. /** the combined length of all slices in the array */
  77. size_t length;
  78. /** inlined elements to avoid allocations */
  79. grpc_slice inlined[GRPC_SLICE_BUFFER_INLINE_ELEMENTS];
  80. } grpc_slice_buffer;
  81. #define GRPC_SLICE_START_PTR(slice) \
  82. ((slice).refcount ? (slice).data.refcounted.bytes \
  83. : (slice).data.inlined.bytes)
  84. #define GRPC_SLICE_LENGTH(slice) \
  85. ((slice).refcount ? (slice).data.refcounted.length \
  86. : (slice).data.inlined.length)
  87. #define GRPC_SLICE_SET_LENGTH(slice, newlen) \
  88. ((slice).refcount ? ((slice).data.refcounted.length = (size_t)(newlen)) \
  89. : ((slice).data.inlined.length = (uint8_t)(newlen)))
  90. #define GRPC_SLICE_END_PTR(slice) \
  91. GRPC_SLICE_START_PTR(slice) + GRPC_SLICE_LENGTH(slice)
  92. #define GRPC_SLICE_IS_EMPTY(slice) (GRPC_SLICE_LENGTH(slice) == 0)
  93. #ifdef GRPC_ALLOW_GPR_SLICE_FUNCTIONS
  94. /* Duplicate GPR_* definitions */
  95. #define GPR_SLICE_START_PTR(slice) \
  96. ((slice).refcount ? (slice).data.refcounted.bytes \
  97. : (slice).data.inlined.bytes)
  98. #define GPR_SLICE_LENGTH(slice) \
  99. ((slice).refcount ? (slice).data.refcounted.length \
  100. : (slice).data.inlined.length)
  101. #define GPR_SLICE_SET_LENGTH(slice, newlen) \
  102. ((slice).refcount ? ((slice).data.refcounted.length = (size_t)(newlen)) \
  103. : ((slice).data.inlined.length = (uint8_t)(newlen)))
  104. #define GPR_SLICE_END_PTR(slice) \
  105. GRPC_SLICE_START_PTR(slice) + GRPC_SLICE_LENGTH(slice)
  106. #define GPR_SLICE_IS_EMPTY(slice) (GRPC_SLICE_LENGTH(slice) == 0)
  107. #endif /* GRPC_ALLOW_GPR_SLICE_FUNCTIONS */
  108. #endif /* GRPC_IMPL_CODEGEN_SLICE_H */