Debian Codegraph @

src/core/lib/slice/slice_buffer.cc

raw
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
19 #include <grpc/support/port_platform.h>
20
21 #include "src/core/lib/slice/slice_buffer.h"
22
23 #include <string.h>
24
25 #include <cstdint>
26 #include <utility>
27
28 #include <grpc/slice.h>
29 #include <grpc/slice_buffer.h>
30 #include <grpc/support/alloc.h>
31 #include <grpc/support/log.h>
32
33 #include "src/core/lib/slice/slice_internal.h"
34
35 namespace grpc_core {
36
37 void SliceBuffer::Append(Slice slice) {
38 grpc_slice_buffer_add(&slice_buffer_, slice.TakeCSlice());
39 }
40
41 size_t SliceBuffer::AppendIndexed(Slice slice) {
42 return grpc_slice_buffer_add_indexed(&slice_buffer_, slice.TakeCSlice());
43 }
44
45 Slice SliceBuffer::TakeFirst() {
46 return Slice(grpc_slice_buffer_take_first(&slice_buffer_));
47 }
48
49 void SliceBuffer::Prepend(Slice slice) {
50 grpc_slice_buffer_undo_take_first(&slice_buffer_, slice.TakeCSlice());
51 }
52
53 Slice SliceBuffer::RefSlice(size_t index) const {
54 return Slice(CSliceRef(slice_buffer_.slices[index]));
55 }
56
57 std::string SliceBuffer::JoinIntoString() const {
58 std::string result;
59 result.reserve(slice_buffer_.length);
60 for (size_t i = 0; i < slice_buffer_.count; i++) {
61 result.append(reinterpret_cast<const char*>(
62 GRPC_SLICE_START_PTR(slice_buffer_.slices[i])),
63 GRPC_SLICE_LENGTH(slice_buffer_.slices[i]));
64 }
65 return result;
66 }
67
68 } // namespace grpc_core
69
70 /* grow a buffer; requires GRPC_SLICE_BUFFER_INLINE_ELEMENTS > 1 */
71 #define GROW(x) (3 * (x) / 2)
72
73 /* Typically, we do not actually need to embiggen (by calling
74 * memmove/malloc/realloc) - only if we were up against the full capacity of the
75 * slice buffer. If do_embiggen is inlined, the compiler clobbers multiple
76 * registers pointlessly in the common case. */
77 static void GPR_ATTRIBUTE_NOINLINE do_embiggen(grpc_slice_buffer* sb,
78 const size_t slice_count,
79 const size_t slice_offset) {
80 if (slice_offset != 0) {
81 /* Make room by moving elements if there's still space unused */
82 memmove(sb->base_slices, sb->slices, sb->count * sizeof(grpc_slice));
83 sb->slices = sb->base_slices;
84 } else {
85 /* Allocate more memory if no more space is available */
86 const size_t new_capacity = GROW(sb->capacity);
87 sb->capacity = new_capacity;
88 if (sb->base_slices == sb->inlined) {
89 sb->base_slices = static_cast<grpc_slice*>(
90 gpr_malloc(new_capacity * sizeof(grpc_slice)));
91 memcpy(sb->base_slices, sb->inlined, slice_count * sizeof(grpc_slice));
92 } else {
93 sb->base_slices = static_cast<grpc_slice*>(
94 gpr_realloc(sb->base_slices, new_capacity * sizeof(grpc_slice)));
95 }
96
97 sb->slices = sb->base_slices + slice_offset;
98 }
99 }
100
101 static void maybe_embiggen(grpc_slice_buffer* sb) {
102 if (sb->count == 0) {
103 sb->slices = sb->base_slices;
104 return;
105 }
106
107 /* How far away from sb->base_slices is sb->slices pointer */
108 size_t slice_offset = static_cast<size_t>(sb->slices - sb->base_slices);
109 size_t slice_count = sb->count + slice_offset;
110 if (GPR_UNLIKELY(slice_count == sb->capacity)) {
111 do_embiggen(sb, slice_count, slice_offset);
112 }
113 }
114
115 void grpc_slice_buffer_init(grpc_slice_buffer* sb) {
116 sb->count = 0;
117 sb->length = 0;
118 sb->capacity = GRPC_SLICE_BUFFER_INLINE_ELEMENTS;
119 sb->base_slices = sb->slices = sb->inlined;
120 }
121
122 void grpc_slice_buffer_destroy(grpc_slice_buffer* sb) {
123 grpc_slice_buffer_reset_and_unref(sb);
124 if (sb->base_slices != sb->inlined) {
125 gpr_free(sb->base_slices);
126 // As a precaution, set sb->base_slices to equal sb->inlined
127 // to prevent a double free attempt if grpc_slice_buffer_destroy
128 // is invoked two times on the same slice buffer.
129 sb->base_slices = sb->slices = sb->inlined;
130 }
131 }
132
133 uint8_t* grpc_slice_buffer_tiny_add(grpc_slice_buffer* sb, size_t n) {
134 grpc_slice* back;
135 uint8_t* out;
136
137 sb->length += n;
138
139 if (sb->count == 0) goto add_first;
140 back = &sb->slices[sb->count - 1];
141 if (back->refcount) goto add_new;
142 if ((back->data.inlined.length + n) > sizeof(back->data.inlined.bytes)) {
143 goto add_new;
144 }
145 out = back->data.inlined.bytes + back->data.inlined.length;
146 back->data.inlined.length =
147 static_cast<uint8_t>(back->data.inlined.length + n);
148 return out;
149
150 add_new:
151 maybe_embiggen(sb);
152 add_first:
153 back = &sb->slices[sb->count];
154 sb->count++;
155 back->refcount = nullptr;
156 back->data.inlined.length = static_cast<uint8_t>(n);
157 return back->data.inlined.bytes;
158 }
159
160 size_t grpc_slice_buffer_add_indexed(grpc_slice_buffer* sb, grpc_slice s) {
161 size_t out = sb->count;
162 maybe_embiggen(sb);
163 sb->slices[out] = s;
164 sb->length += GRPC_SLICE_LENGTH(s);
165 sb->count = out + 1;
166 return out;
167 }
168
169 void grpc_slice_buffer_add(grpc_slice_buffer* sb, grpc_slice s) {
170 size_t n = sb->count;
171 grpc_slice* back = nullptr;
172 if (n != 0) {
173 back = &sb->slices[n - 1];
174 }
175 if (s.refcount != nullptr && back != nullptr &&
176 s.refcount == back->refcount &&
177 GRPC_SLICE_START_PTR(s) == GRPC_SLICE_END_PTR(*back)) {
178 // Merge the two slices into one because they are contiguous and share the
179 // same refcount object.
180 back->data.refcounted.length += GRPC_SLICE_LENGTH(s);
181 sb->length += GRPC_SLICE_LENGTH(s);
182 // Unref the merged slice.
183 grpc_core::CSliceUnref(s);
184 // early out
185 return;
186 }
187
188 if (!s.refcount && n) {
189 /* if both the last slice in the slice buffer and the slice being added
190 are inlined (that is, that they carry their data inside the slice data
191 structure), and the back slice is not full, then concatenate directly
192 into the back slice, preventing many small slices being passed into
193 writes */
194 if (!back->refcount &&
195 back->data.inlined.length < GRPC_SLICE_INLINED_SIZE) {
196 if (s.data.inlined.length + back->data.inlined.length <=
197 GRPC_SLICE_INLINED_SIZE) {
198 memcpy(back->data.inlined.bytes + back->data.inlined.length,
199 s.data.inlined.bytes, s.data.inlined.length);
200 back->data.inlined.length = static_cast<uint8_t>(
201 back->data.inlined.length + s.data.inlined.length);
202 } else {
203 size_t cp1 = GRPC_SLICE_INLINED_SIZE - back->data.inlined.length;
204 memcpy(back->data.inlined.bytes + back->data.inlined.length,
205 s.data.inlined.bytes, cp1);
206 back->data.inlined.length = GRPC_SLICE_INLINED_SIZE;
207 maybe_embiggen(sb);
208 back = &sb->slices[n];
209 sb->count = n + 1;
210 back->refcount = nullptr;
211 back->data.inlined.length =
212 static_cast<uint8_t>(s.data.inlined.length - cp1);
213 memcpy(back->data.inlined.bytes, s.data.inlined.bytes + cp1,
214 s.data.inlined.length - cp1);
215 }
216 sb->length += s.data.inlined.length;
217 return; /* early out */
218 }
219 }
220 grpc_slice_buffer_add_indexed(sb, s);
221 }
222
223 void grpc_slice_buffer_addn(grpc_slice_buffer* sb, grpc_slice* s, size_t n) {
224 size_t i;
225 for (i = 0; i < n; i++) {
226 grpc_slice_buffer_add(sb, s[i]);
227 }
228 }
229
230 void grpc_slice_buffer_pop(grpc_slice_buffer* sb) {
231 if (sb->count != 0) {
232 size_t count = --sb->count;
233 sb->length -= GRPC_SLICE_LENGTH(sb->slices[count]);
234 }
235 }
236
237 void grpc_slice_buffer_reset_and_unref(grpc_slice_buffer* sb) {
238 size_t i;
239 for (i = 0; i < sb->count; i++) {
240 grpc_core::CSliceUnref(sb->slices[i]);
241 }
242
243 sb->count = 0;
244 sb->length = 0;
245 sb->slices = sb->base_slices;
246 }
247
248 void grpc_slice_buffer_swap(grpc_slice_buffer* a, grpc_slice_buffer* b) {
249 size_t a_offset = static_cast<size_t>(a->slices - a->base_slices);
250 size_t b_offset = static_cast<size_t>(b->slices - b->base_slices);
251
252 size_t a_count = a->count + a_offset;
253 size_t b_count = b->count + b_offset;
254
255 if (a->base_slices == a->inlined) {
256 if (b->base_slices == b->inlined) {
257 /* swap contents of inlined buffer */
258 grpc_slice temp[GRPC_SLICE_BUFFER_INLINE_ELEMENTS];
259 memcpy(temp, a->base_slices, a_count * sizeof(grpc_slice));
260 memcpy(a->base_slices, b->base_slices, b_count * sizeof(grpc_slice));
261 memcpy(b->base_slices, temp, a_count * sizeof(grpc_slice));
262 } else {
263 /* a is inlined, b is not - copy a inlined into b, fix pointers */
264 a->base_slices = b->base_slices;
265 b->base_slices = b->inlined;
266 memcpy(b->base_slices, a->inlined, a_count * sizeof(grpc_slice));
267 }
268 } else if (b->base_slices == b->inlined) {
269 /* b is inlined, a is not - copy b inlined int a, fix pointers */
270 b->base_slices = a->base_slices;
271 a->base_slices = a->inlined;
272 memcpy(a->base_slices, b->inlined, b_count * sizeof(grpc_slice));
273 } else {
274 /* no inlining: easy swap */
275 std::swap(a->base_slices, b->base_slices);
276 }
277
278 /* Update the slices pointers (cannot do a std::swap on slices fields here).
279 * Also note that since the base_slices pointers are already swapped we need
280 * use 'b_offset' for 'a->base_slices' and vice versa */
281 a->slices = a->base_slices + b_offset;
282 b->slices = b->base_slices + a_offset;
283
284 /* base_slices and slices fields are correctly set. Swap all other fields */
285 std::swap(a->count, b->count);
286 std::swap(a->capacity, b->capacity);
287 std::swap(a->length, b->length);
288 }
289
290 void grpc_slice_buffer_move_into(grpc_slice_buffer* src,
291 grpc_slice_buffer* dst) {
292 /* anything to move? */
293 if (src->count == 0) {
294 return;
295 }
296 /* anything in dst? */
297 if (dst->count == 0) {
298 grpc_slice_buffer_swap(src, dst);
299 return;
300 }
301 /* both buffers have data - copy, and reset src */
302 grpc_slice_buffer_addn(dst, src->slices, src->count);
303 src->count = 0;
304 src->length = 0;
305 }
306
307 template <bool incref>
308 static void slice_buffer_move_first_maybe_ref(grpc_slice_buffer* src, size_t n,
309 grpc_slice_buffer* dst) {
310 GPR_ASSERT(src->length >= n);
311 if (src->length == n) {
312 grpc_slice_buffer_move_into(src, dst);
313 return;
314 }
315
316 size_t output_len = dst->length + n;
317 size_t new_input_len = src->length - n;
318
319 while (src->count > 0) {
320 grpc_slice slice = grpc_slice_buffer_take_first(src);
321 size_t slice_len = GRPC_SLICE_LENGTH(slice);
322 if (n > slice_len) {
323 grpc_slice_buffer_add(dst, slice);
324 n -= slice_len;
325 } else if (n == slice_len) {
326 grpc_slice_buffer_add(dst, slice);
327 break;
328 } else if (incref) { /* n < slice_len */
329 grpc_slice_buffer_undo_take_first(
330 src, grpc_slice_split_tail_maybe_ref(&slice, n, GRPC_SLICE_REF_BOTH));
331 GPR_ASSERT(GRPC_SLICE_LENGTH(slice) == n);
332 grpc_slice_buffer_add(dst, slice);
333 break;
334 } else { /* n < slice_len */
335 grpc_slice_buffer_undo_take_first(
336 src, grpc_slice_split_tail_maybe_ref(&slice, n, GRPC_SLICE_REF_TAIL));
337 GPR_ASSERT(GRPC_SLICE_LENGTH(slice) == n);
338 grpc_slice_buffer_add_indexed(dst, slice);
339 break;
340 }
341 }
342 GPR_ASSERT(dst->length == output_len);
343 GPR_ASSERT(src->length == new_input_len);
344 GPR_ASSERT(src->count > 0);
345 }
346
347 void grpc_slice_buffer_move_first(grpc_slice_buffer* src, size_t n,
348 grpc_slice_buffer* dst) {
349 slice_buffer_move_first_maybe_ref<true>(src, n, dst);
350 }
351
352 void grpc_slice_buffer_move_first_no_ref(grpc_slice_buffer* src, size_t n,
353 grpc_slice_buffer* dst) {
354 slice_buffer_move_first_maybe_ref<false>(src, n, dst);
355 }
356
357 void grpc_slice_buffer_move_first_into_buffer(grpc_slice_buffer* src, size_t n,
358 void* dst) {
359 char* dstp = static_cast<char*>(dst);
360 GPR_ASSERT(src->length >= n);
361
362 while (n > 0) {
363 grpc_slice slice = grpc_slice_buffer_take_first(src);
364 size_t slice_len = GRPC_SLICE_LENGTH(slice);
365 if (slice_len > n) {
366 memcpy(dstp, GRPC_SLICE_START_PTR(slice), n);
367 grpc_slice_buffer_undo_take_first(
368 src, grpc_slice_sub_no_ref(slice, n, slice_len));
369 n = 0;
370 } else if (slice_len == n) {
371 memcpy(dstp, GRPC_SLICE_START_PTR(slice), n);
372 grpc_core::CSliceUnref(slice);
373 n = 0;
374 } else {
375 memcpy(dstp, GRPC_SLICE_START_PTR(slice), slice_len);
376 dstp += slice_len;
377 n -= slice_len;
378 grpc_core::CSliceUnref(slice);
379 }
380 }
381 }
382
383 void grpc_slice_buffer_copy_first_into_buffer(grpc_slice_buffer* src, size_t n,
384 void* dst) {
385 uint8_t* dstp = static_cast<uint8_t*>(dst);
386 GPR_ASSERT(src->length >= n);
387
388 for (size_t i = 0; i < src->count; i++) {
389 grpc_slice slice = src->slices[i];
390 size_t slice_len = GRPC_SLICE_LENGTH(slice);
391 if (slice_len >= n) {
392 memcpy(dstp, GRPC_SLICE_START_PTR(slice), n);
393 return;
394 }
395 memcpy(dstp, GRPC_SLICE_START_PTR(slice), slice_len);
396 dstp += slice_len;
397 n -= slice_len;
398 }
399 }
400
401 void grpc_slice_buffer_trim_end(grpc_slice_buffer* sb, size_t n,
402 grpc_slice_buffer* garbage) {
403 GPR_ASSERT(n <= sb->length);
404 sb->length -= n;
405 for (;;) {
406 size_t idx = sb->count - 1;
407 grpc_slice slice = sb->slices[idx];
408 size_t slice_len = GRPC_SLICE_LENGTH(slice);
409 if (slice_len > n) {
410 sb->slices[idx] = grpc_slice_split_head(&slice, slice_len - n);
411 if (garbage) {
412 grpc_slice_buffer_add_indexed(garbage, slice);
413 } else {
414 grpc_core::CSliceUnref(slice);
415 }
416 return;
417 } else if (slice_len == n) {
418 if (garbage) {
419 grpc_slice_buffer_add_indexed(garbage, slice);
420 } else {
421 grpc_core::CSliceUnref(slice);
422 }
423 sb->count = idx;
424 return;
425 } else {
426 if (garbage) {
427 grpc_slice_buffer_add_indexed(garbage, slice);
428 } else {
429 grpc_core::CSliceUnref(slice);
430 }
431 n -= slice_len;
432 sb->count = idx;
433 }
434 }
435 }
436
437 grpc_slice grpc_slice_buffer_take_first(grpc_slice_buffer* sb) {
438 grpc_slice slice;
439 GPR_ASSERT(sb->count > 0);
440 slice = sb->slices[0];
441 sb->slices++;
442 sb->count--;
443 sb->length -= GRPC_SLICE_LENGTH(slice);
444
445 return slice;
446 }
447
448 void grpc_slice_buffer_remove_first(grpc_slice_buffer* sb) {
449 GPR_DEBUG_ASSERT(sb->count > 0);
450 sb->length -= GRPC_SLICE_LENGTH(sb->slices[0]);
451 grpc_core::CSliceUnref(sb->slices[0]);
452 sb->slices++;
453 if (--sb->count == 0) {
454 sb->slices = sb->base_slices;
455 }
456 }
457
458 void grpc_slice_buffer_sub_first(grpc_slice_buffer* sb, size_t begin,
459 size_t end) {
460 // TODO(soheil): Introduce a ptr version for sub.
461 sb->length -= GRPC_SLICE_LENGTH(sb->slices[0]);
462 sb->slices[0] = grpc_slice_sub_no_ref(sb->slices[0], begin, end);
463 sb->length += end - begin;
464 }
465
466 void grpc_slice_buffer_undo_take_first(grpc_slice_buffer* sb,
467 grpc_slice slice) {
468 sb->slices--;
469 sb->slices[0] = slice;
470 sb->count++;
471 sb->length += GRPC_SLICE_LENGTH(slice);
472 }