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