Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
Craig Tiller | 5b676a6 | 2016-10-26 21:09:29 -0700 | [diff] [blame] | 34 | #ifndef GRPC_SLICE_H |
| 35 | #define GRPC_SLICE_H |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 36 | |
David Garcia Quintas | 2425bbb | 2016-01-25 17:32:48 -0800 | [diff] [blame] | 37 | #include <grpc/impl/codegen/slice.h> |
David Garcia Quintas | 61887dc | 2016-07-27 23:17:34 -0700 | [diff] [blame] | 38 | #include <grpc/support/sync.h> |
David Garcia Quintas | e8fd66b | 2016-07-27 22:05:37 -0700 | [diff] [blame] | 39 | |
| 40 | #ifdef __cplusplus |
| 41 | extern "C" { |
| 42 | #endif |
| 43 | |
Alexander Polcyn | d809a15 | 2017-05-03 14:49:41 -0700 | [diff] [blame] | 44 | /** Increment the refcount of s. Requires slice is initialized. |
David Garcia Quintas | e8fd66b | 2016-07-27 22:05:37 -0700 | [diff] [blame] | 45 | Returns s. */ |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 46 | GPRAPI grpc_slice grpc_slice_ref(grpc_slice s); |
David Garcia Quintas | e8fd66b | 2016-07-27 22:05:37 -0700 | [diff] [blame] | 47 | |
Alexander Polcyn | d809a15 | 2017-05-03 14:49:41 -0700 | [diff] [blame] | 48 | /** Decrement the ref count of s. If the ref count of s reaches zero, all |
David Garcia Quintas | e8fd66b | 2016-07-27 22:05:37 -0700 | [diff] [blame] | 49 | slices sharing the ref count are destroyed, and considered no longer |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 50 | initialized. If s is ultimately derived from a call to grpc_slice_new(start, |
David Garcia Quintas | e8fd66b | 2016-07-27 22:05:37 -0700 | [diff] [blame] | 51 | len, dest) where dest!=NULL , then (*dest)(start) is called, else if s is |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 52 | ultimately derived from a call to grpc_slice_new_with_len(start, len, dest) |
David Garcia Quintas | e8fd66b | 2016-07-27 22:05:37 -0700 | [diff] [blame] | 53 | where dest!=NULL , then (*dest)(start, len). Requires s initialized. */ |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 54 | GPRAPI void grpc_slice_unref(grpc_slice s); |
David Garcia Quintas | e8fd66b | 2016-07-27 22:05:37 -0700 | [diff] [blame] | 55 | |
Alexander Polcyn | d809a15 | 2017-05-03 14:49:41 -0700 | [diff] [blame] | 56 | /** Copy slice - create a new slice that contains the same data as s */ |
Craig Tiller | 85bf34a | 2017-04-21 16:14:59 -0700 | [diff] [blame] | 57 | GPRAPI grpc_slice grpc_slice_copy(grpc_slice s); |
| 58 | |
Alexander Polcyn | d809a15 | 2017-05-03 14:49:41 -0700 | [diff] [blame] | 59 | /** Create a slice pointing at some data. Calls malloc to allocate a refcount |
David Garcia Quintas | e8fd66b | 2016-07-27 22:05:37 -0700 | [diff] [blame] | 60 | for the object, and arranges that destroy will be called with the pointer |
| 61 | passed in at destruction. */ |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 62 | GPRAPI grpc_slice grpc_slice_new(void *p, size_t len, void (*destroy)(void *)); |
David Garcia Quintas | e8fd66b | 2016-07-27 22:05:37 -0700 | [diff] [blame] | 63 | |
Alexander Polcyn | d809a15 | 2017-05-03 14:49:41 -0700 | [diff] [blame] | 64 | /** Equivalent to grpc_slice_new, but with a separate pointer that is |
David Garcia Quintas | e8fd66b | 2016-07-27 22:05:37 -0700 | [diff] [blame] | 65 | passed to the destroy function. This function can be useful when |
| 66 | the data is part of a larger structure that must be destroyed when |
| 67 | the data is no longer needed. */ |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 68 | GPRAPI grpc_slice grpc_slice_new_with_user_data(void *p, size_t len, |
| 69 | void (*destroy)(void *), |
| 70 | void *user_data); |
David Garcia Quintas | e8fd66b | 2016-07-27 22:05:37 -0700 | [diff] [blame] | 71 | |
Alexander Polcyn | d809a15 | 2017-05-03 14:49:41 -0700 | [diff] [blame] | 72 | /** Equivalent to grpc_slice_new, but with a two argument destroy function that |
David Garcia Quintas | e8fd66b | 2016-07-27 22:05:37 -0700 | [diff] [blame] | 73 | also takes the slice length. */ |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 74 | GPRAPI grpc_slice grpc_slice_new_with_len(void *p, size_t len, |
| 75 | void (*destroy)(void *, size_t)); |
David Garcia Quintas | e8fd66b | 2016-07-27 22:05:37 -0700 | [diff] [blame] | 76 | |
Alexander Polcyn | d809a15 | 2017-05-03 14:49:41 -0700 | [diff] [blame] | 77 | /** Equivalent to grpc_slice_new(malloc(len), len, free), but saves one malloc() |
David Garcia Quintas | e8fd66b | 2016-07-27 22:05:37 -0700 | [diff] [blame] | 78 | call. |
| 79 | Aborts if malloc() fails. */ |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 80 | GPRAPI grpc_slice grpc_slice_malloc(size_t length); |
Craig Tiller | 423d6fd | 2017-04-12 13:15:45 -0700 | [diff] [blame] | 81 | GPRAPI grpc_slice grpc_slice_malloc_large(size_t length); |
| 82 | |
| 83 | #define GRPC_SLICE_MALLOC(len) \ |
| 84 | ((len) <= GRPC_SLICE_INLINED_SIZE \ |
| 85 | ? (grpc_slice){.refcount = NULL, \ |
| 86 | .data.inlined = {.length = (uint8_t)(len)}} \ |
| 87 | : grpc_slice_malloc_large((len))) |
David Garcia Quintas | e8fd66b | 2016-07-27 22:05:37 -0700 | [diff] [blame] | 88 | |
Alexander Polcyn | d809a15 | 2017-05-03 14:49:41 -0700 | [diff] [blame] | 89 | /** Intern a slice: |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 90 | |
| 91 | The return value for two invocations of this function with the same sequence |
| 92 | of bytes is a slice which points to the same memory. */ |
| 93 | GPRAPI grpc_slice grpc_slice_intern(grpc_slice slice); |
| 94 | |
Alexander Polcyn | d809a15 | 2017-05-03 14:49:41 -0700 | [diff] [blame] | 95 | /** Create a slice by copying a string. |
David Garcia Quintas | e8fd66b | 2016-07-27 22:05:37 -0700 | [diff] [blame] | 96 | Does not preserve null terminators. |
| 97 | Equivalent to: |
| 98 | size_t len = strlen(source); |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 99 | grpc_slice slice = grpc_slice_malloc(len); |
David Garcia Quintas | e8fd66b | 2016-07-27 22:05:37 -0700 | [diff] [blame] | 100 | memcpy(slice->data, source, len); */ |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 101 | GPRAPI grpc_slice grpc_slice_from_copied_string(const char *source); |
David Garcia Quintas | e8fd66b | 2016-07-27 22:05:37 -0700 | [diff] [blame] | 102 | |
Alexander Polcyn | d809a15 | 2017-05-03 14:49:41 -0700 | [diff] [blame] | 103 | /** Create a slice by copying a buffer. |
David Garcia Quintas | e8fd66b | 2016-07-27 22:05:37 -0700 | [diff] [blame] | 104 | Equivalent to: |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 105 | grpc_slice slice = grpc_slice_malloc(len); |
David Garcia Quintas | e8fd66b | 2016-07-27 22:05:37 -0700 | [diff] [blame] | 106 | memcpy(slice->data, source, len); */ |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 107 | GPRAPI grpc_slice grpc_slice_from_copied_buffer(const char *source, size_t len); |
David Garcia Quintas | e8fd66b | 2016-07-27 22:05:37 -0700 | [diff] [blame] | 108 | |
Alexander Polcyn | d809a15 | 2017-05-03 14:49:41 -0700 | [diff] [blame] | 109 | /** Create a slice pointing to constant memory */ |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 110 | GPRAPI grpc_slice grpc_slice_from_static_string(const char *source); |
David Garcia Quintas | e8fd66b | 2016-07-27 22:05:37 -0700 | [diff] [blame] | 111 | |
Alexander Polcyn | d809a15 | 2017-05-03 14:49:41 -0700 | [diff] [blame] | 112 | /** Create a slice pointing to constant memory */ |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 113 | GPRAPI grpc_slice grpc_slice_from_static_buffer(const void *source, size_t len); |
| 114 | |
Alexander Polcyn | d809a15 | 2017-05-03 14:49:41 -0700 | [diff] [blame] | 115 | /** Return a result slice derived from s, which shares a ref count with \a s, |
David Garcia Quintas | 2f4679e | 2017-04-14 13:06:27 -0700 | [diff] [blame] | 116 | where result.data==s.data+begin, and result.length==end-begin. The ref count |
| 117 | of \a s is increased by one. Do not assign result back to \a s. |
David Garcia Quintas | e8fd66b | 2016-07-27 22:05:37 -0700 | [diff] [blame] | 118 | Requires s initialized, begin <= end, begin <= s.length, and |
| 119 | end <= source->length. */ |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 120 | GPRAPI grpc_slice grpc_slice_sub(grpc_slice s, size_t begin, size_t end); |
David Garcia Quintas | e8fd66b | 2016-07-27 22:05:37 -0700 | [diff] [blame] | 121 | |
Alexander Polcyn | d809a15 | 2017-05-03 14:49:41 -0700 | [diff] [blame] | 122 | /** The same as grpc_slice_sub, but without altering the ref count */ |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 123 | GPRAPI grpc_slice grpc_slice_sub_no_ref(grpc_slice s, size_t begin, size_t end); |
David Garcia Quintas | e8fd66b | 2016-07-27 22:05:37 -0700 | [diff] [blame] | 124 | |
Alexander Polcyn | d809a15 | 2017-05-03 14:49:41 -0700 | [diff] [blame] | 125 | /** Splits s into two: modifies s to be s[0:split], and returns a new slice, |
David Garcia Quintas | e8fd66b | 2016-07-27 22:05:37 -0700 | [diff] [blame] | 126 | sharing a refcount with s, that contains s[split:s.length]. |
| 127 | Requires s intialized, split <= s.length */ |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 128 | GPRAPI grpc_slice grpc_slice_split_tail(grpc_slice *s, size_t split); |
David Garcia Quintas | e8fd66b | 2016-07-27 22:05:37 -0700 | [diff] [blame] | 129 | |
Craig Tiller | 0d23d89 | 2017-04-20 14:46:37 -0700 | [diff] [blame] | 130 | typedef enum { |
| 131 | GRPC_SLICE_REF_TAIL = 1, |
| 132 | GRPC_SLICE_REF_HEAD = 2, |
| 133 | GRPC_SLICE_REF_BOTH = 1 + 2 |
| 134 | } grpc_slice_ref_whom; |
| 135 | |
Alexander Polcyn | d809a15 | 2017-05-03 14:49:41 -0700 | [diff] [blame] | 136 | /** The same as grpc_slice_split_tail, but with an option to skip altering |
Craig Tiller | 255ea13 | 2017-04-12 17:18:56 -0700 | [diff] [blame] | 137 | * refcounts (grpc_slice_split_tail_maybe_ref(..., true) is equivalent to |
| 138 | * grpc_slice_split_tail(...)) */ |
| 139 | GPRAPI grpc_slice grpc_slice_split_tail_maybe_ref(grpc_slice *s, size_t split, |
Craig Tiller | 0d23d89 | 2017-04-20 14:46:37 -0700 | [diff] [blame] | 140 | grpc_slice_ref_whom ref_whom); |
Craig Tiller | 423d6fd | 2017-04-12 13:15:45 -0700 | [diff] [blame] | 141 | |
Alexander Polcyn | d809a15 | 2017-05-03 14:49:41 -0700 | [diff] [blame] | 142 | /** Splits s into two: modifies s to be s[split:s.length], and returns a new |
David Garcia Quintas | e8fd66b | 2016-07-27 22:05:37 -0700 | [diff] [blame] | 143 | slice, sharing a refcount with s, that contains s[0:split]. |
| 144 | Requires s intialized, split <= s.length */ |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 145 | GPRAPI grpc_slice grpc_slice_split_head(grpc_slice *s, size_t split); |
David Garcia Quintas | e8fd66b | 2016-07-27 22:05:37 -0700 | [diff] [blame] | 146 | |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 147 | GPRAPI grpc_slice grpc_empty_slice(void); |
| 148 | |
| 149 | GPRAPI uint32_t grpc_slice_default_hash_impl(grpc_slice s); |
| 150 | GPRAPI int grpc_slice_default_eq_impl(grpc_slice a, grpc_slice b); |
| 151 | |
| 152 | GPRAPI int grpc_slice_eq(grpc_slice a, grpc_slice b); |
Craig Tiller | 0451c3d | 2016-11-17 09:34:14 -0800 | [diff] [blame] | 153 | |
Alexander Polcyn | d809a15 | 2017-05-03 14:49:41 -0700 | [diff] [blame] | 154 | /** Returns <0 if a < b, ==0 if a == b, >0 if a > b |
David Garcia Quintas | e8fd66b | 2016-07-27 22:05:37 -0700 | [diff] [blame] | 155 | The order is arbitrary, and is not guaranteed to be stable across different |
| 156 | versions of the API. */ |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 157 | GPRAPI int grpc_slice_cmp(grpc_slice a, grpc_slice b); |
| 158 | GPRAPI int grpc_slice_str_cmp(grpc_slice a, const char *b); |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 159 | GPRAPI int grpc_slice_buf_cmp(grpc_slice a, const void *b, size_t blen); |
| 160 | |
Alexander Polcyn | d809a15 | 2017-05-03 14:49:41 -0700 | [diff] [blame] | 161 | /** return non-zero if the first blen bytes of a are equal to b */ |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 162 | GPRAPI int grpc_slice_buf_start_eq(grpc_slice a, const void *b, size_t blen); |
| 163 | |
Alexander Polcyn | d809a15 | 2017-05-03 14:49:41 -0700 | [diff] [blame] | 164 | /** return the index of the last instance of \a c in \a s, or -1 if not found */ |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 165 | GPRAPI int grpc_slice_rchr(grpc_slice s, char c); |
| 166 | GPRAPI int grpc_slice_chr(grpc_slice s, char c); |
| 167 | |
Alexander Polcyn | d809a15 | 2017-05-03 14:49:41 -0700 | [diff] [blame] | 168 | /** return the index of the first occurance of \a needle in \a haystack, or -1 |
Alexander Polcyn | 41ff2e1 | 2017-05-10 15:19:01 -0700 | [diff] [blame] | 169 | if it's not found */ |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 170 | GPRAPI int grpc_slice_slice(grpc_slice haystack, grpc_slice needle); |
| 171 | |
| 172 | GPRAPI uint32_t grpc_slice_hash(grpc_slice s); |
Craig Tiller | e52bbb1 | 2016-11-10 15:34:06 -0800 | [diff] [blame] | 173 | |
Alexander Polcyn | d809a15 | 2017-05-03 14:49:41 -0700 | [diff] [blame] | 174 | /** Do two slices point at the same memory, with the same length |
Craig Tiller | b121fc7 | 2016-11-03 15:22:59 -0700 | [diff] [blame] | 175 | If a or b is inlined, actually compares data */ |
Craig Tiller | d57a148 | 2016-11-16 14:45:10 -0800 | [diff] [blame] | 176 | GPRAPI int grpc_slice_is_equivalent(grpc_slice a, grpc_slice b); |
Craig Tiller | b121fc7 | 2016-11-03 15:22:59 -0700 | [diff] [blame] | 177 | |
Alexander Polcyn | d809a15 | 2017-05-03 14:49:41 -0700 | [diff] [blame] | 178 | /** Return a slice pointing to newly allocated memory that has the same contents |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 179 | * as \a s */ |
| 180 | GPRAPI grpc_slice grpc_slice_dup(grpc_slice a); |
| 181 | |
Alexander Polcyn | d809a15 | 2017-05-03 14:49:41 -0700 | [diff] [blame] | 182 | /** Return a copy of slice as a C string. Offers no protection against embedded |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 183 | NULL's. Returned string must be freed with gpr_free. */ |
| 184 | GPRAPI char *grpc_slice_to_c_string(grpc_slice s); |
| 185 | |
David Garcia Quintas | e8fd66b | 2016-07-27 22:05:37 -0700 | [diff] [blame] | 186 | #ifdef __cplusplus |
| 187 | } |
| 188 | #endif |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 189 | |
Craig Tiller | 5b676a6 | 2016-10-26 21:09:29 -0700 | [diff] [blame] | 190 | #endif /* GRPC_SLICE_H */ |