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