2013-11-15 18:02:17 +00:00
|
|
|
// Copyright 2013 Google Inc. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
// Sliding window over the input data.
|
|
|
|
|
|
|
|
#ifndef BROTLI_ENC_RINGBUFFER_H_
|
|
|
|
#define BROTLI_ENC_RINGBUFFER_H_
|
|
|
|
|
2014-10-28 13:05:53 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2015-04-01 14:10:15 +00:00
|
|
|
#include "./port.h"
|
|
|
|
|
|
|
|
namespace brotli {
|
|
|
|
|
2013-11-15 18:02:17 +00:00
|
|
|
// A RingBuffer(window_bits, tail_bits) contains `1 << window_bits' bytes of
|
|
|
|
// data in a circular manner: writing a byte writes it to
|
|
|
|
// `position() % (1 << window_bits)'. For convenience, the RingBuffer array
|
|
|
|
// contains another copy of the first `1 << tail_bits' bytes:
|
|
|
|
// buffer_[i] == buffer_[i + (1 << window_bits)] if i < (1 << tail_bits).
|
|
|
|
class RingBuffer {
|
|
|
|
public:
|
|
|
|
RingBuffer(int window_bits, int tail_bits)
|
2015-04-01 14:10:15 +00:00
|
|
|
: window_bits_(window_bits),
|
|
|
|
mask_((1 << window_bits) - 1),
|
|
|
|
tail_size_(1 << tail_bits),
|
|
|
|
pos_(0) {
|
2015-08-28 14:09:23 +00:00
|
|
|
static const int kSlackForEightByteHashingEverywhere = 7;
|
2015-04-01 14:10:15 +00:00
|
|
|
const int buflen = (1 << window_bits_) + tail_size_;
|
2015-08-28 14:09:23 +00:00
|
|
|
buffer_ = new uint8_t[buflen + kSlackForEightByteHashingEverywhere];
|
|
|
|
for (int i = 0; i < kSlackForEightByteHashingEverywhere; ++i) {
|
2013-11-15 18:02:17 +00:00
|
|
|
buffer_[buflen + i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
~RingBuffer() {
|
|
|
|
delete [] buffer_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push bytes into the ring buffer.
|
|
|
|
void Write(const uint8_t *bytes, size_t n) {
|
2015-04-01 14:10:15 +00:00
|
|
|
const size_t masked_pos = pos_ & mask_;
|
2013-11-15 18:02:17 +00:00
|
|
|
// The length of the writes is limited so that we do not need to worry
|
|
|
|
// about a write
|
|
|
|
WriteTail(bytes, n);
|
2015-04-01 14:10:15 +00:00
|
|
|
if (PREDICT_TRUE(masked_pos + n <= (1 << window_bits_))) {
|
2013-11-15 18:02:17 +00:00
|
|
|
// A single write fits.
|
|
|
|
memcpy(&buffer_[masked_pos], bytes, n);
|
|
|
|
} else {
|
|
|
|
// Split into two writes.
|
|
|
|
// Copy into the end of the buffer, including the tail buffer.
|
|
|
|
memcpy(&buffer_[masked_pos], bytes,
|
2015-04-01 14:10:15 +00:00
|
|
|
std::min(n, ((1 << window_bits_) + tail_size_) - masked_pos));
|
2015-09-21 19:04:07 +00:00
|
|
|
// Copy into the beginning of the buffer
|
2013-11-15 18:02:17 +00:00
|
|
|
memcpy(&buffer_[0], bytes + ((1 << window_bits_) - masked_pos),
|
|
|
|
n - ((1 << window_bits_) - masked_pos));
|
|
|
|
}
|
|
|
|
pos_ += n;
|
|
|
|
}
|
|
|
|
|
2015-04-01 14:10:15 +00:00
|
|
|
void Reset() {
|
|
|
|
pos_ = 0;
|
|
|
|
}
|
|
|
|
|
2013-11-15 18:02:17 +00:00
|
|
|
// Logical cursor position in the ring buffer.
|
|
|
|
size_t position() const { return pos_; }
|
|
|
|
|
2015-04-01 14:10:15 +00:00
|
|
|
// Bit mask for getting the physical position for a logical position.
|
|
|
|
size_t mask() const { return mask_; }
|
|
|
|
|
2013-11-15 18:02:17 +00:00
|
|
|
uint8_t *start() { return &buffer_[0]; }
|
|
|
|
const uint8_t *start() const { return &buffer_[0]; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
void WriteTail(const uint8_t *bytes, size_t n) {
|
2015-04-01 14:10:15 +00:00
|
|
|
const size_t masked_pos = pos_ & mask_;
|
|
|
|
if (PREDICT_FALSE(masked_pos < tail_size_)) {
|
2013-11-15 18:02:17 +00:00
|
|
|
// Just fill the tail buffer with the beginning data.
|
|
|
|
const size_t p = (1 << window_bits_) + masked_pos;
|
2015-04-01 14:10:15 +00:00
|
|
|
memcpy(&buffer_[p], bytes, std::min(n, tail_size_ - masked_pos));
|
2013-11-15 18:02:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-01 14:10:15 +00:00
|
|
|
// Size of the ringbuffer is (1 << window_bits) + tail_size_.
|
2013-11-15 18:02:17 +00:00
|
|
|
const int window_bits_;
|
2015-04-01 14:10:15 +00:00
|
|
|
const size_t mask_;
|
|
|
|
const size_t tail_size_;
|
2013-11-15 18:02:17 +00:00
|
|
|
|
|
|
|
// Position to write in the ring buffer.
|
|
|
|
size_t pos_;
|
|
|
|
// The actual ring buffer containing the data and the copy of the beginning
|
|
|
|
// as a tail.
|
|
|
|
uint8_t *buffer_;
|
|
|
|
};
|
|
|
|
|
2015-04-01 14:10:15 +00:00
|
|
|
} // namespace brotli
|
|
|
|
|
2013-11-15 18:02:17 +00:00
|
|
|
#endif // BROTLI_ENC_RINGBUFFER_H_
|