Skip to content

Commit 275e1d1

Browse files
committed
Core (Memory): Streamline AlignedAllocator to meet C++17 allocator requirements.
1 parent 9c40351 commit 275e1d1

File tree

1 file changed

+20
-65
lines changed

1 file changed

+20
-65
lines changed

libvisual/libvisual/lv_aligned_allocator.hpp

Lines changed: 20 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,59 @@
11
#ifndef _LV_ALIGNED_ALLOCATOR_HPP
22
#define _LV_ALIGNED_ALLOCATOR_HPP
33

4-
#include <cstddef>
5-
#include <cstdlib>
6-
#include <new>
74
#include <libvisual/lv_mem.h>
5+
#include <cstdlib>
6+
#include <limits>
7+
#include <memory>
8+
#include <stdexcept>
89

9-
namespace LV {
10-
11-
template <typename T, std::size_t alignment>
12-
struct AlignedAllocator;
13-
14-
template <std::size_t alignment>
15-
struct AlignedAllocator<void, alignment>
16-
{
17-
typedef void value_type;
18-
typedef void* pointer;
19-
typedef void const* const_pointer;
20-
21-
template <typename U>
22-
struct rebind { typedef AlignedAllocator<U, alignment> other; };
23-
};
24-
10+
namespace LV
11+
{
2512
//! Aligned memory allocator.
2613
//!
27-
//! @tparam T type of object to allocate
2814
//! @tparam alignment alignment boundary
15+
//! @tparam T type of object to allocate
2916
//!
3017
//! AlignedAllocator is an implementation of the C++ Allocator concept for use with standard library containers.
3118
//!
32-
template <typename T, std::size_t alignment>
19+
template <std::size_t alignment, typename T = std::byte>
3320
struct AlignedAllocator
3421
{
35-
typedef T* pointer;
36-
typedef T& reference;
37-
typedef T const* const_pointer;
38-
typedef T const& const_reference;
39-
typedef T value_type;
40-
typedef std::size_t size_type;
41-
typedef std::ptrdiff_t difference_type;
22+
using value_type = T;
4223

43-
template <typename U>
44-
struct rebind { typedef AlignedAllocator<U, alignment> other; };
45-
46-
AlignedAllocator ()
47-
{}
24+
AlignedAllocator () = default;
4825

4926
template <typename U>
50-
AlignedAllocator (AlignedAllocator<U, alignment>&)
27+
constexpr AlignedAllocator (AlignedAllocator<alignment, U> const&) noexcept
5128
{}
5229

53-
pointer address (reference x) const
30+
[[nodiscard]] T* allocate (std::size_t n)
5431
{
55-
return &x;
56-
}
32+
if (n > std::numeric_limits<std::size_t>::max () / sizeof (T))
33+
throw std::bad_array_new_length {};
5734

58-
const_pointer address (const_reference x) const
59-
{
60-
return &x;
61-
}
62-
63-
pointer allocate (size_type n, typename AlignedAllocator<void, alignment>::pointer = nullptr)
64-
{
65-
void* ptr = visual_mem_malloc_aligned (n * sizeof (T), alignment);
35+
auto ptr = visual_mem_malloc_aligned (n * sizeof (T), alignment);
6636

6737
if (!ptr)
68-
throw std::bad_alloc ();
38+
throw std::bad_alloc {};
6939

70-
return pointer (ptr);
40+
return ptr;
7141
}
7242

73-
void deallocate (pointer ptr, size_type) noexcept
43+
void deallocate (T* ptr, std::size_t) noexcept
7444
{
7545
visual_mem_free_aligned (ptr);
7646
}
77-
78-
size_type max_size () const
79-
{
80-
return size_type (-1) / sizeof (T);
81-
}
82-
83-
void construct (pointer p, const_reference x)
84-
{
85-
new (p) T (x);
86-
}
87-
88-
void destroy (pointer p)
89-
{
90-
p->~T ();
91-
}
9247
};
9348

9449
template <typename T, typename U, std::size_t alignment>
95-
bool operator== (AlignedAllocator<T, alignment> const&, AlignedAllocator<U, alignment> const&)
50+
bool operator== (AlignedAllocator<alignment, T> const&, AlignedAllocator<alignment, U> const&)
9651
{
9752
return true;
9853
}
9954

10055
template <typename T, typename U, std::size_t alignment>
101-
bool operator!= (AlignedAllocator<T, alignment> const&, AlignedAllocator<U, alignment> const&)
56+
bool operator!= (AlignedAllocator<alignment, T> const&, AlignedAllocator<alignment, U> const&)
10257
{
10358
return false;
10459
}

0 commit comments

Comments
 (0)