Coverage Report

Created: 2026-04-01 07:24

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
44.3k
      : memory_manager_(memory_manager), size_(0), capacity_(0) {}
34
35
  static StatusOr<PaddedBytes> WithInitialSpace(
36
17.8k
      JxlMemoryManager* memory_manager, size_t size) {
37
17.8k
    PaddedBytes result(memory_manager);
38
17.8k
    JXL_RETURN_IF_ERROR(result.Init(size));
39
17.8k
    return result;
40
17.8k
  }
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
35.6k
      : memory_manager_(other.memory_manager_),
49
35.6k
        size_(other.size_),
50
35.6k
        capacity_(other.capacity_),
51
35.6k
        data_(std::move(other.data_)) {
52
35.6k
    other.size_ = other.capacity_ = 0;
53
35.6k
  }
54
17.8k
  PaddedBytes& operator=(PaddedBytes&& other) noexcept {
55
17.8k
    memory_manager_ = other.memory_manager_;
56
17.8k
    size_ = other.size_;
57
17.8k
    capacity_ = other.capacity_;
58
17.8k
    data_ = std::move(other.data_);
59
60
17.8k
    if (&other != this) {
61
17.8k
      other.size_ = other.capacity_ = 0;
62
17.8k
    }
63
17.8k
    return *this;
64
17.8k
  }
65
66
7.28k
  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
245k
  Status reserve(size_t capacity) {
80
245k
    if (capacity <= capacity_) return true;
81
82
45.5k
    size_t new_capacity = std::max(capacity, 3 * capacity_ / 2);
83
84
45.5k
    return change_capacity(new_capacity);
85
245k
  }
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
212k
  Status resize(size_t size) {
94
212k
    JXL_RETURN_IF_ERROR(reserve(size));
95
212k
    size_ = size;
96
212k
    return true;
97
212k
  }
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
5.44M
  Status push_back(uint8_t x) {
111
5.44M
    if (size_ == capacity_) {
112
14.6k
      JXL_RETURN_IF_ERROR(reserve(capacity_ + 1));
113
14.6k
    }
114
115
5.44M
    data_.address<uint8_t>()[size_++] = x;
116
5.44M
    return true;
117
5.44M
  }
118
119
1.22M
  size_t size() const { return size_; }
120
0
  size_t capacity() const { return capacity_; }
121
122
86.4M
  uint8_t* data() { return data_.address<uint8_t>(); }
123
7.02k
  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
6.15k
  void clear() {
128
    // Not passing on the Status, because resizing to 0 cannot fail.
129
6.15k
    static_cast<void>(resize(0));
130
6.15k
  }
131
7.00k
  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
0
  uint8_t* begin() { return data(); }
140
0
  const uint8_t* begin() const { return data(); }
141
0
  uint8_t* end() { return begin() + size(); }
142
0
  const uint8_t* end() const { return begin() + size(); }
143
144
85.4M
  uint8_t& operator[](const size_t i) {
145
85.4M
    BoundsCheck(i);
146
85.4M
    return data()[i];
147
85.4M
  }
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
71.2k
  Status append(const T& other) {
155
71.2k
    return append(
156
71.2k
        reinterpret_cast<const uint8_t*>(other.data()),
157
71.2k
        reinterpret_cast<const uint8_t*>(other.data()) + other.size());
158
71.2k
  }
jxl::Status jxl::PaddedBytes::append<std::__1::array<unsigned char, 4ul> >(std::__1::array<unsigned char, 4ul> const&)
Line
Count
Source
154
67.7k
  Status append(const T& other) {
155
67.7k
    return append(
156
67.7k
        reinterpret_cast<const uint8_t*>(other.data()),
157
67.7k
        reinterpret_cast<const uint8_t*>(other.data()) + other.size());
158
67.7k
  }
jxl::Status jxl::PaddedBytes::append<std::__1::array<unsigned char, 128ul> >(std::__1::array<unsigned char, 128ul> const&)
Line
Count
Source
154
3.50k
  Status append(const T& other) {
155
3.50k
    return append(
156
3.50k
        reinterpret_cast<const uint8_t*>(other.data()),
157
3.50k
        reinterpret_cast<const uint8_t*>(other.data()) + other.size());
158
3.50k
  }
159
160
71.2k
  Status append(const uint8_t* begin, const uint8_t* end) {
161
71.2k
    if (end - begin > 0) {
162
71.2k
      size_t old_size = size();
163
71.2k
      JXL_RETURN_IF_ERROR(resize(size() + (end - begin)));
164
71.2k
      memcpy(data() + old_size, begin, end - begin);
165
71.2k
    }
166
71.2k
    return true;
167
71.2k
  }
168
169
 private:
170
17.8k
  Status Init(size_t size) {
171
17.8k
    size_ = size;
172
17.8k
    return reserve(size);
173
17.8k
  }
174
175
85.4M
  void BoundsCheck(size_t i) const {
176
    // <= is safe due to padding and required by BitWriter.
177
85.4M
    JXL_DASSERT(i <= size());
178
85.4M
  }
179
180
45.5k
  Status change_capacity(size_t new_capacity) {
181
45.5k
    JXL_DASSERT(new_capacity >= size_);
182
183
45.5k
    new_capacity = std::max<size_t>(64, new_capacity);
184
185
    // BitWriter writes up to 7 bytes past the end.
186
45.5k
    JXL_ASSIGN_OR_RETURN(
187
45.5k
        AlignedMemory new_data,
188
45.5k
        AlignedMemory::Create(memory_manager_, new_capacity + 8));
189
190
45.5k
    if (data_.address<void>() == nullptr) {
191
      // First allocation: ensure first byte is initialized (won't be copied).
192
26.0k
      new_data.address<uint8_t>()[0] = 0;
193
26.0k
    } else {
194
      // Subsequent resize: copy existing data to new location.
195
19.5k
      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
19.5k
      new_data.address<uint8_t>()[size_] = 0;
199
19.5k
    }
200
201
45.5k
    capacity_ = new_capacity;
202
45.5k
    data_ = std::move(new_data);
203
45.5k
    return true;
204
45.5k
  }
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_