byte_buffer_queue.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #include "src/core/surface/byte_buffer_queue.h"
  34. #include <grpc/support/alloc.h>
  35. #include <grpc/support/useful.h>
  36. static void bba_destroy(grpc_bbq_array *array, size_t start_pos) {
  37. size_t i;
  38. for (i = start_pos; i < array->count; i++) {
  39. grpc_byte_buffer_destroy(array->data[i]);
  40. }
  41. gpr_free(array->data);
  42. }
  43. /* Append an operation to an array, expanding as needed */
  44. static void bba_push(grpc_bbq_array *a, grpc_byte_buffer *buffer) {
  45. if (a->count == a->capacity) {
  46. a->capacity = GPR_MAX(a->capacity * 2, 8);
  47. a->data = gpr_realloc(a->data, sizeof(grpc_byte_buffer *) * a->capacity);
  48. }
  49. a->data[a->count++] = buffer;
  50. }
  51. void grpc_bbq_destroy(grpc_byte_buffer_queue *q) {
  52. bba_destroy(&q->filling, 0);
  53. bba_destroy(&q->draining, q->drain_pos);
  54. }
  55. int grpc_bbq_empty(grpc_byte_buffer_queue *q) {
  56. return (q->drain_pos == q->draining.count && q->filling.count == 0);
  57. }
  58. void grpc_bbq_push(grpc_byte_buffer_queue *q, grpc_byte_buffer *buffer) {
  59. bba_push(&q->filling, buffer);
  60. }
  61. void grpc_bbq_flush(grpc_byte_buffer_queue *q) {
  62. grpc_byte_buffer *bb;
  63. while ((bb = grpc_bbq_pop(q))) {
  64. grpc_byte_buffer_destroy(bb);
  65. }
  66. }
  67. grpc_byte_buffer *grpc_bbq_pop(grpc_byte_buffer_queue *q) {
  68. grpc_bbq_array temp_array;
  69. if (q->drain_pos == q->draining.count) {
  70. if (q->filling.count == 0) {
  71. return NULL;
  72. }
  73. q->draining.count = 0;
  74. q->drain_pos = 0;
  75. /* swap arrays */
  76. temp_array = q->filling;
  77. q->filling = q->draining;
  78. q->draining = temp_array;
  79. }
  80. return q->draining.data[q->drain_pos++];
  81. }