Coverage Report

Created: 2026-08-14 08:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjxl/lib/jxl/memory_manager_internal.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_MEMORY_MANAGER_INTERNAL_H_
7
#define LIB_JXL_MEMORY_MANAGER_INTERNAL_H_
8
9
// Memory allocator with support for alignment + misalignment.
10
11
#include <jxl/memory_manager.h>
12
13
#include <cstddef>
14
#include <memory>
15
#include <utility>
16
17
#include "lib/jxl/base/common.h"
18
#include "lib/jxl/base/compiler_specific.h"
19
#include "lib/jxl/base/status.h"
20
21
namespace jxl {
22
23
namespace memory_manager_internal {
24
25
// To avoid RFOs, match L2 fill size (pairs of lines); 2 x cache line size.
26
static constexpr size_t kAlignment = 2 * 64;
27
static_assert((kAlignment & (kAlignment - 1)) == 0,
28
              "kAlignment must be a power of 2");
29
30
// Minimum multiple for which cache set conflicts and/or loads blocked by
31
// preceding stores can occur.
32
static constexpr size_t kNumAlignmentGroups = 16;
33
static constexpr size_t kAlias = kNumAlignmentGroups * kAlignment;
34
static_assert((kNumAlignmentGroups & (kNumAlignmentGroups - 1)) == 0,
35
              "kNumAlignmentGroups must be a power of 2");
36
37
}  // namespace memory_manager_internal
38
39
// Initializes the memory manager instance with the passed one. The
40
// MemoryManager passed in |memory_manager| may be NULL or contain NULL
41
// functions which will be initialized with the default ones. If either alloc
42
// or free are NULL, then both must be NULL, otherwise this function returns an
43
// error.
44
Status MemoryManagerInit(JxlMemoryManager* self,
45
                         const JxlMemoryManager* memory_manager);
46
47
void* MemoryManagerAlloc(const JxlMemoryManager* memory_manager, size_t size);
48
void MemoryManagerFree(const JxlMemoryManager* memory_manager, void* address);
49
50
// Helper class to be used as a deleter in a unique_ptr<T> call.
51
class MemoryManagerDeleteHelper {
52
 public:
53
  explicit MemoryManagerDeleteHelper(const JxlMemoryManager* memory_manager)
54
1.56k
      : memory_manager_(memory_manager) {}
55
56
  // Delete and free the passed pointer using the memory_manager.
57
  template <typename T>
58
852
  void operator()(T* address) const {
59
852
    if (!address) {
60
0
      return;
61
0
    }
62
852
    address->~T();
63
852
    return memory_manager_->free(memory_manager_->opaque, address);
64
852
  }
void jxl::MemoryManagerDeleteHelper::operator()<jxl::JxlEncoderQueuedFrame>(jxl::JxlEncoderQueuedFrame*) const
Line
Count
Source
58
284
  void operator()(T* address) const {
59
284
    if (!address) {
60
0
      return;
61
0
    }
62
284
    address->~T();
63
284
    return memory_manager_->free(memory_manager_->opaque, address);
64
284
  }
Unexecuted instantiation: void jxl::MemoryManagerDeleteHelper::operator()<jxl::JxlEncoderQueuedBox>(jxl::JxlEncoderQueuedBox*) const
void jxl::MemoryManagerDeleteHelper::operator()<jxl::ThreadPool>(jxl::ThreadPool*) const
Line
Count
Source
58
284
  void operator()(T* address) const {
59
284
    if (!address) {
60
0
      return;
61
0
    }
62
284
    address->~T();
63
284
    return memory_manager_->free(memory_manager_->opaque, address);
64
284
  }
void jxl::MemoryManagerDeleteHelper::operator()<JxlEncoderFrameSettings>(JxlEncoderFrameSettings*) const
Line
Count
Source
58
284
  void operator()(T* address) const {
59
284
    if (!address) {
60
0
      return;
61
0
    }
62
284
    address->~T();
63
284
    return memory_manager_->free(memory_manager_->opaque, address);
64
284
  }
65
66
 private:
67
  const JxlMemoryManager* memory_manager_;
68
};
69
70
template <typename T>
71
using MemoryManagerUniquePtr = std::unique_ptr<T, MemoryManagerDeleteHelper>;
72
73
// Creates a new object T allocating it with the memory allocator into a
74
// unique_ptr; not to be used outside JXL_MEMORY_MANAGER_MAKE_UNIQUE_OR_RETURN.
75
template <typename T, typename... Args>
76
JXL_INLINE MemoryManagerUniquePtr<T> MemoryManagerMakeUniquePrivate(
77
852
    const JxlMemoryManager* memory_manager, Args&&... args) {
78
852
  T* mem =
79
852
      static_cast<T*>(memory_manager->alloc(memory_manager->opaque, sizeof(T)));
80
852
  if (!mem) {
81
    // Allocation error case.
82
0
    return MemoryManagerUniquePtr<T>(nullptr,
83
0
                                     MemoryManagerDeleteHelper(memory_manager));
84
0
  }
85
852
  return MemoryManagerUniquePtr<T>(new (mem) T(std::forward<Args>(args)...),
86
852
                                   MemoryManagerDeleteHelper(memory_manager));
87
852
}
std::__1::unique_ptr<JxlEncoderFrameSettings, jxl::MemoryManagerDeleteHelper> jxl::MemoryManagerMakeUniquePrivate<JxlEncoderFrameSettings>(JxlMemoryManagerStruct const*)
Line
Count
Source
77
284
    const JxlMemoryManager* memory_manager, Args&&... args) {
78
284
  T* mem =
79
284
      static_cast<T*>(memory_manager->alloc(memory_manager->opaque, sizeof(T)));
80
284
  if (!mem) {
81
    // Allocation error case.
82
0
    return MemoryManagerUniquePtr<T>(nullptr,
83
0
                                     MemoryManagerDeleteHelper(memory_manager));
84
0
  }
85
284
  return MemoryManagerUniquePtr<T>(new (mem) T(std::forward<Args>(args)...),
86
284
                                   MemoryManagerDeleteHelper(memory_manager));
87
284
}
std::__1::unique_ptr<jxl::ThreadPool, jxl::MemoryManagerDeleteHelper> jxl::MemoryManagerMakeUniquePrivate<jxl::ThreadPool, int (*&)(void*, void*, int (*)(void*, unsigned long), void (*)(void*, unsigned int, unsigned long), unsigned int, unsigned int), void*&>(JxlMemoryManagerStruct const*, int (*&)(void*, void*, int (*)(void*, unsigned long), void (*)(void*, unsigned int, unsigned long), unsigned int, unsigned int), void*&)
Line
Count
Source
77
284
    const JxlMemoryManager* memory_manager, Args&&... args) {
78
284
  T* mem =
79
284
      static_cast<T*>(memory_manager->alloc(memory_manager->opaque, sizeof(T)));
80
284
  if (!mem) {
81
    // Allocation error case.
82
0
    return MemoryManagerUniquePtr<T>(nullptr,
83
0
                                     MemoryManagerDeleteHelper(memory_manager));
84
0
  }
85
284
  return MemoryManagerUniquePtr<T>(new (mem) T(std::forward<Args>(args)...),
86
284
                                   MemoryManagerDeleteHelper(memory_manager));
87
284
}
std::__1::unique_ptr<jxl::JxlEncoderQueuedFrame, jxl::MemoryManagerDeleteHelper> jxl::MemoryManagerMakeUniquePrivate<jxl::JxlEncoderQueuedFrame, jxl::JxlEncoderQueuedFrame>(JxlMemoryManagerStruct const*, jxl::JxlEncoderQueuedFrame&&)
Line
Count
Source
77
284
    const JxlMemoryManager* memory_manager, Args&&... args) {
78
284
  T* mem =
79
284
      static_cast<T*>(memory_manager->alloc(memory_manager->opaque, sizeof(T)));
80
284
  if (!mem) {
81
    // Allocation error case.
82
0
    return MemoryManagerUniquePtr<T>(nullptr,
83
0
                                     MemoryManagerDeleteHelper(memory_manager));
84
0
  }
85
284
  return MemoryManagerUniquePtr<T>(new (mem) T(std::forward<Args>(args)...),
86
284
                                   MemoryManagerDeleteHelper(memory_manager));
87
284
}
Unexecuted instantiation: std::__1::unique_ptr<jxl::JxlEncoderQueuedBox, jxl::MemoryManagerDeleteHelper> jxl::MemoryManagerMakeUniquePrivate<jxl::JxlEncoderQueuedBox>(JxlMemoryManagerStruct const*)
88
89
// NOLINTBEGIN(bugprone-macro-parentheses)
90
// NB: ARGS should be in parentheses on instantiation side; it contains
91
//     arguments for MemoryManagerMakeUniquePrivate, including `memory_manager`.
92
#define JXL_MEMORY_MANAGER_MAKE_UNIQUE_OR_RETURN(NAME, TYPE, ARGS, RETURN) \
93
852
  auto NAME = ::jxl::MemoryManagerMakeUniquePrivate<TYPE> ARGS;            \
94
852
  if (!NAME) {                                                             \
95
0
    return RETURN;                                                         \
96
0
  }
97
// NOLINTEND(bugprone-macro-parentheses)
98
99
// Returns recommended distance in bytes between the start of two consecutive
100
// rows.
101
StatusOr<size_t> BytesPerRow(size_t xsize, size_t sizeof_t);
102
103
class AlignedMemory {
104
 public:
105
  AlignedMemory()
106
155M
      : allocation_(nullptr), memory_manager_(nullptr), address_(nullptr) {}
107
108
  // Copy disallowed.
109
  AlignedMemory(const AlignedMemory& other) = delete;
110
  AlignedMemory& operator=(const AlignedMemory& other) = delete;
111
112
  // Custom move.
113
  AlignedMemory(AlignedMemory&& other) noexcept;
114
  AlignedMemory& operator=(AlignedMemory&& other) noexcept;
115
116
  ~AlignedMemory();
117
118
  static StatusOr<AlignedMemory> Create(JxlMemoryManager* memory_manager,
119
                                        size_t size, size_t pre_padding = 0);
120
121
21.1k
  explicit operator bool() const noexcept { return (address_ != nullptr); }
122
123
  template <typename T>
124
4.30G
  T* address() const {
125
4.30G
    return reinterpret_cast<T*>(address_);
126
4.30G
  }
unsigned char* jxl::AlignedMemory::address<unsigned char>() const
Line
Count
Source
124
4.24G
  T* address() const {
125
4.24G
    return reinterpret_cast<T*>(address_);
126
4.24G
  }
void* jxl::AlignedMemory::address<void>() const
Line
Count
Source
124
27.5M
  T* address() const {
125
27.5M
    return reinterpret_cast<T*>(address_);
126
27.5M
  }
jxl::AliasTable::Entry* jxl::AlignedMemory::address<jxl::AliasTable::Entry>() const
Line
Count
Source
124
519k
  T* address() const {
125
519k
    return reinterpret_cast<T*>(address_);
126
519k
  }
unsigned int* jxl::AlignedMemory::address<unsigned int>() const
Line
Count
Source
124
78.3k
  T* address() const {
125
78.3k
    return reinterpret_cast<T*>(address_);
126
78.3k
  }
float* jxl::AlignedMemory::address<float>() const
Line
Count
Source
124
39.5M
  T* address() const {
125
39.5M
    return reinterpret_cast<T*>(address_);
126
39.5M
  }
long* jxl::AlignedMemory::address<long>() const
Line
Count
Source
124
52.1k
  T* address() const {
125
52.1k
    return reinterpret_cast<T*>(address_);
126
52.1k
  }
unsigned long* jxl::AlignedMemory::address<unsigned long>() const
Line
Count
Source
124
52.1k
  T* address() const {
125
52.1k
    return reinterpret_cast<T*>(address_);
126
52.1k
  }
int* jxl::AlignedMemory::address<int>() const
Line
Count
Source
124
42.1k
  T* address() const {
125
42.1k
    return reinterpret_cast<T*>(address_);
126
42.1k
  }
short* jxl::AlignedMemory::address<short>() const
Line
Count
Source
124
41.9k
  T* address() const {
125
41.9k
    return reinterpret_cast<T*>(address_);
126
41.9k
  }
unsigned short* jxl::AlignedMemory::address<unsigned short>() const
Line
Count
Source
124
390k
  T* address() const {
125
390k
    return reinterpret_cast<T*>(address_);
126
390k
  }
Unexecuted instantiation: double* jxl::AlignedMemory::address<double>() const
jxl::GroupDecCache* jxl::AlignedMemory::address<jxl::GroupDecCache>() const
Line
Count
Source
124
2.55k
  T* address() const {
125
2.55k
    return reinterpret_cast<T*>(address_);
126
2.55k
  }
Unexecuted instantiation: enc_coeff_order.cc:jxl::ComputeCoeffOrder(jxl::SpeedTier, jxl::ACImage const&, jxl::AcStrategyImage const&, jxl::FrameDimensions const&, unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int*)::PosAndCount* jxl::AlignedMemory::address<jxl::ComputeCoeffOrder(jxl::SpeedTier, jxl::ACImage const&, jxl::AcStrategyImage const&, jxl::FrameDimensions const&, unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int*)::PosAndCount>() const
127
1.36M
  JxlMemoryManager* memory_manager() const { return memory_manager_; }
128
129
  // TODO(eustas): we can offer "actually accessible" size; it is 0-2KiB bigger
130
  //               than requested size, due to generous alignment;
131
  //               might be useful for resizeable containers (e.g. PaddedBytes)
132
133
 private:
134
  AlignedMemory(JxlMemoryManager* memory_manager, void* allocation,
135
                size_t pre_padding);
136
137
  void* allocation_;
138
  JxlMemoryManager* memory_manager_;
139
  void* address_;
140
};
141
142
template <typename T>
143
class AlignedArray {
144
 public:
145
852
  AlignedArray() : size_(0) {}
146
147
  static StatusOr<AlignedArray> Create(JxlMemoryManager* memory_manager,
148
852
                                       size_t size) {
149
852
    size_t storage_size;
150
852
    if (!SafeMul(size, sizeof(T), storage_size)) {
151
0
      return JXL_FAILURE("Allocation too large");
152
0
    }
153
852
    JXL_ASSIGN_OR_RETURN(AlignedMemory storage,
154
852
                         AlignedMemory::Create(memory_manager, storage_size));
155
852
    T* items = storage.address<T>();
156
2.55k
    for (size_t i = 0; i < size; ++i) {
157
1.70k
      new (items + i) T();
158
1.70k
    }
159
852
    return AlignedArray<T>(std::move(storage), size);
160
852
  }
161
162
  // Copy disallowed.
163
  AlignedArray(const AlignedArray& other) = delete;
164
  AlignedArray& operator=(const AlignedArray& other) = delete;
165
166
  // Custom move.
167
1.70k
  AlignedArray(AlignedArray&& other) noexcept {
168
1.70k
    size_ = other.size_;
169
1.70k
    storage_ = std::move(other.storage_);
170
1.70k
    other.size_ = 0;
171
1.70k
  }
172
173
852
  AlignedArray& operator=(AlignedArray&& other) noexcept {
174
852
    if (this == &other) return *this;
175
852
    size_ = other.size_;
176
852
    storage_ = std::move(other.storage_);
177
852
    other.size_ = 0;
178
852
    return *this;
179
852
  }
180
181
3.40k
  ~AlignedArray() {
182
3.40k
    if (!size_) return;
183
852
    T* items = storage_.address<T>();
184
2.55k
    for (size_t i = 0; i < size_; ++i) {
185
1.70k
      items[i].~T();
186
1.70k
    }
187
852
  }
188
189
852
  T& operator[](const size_t i) {
190
852
    JXL_DASSERT(i < size_);
191
852
    return *(storage_.address<T>() + i);
192
852
  }
193
  const T& operator[](const size_t i) const {
194
    JXL_DASSERT(i < size_);
195
    return *(storage_.address<T>() + i);
196
  }
197
198
 private:
199
  explicit AlignedArray(AlignedMemory&& storage, size_t size)
200
852
      : size_(size), storage_(std::move(storage)) {}
201
  size_t size_;
202
  AlignedMemory storage_;
203
};
204
205
}  // namespace jxl
206
207
#endif  // LIB_JXL_MEMORY_MANAGER_INTERNAL_H_