// Copyright (c) the JPEG XL Project Authors. All rights reserved. // // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file.
// Provides a subset of the std::vector interface with some differences: // - allows BitWriter to write 64 bits at a time without bounds checking; // - ONLY zero-initializes the first byte (required by BitWriter); // - ensures cache-line alignment. class PaddedBytes { public: // Required for output params. explicit PaddedBytes(JxlMemoryManager* memory_manager)
: memory_manager_(memory_manager), size_(0), capacity_(0) {}
// If current capacity is greater than requested, then no-op. Otherwise // copies existing data to newly allocated "data_". // The new capacity will be at least 1.5 times the old capacity. This ensures // that we avoid quadratic behaviour.
Status reserve(size_t capacity) { if (capacity <= capacity_) returntrue;
// BitWriter writes up to 7 bytes past the end.
JXL_ASSIGN_OR_RETURN(
AlignedMemory new_data,
AlignedMemory::Create(memory_manager_, new_capacity + 8));
if (data_.address<void>() == nullptr) { // First allocation: ensure first byte is initialized (won't be copied).
new_data.address<uint8_t>()[0] = 0;
} else { // Subsequent resize: copy existing data to new location.
memmove(new_data.address<void>(), data_.address<void>(), size_); // Ensure that the first new byte is initialized, to allow write_bits to // safely append to the newly-resized PaddedBytes.
new_data.address<uint8_t>()[size_] = 0;
}
// NOTE: unlike vector, this does not initialize the new data! // However, we guarantee that write_bits can safely append after // the resize, as we zero-initialize the first new byte of data. // If size < capacity(), does not invalidate the memory.
Status resize(size_t size) {
JXL_RETURN_IF_ERROR(reserve(size));
size_ = size; returntrue;
}
// resize(size) plus explicit initialization of the new data with `value`.
Status resize(size_t size, uint8_t value) {
size_t old_size = size_;
JXL_RETURN_IF_ERROR(resize(size)); if (size_ > old_size) {
memset(data() + old_size, value, size_ - old_size);
} returntrue;
}
// Amortized constant complexity due to exponential growth.
Status push_back(uint8_t x) { if (size_ == capacity_) {
JXL_RETURN_IF_ERROR(reserve(capacity_ + 1));
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.