\internal Like malloc, but the returned pointer is guaranteed to be aligned to `alignment`. * Fast, but wastes `alignment` additional bytes of memory. Does not throw any exception. */
| 140 | * Fast, but wastes `alignment` additional bytes of memory. Does not throw any exception. |
| 141 | */ |
| 142 | EIGEN_DEVICE_FUNC inline void* handmade_aligned_malloc(std::size_t size, |
| 143 | std::size_t alignment = EIGEN_DEFAULT_ALIGN_BYTES) { |
| 144 | eigen_assert(alignment >= sizeof(void*) && alignment <= 128 && (alignment & (alignment - 1)) == 0 && |
| 145 | "Alignment must be at least sizeof(void*), less than or equal to 128, and a power of 2"); |
| 146 | |
| 147 | check_that_malloc_is_allowed(); |
| 148 | EIGEN_USING_STD(malloc) |
| 149 | void* original = malloc(size + alignment); |
| 150 | if (original == nullptr) return nullptr; |
| 151 | uint8_t offset = static_cast<uint8_t>(alignment - (reinterpret_cast<std::size_t>(original) & (alignment - 1))); |
| 152 | void* aligned = static_cast<void*>(static_cast<uint8_t*>(original) + offset); |
| 153 | *(static_cast<uint8_t*>(aligned) - 1) = offset; |
| 154 | return aligned; |
| 155 | } |
| 156 | |
| 157 | /** \internal Frees memory allocated with handmade_aligned_malloc */ |
| 158 | EIGEN_DEVICE_FUNC inline void handmade_aligned_free(void* ptr) { |
no test coverage detected