ngtcp2_ringbuf.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * ngtcp2
  3. *
  4. * Copyright (c) 2017 ngtcp2 contributors
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #ifndef NGTCP2_RINGBUF_H
  26. #define NGTCP2_RINGBUF_H
  27. #ifdef HAVE_CONFIG_H
  28. # include <config.h>
  29. #endif /* defined(HAVE_CONFIG_H) */
  30. #include <ngtcp2/ngtcp2.h>
  31. #include "ngtcp2_mem.h"
  32. typedef struct ngtcp2_ringbuf {
  33. /* buf points to the underlying buffer. */
  34. uint8_t *buf;
  35. const ngtcp2_mem *mem;
  36. /* mask is the bit mask to cover all bits for the maximum number of
  37. elements. The maximum number of elements is mask + 1. */
  38. size_t mask;
  39. /* size is the size of each element. */
  40. size_t size;
  41. /* first is the offset to the first element. */
  42. size_t first;
  43. /* len is the number of elements actually stored. */
  44. size_t len;
  45. } ngtcp2_ringbuf;
  46. /*
  47. * ngtcp2_ringbuf_init initializes |rb|. |nmemb| is the number of
  48. * elements that can be stored in this buffer. |size| is the size of
  49. * each element. |nmemb| must be power of 2.
  50. *
  51. * This function returns 0 if it succeeds, or one of the following
  52. * negative error codes:
  53. *
  54. * NGTCP2_ERR_NOMEM
  55. * Out of memory.
  56. */
  57. int ngtcp2_ringbuf_init(ngtcp2_ringbuf *rb, size_t nmemb, size_t size,
  58. const ngtcp2_mem *mem);
  59. /*
  60. * ngtcp2_ringbuf_buf_init initializes |rb| with given buffer and
  61. * size. Same restrictions are applied as ngtcp2_ringbuf_init.
  62. */
  63. void ngtcp2_ringbuf_buf_init(ngtcp2_ringbuf *rb, size_t nmemb, size_t size,
  64. uint8_t *buf, const ngtcp2_mem *mem);
  65. /*
  66. * ngtcp2_ringbuf_free frees resources allocated for |rb|. This
  67. * function does not free the memory pointed by |rb|.
  68. */
  69. void ngtcp2_ringbuf_free(ngtcp2_ringbuf *rb);
  70. /* ngtcp2_ringbuf_push_front moves the offset to the first element in
  71. the buffer backward, and returns the pointer to the element.
  72. Caller can store data to the buffer pointed by the returned
  73. pointer. If this action exceeds the capacity of the ring buffer,
  74. this function returns the pointer to the last element, and rb->len
  75. remains unchanged. */
  76. void *ngtcp2_ringbuf_push_front(ngtcp2_ringbuf *rb);
  77. /* ngtcp2_ringbuf_push_back moves the offset to the last element in
  78. the buffer forward, and returns the pointer to the element. Caller
  79. can store data to the buffer pointed by the returned pointer. If
  80. this action exceeds the capacity of the ring buffer, this function
  81. returns the pointer to the first element, and rb->len remains
  82. unchanged. */
  83. void *ngtcp2_ringbuf_push_back(ngtcp2_ringbuf *rb);
  84. /*
  85. * ngtcp2_ringbuf_pop_front removes first element in |rb|.
  86. */
  87. void ngtcp2_ringbuf_pop_front(ngtcp2_ringbuf *rb);
  88. /*
  89. * ngtcp2_ringbuf_pop_back removes the last element in |rb|.
  90. */
  91. void ngtcp2_ringbuf_pop_back(ngtcp2_ringbuf *rb);
  92. /* ngtcp2_ringbuf_resize changes the number of elements stored. This
  93. does not change the capacity of the underlying buffer. */
  94. void ngtcp2_ringbuf_resize(ngtcp2_ringbuf *rb, size_t len);
  95. /* ngtcp2_ringbuf_get returns the pointer to the element at
  96. |offset|. */
  97. void *ngtcp2_ringbuf_get(const ngtcp2_ringbuf *rb, size_t offset);
  98. /* ngtcp2_ringbuf_len returns the number of elements stored. */
  99. #define ngtcp2_ringbuf_len(RB) ((RB)->len)
  100. /* ngtcp2_ringbuf_full returns nonzero if |rb| is full. */
  101. int ngtcp2_ringbuf_full(ngtcp2_ringbuf *rb);
  102. /* ngtcp2_static_ringbuf_def defines ngtcp2_ringbuf struct wrapper
  103. which uses a statically allocated buffer. ngtcp2_ringbuf_free
  104. should never be called for rb field. */
  105. #define ngtcp2_static_ringbuf_def(NAME, NMEMB, SIZE) \
  106. typedef struct ngtcp2_static_ringbuf_##NAME { \
  107. ngtcp2_ringbuf rb; \
  108. uint8_t buf[(NMEMB) * (SIZE)]; \
  109. } ngtcp2_static_ringbuf_##NAME; \
  110. \
  111. static inline void ngtcp2_static_ringbuf_##NAME##_init( \
  112. ngtcp2_static_ringbuf_##NAME *srb) { \
  113. ngtcp2_ringbuf_buf_init(&srb->rb, (NMEMB), (SIZE), srb->buf, NULL); \
  114. }
  115. #endif /* !defined(NGTCP2_RINGBUF_H) */