11/* **********************************************************************************************************************
22* *
3- * libscopehal v0.1 *
3+ * libscopehal *
44* *
5- * Copyright (c) 2012-2021 Andrew D. Zonenberg and contributors *
5+ * Copyright (c) 2012-2024 Andrew D. Zonenberg and contributors *
66* All rights reserved. *
77* *
88* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the *
3131 @file
3232 @author Andrew D. Zonenberg
3333 @brief Declaration of AlignedAllocator
34+ @ingroup core
3435 */
3536
3637#ifndef AlignedAllocator_h
4445 @brief Aligned memory allocator for STL containers
4546
4647 Based on https://devblogs.microsoft.com/cppblog/the-mallocator/
48+
49+ @ingroup core
4750 */
4851template <class T , size_t alignment>
4952class AlignedAllocator
5053{
5154public:
5255
53- // Standard typedefs
56+ // /@brief Pointer to the allocated type
5457 typedef T* pointer;
58+
59+ // /@brief Const pointer to the allocated type
5560 typedef const T* const_pointer;
61+
62+ // /@brief Reference to the allocated type
5663 typedef T& reference;
64+
65+ // /@brief Const reference to the allocated type
5766 typedef const T& const_reference;
67+
68+ // /@brief The allocated type
5869 typedef T value_type;
70+
71+ // /@brief Type of the size of an allocated object
5972 typedef size_t size_type;
73+
74+ // /@brief Type of the difference between two allocated pointers
6075 typedef ptrdiff_t difference_type;
6176
62- // Overloads in case somebody overloaded the unary operator&()
63- // (which is pretty weird but the spec allows it)
77+ /* *
78+ @brief Get the address of an object
79+
80+ Overloaded in case somebody overloaded the unary operator&()
81+ (which is pretty weird but the spec allows it)
82+
83+ @param rhs The object to get the address of
84+ */
6485 T* address (T& rhs)
6586 { return &rhs; }
6687
88+ /* *
89+ @brief Get the address of an object
90+
91+ Overloaded in case somebody overloaded the unary operator&()
92+ (which is pretty weird but the spec allows it)
93+
94+ @param rhs The object to get the address of
95+ */
6796 const T* address (T& rhs) const
6897 { return &rhs; }
6998
99+ /* *
100+ @brief Get the max possible allocation size the allocator supports
101+
102+ (Does not necessarily mean that we have enough RAM to do so, only enough address space)
103+ */
70104 size_t max_size () const
71105 { return (static_cast <size_t >(0 ) - static_cast <size_t >(1 )) / sizeof (T); }
72106
73- // RTTI and construction helpers
107+ // /@brief Rebind to a different type of allocator
74108 template <typename U>
75109 struct rebind
76110 {
77111 typedef AlignedAllocator<U, alignment> other;
78112 };
79113
114+ /* *
115+ @brief Check if two allocators are the same
116+
117+ @param other The other object
118+ */
80119 bool operator !=(const AlignedAllocator& other) const
81120 { return !(*this == other); }
82121
83122 // Look at that, a placement new! First time I've ever used one.
123+ /* *
124+ @brief Construct an object in-place given a reference one
125+
126+ @param p Destination object
127+ @param t Source object
128+ */
84129 void construct (T* const p, const T& t) const
85130 { new ( static_cast <void *>(p) ) T (t); }
86131
@@ -106,7 +151,11 @@ class AlignedAllocator
106151 ~AlignedAllocator ()
107152 {}
108153
109- // Now for the fun part
154+ /* *
155+ @brief Allocate a block of memory
156+
157+ @param n Size in bytes (internally rounded up to our alignment)
158+ */
110159 T* allocate (size_t n) const
111160 {
112161 // Fail if we got an invalid size
@@ -136,7 +185,13 @@ class AlignedAllocator
136185 return ret;
137186 }
138187
139- void deallocate (T* const p, const size_t /* unused*/ ) const
188+ /* *
189+ @brief Free a block of memory
190+
191+ @param p Block to free
192+ @param unused Size of block (ignored)
193+ */
194+ void deallocate (T* const p, [[maybe_unused]] const size_t unused) const
140195 {
141196#ifdef _WIN32
142197 _aligned_free (p);
@@ -145,13 +200,23 @@ class AlignedAllocator
145200#endif
146201 }
147202
148- // convenience wrapper
203+ /* *
204+ @brief Free a single object
205+
206+ @param p Object to free
207+ */
149208 void deallocate (T* const p) const
150209 { deallocate (p, 1 ); }
151210
152211 // Not quite sure what this is for but apparently we need it?
212+ /* *
213+ @brief Allocate an object
214+
215+ @param n Size in bytes
216+ @param hint Ignored
217+ */
153218 template <typename U>
154- T* allocate (const size_t n, const U* /* const hint */ const )
219+ T* allocate (const size_t n, [[maybe_unused]] const U* const hint)
155220 { return allocate (n); }
156221
157222 // Disallow assignment
0 commit comments