Line data Source code
1 : /*
2 : *
3 : * Copyright 2015, Google Inc.
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 :
40 529187 : gpr_slice gpr_empty_slice(void) {
41 : gpr_slice out;
42 529037 : out.refcount = 0;
43 529037 : out.data.inlined.length = 0;
44 529187 : return out;
45 : }
46 :
47 18166107 : gpr_slice gpr_slice_ref(gpr_slice slice) {
48 18166107 : if (slice.refcount) {
49 6225254 : slice.refcount->ref(slice.refcount);
50 : }
51 18176722 : return slice;
52 : }
53 :
54 64027987 : void gpr_slice_unref(gpr_slice slice) {
55 64027987 : if (slice.refcount) {
56 9461554 : slice.refcount->unref(slice.refcount);
57 : }
58 64028026 : }
59 :
60 : /* gpr_slice_from_static_string support structure - a refcount that does
61 : nothing */
62 9521489 : static void noop_ref_or_unref(void *unused) {}
63 :
64 : static gpr_slice_refcount noop_refcount = {noop_ref_or_unref,
65 : noop_ref_or_unref};
66 :
67 300325 : gpr_slice gpr_slice_from_static_string(const char *s) {
68 : gpr_slice slice;
69 300238 : slice.refcount = &noop_refcount;
70 300238 : slice.data.refcounted.bytes = (gpr_uint8 *)s;
71 300325 : slice.data.refcounted.length = strlen(s);
72 300325 : return slice;
73 : }
74 :
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 5122 : static void new_slice_ref(void *p) {
85 5122 : new_slice_refcount *r = p;
86 5122 : gpr_ref(&r->refs);
87 5122 : }
88 :
89 11077 : static void new_slice_unref(void *p) {
90 11037 : new_slice_refcount *r = p;
91 11077 : if (gpr_unref(&r->refs)) {
92 5955 : r->user_destroy(r->user_data);
93 5955 : gpr_free(r);
94 : }
95 11077 : }
96 :
97 6052 : gpr_slice gpr_slice_new(void *p, size_t len, void (*destroy)(void *)) {
98 : gpr_slice slice;
99 6052 : new_slice_refcount *rc = gpr_malloc(sizeof(new_slice_refcount));
100 6052 : gpr_ref_init(&rc->refs, 1);
101 6052 : rc->rc.ref = new_slice_ref;
102 6052 : rc->rc.unref = new_slice_unref;
103 6052 : rc->user_destroy = destroy;
104 6052 : rc->user_data = p;
105 :
106 6052 : slice.refcount = &rc->rc;
107 6012 : slice.data.refcounted.bytes = p;
108 6012 : slice.data.refcounted.length = len;
109 6052 : 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 5 : static void new_with_len_ref(void *p) {
123 5 : new_with_len_slice_refcount *r = p;
124 5 : gpr_ref(&r->refs);
125 5 : }
126 :
127 6 : static void new_with_len_unref(void *p) {
128 6 : new_with_len_slice_refcount *r = p;
129 6 : if (gpr_unref(&r->refs)) {
130 1 : r->user_destroy(r->user_data, r->user_length);
131 1 : gpr_free(r);
132 : }
133 6 : }
134 :
135 1 : gpr_slice gpr_slice_new_with_len(void *p, size_t len,
136 : void (*destroy)(void *, size_t)) {
137 : gpr_slice slice;
138 1 : new_with_len_slice_refcount *rc =
139 : gpr_malloc(sizeof(new_with_len_slice_refcount));
140 1 : gpr_ref_init(&rc->refs, 1);
141 1 : rc->rc.ref = new_with_len_ref;
142 1 : rc->rc.unref = new_with_len_unref;
143 1 : rc->user_destroy = destroy;
144 1 : rc->user_data = p;
145 1 : rc->user_length = len;
146 :
147 1 : slice.refcount = &rc->rc;
148 1 : slice.data.refcounted.bytes = p;
149 1 : slice.data.refcounted.length = len;
150 1 : return slice;
151 : }
152 :
153 14189 : gpr_slice gpr_slice_from_copied_buffer(const char *source, size_t length) {
154 14189 : gpr_slice slice = gpr_slice_malloc(length);
155 14189 : memcpy(GPR_SLICE_START_PTR(slice), source, length);
156 14189 : return slice;
157 : }
158 :
159 11373 : gpr_slice gpr_slice_from_copied_string(const char *source) {
160 11373 : 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 3174329 : static void malloc_ref(void *p) {
169 3172449 : malloc_refcount *r = p;
170 3174329 : gpr_ref(&r->refs);
171 3174344 : }
172 :
173 3404324 : static void malloc_unref(void *p) {
174 3401393 : malloc_refcount *r = p;
175 3404324 : if (gpr_unref(&r->refs)) {
176 230014 : gpr_free(r);
177 : }
178 3404361 : }
179 :
180 39426011 : gpr_slice gpr_slice_malloc(size_t length) {
181 : gpr_slice slice;
182 :
183 39426011 : 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 230023 : 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 230027 : gpr_ref_init(&rc->refs, 1);
198 :
199 286260 : rc->base.ref = malloc_ref;
200 286260 : 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 286260 : slice.refcount = &rc->base;
205 : /* The data bytes are placed immediately after the refcount struct */
206 286260 : slice.data.refcounted.bytes = (gpr_uint8 *)(rc + 1);
207 : /* And the length of the block is set to the requested length */
208 285207 : slice.data.refcounted.length = length;
209 : } else {
210 : /* small slice: just inline the data */
211 39194741 : slice.refcount = NULL;
212 39195988 : slice.data.inlined.length = (gpr_uint8)length;
213 : }
214 39482248 : return slice;
215 : }
216 :
217 25156106 : gpr_slice gpr_slice_sub_no_ref(gpr_slice source, size_t begin, size_t end) {
218 : gpr_slice subset;
219 :
220 25156106 : GPR_ASSERT(end >= begin);
221 :
222 25156106 : if (source.refcount) {
223 : /* Enforce preconditions */
224 10628229 : GPR_ASSERT(source.data.refcounted.length >= end);
225 :
226 : /* Build the result */
227 10628229 : subset.refcount = source.refcount;
228 : /* Point into the source array */
229 10628229 : subset.data.refcounted.bytes = source.data.refcounted.bytes + begin;
230 10628229 : subset.data.refcounted.length = end - begin;
231 : } else {
232 : /* Enforce preconditions */
233 14527877 : GPR_ASSERT(source.data.inlined.length >= end);
234 14527877 : subset.refcount = NULL;
235 14527877 : subset.data.inlined.length = (gpr_uint8)(end - begin);
236 14527877 : memcpy(subset.data.inlined.bytes, source.data.inlined.bytes + begin,
237 : end - begin);
238 : }
239 25156106 : return subset;
240 : }
241 :
242 8898416 : gpr_slice gpr_slice_sub(gpr_slice source, size_t begin, size_t end) {
243 : gpr_slice subset;
244 :
245 8898416 : if (end - begin <= sizeof(subset.data.inlined.bytes)) {
246 8606930 : subset.refcount = NULL;
247 8606930 : subset.data.inlined.length = (gpr_uint8)(end - begin);
248 8606930 : memcpy(subset.data.inlined.bytes, GPR_SLICE_START_PTR(source) + begin,
249 : end - begin);
250 : } else {
251 291486 : subset = gpr_slice_sub_no_ref(source, begin, end);
252 : /* Bump the refcount */
253 291486 : subset.refcount->ref(subset.refcount);
254 : }
255 8898597 : return subset;
256 : }
257 :
258 12075 : gpr_slice gpr_slice_split_tail(gpr_slice *source, size_t split) {
259 : gpr_slice tail;
260 :
261 12075 : if (source->refcount == NULL) {
262 : /* inlined data, copy it out */
263 136 : GPR_ASSERT(source->data.inlined.length >= split);
264 136 : tail.refcount = NULL;
265 136 : tail.data.inlined.length = (gpr_uint8)(source->data.inlined.length - split);
266 136 : memcpy(tail.data.inlined.bytes, source->data.inlined.bytes + split,
267 136 : tail.data.inlined.length);
268 136 : source->data.inlined.length = (gpr_uint8)split;
269 : } else {
270 11939 : size_t tail_length = source->data.refcounted.length - split;
271 11939 : GPR_ASSERT(source->data.refcounted.length >= split);
272 11939 : if (tail_length < sizeof(tail.data.inlined.bytes)) {
273 : /* Copy out the bytes - it'll be cheaper than refcounting */
274 1744 : tail.refcount = NULL;
275 1744 : tail.data.inlined.length = (gpr_uint8)tail_length;
276 1744 : memcpy(tail.data.inlined.bytes, source->data.refcounted.bytes + split,
277 : tail_length);
278 : } else {
279 : /* Build the result */
280 10195 : tail.refcount = source->refcount;
281 : /* Bump the refcount */
282 10195 : tail.refcount->ref(tail.refcount);
283 : /* Point into the source array */
284 10195 : tail.data.refcounted.bytes = source->data.refcounted.bytes + split;
285 10195 : tail.data.refcounted.length = tail_length;
286 : }
287 11939 : source->data.refcounted.length = split;
288 : }
289 :
290 12075 : return tail;
291 : }
292 :
293 3137334 : gpr_slice gpr_slice_split_head(gpr_slice *source, size_t split) {
294 : gpr_slice head;
295 :
296 3137334 : if (source->refcount == NULL) {
297 242 : GPR_ASSERT(source->data.inlined.length >= split);
298 :
299 242 : head.refcount = NULL;
300 242 : head.data.inlined.length = (gpr_uint8)split;
301 242 : memcpy(head.data.inlined.bytes, source->data.inlined.bytes, split);
302 242 : source->data.inlined.length =
303 242 : (gpr_uint8)(source->data.inlined.length - split);
304 242 : memmove(source->data.inlined.bytes, source->data.inlined.bytes + split,
305 242 : source->data.inlined.length);
306 3137092 : } else if (split < sizeof(head.data.inlined.bytes)) {
307 438372 : GPR_ASSERT(source->data.refcounted.length >= split);
308 :
309 438372 : head.refcount = NULL;
310 438372 : head.data.inlined.length = (gpr_uint8)split;
311 438372 : memcpy(head.data.inlined.bytes, source->data.refcounted.bytes, split);
312 438372 : source->data.refcounted.bytes += split;
313 438372 : source->data.refcounted.length -= split;
314 : } else {
315 2698720 : GPR_ASSERT(source->data.refcounted.length >= split);
316 :
317 : /* Build the result */
318 2698720 : head.refcount = source->refcount;
319 : /* Bump the refcount */
320 2698720 : head.refcount->ref(head.refcount);
321 : /* Point into the source array */
322 2698744 : head.data.refcounted.bytes = source->data.refcounted.bytes;
323 2698744 : head.data.refcounted.length = split;
324 2698744 : source->data.refcounted.bytes += split;
325 2698744 : source->data.refcounted.length -= split;
326 : }
327 :
328 3137358 : return head;
329 : }
330 :
331 567 : int gpr_slice_cmp(gpr_slice a, gpr_slice b) {
332 567 : int d = (int)(GPR_SLICE_LENGTH(a) - GPR_SLICE_LENGTH(b));
333 567 : if (d != 0) return d;
334 617 : return memcmp(GPR_SLICE_START_PTR(a), GPR_SLICE_START_PTR(b),
335 617 : GPR_SLICE_LENGTH(a));
336 : }
337 :
338 4002432 : int gpr_slice_str_cmp(gpr_slice a, const char *b) {
339 4002432 : size_t b_length = strlen(b);
340 4002432 : int d = (int)(GPR_SLICE_LENGTH(a) - b_length);
341 4002432 : if (d != 0) return d;
342 4002428 : return memcmp(GPR_SLICE_START_PTR(a), b, b_length);
343 : }
|