nghttp3_ringbuf.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * nghttp3
  3. *
  4. * Copyright (c) 2019 nghttp3 contributors
  5. * Copyright (c) 2017 ngtcp2 contributors
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining
  8. * a copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sublicense, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be
  16. * included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. */
  26. #ifndef NGHTTP3_RINGBUF_H
  27. #define NGHTTP3_RINGBUF_H
  28. #ifdef HAVE_CONFIG_H
  29. # include <config.h>
  30. #endif /* defined(HAVE_CONFIG_H) */
  31. #include <nghttp3/nghttp3.h>
  32. #include "nghttp3_mem.h"
  33. typedef struct nghttp3_ringbuf {
  34. /* buf points to the underlying buffer. */
  35. uint8_t *buf;
  36. const nghttp3_mem *mem;
  37. /* nmemb is the number of elements that can be stored in this ring
  38. buffer. */
  39. size_t nmemb;
  40. /* size is the size of each element. */
  41. size_t size;
  42. /* first is the offset to the first element. */
  43. size_t first;
  44. /* len is the number of elements actually stored. */
  45. size_t len;
  46. } nghttp3_ringbuf;
  47. /*
  48. * nghttp3_ringbuf_init initializes |rb|. |nmemb| is the number of
  49. * elements that can be stored in this buffer. |size| is the size of
  50. * each element. |size| must be power of 2.
  51. *
  52. * This function returns 0 if it succeeds, or one of the following
  53. * negative error codes:
  54. *
  55. * NGHTTP3_ERR_NOMEM
  56. * Out of memory.
  57. */
  58. int nghttp3_ringbuf_init(nghttp3_ringbuf *rb, size_t nmemb, size_t size,
  59. const nghttp3_mem *mem);
  60. /*
  61. * nghttp3_ringbuf_free frees resources allocated for |rb|. This
  62. * function does not free the memory pointed by |rb|.
  63. */
  64. void nghttp3_ringbuf_free(nghttp3_ringbuf *rb);
  65. /* nghttp3_ringbuf_push_front moves the offset to the first element in
  66. the buffer backward, and returns the pointer to the element.
  67. Caller can store data to the buffer pointed by the returned
  68. pointer. If this action exceeds the capacity of the ring buffer,
  69. the last element is silently overwritten, and rb->len remains
  70. unchanged. */
  71. void *nghttp3_ringbuf_push_front(nghttp3_ringbuf *rb);
  72. /* nghttp3_ringbuf_push_back moves the offset to the last element in
  73. the buffer forward, and returns the pointer to the element. Caller
  74. can store data to the buffer pointed by the returned pointer. If
  75. this action exceeds the capacity of the ring buffer, the first
  76. element is silently overwritten, and rb->len remains unchanged. */
  77. void *nghttp3_ringbuf_push_back(nghttp3_ringbuf *rb);
  78. /*
  79. * nghttp3_ringbuf_pop_front removes first element in |rb|.
  80. */
  81. void nghttp3_ringbuf_pop_front(nghttp3_ringbuf *rb);
  82. /*
  83. * nghttp3_ringbuf_pop_back removes the last element in |rb|.
  84. */
  85. void nghttp3_ringbuf_pop_back(nghttp3_ringbuf *rb);
  86. /* nghttp3_ringbuf_resize changes the number of elements stored. This
  87. does not change the capacity of the underlying buffer. */
  88. void nghttp3_ringbuf_resize(nghttp3_ringbuf *rb, size_t len);
  89. /* nghttp3_ringbuf_get returns the pointer to the element at
  90. |offset|. */
  91. void *nghttp3_ringbuf_get(nghttp3_ringbuf *rb, size_t offset);
  92. /* nghttp3_ringbuf_len returns the number of elements stored. */
  93. #define nghttp3_ringbuf_len(RB) ((RB)->len)
  94. /* nghttp3_ringbuf_full returns nonzero if |rb| is full. */
  95. int nghttp3_ringbuf_full(nghttp3_ringbuf *rb);
  96. int nghttp3_ringbuf_reserve(nghttp3_ringbuf *rb, size_t nmemb);
  97. #endif /* !defined(NGHTTP3_RINGBUF_H) */