| 2190 | */ |
| 2191 | template<typename T> |
| 2192 | T* aligned_malloc(size_t size, size_t align) |
| 2193 | { |
| 2194 | void* ptr; |
| 2195 | int error = 0; |
| 2196 | |
| 2197 | // Don't allow this to under-align a type |
| 2198 | size_t min_align = astc::max(alignof(T), sizeof(void*)); |
| 2199 | size_t real_align = astc::max(min_align, align); |
| 2200 | |
| 2201 | #if defined(_WIN32) |
| 2202 | ptr = _aligned_malloc(size, real_align); |
| 2203 | #else |
| 2204 | error = posix_memalign(&ptr, real_align, size); |
| 2205 | #endif |
| 2206 | |
| 2207 | if (error || (!ptr)) |
| 2208 | { |
| 2209 | return nullptr; |
| 2210 | } |
| 2211 | |
| 2212 | return static_cast<T*>(ptr); |
| 2213 | } |
| 2214 | |
| 2215 | /** |
| 2216 | * @brief Free an aligned memory buffer. |