Coverage Report

Created: 2026-03-12 07:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjxl/lib/jxl/padded_bytes.h
Line
Count
Source
1
// Copyright (c) the JPEG XL Project Authors. All rights reserved.
2
//
3
// Use of this source code is governed by a BSD-style
4
// license that can be found in the LICENSE file.
5
6
#ifndef LIB_JXL_BASE_PADDED_BYTES_H_
7
#define LIB_JXL_BASE_PADDED_BYTES_H_
8
9
// std::vector replacement with padding to reduce bounds checks in WriteBits
10
11
#include <jxl/memory_manager.h>
12
13
#include <algorithm>  // max
14
#include <cstddef>
15
#include <cstdint>
16
#include <cstring>  // memcpy
17
#include <initializer_list>
18
#include <utility>  // swap
19
20
#include "lib/jxl/base/status.h"
21
#include "lib/jxl/memory_manager_internal.h"
22
23
namespace jxl {
24
25
// Provides a subset of the std::vector interface with some differences:
26
// - allows BitWriter to write 64 bits at a time without bounds checking;
27
// - ONLY zero-initializes the first byte (required by BitWriter);
28
// - ensures cache-line alignment.
29
class PaddedBytes {
30
 public:
31
  // Required for output params.
32
  explicit PaddedBytes(JxlMemoryManager* memory_manager)
33
92.6M
      : memory_manager_(memory_manager), size_(0), capacity_(0) {}
34
35
  static StatusOr<PaddedBytes> WithInitialSpace(
36
1.52M
      JxlMemoryManager* memory_manager, size_t size) {
37
1.52M
    PaddedBytes result(memory_manager);
38
1.52M
    JXL_RETURN_IF_ERROR(result.Init(size));
39
1.52M
    return result;
40
1.52M
  }
41
42
  // Deleting copy constructor and copy assignment operator to prevent copying
43
  PaddedBytes(const PaddedBytes&) = delete;
44
  PaddedBytes& operator=(const PaddedBytes&) = delete;
45
46
  // default is not OK - need to set other.size_ to 0!
47
  PaddedBytes(PaddedBytes&& other) noexcept
48
3.04M
      : memory_manager_(other.memory_manager_),
49
3.04M
        size_(other.size_),
50
3.04M
        capacity_(other.capacity_),
51
3.04M
        data_(std::move(other.data_)) {
52
3.04M
    other.size_ = other.capacity_ = 0;
53
3.04M
  }
54
1.52M
  PaddedBytes& operator=(PaddedBytes&& other) noexcept {
55
1.52M
    memory_manager_ = other.memory_manager_;
56
1.52M
    size_ = other.size_;
57
1.52M
    capacity_ = other.capacity_;
58
1.52M
    data_ = std::move(other.data_);
59
60
1.52M
    if (&other != this) {
61
1.52M
      other.size_ = other.capacity_ = 0;
62
1.52M
    }
63
1.52M
    return *this;
64
1.52M
  }
65
66
639k
  JxlMemoryManager* memory_manager() const { return memory_manager_; }
67
68
0
  void swap(PaddedBytes& other) noexcept {
69
0
    std::swap(memory_manager_, other.memory_manager_);
70
0
    std::swap(size_, other.size_);
71
0
    std::swap(capacity_, other.capacity_);
72
0
    std::swap(data_, other.data_);
73
0
  }
74
75
  // If current capacity is greater than requested, then no-op. Otherwise
76
  // copies existing data to newly allocated "data_".
77
  // The new capacity will be at least 1.5 times the old capacity. This ensures
78
  // that we avoid quadratic behaviour.
79
80.8M
  Status reserve(size_t capacity) {
80
80.8M
    if (capacity <= capacity_) return true;
81
82
3.44M
    size_t new_capacity = std::max(capacity, 3 * capacity_ / 2);
83
84
3.44M
    return change_capacity(new_capacity);
85
80.8M
  }
86
87
0
  Status shrink_to_fit() { return change_capacity(size_); }
88
89
  // NOTE: unlike vector, this does not initialize the new data!
90
  // However, we guarantee that write_bits can safely append after
91
  // the resize, as we zero-initialize the first new byte of data.
92
  // If size < capacity(), does not invalidate the memory.
93
77.4M
  Status resize(size_t size) {
94
77.4M
    JXL_RETURN_IF_ERROR(reserve(size));
95
77.4M
    size_ = size;
96
77.4M
    return true;
97
77.4M
  }
98
99
  // resize(size) plus explicit initialization of the new data with `value`.
100
0
  Status resize(size_t size, uint8_t value) {
101
0
    size_t old_size = size_;
102
0
    JXL_RETURN_IF_ERROR(resize(size));
103
0
    if (size_ > old_size) {
104
0
      memset(data() + old_size, value, size_ - old_size);
105
0
    }
106
0
    return true;
107
0
  }
108
109
  // Amortized constant complexity due to exponential growth.
110
422M
  Status push_back(uint8_t x) {
111
422M
    if (size_ == capacity_) {
112
1.87M
      JXL_RETURN_IF_ERROR(reserve(capacity_ + 1));
113
1.87M
    }
114
115
422M
    data_.address<uint8_t>()[size_++] = x;
116
422M
    return true;
117
422M
  }
118
119
2.74G
  size_t size() const { return size_; }
120
0
  size_t capacity() const { return capacity_; }
121
122
2.18G
  uint8_t* data() { return data_.address<uint8_t>(); }
123
5.64k
  const uint8_t* data() const { return data_.address<uint8_t>(); }
124
125
  // std::vector operations implemented in terms of the public interface above.
126
127
2.86k
  void clear() {
128
    // Not passing on the Status, because resizing to 0 cannot fail.
129
2.86k
    static_cast<void>(resize(0));
130
2.86k
  }
131
533M
  bool empty() const { return size() == 0; }
132
133
0
  Status assign(std::initializer_list<uint8_t> il) {
134
0
    JXL_RETURN_IF_ERROR(resize(il.size()));
135
0
    memcpy(data(), il.begin(), il.size());
136
0
    return true;
137
0
  }
138
139
5.14M
  uint8_t* begin() { return data(); }
140
808
  const uint8_t* begin() const { return data(); }
141
2.57M
  uint8_t* end() { return begin() + size(); }
142
404
  const uint8_t* end() const { return begin() + size(); }
143
144
2.08G
  uint8_t& operator[](const size_t i) {
145
2.08G
    BoundsCheck(i);
146
2.08G
    return data()[i];
147
2.08G
  }
148
0
  const uint8_t& operator[](const size_t i) const {
149
0
    BoundsCheck(i);
150
0
    return data()[i];
151
0
  }
152
153
  template <typename T>
154
19.8M
  Status append(const T& other) {
155
19.8M
    return append(
156
19.8M
        reinterpret_cast<const uint8_t*>(other.data()),
157
19.8M
        reinterpret_cast<const uint8_t*>(other.data()) + other.size());
158
19.8M
  }
jxl::Status jxl::PaddedBytes::append<std::__1::array<unsigned char, 128ul> >(std::__1::array<unsigned char, 128ul> const&)
Line
Count
Source
154
9.78k
  Status append(const T& other) {
155
9.78k
    return append(
156
9.78k
        reinterpret_cast<const uint8_t*>(other.data()),
157
9.78k
        reinterpret_cast<const uint8_t*>(other.data()) + other.size());
158
9.78k
  }
jxl::Status jxl::PaddedBytes::append<std::__1::array<unsigned char, 4ul> >(std::__1::array<unsigned char, 4ul> const&)
Line
Count
Source
154
19.8M
  Status append(const T& other) {
155
19.8M
    return append(
156
19.8M
        reinterpret_cast<const uint8_t*>(other.data()),
157
19.8M
        reinterpret_cast<const uint8_t*>(other.data()) + other.size());
158
19.8M
  }
159
160
19.8M
  Status append(const uint8_t* begin, const uint8_t* end) {
161
19.8M
    if (end - begin > 0) {
162
19.8M
      size_t old_size = size();
163
19.8M
      JXL_RETURN_IF_ERROR(resize(size() + (end - begin)));
164
19.8M
      memcpy(data() + old_size, begin, end - begin);
165
19.8M
    }
166
19.8M
    return true;
167
19.8M
  }
168
169
 private:
170
1.52M
  Status Init(size_t size) {
171
1.52M
    size_ = size;
172
1.52M
    return reserve(size);
173
1.52M
  }
174
175
2.08G
  void BoundsCheck(size_t i) const {
176
    // <= is safe due to padding and required by BitWriter.
177
2.08G
    JXL_DASSERT(i <= size());
178
2.08G
  }
179
180
3.44M
  Status change_capacity(size_t new_capacity) {
181
3.44M
    JXL_DASSERT(new_capacity >= size_);
182
183
3.44M
    new_capacity = std::max<size_t>(64, new_capacity);
184
185
    // BitWriter writes up to 7 bytes past the end.
186
3.44M
    JXL_ASSIGN_OR_RETURN(
187
3.44M
        AlignedMemory new_data,
188
3.44M
        AlignedMemory::Create(memory_manager_, new_capacity + 8));
189
190
3.44M
    if (data_.address<void>() == nullptr) {
191
      // First allocation: ensure first byte is initialized (won't be copied).
192
3.32M
      new_data.address<uint8_t>()[0] = 0;
193
3.32M
    } else {
194
      // Subsequent resize: copy existing data to new location.
195
119k
      memmove(new_data.address<void>(), data_.address<void>(), size_);
196
      // Ensure that the first new byte is initialized, to allow write_bits to
197
      // safely append to the newly-resized PaddedBytes.
198
119k
      new_data.address<uint8_t>()[size_] = 0;
199
119k
    }
200
201
3.44M
    capacity_ = new_capacity;
202
3.44M
    data_ = std::move(new_data);
203
3.44M
    return true;
204
3.44M
  }
205
206
  JxlMemoryManager* memory_manager_;
207
  size_t size_;
208
  size_t capacity_;
209
  AlignedMemory data_;
210
};
211
212
}  // namespace jxl
213
214
#endif  // LIB_JXL_BASE_PADDED_BYTES_H_