2 * Copyright (c) 2020 Arm Limited.
4 * SPDX-License-Identifier: MIT
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 #include <vulkan/vulkan.h>
38 * @brief Minimalistic wrapper of VkAllocationCallbacks.
44 * @brief Construct a new wrapper for the given VK callbacks and scope.
45 * @param callbacks Pointer to allocation callbacks. If this is @c nullptr, then default
46 * allocation callbacks are used. These can be accessed through #m_callbacks.
47 * @param scope The scope to use for this allocator.
49 allocator(const VkAllocationCallbacks *callbacks, VkSystemAllocationScope scope);
52 * @brief Get a pointer to the allocation callbacks provided while constructing this object.
53 * @return a copy of the #VkAllocationCallback argument provided in the allocator constructor
54 * or @c nullptr if this argument was provided as @c nullptr.
55 * @note The #m_callbacks member is always populated with callable pointers for pfnAllocation,
56 * pfnReallocation and pfnFree.
58 const VkAllocationCallbacks *get_original_callbacks() const;
61 * @brief Helper method to allocate and construct objects with a custom allocator.
62 * @param num_objects Number of objects to create.
63 * @return Pointer to the newly created objects or @c nullptr if allocation failed.
65 template <typename T, typename... arg_types>
66 T *create(size_t num_objects, arg_types &&... args) const noexcept;
69 * @brief Helper method to destroy and deallocate objects constructed with allocator::create().
70 * @param num_objects Number of objects to destroy.
73 void destroy(size_t num_objects, T *obj) const noexcept;
75 VkAllocationCallbacks m_callbacks;
76 VkSystemAllocationScope m_scope;
80 * @brief Implementation of an allocator that can be used with STL containers.
83 class custom_allocator
89 custom_allocator(const allocator &alloc)
95 custom_allocator(const custom_allocator<U> &other)
96 : m_alloc(other.get_data())
100 const allocator &get_data() const
105 pointer allocate(size_t n) const
107 size_t size = n * sizeof(T);
108 auto &cb = m_alloc.m_callbacks;
109 void *ret = cb.pfnAllocation(cb.pUserData, size, alignof(T), m_alloc.m_scope);
111 throw std::bad_alloc();
112 return reinterpret_cast<pointer>(ret);
115 pointer allocate(size_t n, void *ptr) const
117 size_t size = n * sizeof(T);
118 auto &cb = m_alloc.m_callbacks;
119 void *ret = cb.pfnReallocation(cb.pUserData, ptr, size, alignof(T), m_alloc.m_scope);
121 throw std::bad_alloc();
122 return reinterpret_cast<pointer>(ret);
125 void deallocate(void *ptr, size_t) const noexcept
127 m_alloc.m_callbacks.pfnFree(m_alloc.m_callbacks.pUserData, ptr);
131 const allocator m_alloc;
134 template <typename T, typename U>
135 bool operator==(const custom_allocator<T> &, const custom_allocator<U> &)
140 template <typename T, typename U>
141 bool operator!=(const custom_allocator<T> &, const custom_allocator<U> &)
146 template <typename T, typename... arg_types>
147 T *allocator::create(size_t num_objects, arg_types &&... args) const noexcept
154 custom_allocator<T> allocator(*this);
158 ptr = allocator.allocate(num_objects);
165 size_t objects_constructed = 0;
168 while (objects_constructed < num_objects)
170 T *next_object = &ptr[objects_constructed];
171 new (next_object) T(std::forward<arg_types>(args)...);
172 objects_constructed++;
177 /* We catch all exceptions thrown while constructing the object, not just
180 while (objects_constructed > 0)
182 objects_constructed--;
183 ptr[objects_constructed].~T();
185 allocator.deallocate(ptr, num_objects);
191 template <typename T>
192 void allocator::destroy(size_t num_objects, T *objects) const noexcept
194 assert((objects == nullptr) == (num_objects == 0));
195 if (num_objects == 0)
200 custom_allocator<T> allocator(*this);
201 for (size_t i = 0; i < num_objects; i++)
205 allocator.deallocate(objects, num_objects);
208 template <typename T>
209 void destroy_custom(T *obj)
215 * @brief Vector using a Vulkan custom allocator to allocate its elements.
216 * @note The vector must be passed a custom_allocator during construction and it takes a copy
217 * of it, meaning that the user is free to destroy the custom_allocator after constructing the
220 template <typename T>
221 class vector : public std::vector<T, custom_allocator<T>>
224 using base = std::vector<T, custom_allocator<T>>;
227 /* Delete all methods that can cause allocation failure, i.e. can throw std::bad_alloc.
229 * Rationale: we want to force users to use our corresponding try_... method instead:
230 * this makes the API slightly more annoying to use, but hopefully safer as it encourages
231 * users to check for allocation failures, which is important for Vulkan.
233 * Note: deleting each of these methods (below) deletes all its overloads from the base class,
234 * to be precise: the deleted method covers the methods (all overloads) in the base class.
235 * Note: clear() is already noexcept since C++11.
237 void insert() = delete;
238 void emplace() = delete;
239 void emplace_back() = delete;
240 void push_back() = delete;
241 void resize() = delete;
242 void reserve() = delete;
244 /* Note pop_back(), erase(), clear() do not throw std::bad_alloc exceptions. */
246 /* @brief Like std::vector::push_back, but non throwing.
247 * @return @c false iff the operation could not be performed due to an allocation failure.
249 template <typename... arg_types>
250 bool try_push_back(arg_types &&... args) noexcept
254 base::push_back(std::forward<arg_types>(args)...);
257 catch (const std::bad_alloc &e)
263 /* @brief push back multiple elements at once
264 * @return @c false iff the operation could not be performed due to an allocation failure.
266 bool try_push_back_many(const T *begin, const T *end) noexcept
268 for (const T *it = begin; it != end; ++it)
270 if (!try_push_back(*it))
278 /* @brief Like std::vector::resize, but non throwing.
279 * @return @c false iff the operation could not be performed due to an allocation failure.
281 template <typename... arg_types>
282 bool try_resize(arg_types &&... args) noexcept
286 base::resize(std::forward<arg_types>(args)...);
289 catch (const std::bad_alloc &e)
296 } /* namespace util */