ringbuffer.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* Copyright 2013 Google Inc. All Rights Reserved.
  2. Distributed under MIT license.
  3. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
  4. */
  5. /* Sliding window over the input data. */
  6. #ifndef BROTLI_ENC_RINGBUFFER_H_
  7. #define BROTLI_ENC_RINGBUFFER_H_
  8. #include <string.h> /* memcpy */
  9. #include "../common/platform.h"
  10. #include <brotli/types.h>
  11. #include "./memory.h"
  12. #include "./quality.h"
  13. #if defined(__cplusplus) || defined(c_plusplus)
  14. extern "C" {
  15. #endif
  16. /* A RingBuffer(window_bits, tail_bits) contains `1 << window_bits' bytes of
  17. data in a circular manner: writing a byte writes it to:
  18. `position() % (1 << window_bits)'.
  19. For convenience, the RingBuffer array contains another copy of the
  20. first `1 << tail_bits' bytes:
  21. buffer_[i] == buffer_[i + (1 << window_bits)], if i < (1 << tail_bits),
  22. and another copy of the last two bytes:
  23. buffer_[-1] == buffer_[(1 << window_bits) - 1] and
  24. buffer_[-2] == buffer_[(1 << window_bits) - 2]. */
  25. typedef struct RingBuffer {
  26. /* Size of the ring-buffer is (1 << window_bits) + tail_size_. */
  27. const uint32_t size_;
  28. const uint32_t mask_;
  29. const uint32_t tail_size_;
  30. const uint32_t total_size_;
  31. uint32_t cur_size_;
  32. /* Position to write in the ring buffer. */
  33. uint32_t pos_;
  34. /* The actual ring buffer containing the copy of the last two bytes, the data,
  35. and the copy of the beginning as a tail. */
  36. uint8_t* data_;
  37. /* The start of the ring-buffer. */
  38. uint8_t* buffer_;
  39. } RingBuffer;
  40. static BROTLI_INLINE void RingBufferInit(RingBuffer* rb) {
  41. rb->cur_size_ = 0;
  42. rb->pos_ = 0;
  43. rb->data_ = 0;
  44. rb->buffer_ = 0;
  45. }
  46. static BROTLI_INLINE void RingBufferSetup(
  47. const BrotliEncoderParams* params, RingBuffer* rb) {
  48. int window_bits = ComputeRbBits(params);
  49. int tail_bits = params->lgblock;
  50. *(uint32_t*)&rb->size_ = 1u << window_bits;
  51. *(uint32_t*)&rb->mask_ = (1u << window_bits) - 1;
  52. *(uint32_t*)&rb->tail_size_ = 1u << tail_bits;
  53. *(uint32_t*)&rb->total_size_ = rb->size_ + rb->tail_size_;
  54. }
  55. static BROTLI_INLINE void RingBufferFree(MemoryManager* m, RingBuffer* rb) {
  56. BROTLI_FREE(m, rb->data_);
  57. }
  58. /* Allocates or re-allocates data_ to the given length + plus some slack
  59. region before and after. Fills the slack regions with zeros. */
  60. static BROTLI_INLINE void RingBufferInitBuffer(
  61. MemoryManager* m, const uint32_t buflen, RingBuffer* rb) {
  62. static const size_t kSlackForEightByteHashingEverywhere = 7;
  63. uint8_t* new_data = BROTLI_ALLOC(
  64. m, uint8_t, 2 + buflen + kSlackForEightByteHashingEverywhere);
  65. size_t i;
  66. if (BROTLI_IS_OOM(m)) return;
  67. if (rb->data_) {
  68. memcpy(new_data, rb->data_,
  69. 2 + rb->cur_size_ + kSlackForEightByteHashingEverywhere);
  70. BROTLI_FREE(m, rb->data_);
  71. }
  72. rb->data_ = new_data;
  73. rb->cur_size_ = buflen;
  74. rb->buffer_ = rb->data_ + 2;
  75. rb->buffer_[-2] = rb->buffer_[-1] = 0;
  76. for (i = 0; i < kSlackForEightByteHashingEverywhere; ++i) {
  77. rb->buffer_[rb->cur_size_ + i] = 0;
  78. }
  79. }
  80. static BROTLI_INLINE void RingBufferWriteTail(
  81. const uint8_t* bytes, size_t n, RingBuffer* rb) {
  82. const size_t masked_pos = rb->pos_ & rb->mask_;
  83. if (BROTLI_PREDICT_FALSE(masked_pos < rb->tail_size_)) {
  84. /* Just fill the tail buffer with the beginning data. */
  85. const size_t p = rb->size_ + masked_pos;
  86. memcpy(&rb->buffer_[p], bytes,
  87. BROTLI_MIN(size_t, n, rb->tail_size_ - masked_pos));
  88. }
  89. }
  90. /* Push bytes into the ring buffer. */
  91. static BROTLI_INLINE void RingBufferWrite(
  92. MemoryManager* m, const uint8_t* bytes, size_t n, RingBuffer* rb) {
  93. if (rb->pos_ == 0 && n < rb->tail_size_) {
  94. /* Special case for the first write: to process the first block, we don't
  95. need to allocate the whole ring-buffer and we don't need the tail
  96. either. However, we do this memory usage optimization only if the
  97. first write is less than the tail size, which is also the input block
  98. size, otherwise it is likely that other blocks will follow and we
  99. will need to reallocate to the full size anyway. */
  100. rb->pos_ = (uint32_t)n;
  101. RingBufferInitBuffer(m, rb->pos_, rb);
  102. if (BROTLI_IS_OOM(m)) return;
  103. memcpy(rb->buffer_, bytes, n);
  104. return;
  105. }
  106. if (rb->cur_size_ < rb->total_size_) {
  107. /* Lazily allocate the full buffer. */
  108. RingBufferInitBuffer(m, rb->total_size_, rb);
  109. if (BROTLI_IS_OOM(m)) return;
  110. /* Initialize the last two bytes to zero, so that we don't have to worry
  111. later when we copy the last two bytes to the first two positions. */
  112. rb->buffer_[rb->size_ - 2] = 0;
  113. rb->buffer_[rb->size_ - 1] = 0;
  114. }
  115. {
  116. const size_t masked_pos = rb->pos_ & rb->mask_;
  117. /* The length of the writes is limited so that we do not need to worry
  118. about a write */
  119. RingBufferWriteTail(bytes, n, rb);
  120. if (BROTLI_PREDICT_TRUE(masked_pos + n <= rb->size_)) {
  121. /* A single write fits. */
  122. memcpy(&rb->buffer_[masked_pos], bytes, n);
  123. } else {
  124. /* Split into two writes.
  125. Copy into the end of the buffer, including the tail buffer. */
  126. memcpy(&rb->buffer_[masked_pos], bytes,
  127. BROTLI_MIN(size_t, n, rb->total_size_ - masked_pos));
  128. /* Copy into the beginning of the buffer */
  129. memcpy(&rb->buffer_[0], bytes + (rb->size_ - masked_pos),
  130. n - (rb->size_ - masked_pos));
  131. }
  132. }
  133. {
  134. BROTLI_BOOL not_first_lap = (rb->pos_ & (1u << 31)) != 0;
  135. uint32_t rb_pos_mask = (1u << 31) - 1;
  136. rb->buffer_[-2] = rb->buffer_[rb->size_ - 2];
  137. rb->buffer_[-1] = rb->buffer_[rb->size_ - 1];
  138. rb->pos_ = (rb->pos_ & rb_pos_mask) + (uint32_t)(n & rb_pos_mask);
  139. if (not_first_lap) {
  140. /* Wrap, but preserve not-a-first-lap feature. */
  141. rb->pos_ |= 1u << 31;
  142. }
  143. }
  144. }
  145. #if defined(__cplusplus) || defined(c_plusplus)
  146. } /* extern "C" */
  147. #endif
  148. #endif /* BROTLI_ENC_RINGBUFFER_H_ */