slice.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. /** Create a slice by copying a string.
  60. Does not preserve null terminators.
  61. Equivalent to:
  62. size_t len = strlen(source);
  63. grpc_slice slice = grpc_slice_malloc(len);
  64. memcpy(slice->data, source, len); */
  65. GPRAPI grpc_slice grpc_slice_from_copied_string(const char* source);
  66. /** Create a slice by copying a buffer.
  67. Equivalent to:
  68. grpc_slice slice = grpc_slice_malloc(len);
  69. memcpy(slice->data, source, len); */
  70. GPRAPI grpc_slice grpc_slice_from_copied_buffer(const char* source, size_t len);
  71. /** Create a slice pointing to constant memory */
  72. GPRAPI grpc_slice grpc_slice_from_static_string(const char* source);
  73. /** Create a slice pointing to constant memory */
  74. GPRAPI grpc_slice grpc_slice_from_static_buffer(const void* source, size_t len);
  75. /** Return a result slice derived from s, which shares a ref count with \a s,
  76. where result.data==s.data+begin, and result.length==end-begin. The ref count
  77. of \a s is increased by one. Do not assign result back to \a s.
  78. Requires s initialized, begin <= end, begin <= s.length, and
  79. end <= source->length. */
  80. GPRAPI grpc_slice grpc_slice_sub(grpc_slice s, size_t begin, size_t end);
  81. /** The same as grpc_slice_sub, but without altering the ref count */
  82. GPRAPI grpc_slice grpc_slice_sub_no_ref(grpc_slice s, size_t begin, size_t end);
  83. /** Splits s into two: modifies s to be s[0:split], and returns a new slice,
  84. sharing a refcount with s, that contains s[split:s.length].
  85. Requires s initialized, split <= s.length */
  86. GPRAPI grpc_slice grpc_slice_split_tail(grpc_slice* s, size_t split);
  87. typedef enum {
  88. GRPC_SLICE_REF_TAIL = 1,
  89. GRPC_SLICE_REF_HEAD = 2,
  90. GRPC_SLICE_REF_BOTH = 1 + 2
  91. } grpc_slice_ref_whom;
  92. /** The same as grpc_slice_split_tail, but with an option to skip altering
  93. * refcounts (grpc_slice_split_tail_maybe_ref(..., true) is equivalent to
  94. * grpc_slice_split_tail(...)) */
  95. GPRAPI grpc_slice grpc_slice_split_tail_maybe_ref(grpc_slice* s, size_t split,
  96. grpc_slice_ref_whom ref_whom);
  97. /** Splits s into two: modifies s to be s[split:s.length], and returns a new
  98. slice, sharing a refcount with s, that contains s[0:split].
  99. Requires s initialized, split <= s.length */
  100. GPRAPI grpc_slice grpc_slice_split_head(grpc_slice* s, size_t split);
  101. GPRAPI grpc_slice grpc_empty_slice(void);
  102. GPRAPI int grpc_slice_eq(grpc_slice a, grpc_slice b);
  103. /** Returns <0 if a < b, ==0 if a == b, >0 if a > b
  104. The order is arbitrary, and is not guaranteed to be stable across different
  105. versions of the API. */
  106. GPRAPI int grpc_slice_cmp(grpc_slice a, grpc_slice b);
  107. GPRAPI int grpc_slice_str_cmp(grpc_slice a, const char* b);
  108. /** return non-zero if the first blen bytes of a are equal to b */
  109. GPRAPI int grpc_slice_buf_start_eq(grpc_slice a, const void* b, size_t blen);
  110. /** return the index of the last instance of \a c in \a s, or -1 if not found */
  111. GPRAPI int grpc_slice_rchr(grpc_slice s, char c);
  112. GPRAPI int grpc_slice_chr(grpc_slice s, char c);
  113. /** return the index of the first occurrence of \a needle in \a haystack, or -1
  114. if it's not found */
  115. GPRAPI int grpc_slice_slice(grpc_slice haystack, grpc_slice needle);
  116. /** Do two slices point at the same memory, with the same length
  117. If a or b is inlined, actually compares data */
  118. GPRAPI int grpc_slice_is_equivalent(grpc_slice a, grpc_slice b);
  119. /** Return a slice pointing to newly allocated memory that has the same contents
  120. * as \a s */
  121. GPRAPI grpc_slice grpc_slice_dup(grpc_slice a);
  122. /** Return a copy of slice as a C string. Offers no protection against embedded
  123. NULL's. Returned string must be freed with gpr_free. */
  124. GPRAPI char* grpc_slice_to_c_string(grpc_slice s);
  125. #ifdef __cplusplus
  126. }
  127. #endif
  128. #endif /* GRPC_SLICE_H */