Coverage Report

Created: 2025-08-12 07:37

/src/libjxl/lib/jxl/memory_manager_internal.h
Line
Count
Source (jump to first uncovered line)
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.32k
      : memory_manager_(memory_manager) {}
54
55
  // Delete and free the passed pointer using the memory_manager.
56
  template <typename T>
57
667
  void operator()(T* address) const {
58
667
    if (!address) {
59
0
      return;
60
0
    }
61
667
    address->~T();
62
667
    return memory_manager_->free(memory_manager_->opaque, address);
63
667
  }
void jxl::MemoryManagerDeleteHelper::operator()<jxl::JxlEncoderQueuedFrame>(jxl::JxlEncoderQueuedFrame*) const
Line
Count
Source
57
162
  void operator()(T* address) const {
58
162
    if (!address) {
59
0
      return;
60
0
    }
61
162
    address->~T();
62
162
    return memory_manager_->free(memory_manager_->opaque, address);
63
162
  }
void jxl::MemoryManagerDeleteHelper::operator()<jxl::JxlEncoderQueuedBox>(jxl::JxlEncoderQueuedBox*) const
Line
Count
Source
57
43
  void operator()(T* address) const {
58
43
    if (!address) {
59
0
      return;
60
0
    }
61
43
    address->~T();
62
43
    return memory_manager_->free(memory_manager_->opaque, address);
63
43
  }
void jxl::MemoryManagerDeleteHelper::operator()<JxlEncoderFrameSettings>(JxlEncoderFrameSettings*) const
Line
Count
Source
57
215
  void operator()(T* address) const {
58
215
    if (!address) {
59
0
      return;
60
0
    }
61
215
    address->~T();
62
215
    return memory_manager_->free(memory_manager_->opaque, address);
63
215
  }
void jxl::MemoryManagerDeleteHelper::operator()<jxl::ThreadPool>(jxl::ThreadPool*) const
Line
Count
Source
57
247
  void operator()(T* address) const {
58
247
    if (!address) {
59
0
      return;
60
0
    }
61
247
    address->~T();
62
247
    return memory_manager_->free(memory_manager_->opaque, address);
63
247
  }
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
667
    const JxlMemoryManager* memory_manager, Args&&... args) {
77
667
  T* mem =
78
667
      static_cast<T*>(memory_manager->alloc(memory_manager->opaque, sizeof(T)));
79
667
  if (!mem) {
80
    // Allocation error case.
81
0
    return MemoryManagerUniquePtr<T>(nullptr,
82
0
                                     MemoryManagerDeleteHelper(memory_manager));
83
0
  }
84
667
  return MemoryManagerUniquePtr<T>(new (mem) T(std::forward<Args>(args)...),
85
667
                                   MemoryManagerDeleteHelper(memory_manager));
86
667
}
std::__1::unique_ptr<JxlEncoderFrameSettings, jxl::MemoryManagerDeleteHelper> jxl::MemoryManagerMakeUniquePrivate<JxlEncoderFrameSettings>(JxlMemoryManagerStruct const*)
Line
Count
Source
76
215
    const JxlMemoryManager* memory_manager, Args&&... args) {
77
215
  T* mem =
78
215
      static_cast<T*>(memory_manager->alloc(memory_manager->opaque, sizeof(T)));
79
215
  if (!mem) {
80
    // Allocation error case.
81
0
    return MemoryManagerUniquePtr<T>(nullptr,
82
0
                                     MemoryManagerDeleteHelper(memory_manager));
83
0
  }
84
215
  return MemoryManagerUniquePtr<T>(new (mem) T(std::forward<Args>(args)...),
85
215
                                   MemoryManagerDeleteHelper(memory_manager));
86
215
}
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
247
    const JxlMemoryManager* memory_manager, Args&&... args) {
77
247
  T* mem =
78
247
      static_cast<T*>(memory_manager->alloc(memory_manager->opaque, sizeof(T)));
79
247
  if (!mem) {
80
    // Allocation error case.
81
0
    return MemoryManagerUniquePtr<T>(nullptr,
82
0
                                     MemoryManagerDeleteHelper(memory_manager));
83
0
  }
84
247
  return MemoryManagerUniquePtr<T>(new (mem) T(std::forward<Args>(args)...),
85
247
                                   MemoryManagerDeleteHelper(memory_manager));
86
247
}
std::__1::unique_ptr<jxl::JxlEncoderQueuedFrame, jxl::MemoryManagerDeleteHelper> jxl::MemoryManagerMakeUniquePrivate<jxl::JxlEncoderQueuedFrame, jxl::JxlEncoderQueuedFrame>(JxlMemoryManagerStruct const*, jxl::JxlEncoderQueuedFrame&&)
Line
Count
Source
76
162
    const JxlMemoryManager* memory_manager, Args&&... args) {
77
162
  T* mem =
78
162
      static_cast<T*>(memory_manager->alloc(memory_manager->opaque, sizeof(T)));
79
162
  if (!mem) {
80
    // Allocation error case.
81
0
    return MemoryManagerUniquePtr<T>(nullptr,
82
0
                                     MemoryManagerDeleteHelper(memory_manager));
83
0
  }
84
162
  return MemoryManagerUniquePtr<T>(new (mem) T(std::forward<Args>(args)...),
85
162
                                   MemoryManagerDeleteHelper(memory_manager));
86
162
}
std::__1::unique_ptr<jxl::JxlEncoderQueuedBox, jxl::MemoryManagerDeleteHelper> jxl::MemoryManagerMakeUniquePrivate<jxl::JxlEncoderQueuedBox>(JxlMemoryManagerStruct const*)
Line
Count
Source
76
43
    const JxlMemoryManager* memory_manager, Args&&... args) {
77
43
  T* mem =
78
43
      static_cast<T*>(memory_manager->alloc(memory_manager->opaque, sizeof(T)));
79
43
  if (!mem) {
80
    // Allocation error case.
81
0
    return MemoryManagerUniquePtr<T>(nullptr,
82
0
                                     MemoryManagerDeleteHelper(memory_manager));
83
0
  }
84
43
  return MemoryManagerUniquePtr<T>(new (mem) T(std::forward<Args>(args)...),
85
43
                                   MemoryManagerDeleteHelper(memory_manager));
86
43
}
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
667
  auto NAME = ::jxl::MemoryManagerMakeUniquePrivate<TYPE> ARGS;            \
93
667
  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
36.8M
      : 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
4.18k
  explicit operator bool() const noexcept { return (address_ != nullptr); }
121
122
  template <typename T>
123
768M
  T* address() const {
124
768M
    return reinterpret_cast<T*>(address_);
125
768M
  }
void* jxl::AlignedMemory::address<void>() const
Line
Count
Source
123
33.2M
  T* address() const {
124
33.2M
    return reinterpret_cast<T*>(address_);
125
33.2M
  }
unsigned char* jxl::AlignedMemory::address<unsigned char>() const
Line
Count
Source
123
735M
  T* address() const {
124
735M
    return reinterpret_cast<T*>(address_);
125
735M
  }
unsigned int* jxl::AlignedMemory::address<unsigned int>() const
Line
Count
Source
123
96.1k
  T* address() const {
124
96.1k
    return reinterpret_cast<T*>(address_);
125
96.1k
  }
int* jxl::AlignedMemory::address<int>() const
Line
Count
Source
123
6.34k
  T* address() const {
124
6.34k
    return reinterpret_cast<T*>(address_);
125
6.34k
  }
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
Line
Count
Source
123
2.18k
  T* address() const {
124
2.18k
    return reinterpret_cast<T*>(address_);
125
2.18k
  }
float* jxl::AlignedMemory::address<float>() const
Line
Count
Source
123
39.0k
  T* address() const {
124
39.0k
    return reinterpret_cast<T*>(address_);
125
39.0k
  }
jxl::GroupDecCache* jxl::AlignedMemory::address<jxl::GroupDecCache>() const
Line
Count
Source
123
2.58k
  T* address() const {
124
2.58k
    return reinterpret_cast<T*>(address_);
125
2.58k
  }
jxl::AliasTable::Entry* jxl::AlignedMemory::address<jxl::AliasTable::Entry>() const
Line
Count
Source
123
137k
  T* address() const {
124
137k
    return reinterpret_cast<T*>(address_);
125
137k
  }
short* jxl::AlignedMemory::address<short>() const
Line
Count
Source
123
5.79k
  T* address() const {
124
5.79k
    return reinterpret_cast<T*>(address_);
125
5.79k
  }
unsigned short* jxl::AlignedMemory::address<unsigned short>() const
Line
Count
Source
123
47.0k
  T* address() const {
124
47.0k
    return reinterpret_cast<T*>(address_);
125
47.0k
  }
126
134k
  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
486
  AlignedArray() : size_(0) {}
145
146
  static StatusOr<AlignedArray> Create(JxlMemoryManager* memory_manager,
147
486
                                       size_t size) {
148
486
    size_t storage_size = size * sizeof(T);
149
486
    JXL_ASSIGN_OR_RETURN(AlignedMemory storage,
150
486
                         AlignedMemory::Create(memory_manager, storage_size));
151
486
    T* items = storage.address<T>();
152
972
    for (size_t i = 0; i < size; ++i) {
153
486
      new (items + i) T();
154
486
    }
155
486
    return AlignedArray<T>(std::move(storage), size);
156
486
  }
157
158
  // Copy disallowed.
159
  AlignedArray(const AlignedArray& other) = delete;
160
  AlignedArray& operator=(const AlignedArray& other) = delete;
161
162
  // Custom move.
163
972
  AlignedArray(AlignedArray&& other) noexcept {
164
972
    size_ = other.size_;
165
972
    storage_ = std::move(other.storage_);
166
972
    other.size_ = 0;
167
972
  }
168
169
486
  AlignedArray& operator=(AlignedArray&& other) noexcept {
170
486
    if (this == &other) return *this;
171
486
    size_ = other.size_;
172
486
    storage_ = std::move(other.storage_);
173
486
    other.size_ = 0;
174
486
    return *this;
175
486
  }
176
177
1.94k
  ~AlignedArray() {
178
1.94k
    if (!size_) return;
179
486
    T* items = storage_.address<T>();
180
972
    for (size_t i = 0; i < size_; ++i) {
181
486
      items[i].~T();
182
486
    }
183
486
  }
184
185
1.61k
  T& operator[](const size_t i) {
186
1.61k
    JXL_DASSERT(i < size_);
187
1.61k
    return *(storage_.address<T>() + i);
188
1.61k
  }
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
486
      : 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_