]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/lazy_allocator.hh
Merge pull request #7908 from omoerbeek/rec-4.1.14-changelog
[thirdparty/pdns.git] / pdns / lazy_allocator.hh
CommitLineData
12471842
PL
1/*
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 *
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.
8 *
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.
12 *
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.
17 *
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.
21 */
5cf909f3
AN
22#ifndef LAZY_ALLOCATOR_HH
23#define LAZY_ALLOCATOR_HH
24
25#include <cstddef>
26#include <utility>
27#include <type_traits>
15880e10
OM
28#include <new>
29#include <sys/mman.h>
30
31// On OpenBSD mem used as stack should be marked MAP_STACK
32#if !defined(MAP_STACK)
33#define MAP_STACK 0
34#endif
5cf909f3
AN
35
36template <typename T>
37struct lazy_allocator {
38 using value_type = T;
39 using pointer = T*;
40 using size_type = std::size_t;
41 static_assert (std::is_trivial<T>::value,
42 "lazy_allocator must only be used with trivial types");
43
44 pointer
45 allocate (size_type const n) {
584ab041 46#ifdef __OpenBSD__
70a29b7d 47 void *p = mmap(nullptr, n * sizeof(value_type),
15880e10
OM
48 PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON | MAP_STACK, -1, 0);
49 if (p == MAP_FAILED)
50 throw std::bad_alloc();
51 return static_cast<pointer>(p);
584ab041
OM
52#else
53 return static_cast<pointer>(::operator new (n * sizeof(value_type)));
54#endif
5cf909f3
AN
55 }
56
57 void
58 deallocate (pointer const ptr, size_type const n) noexcept {
584ab041 59#ifdef __OpenBSD__
15880e10 60 munmap(ptr, n * sizeof(value_type));
584ab041
OM
61#else
62#if defined(__cpp_sized_deallocation) && (__cpp_sized_deallocation >= 201309)
63 ::operator delete (ptr, n * sizeof(value_type));
64#else
65 (void) n;
66 ::operator delete (ptr);
67#endif
68#endif
5cf909f3
AN
69 }
70
71 void construct (T*) const noexcept {}
72
73 template <typename X, typename... Args>
74 void
75 construct (X* place, Args&&... args) const noexcept {
76 new (static_cast<void*>(place)) X (std::forward<Args>(args)...);
77 }
78};
79
80template <typename T> inline
81bool operator== (lazy_allocator<T> const&, lazy_allocator<T> const&) noexcept {
82 return true;
83}
84
85template <typename T> inline
86bool operator!= (lazy_allocator<T> const&, lazy_allocator<T> const&) noexcept {
87 return false;
88}
89
90#endif // LAZY_ALLOCATOR_HH