File tree Expand file tree Collapse file tree 2 files changed +36
-1
lines changed Expand file tree Collapse file tree 2 files changed +36
-1
lines changed Original file line number Diff line number Diff line change 27
27
#undef max
28
28
#endif
29
29
#include < vector>
30
+ #include " default_init_allocator.h"
30
31
// It is possible to restore these defines, but one can use _min and _max instead. Or std::min, std::max.
31
32
32
33
class AsyncBasicResponse : public AsyncWebServerResponse {
@@ -46,7 +47,7 @@ class AsyncAbstractResponse: public AsyncWebServerResponse {
46
47
// This is inefficient with vector, but if we use some other container,
47
48
// we won't be able to access it as contiguous array of bytes when reading from it,
48
49
// so by gaining performance in one place, we'll lose it in another.
49
- std::vector<uint8_t > _packet, _cache;
50
+ std::vector<uint8_t , default_init_allocator< uint8_t > > _packet, _cache;
50
51
size_t _readDataFromCacheOrContent (uint8_t * data, const size_t len);
51
52
size_t _fillBufferAndProcessTemplates (uint8_t * buf, size_t maxLen);
52
53
protected:
Original file line number Diff line number Diff line change
1
+ #pragma once
2
+
3
+ // A C++ allocator that default-initializes objects
4
+ // This principally useful for vectors of POD types, where default allocation is a no-op; so the vector can be resized "for free".
5
+ //
6
+ // Code shamelessly stolen from https://stackoverflow.com/a/21028912/8715474
7
+
8
+ #include < memory>
9
+
10
+ template <typename T, typename A = std::allocator<T>>
11
+ class default_init_allocator : public A {
12
+ typedef std::allocator_traits<A> a_t ;
13
+ public:
14
+ // http://en.cppreference.com/w/cpp/language/using_declaration
15
+ using A::A; // Inherit constructors from A
16
+
17
+ template <typename U> struct rebind {
18
+ using other =
19
+ default_init_allocator
20
+ < U, typename a_t ::template rebind_alloc<U> >;
21
+ };
22
+
23
+ template <typename U>
24
+ void construct (U* ptr)
25
+ noexcept (std::is_nothrow_default_constructible<U>::value) {
26
+ ::new (static_cast <void *>(ptr)) U;
27
+ }
28
+
29
+ template <typename U, typename ...Args>
30
+ void construct (U* ptr, Args&&... args) {
31
+ a_t::construct (static_cast <A&>(*this ),
32
+ ptr, std::forward<Args>(args)...);
33
+ }
34
+ };
You can’t perform that action at this time.
0 commit comments