From 1e2cff48c91c0da1c4c2feecf9beffbd5fec5bd2 Mon Sep 17 00:00:00 2001 From: Michael Fabian 'Xaymar' Dirks Date: Mon, 8 Jan 2018 19:01:26 +0100 Subject: [PATCH] util-memory: Implement aligned std::allocator --- source/util-memory.h | 69 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/source/util-memory.h b/source/util-memory.h index e324cd2e..d5d2ebd3 100644 --- a/source/util-memory.h +++ b/source/util-memory.h @@ -18,9 +18,78 @@ */ #pragma once +#include +#include namespace util { void* malloc_aligned(size_t align, size_t size); void free_aligned(void* mem); + + template + 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 + inline AlignmentAllocator(const AlignmentAllocator &) 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 + struct rebind { + typedef AlignmentAllocator other; + }; + + bool operator!=(const AlignmentAllocator& 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& other) const { + return true; + } + }; };