21 #ifndef LIB_LIGHTMETRICA_ALIGN_H
22 #define LIB_LIGHTMETRICA_ALIGN_H
34 LM_PUBLIC_API
void* aligned_malloc(
size_t size,
size_t align);
40 LM_PUBLIC_API
void aligned_free(
void* p);
47 LM_FORCE_INLINE
bool is_aligned(
const void* p,
size_t align)
49 return (uintptr_t)p % align == 0;
56 template <
size_t Align>
65 void*
operator new(std::size_t size)
throw (std::bad_alloc)
67 void* p = aligned_malloc( size, Align);
68 if (!p)
throw std:: bad_alloc();
72 void operator delete(
void* p)
92 void*
operator new(std::size_t size)
throw (std::bad_alloc);
93 void operator delete(
void* p);
102 template <
typename T, std::
size_t Alignment>
109 typedef const T * const_pointer;
110 typedef T& reference;
111 typedef const T& const_reference;
112 typedef T value_type;
113 typedef std::size_t size_type;
114 typedef ptrdiff_t difference_type;
116 T * address(T& r)
const {
return &r; }
117 const T * address(
const T& s)
const {
return &s; }
119 std::size_t max_size()
const
123 return (static_cast<std::size_t>(0) - static_cast<std::size_t>(1)) /
sizeof(T);
127 template <
typename U>
135 return !(*
this == other);
138 void construct(T *
const p,
const T& t)
const
140 void *
const pv =
static_cast<void *
>(p);
144 void destroy(T *
const p)
const
166 T * allocate(
const std::size_t n)
const
183 throw std::length_error(
"aligned_allocator<T>::allocate() - Integer overflow.");
187 void *
const pv = aligned_malloc(n *
sizeof(T), Alignment);
192 throw std::bad_alloc();
195 return static_cast<T *
>(pv);
198 void deallocate(T *
const p,
const std::size_t )
const
205 template <
typename U>
206 T * allocate(
const std::size_t n,
const U * )
const
228 template <std::
size_t Align>
232 typedef std::size_t size_type;
233 typedef std::ptrdiff_t difference_type;
234 static char* malloc(
const size_type bytes) {
return static_cast<char*
>(aligned_malloc(bytes, Align)); }
235 static void free(
const char* block) { aligned_free((
void*)block); }
241 #endif // LIB_LIGHTMETRICA_ALIGN_H