Coverage Report

Created: 2025-11-16 07:14

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