Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 8a9fd52 | 2016-03-25 17:09:29 -0700 | [diff] [blame^] | 3 | * Copyright 2015-2016, Google Inc. |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 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/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 Tiller | 9850510 | 2015-06-16 11:33:15 -0700 | [diff] [blame] | 43 | void grpc_chttp2_incoming_metadata_buffer_init( |
| 44 | grpc_chttp2_incoming_metadata_buffer *buffer) { |
Craig Tiller | 143e7bf | 2015-07-13 08:41:49 -0700 | [diff] [blame] | 45 | buffer->deadline = gpr_inf_future(GPR_CLOCK_REALTIME); |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Craig Tiller | 9850510 | 2015-06-16 11:33:15 -0700 | [diff] [blame] | 48 | void grpc_chttp2_incoming_metadata_buffer_destroy( |
| 49 | grpc_chttp2_incoming_metadata_buffer *buffer) { |
Craig Tiller | f6d2f1f | 2015-06-30 12:33:29 -0700 | [diff] [blame] | 50 | size_t i; |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 51 | if (!buffer->published) { |
| 52 | for (i = 0; i < buffer->count; i++) { |
| 53 | GRPC_MDELEM_UNREF(buffer->elems[i].md); |
| 54 | } |
Craig Tiller | f6d2f1f | 2015-06-30 12:33:29 -0700 | [diff] [blame] | 55 | } |
Craig Tiller | 1937b06 | 2015-06-16 08:47:38 -0700 | [diff] [blame] | 56 | gpr_free(buffer->elems); |
| 57 | } |
| 58 | |
Craig Tiller | 9850510 | 2015-06-16 11:33:15 -0700 | [diff] [blame] | 59 | void grpc_chttp2_incoming_metadata_buffer_add( |
| 60 | grpc_chttp2_incoming_metadata_buffer *buffer, grpc_mdelem *elem) { |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 61 | GPR_ASSERT(!buffer->published); |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 62 | if (buffer->capacity == buffer->count) { |
Craig Tiller | 9850510 | 2015-06-16 11:33:15 -0700 | [diff] [blame] | 63 | buffer->capacity = GPR_MAX(8, 2 * buffer->capacity); |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 64 | buffer->elems = |
Craig Tiller | 9850510 | 2015-06-16 11:33:15 -0700 | [diff] [blame] | 65 | gpr_realloc(buffer->elems, sizeof(*buffer->elems) * buffer->capacity); |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 66 | } |
Craig Tiller | 9850510 | 2015-06-16 11:33:15 -0700 | [diff] [blame] | 67 | buffer->elems[buffer->count++].md = elem; |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Craig Tiller | 9850510 | 2015-06-16 11:33:15 -0700 | [diff] [blame] | 70 | void grpc_chttp2_incoming_metadata_buffer_set_deadline( |
| 71 | grpc_chttp2_incoming_metadata_buffer *buffer, gpr_timespec deadline) { |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 72 | GPR_ASSERT(!buffer->published); |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame] | 73 | buffer->deadline = deadline; |
| 74 | } |
| 75 | |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 76 | void 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 Tiller | 9850510 | 2015-06-16 11:33:15 -0700 | [diff] [blame] | 84 | } |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 85 | for (i = 0; i < buffer->count - 1; i++) { |
| 86 | buffer->elems[i].next = &buffer->elems[i + 1]; |
Craig Tiller | 9850510 | 2015-06-16 11:33:15 -0700 | [diff] [blame] | 87 | } |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 88 | 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 Tiller | 9850510 | 2015-06-16 11:33:15 -0700 | [diff] [blame] | 94 | } |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 95 | batch->deadline = buffer->deadline; |
Craig Tiller | 9850510 | 2015-06-16 11:33:15 -0700 | [diff] [blame] | 96 | } |