blob: cf3953ce4ea5ff59fae7aeb82644ded3948c9799 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller8a9fd522016-03-25 17:09:29 -07003 * Copyright 2015-2016, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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 Tiller32946d32015-01-15 11:37:30 -080040gpr_slice gpr_empty_slice(void) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080041 gpr_slice out;
42 out.refcount = 0;
43 out.data.inlined.length = 0;
44 return out;
45}
46
47gpr_slice gpr_slice_ref(gpr_slice slice) {
48 if (slice.refcount) {
49 slice.refcount->ref(slice.refcount);
50 }
51 return slice;
52}
53
54void gpr_slice_unref(gpr_slice slice) {
55 if (slice.refcount) {
56 slice.refcount->unref(slice.refcount);
57 }
58}
59
Craig Tiller0e72ede2015-11-19 07:48:53 -080060/* gpr_slice_from_static_string support structure - a refcount that does
61 nothing */
62static void noop_ref_or_unref(void *unused) {}
63
Craig Tillerb774be42015-11-19 07:56:13 -080064static gpr_slice_refcount noop_refcount = {noop_ref_or_unref,
65 noop_ref_or_unref};
Craig Tiller0e72ede2015-11-19 07:48:53 -080066
67gpr_slice gpr_slice_from_static_string(const char *s) {
68 gpr_slice slice;
69 slice.refcount = &noop_refcount;
Craig Tiller7536af02015-12-22 13:49:30 -080070 slice.data.refcounted.bytes = (uint8_t *)s;
Craig Tiller0e72ede2015-11-19 07:48:53 -080071 slice.data.refcounted.length = strlen(s);
72 return slice;
73}
74
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080075/* gpr_slice_new support structures - we create a refcount object extended
76 with the user provided data pointer & destroy function */
77typedef 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
84static void new_slice_ref(void *p) {
85 new_slice_refcount *r = p;
86 gpr_ref(&r->refs);
87}
88
89static 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
97gpr_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 */
114typedef 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
122static void new_with_len_ref(void *p) {
123 new_with_len_slice_refcount *r = p;
124 gpr_ref(&r->refs);
125}
126
127static 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
135gpr_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
153gpr_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
159gpr_slice gpr_slice_from_copied_string(const char *source) {
160 return gpr_slice_from_copied_buffer(source, strlen(source));
161}
162
163typedef struct {
164 gpr_slice_refcount base;
165 gpr_refcount refs;
166} malloc_refcount;
167
168static void malloc_ref(void *p) {
169 malloc_refcount *r = p;
170 gpr_ref(&r->refs);
171}
172
173static void malloc_unref(void *p) {
174 malloc_refcount *r = p;
175 if (gpr_unref(&r->refs)) {
176 gpr_free(r);
177 }
178}
179
180gpr_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 Tiller7536af02015-12-22 13:49:30 -0800206 slice.data.refcounted.bytes = (uint8_t *)(rc + 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800207 /* 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 Tiller7536af02015-12-22 13:49:30 -0800212 slice.data.inlined.length = (uint8_t)length;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800213 }
214 return slice;
215}
216
217gpr_slice gpr_slice_sub_no_ref(gpr_slice source, size_t begin, size_t end) {
218 gpr_slice subset;
219
murgatroid995e71d7a2015-06-19 12:24:44 -0700220 GPR_ASSERT(end >= begin);
221
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800222 if (source.refcount) {
223 /* Enforce preconditions */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800224 GPR_ASSERT(source.data.refcounted.length >= end);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800225
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 {
murgatroid995e71d7a2015-06-19 12:24:44 -0700232 /* Enforce preconditions */
233 GPR_ASSERT(source.data.inlined.length >= end);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800234 subset.refcount = NULL;
Craig Tiller7536af02015-12-22 13:49:30 -0800235 subset.data.inlined.length = (uint8_t)(end - begin);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800236 memcpy(subset.data.inlined.bytes, source.data.inlined.bytes + begin,
237 end - begin);
238 }
239 return subset;
240}
241
242gpr_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 Tiller7536af02015-12-22 13:49:30 -0800247 subset.data.inlined.length = (uint8_t)(end - begin);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800248 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
258gpr_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 Tiller7536af02015-12-22 13:49:30 -0800265 tail.data.inlined.length = (uint8_t)(source->data.inlined.length - split);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800266 memcpy(tail.data.inlined.bytes, source->data.inlined.bytes + split,
267 tail.data.inlined.length);
Craig Tiller7536af02015-12-22 13:49:30 -0800268 source->data.inlined.length = (uint8_t)split;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800269 } 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 Tiller7536af02015-12-22 13:49:30 -0800275 tail.data.inlined.length = (uint8_t)tail_length;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800276 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
293gpr_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 Tiller7536af02015-12-22 13:49:30 -0800300 head.data.inlined.length = (uint8_t)split;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800301 memcpy(head.data.inlined.bytes, source->data.inlined.bytes, split);
Craig Tillerd6c98df2015-08-18 09:33:44 -0700302 source->data.inlined.length =
Craig Tiller7536af02015-12-22 13:49:30 -0800303 (uint8_t)(source->data.inlined.length - split);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800304 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 Tiller7536af02015-12-22 13:49:30 -0800310 head.data.inlined.length = (uint8_t)split;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800311 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
331int gpr_slice_cmp(gpr_slice a, gpr_slice b) {
murgatroid9984976a72015-06-22 15:07:23 -0700332 int d = (int)(GPR_SLICE_LENGTH(a) - GPR_SLICE_LENGTH(b));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800333 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
338int gpr_slice_str_cmp(gpr_slice a, const char *b) {
339 size_t b_length = strlen(b);
murgatroid9984976a72015-06-22 15:07:23 -0700340 int d = (int)(GPR_SLICE_LENGTH(a) - b_length);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800341 if (d != 0) return d;
342 return memcmp(GPR_SLICE_START_PTR(a), b, b_length);
Craig Tiller190d3602015-02-18 09:23:38 -0800343}