Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 8a9fd52 | 2016-03-25 17:09:29 -0700 | [diff] [blame^] | 3 | * Copyright 2015-2016, 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 | |
| 34 | #include <grpc/support/alloc.h> |
| 35 | #include <grpc/support/log.h> |
| 36 | #include <grpc/support/slice.h> |
| 37 | |
| 38 | #include <string.h> |
| 39 | |
Craig Tiller | 32946d3 | 2015-01-15 11:37:30 -0800 | [diff] [blame] | 40 | gpr_slice gpr_empty_slice(void) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 41 | gpr_slice out; |
| 42 | out.refcount = 0; |
| 43 | out.data.inlined.length = 0; |
| 44 | return out; |
| 45 | } |
| 46 | |
| 47 | gpr_slice gpr_slice_ref(gpr_slice slice) { |
| 48 | if (slice.refcount) { |
| 49 | slice.refcount->ref(slice.refcount); |
| 50 | } |
| 51 | return slice; |
| 52 | } |
| 53 | |
| 54 | void gpr_slice_unref(gpr_slice slice) { |
| 55 | if (slice.refcount) { |
| 56 | slice.refcount->unref(slice.refcount); |
| 57 | } |
| 58 | } |
| 59 | |
Craig Tiller | 0e72ede | 2015-11-19 07:48:53 -0800 | [diff] [blame] | 60 | /* gpr_slice_from_static_string support structure - a refcount that does |
| 61 | nothing */ |
| 62 | static void noop_ref_or_unref(void *unused) {} |
| 63 | |
Craig Tiller | b774be4 | 2015-11-19 07:56:13 -0800 | [diff] [blame] | 64 | static gpr_slice_refcount noop_refcount = {noop_ref_or_unref, |
| 65 | noop_ref_or_unref}; |
Craig Tiller | 0e72ede | 2015-11-19 07:48:53 -0800 | [diff] [blame] | 66 | |
| 67 | gpr_slice gpr_slice_from_static_string(const char *s) { |
| 68 | gpr_slice slice; |
| 69 | slice.refcount = &noop_refcount; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 70 | slice.data.refcounted.bytes = (uint8_t *)s; |
Craig Tiller | 0e72ede | 2015-11-19 07:48:53 -0800 | [diff] [blame] | 71 | slice.data.refcounted.length = strlen(s); |
| 72 | return slice; |
| 73 | } |
| 74 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 75 | /* gpr_slice_new support structures - we create a refcount object extended |
| 76 | with the user provided data pointer & destroy function */ |
| 77 | typedef struct new_slice_refcount { |
| 78 | gpr_slice_refcount rc; |
| 79 | gpr_refcount refs; |
| 80 | void (*user_destroy)(void *); |
| 81 | void *user_data; |
| 82 | } new_slice_refcount; |
| 83 | |
| 84 | static void new_slice_ref(void *p) { |
| 85 | new_slice_refcount *r = p; |
| 86 | gpr_ref(&r->refs); |
| 87 | } |
| 88 | |
| 89 | static void new_slice_unref(void *p) { |
| 90 | new_slice_refcount *r = p; |
| 91 | if (gpr_unref(&r->refs)) { |
| 92 | r->user_destroy(r->user_data); |
| 93 | gpr_free(r); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | gpr_slice gpr_slice_new(void *p, size_t len, void (*destroy)(void *)) { |
| 98 | gpr_slice slice; |
| 99 | new_slice_refcount *rc = gpr_malloc(sizeof(new_slice_refcount)); |
| 100 | gpr_ref_init(&rc->refs, 1); |
| 101 | rc->rc.ref = new_slice_ref; |
| 102 | rc->rc.unref = new_slice_unref; |
| 103 | rc->user_destroy = destroy; |
| 104 | rc->user_data = p; |
| 105 | |
| 106 | slice.refcount = &rc->rc; |
| 107 | slice.data.refcounted.bytes = p; |
| 108 | slice.data.refcounted.length = len; |
| 109 | return slice; |
| 110 | } |
| 111 | |
| 112 | /* gpr_slice_new_with_len support structures - we create a refcount object |
| 113 | extended with the user provided data pointer & destroy function */ |
| 114 | typedef struct new_with_len_slice_refcount { |
| 115 | gpr_slice_refcount rc; |
| 116 | gpr_refcount refs; |
| 117 | void *user_data; |
| 118 | size_t user_length; |
| 119 | void (*user_destroy)(void *, size_t); |
| 120 | } new_with_len_slice_refcount; |
| 121 | |
| 122 | static void new_with_len_ref(void *p) { |
| 123 | new_with_len_slice_refcount *r = p; |
| 124 | gpr_ref(&r->refs); |
| 125 | } |
| 126 | |
| 127 | static void new_with_len_unref(void *p) { |
| 128 | new_with_len_slice_refcount *r = p; |
| 129 | if (gpr_unref(&r->refs)) { |
| 130 | r->user_destroy(r->user_data, r->user_length); |
| 131 | gpr_free(r); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | gpr_slice gpr_slice_new_with_len(void *p, size_t len, |
| 136 | void (*destroy)(void *, size_t)) { |
| 137 | gpr_slice slice; |
| 138 | new_with_len_slice_refcount *rc = |
| 139 | gpr_malloc(sizeof(new_with_len_slice_refcount)); |
| 140 | gpr_ref_init(&rc->refs, 1); |
| 141 | rc->rc.ref = new_with_len_ref; |
| 142 | rc->rc.unref = new_with_len_unref; |
| 143 | rc->user_destroy = destroy; |
| 144 | rc->user_data = p; |
| 145 | rc->user_length = len; |
| 146 | |
| 147 | slice.refcount = &rc->rc; |
| 148 | slice.data.refcounted.bytes = p; |
| 149 | slice.data.refcounted.length = len; |
| 150 | return slice; |
| 151 | } |
| 152 | |
| 153 | gpr_slice gpr_slice_from_copied_buffer(const char *source, size_t length) { |
| 154 | gpr_slice slice = gpr_slice_malloc(length); |
| 155 | memcpy(GPR_SLICE_START_PTR(slice), source, length); |
| 156 | return slice; |
| 157 | } |
| 158 | |
| 159 | gpr_slice gpr_slice_from_copied_string(const char *source) { |
| 160 | return gpr_slice_from_copied_buffer(source, strlen(source)); |
| 161 | } |
| 162 | |
| 163 | typedef struct { |
| 164 | gpr_slice_refcount base; |
| 165 | gpr_refcount refs; |
| 166 | } malloc_refcount; |
| 167 | |
| 168 | static void malloc_ref(void *p) { |
| 169 | malloc_refcount *r = p; |
| 170 | gpr_ref(&r->refs); |
| 171 | } |
| 172 | |
| 173 | static void malloc_unref(void *p) { |
| 174 | malloc_refcount *r = p; |
| 175 | if (gpr_unref(&r->refs)) { |
| 176 | gpr_free(r); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | gpr_slice gpr_slice_malloc(size_t length) { |
| 181 | gpr_slice slice; |
| 182 | |
| 183 | if (length > sizeof(slice.data.inlined.bytes)) { |
| 184 | /* Memory layout used by the slice created here: |
| 185 | |
| 186 | +-----------+----------------------------------------------------------+ |
| 187 | | refcount | bytes | |
| 188 | +-----------+----------------------------------------------------------+ |
| 189 | |
| 190 | refcount is a malloc_refcount |
| 191 | bytes is an array of bytes of the requested length |
| 192 | Both parts are placed in the same allocation returned from gpr_malloc */ |
| 193 | malloc_refcount *rc = gpr_malloc(sizeof(malloc_refcount) + length); |
| 194 | |
| 195 | /* Initial refcount on rc is 1 - and it's up to the caller to release |
| 196 | this reference. */ |
| 197 | gpr_ref_init(&rc->refs, 1); |
| 198 | |
| 199 | rc->base.ref = malloc_ref; |
| 200 | rc->base.unref = malloc_unref; |
| 201 | |
| 202 | /* Build up the slice to be returned. */ |
| 203 | /* The slices refcount points back to the allocated block. */ |
| 204 | slice.refcount = &rc->base; |
| 205 | /* The data bytes are placed immediately after the refcount struct */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 206 | slice.data.refcounted.bytes = (uint8_t *)(rc + 1); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 207 | /* And the length of the block is set to the requested length */ |
| 208 | slice.data.refcounted.length = length; |
| 209 | } else { |
| 210 | /* small slice: just inline the data */ |
| 211 | slice.refcount = NULL; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 212 | slice.data.inlined.length = (uint8_t)length; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 213 | } |
| 214 | return slice; |
| 215 | } |
| 216 | |
| 217 | gpr_slice gpr_slice_sub_no_ref(gpr_slice source, size_t begin, size_t end) { |
| 218 | gpr_slice subset; |
| 219 | |
murgatroid99 | 5e71d7a | 2015-06-19 12:24:44 -0700 | [diff] [blame] | 220 | GPR_ASSERT(end >= begin); |
| 221 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 222 | if (source.refcount) { |
| 223 | /* Enforce preconditions */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 224 | GPR_ASSERT(source.data.refcounted.length >= end); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 225 | |
| 226 | /* Build the result */ |
| 227 | subset.refcount = source.refcount; |
| 228 | /* Point into the source array */ |
| 229 | subset.data.refcounted.bytes = source.data.refcounted.bytes + begin; |
| 230 | subset.data.refcounted.length = end - begin; |
| 231 | } else { |
murgatroid99 | 5e71d7a | 2015-06-19 12:24:44 -0700 | [diff] [blame] | 232 | /* Enforce preconditions */ |
| 233 | GPR_ASSERT(source.data.inlined.length >= end); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 234 | subset.refcount = NULL; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 235 | subset.data.inlined.length = (uint8_t)(end - begin); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 236 | memcpy(subset.data.inlined.bytes, source.data.inlined.bytes + begin, |
| 237 | end - begin); |
| 238 | } |
| 239 | return subset; |
| 240 | } |
| 241 | |
| 242 | gpr_slice gpr_slice_sub(gpr_slice source, size_t begin, size_t end) { |
| 243 | gpr_slice subset; |
| 244 | |
| 245 | if (end - begin <= sizeof(subset.data.inlined.bytes)) { |
| 246 | subset.refcount = NULL; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 247 | subset.data.inlined.length = (uint8_t)(end - begin); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 248 | memcpy(subset.data.inlined.bytes, GPR_SLICE_START_PTR(source) + begin, |
| 249 | end - begin); |
| 250 | } else { |
| 251 | subset = gpr_slice_sub_no_ref(source, begin, end); |
| 252 | /* Bump the refcount */ |
| 253 | subset.refcount->ref(subset.refcount); |
| 254 | } |
| 255 | return subset; |
| 256 | } |
| 257 | |
| 258 | gpr_slice gpr_slice_split_tail(gpr_slice *source, size_t split) { |
| 259 | gpr_slice tail; |
| 260 | |
| 261 | if (source->refcount == NULL) { |
| 262 | /* inlined data, copy it out */ |
| 263 | GPR_ASSERT(source->data.inlined.length >= split); |
| 264 | tail.refcount = NULL; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 265 | tail.data.inlined.length = (uint8_t)(source->data.inlined.length - split); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 266 | memcpy(tail.data.inlined.bytes, source->data.inlined.bytes + split, |
| 267 | tail.data.inlined.length); |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 268 | source->data.inlined.length = (uint8_t)split; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 269 | } else { |
| 270 | size_t tail_length = source->data.refcounted.length - split; |
| 271 | GPR_ASSERT(source->data.refcounted.length >= split); |
| 272 | if (tail_length < sizeof(tail.data.inlined.bytes)) { |
| 273 | /* Copy out the bytes - it'll be cheaper than refcounting */ |
| 274 | tail.refcount = NULL; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 275 | tail.data.inlined.length = (uint8_t)tail_length; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 276 | memcpy(tail.data.inlined.bytes, source->data.refcounted.bytes + split, |
| 277 | tail_length); |
| 278 | } else { |
| 279 | /* Build the result */ |
| 280 | tail.refcount = source->refcount; |
| 281 | /* Bump the refcount */ |
| 282 | tail.refcount->ref(tail.refcount); |
| 283 | /* Point into the source array */ |
| 284 | tail.data.refcounted.bytes = source->data.refcounted.bytes + split; |
| 285 | tail.data.refcounted.length = tail_length; |
| 286 | } |
| 287 | source->data.refcounted.length = split; |
| 288 | } |
| 289 | |
| 290 | return tail; |
| 291 | } |
| 292 | |
| 293 | gpr_slice gpr_slice_split_head(gpr_slice *source, size_t split) { |
| 294 | gpr_slice head; |
| 295 | |
| 296 | if (source->refcount == NULL) { |
| 297 | GPR_ASSERT(source->data.inlined.length >= split); |
| 298 | |
| 299 | head.refcount = NULL; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 300 | head.data.inlined.length = (uint8_t)split; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 301 | memcpy(head.data.inlined.bytes, source->data.inlined.bytes, split); |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 302 | source->data.inlined.length = |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 303 | (uint8_t)(source->data.inlined.length - split); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 304 | memmove(source->data.inlined.bytes, source->data.inlined.bytes + split, |
| 305 | source->data.inlined.length); |
| 306 | } else if (split < sizeof(head.data.inlined.bytes)) { |
| 307 | GPR_ASSERT(source->data.refcounted.length >= split); |
| 308 | |
| 309 | head.refcount = NULL; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 310 | head.data.inlined.length = (uint8_t)split; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 311 | memcpy(head.data.inlined.bytes, source->data.refcounted.bytes, split); |
| 312 | source->data.refcounted.bytes += split; |
| 313 | source->data.refcounted.length -= split; |
| 314 | } else { |
| 315 | GPR_ASSERT(source->data.refcounted.length >= split); |
| 316 | |
| 317 | /* Build the result */ |
| 318 | head.refcount = source->refcount; |
| 319 | /* Bump the refcount */ |
| 320 | head.refcount->ref(head.refcount); |
| 321 | /* Point into the source array */ |
| 322 | head.data.refcounted.bytes = source->data.refcounted.bytes; |
| 323 | head.data.refcounted.length = split; |
| 324 | source->data.refcounted.bytes += split; |
| 325 | source->data.refcounted.length -= split; |
| 326 | } |
| 327 | |
| 328 | return head; |
| 329 | } |
| 330 | |
| 331 | int gpr_slice_cmp(gpr_slice a, gpr_slice b) { |
murgatroid99 | 84976a7 | 2015-06-22 15:07:23 -0700 | [diff] [blame] | 332 | int d = (int)(GPR_SLICE_LENGTH(a) - GPR_SLICE_LENGTH(b)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 333 | if (d != 0) return d; |
| 334 | return memcmp(GPR_SLICE_START_PTR(a), GPR_SLICE_START_PTR(b), |
| 335 | GPR_SLICE_LENGTH(a)); |
| 336 | } |
| 337 | |
| 338 | int gpr_slice_str_cmp(gpr_slice a, const char *b) { |
| 339 | size_t b_length = strlen(b); |
murgatroid99 | 84976a7 | 2015-06-22 15:07:23 -0700 | [diff] [blame] | 340 | int d = (int)(GPR_SLICE_LENGTH(a) - b_length); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 341 | if (d != 0) return d; |
| 342 | return memcmp(GPR_SLICE_START_PTR(a), b, b_length); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 343 | } |