]> git.ipfire.org Git - thirdparty/vectorscan.git/commitdiff
AlignedAllocator: simplify and fix for MSVC, C++11
authorJustin Viiret <justin.viiret@intel.com>
Thu, 3 Mar 2016 23:10:26 +0000 (10:10 +1100)
committerMatthew Barr <matthew.barr@intel.com>
Wed, 20 Apr 2016 03:34:54 +0000 (13:34 +1000)
- Simplify thanks to minimal allocator requirements in C++11.
- Add required copy constructor.

src/util/alloc.h

index ab996a8d8f22a423d5ca1228ad8a362768ac7e89..191bc387ee5a6e0b6122cfa5ab8fb74907117365 100644 (file)
@@ -78,51 +78,42 @@ void aligned_free_internal(void *ptr);
 
 /** \brief Aligned allocator class for use with STL containers. Ensures that
  * your objects are aligned to N bytes. */
-template <typename T, std::size_t N> class AlignedAllocator {
+template <class T, std::size_t N>
+class AlignedAllocator {
 public:
-    typedef T value_type;
-    typedef std::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;
-
-    template <typename U> struct rebind {
-        typedef AlignedAllocator<U, N> other;
-    };
-
-    pointer address(reference x) const { return &x; }
-    const_pointer address(const_reference x) const { return &x; }
-
-    size_type max_size() const {
-        return std::numeric_limits<size_type>::max() / sizeof(value_type);
-    }
-
-    pointer allocate(size_type size) const {
-        return static_cast<pointer>(
-            aligned_malloc_internal(size * sizeof(value_type), N));
-    }
+    using value_type = T;
 
-    void deallocate(pointer x, size_type) const { aligned_free_internal(x); }
+    AlignedAllocator() noexcept {}
 
-    void construct(pointer x, const value_type &val) const {
-        new (x) value_type(val);
-    }
+    template <class U, std::size_t N2>
+    AlignedAllocator(const AlignedAllocator<U, N2> &) noexcept {}
 
-    void destroy(pointer p) const { p->~value_type(); }
+    template <class U> struct rebind {
+        using other = AlignedAllocator<U, N>;
+    };
 
-    bool operator==(const AlignedAllocator<T, N> &) const {
-        // All instances of AlignedAllocator can dealloc each others' memory.
-        return true;
+    T *allocate(std::size_t size) const {
+        size_t alloc_size = size * sizeof(T);
+        return static_cast<T *>(aligned_malloc_internal(alloc_size, N));
     }
 
-    bool operator!=(const AlignedAllocator<T, N> &) const {
-        // All instances of AlignedAllocator can dealloc each others' memory.
-        return false;
+    void deallocate(T *x, std::size_t) const noexcept {
+        aligned_free_internal(x);
     }
 };
 
+template <class T, class U, std::size_t N, std::size_t N2>
+bool operator==(const AlignedAllocator<T, N> &,
+                const AlignedAllocator<U, N2> &) {
+    return true;
+}
+
+template <class T, class U, std::size_t N, std::size_t N2>
+bool operator!=(const AlignedAllocator<T, N> &a,
+                const AlignedAllocator<U, N2> &b) {
+    return !(a == b);
+}
+
 } // namespace ue2
 
 #endif