2
0

slice.h 7.1 KB

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