]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/noinitvector.hh
rec: mention rust compiler in compiling docs
[thirdparty/pdns.git] / pdns / noinitvector.hh
1 #pragma once
2
3 #include <memory>
4 #include <new>
5 #include <utility>
6 #include <vector>
7
8 // based on boost::core::noinit_adaptor
9 // The goal is to avoid initialization of the content of a container,
10 // because setting several kB of uint8_t to 0 has a real cost if you
11 // do 100k times per second.
12 template<class Allocator>
13 struct noinit_adaptor: Allocator
14 {
15 template<class U>
16 struct rebind {
17 typedef noinit_adaptor<typename std::allocator_traits<Allocator>::template
18 rebind_alloc<U> > other;
19 };
20
21 noinit_adaptor(): Allocator() { }
22
23 template<class U>
24 noinit_adaptor(U&& u) noexcept : Allocator(std::forward<U>(u)) { }
25
26 template<class U>
27 noinit_adaptor(const noinit_adaptor<U>& u) noexcept : Allocator(static_cast<const U&>(u)) { }
28
29 template<class U>
30 void construct(U* p) {
31 ::new((void*)p) U;
32 }
33
34 template<class U, class V, class... Args>
35 void construct(U* p, V&& v, Args&&... args) {
36 ::new((void*)p) U(std::forward<V>(v), std::forward<Args>(args)...);
37 }
38
39 template<class U>
40 void destroy(U* p) {
41 p->~U();
42 }
43 };
44
45 template<class T, class U>
46 inline bool operator==(const noinit_adaptor<T>& lhs,
47 const noinit_adaptor<U>& rhs) noexcept
48 {
49 return static_cast<const T&>(lhs) == static_cast<const U&>(rhs);
50 }
51
52 template<class T, class U>
53 inline bool operator!=(const noinit_adaptor<T>& lhs,
54 const noinit_adaptor<U>& rhs) noexcept
55 {
56 return !(lhs == rhs);
57 }
58
59 template<class Allocator>
60 inline noinit_adaptor<Allocator> noinit_adapt(const Allocator& a) noexcept
61 {
62 return noinit_adaptor<Allocator>(a);
63 }
64
65 template<class T> using NoInitVector = std::vector<T, noinit_adaptor<std::allocator<T>>>;
66
67 using PacketBuffer = NoInitVector<uint8_t>;