17#ifndef RESONANCE_AUDIO_BASE_ALIGNED_ALLOCATOR_H_
18#define RESONANCE_AUDIO_BASE_ALIGNED_ALLOCATOR_H_
27#include "base/simd_utils.h"
33template <
size_t TypeSize,
size_t Alignment>
34void StaticAlignmentCheck() {
35 const bool alignment_is_power_of_two =
36 !(Alignment == 0) && !(Alignment & (Alignment - 1));
37 static_assert(alignment_is_power_of_two,
"Alignment must be power of two");
39 const bool type_size_is_power_of_two = !(TypeSize & (TypeSize - 1));
40 static_assert(type_size_is_power_of_two,
"Type size must be power of two");
44template <
typename Type,
typename SizeType,
typename Po
interType>
45PointerType AllignedMalloc(SizeType size, SizeType alignment) {
46 const SizeType data_size = size *
sizeof(Type);
47 const SizeType offset = alignment - 1 +
sizeof(PointerType);
48 void* mem_block_begin = malloc(data_size + offset);
49 if (mem_block_begin ==
nullptr) {
53 void** mem_block_aligned =
reinterpret_cast<void**
>(
54 ((
reinterpret_cast<SizeType
>(mem_block_begin) + offset) &
57 mem_block_aligned[-1] = mem_block_begin;
58 return reinterpret_cast<PointerType
>(mem_block_aligned);
62template <
typename Po
interType>
63void AllignedFree(PointerType mem_block_aligned) {
64 free(*(
reinterpret_cast<void**
>(mem_block_aligned) - 1));
72template <
typename Type,
size_t Alignment>
75 typedef typename std::allocator<Type>::pointer Pointer;
76 typedef typename std::allocator<Type>::const_pointer ConstPointer;
77 typedef typename std::allocator<Type>::size_type SizeType;
86 Pointer allocate(SizeType size) {
return allocate(size,
nullptr); }
93 Pointer allocate(SizeType size, ConstPointer ) {
95 return AllignedMalloc<Type, SizeType, Pointer>(size, Alignment);
98 void deallocate(Pointer mem_block_aligned,
size_t size) {
99 AllignedFree<Pointer>(mem_block_aligned);
103 template <
typename U>
109 template <
typename U>
Definition aligned_allocator.h:73
Definition aligned_allocator.h:110