forked from Aircoookie/ESPAsyncWebServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use default init allocator for vectors
This speeds up our case as we don't need to zero the memory first.
- Loading branch information
1 parent
09a4797
commit 9550a53
Showing
2 changed files
with
36 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#pragma once | ||
|
||
// A C++ allocator that default-initializes objects | ||
// This principally useful for vectors of POD types, where default allocation is a no-op; so the vector can be resized "for free". | ||
// | ||
// Code shamelessly stolen from https://stackoverflow.com/a/21028912/8715474 | ||
|
||
#include <memory> | ||
|
||
template <typename T, typename A = std::allocator<T>> | ||
class default_init_allocator : public A { | ||
typedef std::allocator_traits<A> a_t; | ||
public: | ||
// http://en.cppreference.com/w/cpp/language/using_declaration | ||
using A::A; // Inherit constructors from A | ||
|
||
template <typename U> struct rebind { | ||
using other = | ||
default_init_allocator | ||
< U, typename a_t::template rebind_alloc<U> >; | ||
}; | ||
|
||
template <typename U> | ||
void construct(U* ptr) | ||
noexcept(std::is_nothrow_default_constructible<U>::value) { | ||
::new(static_cast<void*>(ptr)) U; | ||
} | ||
|
||
template <typename U, typename...Args> | ||
void construct(U* ptr, Args&&... args) { | ||
a_t::construct(static_cast<A&>(*this), | ||
ptr, std::forward<Args>(args)...); | ||
} | ||
}; |