util-memory: Implement aligned std::allocator

This commit is contained in:
Michael Fabian 'Xaymar' Dirks 2018-01-08 19:01:26 +01:00
parent 8a595501b3
commit 1e2cff48c9

View file

@ -18,9 +18,78 @@
*/
#pragma once
#include <stdlib.h>
#include <malloc.h>
namespace util {
void* malloc_aligned(size_t align, size_t size);
void free_aligned(void* mem);
template <typename T, size_t N = 16>
class AlignmentAllocator {
public:
typedef T value_type;
typedef size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef T * pointer;
typedef const T * const_pointer;
typedef T & reference;
typedef const T & const_reference;
public:
inline AlignmentAllocator() throw () {}
template <typename T2>
inline AlignmentAllocator(const AlignmentAllocator<T2, N> &) throw () {}
inline ~AlignmentAllocator() throw () {}
inline pointer adress(reference r) {
return &r;
}
inline const_pointer adress(const_reference r) const {
return &r;
}
inline pointer allocate(size_type n) {
return (pointer)_aligned_malloc(n*sizeof(value_type), N);
}
inline void deallocate(pointer p, size_type) {
_aligned_free(p);
}
inline void construct(pointer p, const value_type & wert) {
new (p)value_type(wert);
}
inline void destroy(pointer p) {
p->~value_type();
p;
}
inline size_type max_size() const throw () {
return size_type(-1) / sizeof(value_type);
}
template <typename T2>
struct rebind {
typedef AlignmentAllocator<T2, N> other;
};
bool operator!=(const AlignmentAllocator<T, N>& other) const {
return !(*this == other);
}
// Returns true if and only if storage allocated from *this
// can be deallocated from other, and vice versa.
// Always returns true for stateless allocators.
bool operator==(const AlignmentAllocator<T, N>& other) const {
return true;
}
};
};