| 146 | } |
| 147 | |
| 148 | mi_decl_nodiscard mi_decl_restrict void* mi_heap_malloc_aligned(mi_heap_t* heap, size_t size, size_t alignment) mi_attr_noexcept { |
| 149 | #if !MI_PADDING |
| 150 | // without padding, any small sized allocation is naturally aligned (see also `_mi_segment_page_start`) |
| 151 | if (!_mi_is_power_of_two(alignment)) return NULL; |
| 152 | if mi_likely(_mi_is_power_of_two(size) && size >= alignment && size <= MI_SMALL_SIZE_MAX) |
| 153 | #else |
| 154 | // with padding, we can only guarantee this for fixed alignments |
| 155 | if mi_likely((alignment == sizeof(void*) || (alignment == MI_MAX_ALIGN_SIZE && size > (MI_MAX_ALIGN_SIZE/2))) |
| 156 | && size <= MI_SMALL_SIZE_MAX) |
| 157 | #endif |
| 158 | { |
| 159 | // fast path for common alignment and size |
| 160 | return mi_heap_malloc_small(heap, size); |
| 161 | } |
| 162 | else { |
| 163 | return mi_heap_malloc_aligned_at(heap, size, alignment, 0); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | // ------------------------------------------------------ |
| 168 | // Aligned Allocation |