MCPcopy Create free account
hub / github.com/ErrorAtLine0/infinipaint / handmade_aligned_realloc

Function handmade_aligned_realloc

include/Eigen/src/Core/util/Memory.h:174–194  ·  view source on GitHub ↗

\internal * \brief Reallocates aligned memory. * Since we know that our handmade version is based on std::malloc * we can use std::realloc to implement efficient reallocation. */

Source from the content-addressed store, hash-verified

172 * we can use std::realloc to implement efficient reallocation.
173 */
174EIGEN_DEVICE_FUNC inline void* handmade_aligned_realloc(void* ptr, std::size_t new_size, std::size_t old_size,
175 std::size_t alignment = EIGEN_DEFAULT_ALIGN_BYTES) {
176 if (ptr == nullptr) return handmade_aligned_malloc(new_size, alignment);
177 uint8_t old_offset = *(static_cast<uint8_t*>(ptr) - 1);
178 void* old_original = static_cast<uint8_t*>(ptr) - old_offset;
179
180 check_that_malloc_is_allowed();
181 EIGEN_USING_STD(realloc)
182 void* original = realloc(old_original, new_size + alignment);
183 if (original == nullptr) return nullptr;
184 if (original == old_original) return ptr;
185 uint8_t offset = static_cast<uint8_t>(alignment - (reinterpret_cast<std::size_t>(original) & (alignment - 1)));
186 void* aligned = static_cast<void*>(static_cast<uint8_t*>(original) + offset);
187 if (offset != old_offset) {
188 const void* src = static_cast<const void*>(static_cast<uint8_t*>(original) + old_offset);
189 std::size_t count = (std::min)(new_size, old_size);
190 std::memmove(aligned, src, count);
191 }
192 *(static_cast<uint8_t*>(aligned) - 1) = offset;
193 return aligned;
194}
195
196/** \internal Allocates \a size bytes. The returned pointer is guaranteed to have 16 or 32 bytes alignment depending on
197 * the requirements. On allocation error, the returned pointer is null, and std::bad_alloc is thrown.

Callers 1

aligned_reallocFunction · 0.85

Calls 2

handmade_aligned_mallocFunction · 0.85

Tested by

no test coverage detected