nghttp3_ringbuf.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #include "nghttp3_ringbuf.h"
  27. #include <assert.h>
  28. #include <string.h>
  29. #ifdef WIN32
  30. # include <intrin.h>
  31. #endif /* defined(WIN32) */
  32. #include "nghttp3_macro.h"
  33. #ifndef NDEBUG
  34. static int ispow2(size_t n) {
  35. # if defined(_MSC_VER) && !defined(__clang__) && \
  36. (defined(_M_ARM) || (defined(_M_ARM64) && _MSC_VER < 1941))
  37. return n && !(n & (n - 1));
  38. # elif defined(WIN32)
  39. return 1 == __popcnt((unsigned int)n);
  40. # else /* !((defined(_MSC_VER) && !defined(__clang__) && (defined(_M_ARM) || \
  41. (defined(_M_ARM64) && _MSC_VER < 1941))) || defined(WIN32)) */
  42. return 1 == __builtin_popcount((unsigned int)n);
  43. # endif /* !((defined(_MSC_VER) && !defined(__clang__) && (defined(_M_ARM) || \
  44. (defined(_M_ARM64) && _MSC_VER < 1941))) || defined(WIN32)) */
  45. }
  46. #endif /* !defined(NDEBUG) */
  47. int nghttp3_ringbuf_init(nghttp3_ringbuf *rb, size_t nmemb, size_t size,
  48. const nghttp3_mem *mem) {
  49. if (nmemb) {
  50. assert(ispow2(nmemb));
  51. rb->buf = nghttp3_mem_malloc(mem, nmemb * size);
  52. if (rb->buf == NULL) {
  53. return NGHTTP3_ERR_NOMEM;
  54. }
  55. } else {
  56. rb->buf = NULL;
  57. }
  58. rb->mem = mem;
  59. rb->nmemb = nmemb;
  60. rb->size = size;
  61. rb->first = 0;
  62. rb->len = 0;
  63. return 0;
  64. }
  65. void nghttp3_ringbuf_free(nghttp3_ringbuf *rb) {
  66. if (rb == NULL) {
  67. return;
  68. }
  69. nghttp3_mem_free(rb->mem, rb->buf);
  70. }
  71. void *nghttp3_ringbuf_push_front(nghttp3_ringbuf *rb) {
  72. rb->first = (rb->first - 1) & (rb->nmemb - 1);
  73. rb->len = nghttp3_min_size(rb->nmemb, rb->len + 1);
  74. return (void *)&rb->buf[rb->first * rb->size];
  75. }
  76. void *nghttp3_ringbuf_push_back(nghttp3_ringbuf *rb) {
  77. size_t offset = (rb->first + rb->len) & (rb->nmemb - 1);
  78. if (rb->len == rb->nmemb) {
  79. rb->first = (rb->first + 1) & (rb->nmemb - 1);
  80. } else {
  81. ++rb->len;
  82. }
  83. return (void *)&rb->buf[offset * rb->size];
  84. }
  85. void nghttp3_ringbuf_pop_front(nghttp3_ringbuf *rb) {
  86. rb->first = (rb->first + 1) & (rb->nmemb - 1);
  87. --rb->len;
  88. }
  89. void nghttp3_ringbuf_pop_back(nghttp3_ringbuf *rb) {
  90. assert(rb->len);
  91. --rb->len;
  92. }
  93. void nghttp3_ringbuf_resize(nghttp3_ringbuf *rb, size_t len) {
  94. assert(len <= rb->nmemb);
  95. rb->len = len;
  96. }
  97. void *nghttp3_ringbuf_get(nghttp3_ringbuf *rb, size_t offset) {
  98. assert(offset < rb->len);
  99. offset = (rb->first + offset) & (rb->nmemb - 1);
  100. return &rb->buf[offset * rb->size];
  101. }
  102. int nghttp3_ringbuf_full(nghttp3_ringbuf *rb) { return rb->len == rb->nmemb; }
  103. int nghttp3_ringbuf_reserve(nghttp3_ringbuf *rb, size_t nmemb) {
  104. uint8_t *buf;
  105. if (rb->nmemb >= nmemb) {
  106. return 0;
  107. }
  108. assert(ispow2(nmemb));
  109. buf = nghttp3_mem_malloc(rb->mem, nmemb * rb->size);
  110. if (buf == NULL) {
  111. return NGHTTP3_ERR_NOMEM;
  112. }
  113. if (rb->buf != NULL) {
  114. if (rb->first + rb->len <= rb->nmemb) {
  115. memcpy(buf, rb->buf + rb->first * rb->size, rb->len * rb->size);
  116. rb->first = 0;
  117. } else {
  118. memcpy(buf, rb->buf + rb->first * rb->size,
  119. (rb->nmemb - rb->first) * rb->size);
  120. memcpy(buf + (rb->nmemb - rb->first) * rb->size, rb->buf,
  121. (rb->len - (rb->nmemb - rb->first)) * rb->size);
  122. rb->first = 0;
  123. }
  124. nghttp3_mem_free(rb->mem, rb->buf);
  125. }
  126. rb->buf = buf;
  127. rb->nmemb = nmemb;
  128. return 0;
  129. }