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 "src/core/surface/byte_buffer_queue.h"
35 : #include <grpc/support/alloc.h>
36 : #include <grpc/support/useful.h>
37 :
38 5401499 : static void bba_destroy(grpc_bbq_array *array, size_t start_pos) {
39 : size_t i;
40 5401499 : for (i = start_pos; i < array->count; i++) {
41 0 : grpc_byte_buffer_destroy(array->data[i]);
42 : }
43 5401499 : gpr_free(array->data);
44 5401802 : }
45 :
46 : /* Append an operation to an array, expanding as needed */
47 3000093 : static void bba_push(grpc_bbq_array *a, grpc_byte_buffer *buffer) {
48 3000093 : if (a->count == a->capacity) {
49 2700090 : a->capacity = GPR_MAX(a->capacity * 2, 8);
50 2700090 : a->data = gpr_realloc(a->data, sizeof(grpc_byte_buffer *) * a->capacity);
51 : }
52 3000560 : a->data[a->count++] = buffer;
53 3000560 : }
54 :
55 2702323 : void grpc_bbq_destroy(grpc_byte_buffer_queue *q) {
56 2702323 : bba_destroy(&q->filling, 0);
57 2702568 : bba_destroy(&q->draining, q->drain_pos);
58 2703378 : }
59 :
60 14186147 : int grpc_bbq_empty(grpc_byte_buffer_queue *q) {
61 14186147 : return (q->drain_pos == q->draining.count && q->filling.count == 0);
62 : }
63 :
64 2999670 : void grpc_bbq_push(grpc_byte_buffer_queue *q, grpc_byte_buffer *buffer) {
65 2999670 : q->bytes += grpc_byte_buffer_length(buffer);
66 3000120 : bba_push(&q->filling, buffer);
67 3000715 : }
68 :
69 131 : void grpc_bbq_flush(grpc_byte_buffer_queue *q) {
70 : grpc_byte_buffer *bb;
71 413 : while ((bb = grpc_bbq_pop(q))) {
72 151 : grpc_byte_buffer_destroy(bb);
73 : }
74 131 : }
75 :
76 3123268 : size_t grpc_bbq_bytes(grpc_byte_buffer_queue *q) { return q->bytes; }
77 :
78 6692722 : grpc_byte_buffer *grpc_bbq_pop(grpc_byte_buffer_queue *q) {
79 : grpc_bbq_array temp_array;
80 : grpc_byte_buffer *out;
81 :
82 6692722 : if (q->drain_pos == q->draining.count) {
83 6684239 : if (q->filling.count == 0) {
84 3697959 : return NULL;
85 : }
86 2986280 : q->draining.count = 0;
87 2986280 : q->drain_pos = 0;
88 : /* swap arrays */
89 2986280 : temp_array = q->filling;
90 2986280 : q->filling = q->draining;
91 2986280 : q->draining = temp_array;
92 : }
93 :
94 2994763 : out = q->draining.data[q->drain_pos++];
95 2994763 : q->bytes -= grpc_byte_buffer_length(out);
96 2998642 : return out;
97 : }
|