blob: 245d6ac15abb527835235840cb66a95cf1269dbc [file] [log] [blame]
Craig Tiller5dc3b302015-06-15 16:06:50 -07001/*
2 *
Craig Tiller8a9fd522016-03-25 17:09:29 -07003 * Copyright 2015-2016, Google Inc.
Craig Tiller5dc3b302015-06-15 16:06:50 -07004 * 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/transport/chttp2/incoming_metadata.h"
35
36#include <string.h>
37
38#include "src/core/transport/chttp2/internal.h"
39
40#include <grpc/support/alloc.h>
41#include <grpc/support/log.h>
42
Craig Tiller98505102015-06-16 11:33:15 -070043void grpc_chttp2_incoming_metadata_buffer_init(
44 grpc_chttp2_incoming_metadata_buffer *buffer) {
Craig Tiller143e7bf2015-07-13 08:41:49 -070045 buffer->deadline = gpr_inf_future(GPR_CLOCK_REALTIME);
Craig Tiller5dc3b302015-06-15 16:06:50 -070046}
47
Craig Tiller98505102015-06-16 11:33:15 -070048void grpc_chttp2_incoming_metadata_buffer_destroy(
49 grpc_chttp2_incoming_metadata_buffer *buffer) {
Craig Tillerf6d2f1f2015-06-30 12:33:29 -070050 size_t i;
Craig Tiller9d35a1f2015-11-02 14:16:12 -080051 if (!buffer->published) {
52 for (i = 0; i < buffer->count; i++) {
53 GRPC_MDELEM_UNREF(buffer->elems[i].md);
54 }
Craig Tillerf6d2f1f2015-06-30 12:33:29 -070055 }
Craig Tiller1937b062015-06-16 08:47:38 -070056 gpr_free(buffer->elems);
57}
58
Craig Tiller98505102015-06-16 11:33:15 -070059void grpc_chttp2_incoming_metadata_buffer_add(
60 grpc_chttp2_incoming_metadata_buffer *buffer, grpc_mdelem *elem) {
Craig Tiller9d35a1f2015-11-02 14:16:12 -080061 GPR_ASSERT(!buffer->published);
Craig Tiller5dc3b302015-06-15 16:06:50 -070062 if (buffer->capacity == buffer->count) {
Craig Tiller98505102015-06-16 11:33:15 -070063 buffer->capacity = GPR_MAX(8, 2 * buffer->capacity);
Craig Tiller5dc3b302015-06-15 16:06:50 -070064 buffer->elems =
Craig Tiller98505102015-06-16 11:33:15 -070065 gpr_realloc(buffer->elems, sizeof(*buffer->elems) * buffer->capacity);
Craig Tiller5dc3b302015-06-15 16:06:50 -070066 }
Craig Tiller98505102015-06-16 11:33:15 -070067 buffer->elems[buffer->count++].md = elem;
Craig Tiller5dc3b302015-06-15 16:06:50 -070068}
69
Craig Tiller98505102015-06-16 11:33:15 -070070void grpc_chttp2_incoming_metadata_buffer_set_deadline(
71 grpc_chttp2_incoming_metadata_buffer *buffer, gpr_timespec deadline) {
Craig Tiller9d35a1f2015-11-02 14:16:12 -080072 GPR_ASSERT(!buffer->published);
Craig Tiller5dc3b302015-06-15 16:06:50 -070073 buffer->deadline = deadline;
74}
75
Craig Tiller9d35a1f2015-11-02 14:16:12 -080076void grpc_chttp2_incoming_metadata_buffer_publish(
77 grpc_chttp2_incoming_metadata_buffer *buffer, grpc_metadata_batch *batch) {
78 GPR_ASSERT(!buffer->published);
79 buffer->published = 1;
80 if (buffer->count > 0) {
81 size_t i;
82 for (i = 1; i < buffer->count; i++) {
83 buffer->elems[i].prev = &buffer->elems[i - 1];
Craig Tiller98505102015-06-16 11:33:15 -070084 }
Craig Tiller9d35a1f2015-11-02 14:16:12 -080085 for (i = 0; i < buffer->count - 1; i++) {
86 buffer->elems[i].next = &buffer->elems[i + 1];
Craig Tiller98505102015-06-16 11:33:15 -070087 }
Craig Tiller9d35a1f2015-11-02 14:16:12 -080088 buffer->elems[0].prev = NULL;
89 buffer->elems[buffer->count - 1].next = NULL;
90 batch->list.head = &buffer->elems[0];
91 batch->list.tail = &buffer->elems[buffer->count - 1];
92 } else {
93 batch->list.head = batch->list.tail = NULL;
Craig Tiller98505102015-06-16 11:33:15 -070094 }
Craig Tiller9d35a1f2015-11-02 14:16:12 -080095 batch->deadline = buffer->deadline;
Craig Tiller98505102015-06-16 11:33:15 -070096}