2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #ifndef LAZY_ALLOCATOR_HH
23 #define LAZY_ALLOCATOR_HH
27 #include <type_traits>
30 struct lazy_allocator {
33 using size_type = std::size_t;
34 static_assert (std::is_trivial<T>::value,
35 "lazy_allocator must only be used with trivial types");
38 allocate (size_type const n) {
39 return static_cast<pointer>(::operator new (n * sizeof(value_type)));
43 deallocate (pointer const ptr, size_type const n) noexcept {
44 #if defined(__cpp_sized_deallocation) && (__cpp_sized_deallocation >= 201309)
45 ::operator delete (ptr, n * sizeof(value_type));
48 ::operator delete (ptr);
52 void construct (T*) const noexcept {}
54 template <typename X, typename... Args>
56 construct (X* place, Args&&... args) const noexcept {
57 new (static_cast<void*>(place)) X (std::forward<Args>(args)...);
61 template <typename T> inline
62 bool operator== (lazy_allocator<T> const&, lazy_allocator<T> const&) noexcept {
66 template <typename T> inline
67 bool operator!= (lazy_allocator<T> const&, lazy_allocator<T> const&) noexcept {
71 #endif // LAZY_ALLOCATOR_HH