9#ifndef BROTLI_ENC_RINGBUFFER_H_
10#define BROTLI_ENC_RINGBUFFER_H_
16#include "../common/platform.h"
20#if defined(__cplusplus) || defined(c_plusplus)
37 const uint32_t tail_size_;
38 const uint32_t total_size_;
50static BROTLI_INLINE
void RingBufferInit(
RingBuffer* rb) {
57static BROTLI_INLINE
void RingBufferSetup(
59 int window_bits = ComputeRbBits(params);
60 int tail_bits = params->lgblock;
61 *(uint32_t*)&rb->size_ = 1u << window_bits;
62 *(uint32_t*)&rb->mask_ = (1u << window_bits) - 1;
63 *(uint32_t*)&rb->tail_size_ = 1u << tail_bits;
64 *(uint32_t*)&rb->total_size_ = rb->size_ + rb->tail_size_;
68 BROTLI_FREE(m, rb->data_);
73static BROTLI_INLINE
void RingBufferInitBuffer(
75 static const size_t kSlackForEightByteHashingEverywhere = 7;
76 uint8_t* new_data = BROTLI_ALLOC(
77 m, uint8_t, 2 + buflen + kSlackForEightByteHashingEverywhere);
79 if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(new_data))
return;
81 memcpy(new_data, rb->data_,
82 2 + rb->cur_size_ + kSlackForEightByteHashingEverywhere);
83 BROTLI_FREE(m, rb->data_);
86 rb->cur_size_ = buflen;
87 rb->buffer_ = rb->data_ + 2;
88 rb->buffer_[-2] = rb->buffer_[-1] = 0;
89 for (i = 0; i < kSlackForEightByteHashingEverywhere; ++i) {
90 rb->buffer_[rb->cur_size_ + i] = 0;
94static BROTLI_INLINE
void RingBufferWriteTail(
95 const uint8_t* bytes,
size_t n,
RingBuffer* rb) {
96 const size_t masked_pos = rb->pos_ & rb->mask_;
97 if (BROTLI_PREDICT_FALSE(masked_pos < rb->tail_size_)) {
99 const size_t p = rb->size_ + masked_pos;
100 memcpy(&rb->buffer_[p], bytes,
101 BROTLI_MIN(
size_t, n, rb->tail_size_ - masked_pos));
106static BROTLI_INLINE
void RingBufferWrite(
108 if (rb->pos_ == 0 && n < rb->tail_size_) {
115 rb->pos_ = (uint32_t)n;
116 RingBufferInitBuffer(m, rb->pos_, rb);
117 if (BROTLI_IS_OOM(m))
return;
118 memcpy(rb->buffer_, bytes, n);
121 if (rb->cur_size_ < rb->total_size_) {
123 RingBufferInitBuffer(m, rb->total_size_, rb);
124 if (BROTLI_IS_OOM(m))
return;
127 rb->buffer_[rb->size_ - 2] = 0;
128 rb->buffer_[rb->size_ - 1] = 0;
131 rb->buffer_[rb->size_] = 241;
134 const size_t masked_pos = rb->pos_ & rb->mask_;
137 RingBufferWriteTail(bytes, n, rb);
138 if (BROTLI_PREDICT_TRUE(masked_pos + n <= rb->size_)) {
140 memcpy(&rb->buffer_[masked_pos], bytes, n);
144 memcpy(&rb->buffer_[masked_pos], bytes,
145 BROTLI_MIN(
size_t, n, rb->total_size_ - masked_pos));
147 memcpy(&rb->buffer_[0], bytes + (rb->size_ - masked_pos),
148 n - (rb->size_ - masked_pos));
152 BROTLI_BOOL not_first_lap = (rb->pos_ & (1u << 31)) != 0;
153 uint32_t rb_pos_mask = (1u << 31) - 1;
154 rb->buffer_[-2] = rb->buffer_[rb->size_ - 2];
155 rb->buffer_[-1] = rb->buffer_[rb->size_ - 1];
156 rb->pos_ = (rb->pos_ & rb_pos_mask) + (uint32_t)(n & rb_pos_mask);
159 rb->pos_ |= 1u << 31;
164#if defined(__cplusplus) || defined(c_plusplus)
Definition: ringbuffer.h:33
Common types used in decoder and encoder API.
#define BROTLI_BOOL
A portable bool replacement.
Definition: types.h:49