slice.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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_SLICE_H
  19. #define GRPC_SLICE_H
  20. #include <grpc/support/port_platform.h>
  21. #include <grpc/impl/codegen/slice.h> // IWYU pragma: export
  22. #include <grpc/support/sync.h>
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. /** Increment the refcount of s. Requires slice is initialized.
  27. Returns s. */
  28. GPRAPI grpc_slice grpc_slice_ref(grpc_slice s);
  29. /** Decrement the ref count of s. If the ref count of s reaches zero, all
  30. slices sharing the ref count are destroyed, and considered no longer
  31. initialized. If s is ultimately derived from a call to grpc_slice_new(start,
  32. len, dest) where dest!=NULL , then (*dest)(start) is called, else if s is
  33. ultimately derived from a call to grpc_slice_new_with_len(start, len, dest)
  34. where dest!=NULL , then (*dest)(start, len). Requires s initialized. */
  35. GPRAPI void grpc_slice_unref(grpc_slice s);
  36. /** Copy slice - create a new slice that contains the same data as s */
  37. GPRAPI grpc_slice grpc_slice_copy(grpc_slice s);
  38. /** Create a slice pointing at some data. Calls malloc to allocate a refcount
  39. for the object, and arranges that destroy will be called with the pointer
  40. passed in at destruction. */
  41. GPRAPI grpc_slice grpc_slice_new(void* p, size_t len, void (*destroy)(void*));
  42. /** Equivalent to grpc_slice_new, but with a separate pointer that is
  43. passed to the destroy function. This function can be useful when
  44. the data is part of a larger structure that must be destroyed when
  45. the data is no longer needed. */
  46. GPRAPI grpc_slice grpc_slice_new_with_user_data(void* p, size_t len,
  47. void (*destroy)(void*),
  48. void* user_data);
  49. /** Equivalent to grpc_slice_new, but with a two argument destroy function that
  50. also takes the slice length. */
  51. GPRAPI grpc_slice grpc_slice_new_with_len(void* p, size_t len,
  52. void (*destroy)(void*, size_t));
  53. /** Equivalent to grpc_slice_new(malloc(len), len, free), but saves one malloc()
  54. call.
  55. Aborts if malloc() fails. */
  56. GPRAPI grpc_slice grpc_slice_malloc(size_t length);
  57. GPRAPI grpc_slice grpc_slice_malloc_large(size_t length);
  58. #define GRPC_SLICE_MALLOC(len) grpc_slice_malloc(len)
  59. /** Intern a slice:
  60. The return value for two invocations of this function with the same sequence
  61. of bytes is a slice which points to the same memory. */
  62. GPRAPI grpc_slice grpc_slice_intern(grpc_slice slice);
  63. /** Create a slice by copying a string.
  64. Does not preserve null terminators.
  65. Equivalent to:
  66. size_t len = strlen(source);
  67. grpc_slice slice = grpc_slice_malloc(len);
  68. memcpy(slice->data, source, len); */
  69. GPRAPI grpc_slice grpc_slice_from_copied_string(const char* source);
  70. /** Create a slice by copying a buffer.
  71. Equivalent to:
  72. grpc_slice slice = grpc_slice_malloc(len);
  73. memcpy(slice->data, source, len); */
  74. GPRAPI grpc_slice grpc_slice_from_copied_buffer(const char* source, size_t len);
  75. /** Create a slice pointing to constant memory */
  76. GPRAPI grpc_slice grpc_slice_from_static_string(const char* source);
  77. /** Create a slice pointing to constant memory */
  78. GPRAPI grpc_slice grpc_slice_from_static_buffer(const void* source, size_t len);
  79. /** Return a result slice derived from s, which shares a ref count with \a s,
  80. where result.data==s.data+begin, and result.length==end-begin. The ref count
  81. of \a s is increased by one. Do not assign result back to \a s.
  82. Requires s initialized, begin <= end, begin <= s.length, and
  83. end <= source->length. */
  84. GPRAPI grpc_slice grpc_slice_sub(grpc_slice s, size_t begin, size_t end);
  85. /** The same as grpc_slice_sub, but without altering the ref count */
  86. GPRAPI grpc_slice grpc_slice_sub_no_ref(grpc_slice s, size_t begin, size_t end);
  87. /** Splits s into two: modifies s to be s[0:split], and returns a new slice,
  88. sharing a refcount with s, that contains s[split:s.length].
  89. Requires s initialized, split <= s.length */
  90. GPRAPI grpc_slice grpc_slice_split_tail(grpc_slice* s, size_t split);
  91. typedef enum {
  92. GRPC_SLICE_REF_TAIL = 1,
  93. GRPC_SLICE_REF_HEAD = 2,
  94. GRPC_SLICE_REF_BOTH = 1 + 2
  95. } grpc_slice_ref_whom;
  96. /** The same as grpc_slice_split_tail, but with an option to skip altering
  97. * refcounts (grpc_slice_split_tail_maybe_ref(..., true) is equivalent to
  98. * grpc_slice_split_tail(...)) */
  99. GPRAPI grpc_slice grpc_slice_split_tail_maybe_ref(grpc_slice* s, size_t split,
  100. grpc_slice_ref_whom ref_whom);
  101. /** Splits s into two: modifies s to be s[split:s.length], and returns a new
  102. slice, sharing a refcount with s, that contains s[0:split].
  103. Requires s initialized, split <= s.length */
  104. GPRAPI grpc_slice grpc_slice_split_head(grpc_slice* s, size_t split);
  105. GPRAPI grpc_slice grpc_empty_slice(void);
  106. GPRAPI uint32_t grpc_slice_default_hash_impl(grpc_slice s);
  107. GPRAPI int grpc_slice_default_eq_impl(grpc_slice a, grpc_slice b);
  108. GPRAPI int grpc_slice_eq(grpc_slice a, grpc_slice b);
  109. /** Returns <0 if a < b, ==0 if a == b, >0 if a > b
  110. The order is arbitrary, and is not guaranteed to be stable across different
  111. versions of the API. */
  112. GPRAPI int grpc_slice_cmp(grpc_slice a, grpc_slice b);
  113. GPRAPI int grpc_slice_str_cmp(grpc_slice a, const char* b);
  114. /** return non-zero if the first blen bytes of a are equal to b */
  115. GPRAPI int grpc_slice_buf_start_eq(grpc_slice a, const void* b, size_t blen);
  116. /** return the index of the last instance of \a c in \a s, or -1 if not found */
  117. GPRAPI int grpc_slice_rchr(grpc_slice s, char c);
  118. GPRAPI int grpc_slice_chr(grpc_slice s, char c);
  119. /** return the index of the first occurrence of \a needle in \a haystack, or -1
  120. if it's not found */
  121. GPRAPI int grpc_slice_slice(grpc_slice haystack, grpc_slice needle);
  122. GPRAPI uint32_t grpc_slice_hash(grpc_slice s);
  123. /** Do two slices point at the same memory, with the same length
  124. If a or b is inlined, actually compares data */
  125. GPRAPI int grpc_slice_is_equivalent(grpc_slice a, grpc_slice b);
  126. /** Return a slice pointing to newly allocated memory that has the same contents
  127. * as \a s */
  128. GPRAPI grpc_slice grpc_slice_dup(grpc_slice a);
  129. /** Return a copy of slice as a C string. Offers no protection against embedded
  130. NULL's. Returned string must be freed with gpr_free. */
  131. GPRAPI char* grpc_slice_to_c_string(grpc_slice s);
  132. #ifdef __cplusplus
  133. }
  134. #endif
  135. #endif /* GRPC_SLICE_H */