Last active
June 8, 2020 21:21
-
-
Save bstaletic/fedb5aede9b9f54f51c50671ade75d39 to your computer and use it in GitHub Desktop.
Allocator messing around
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
#include <cstdio> | |
#include <memory_resource> | |
#include <new> | |
#include <type_traits> | |
#include <vector> | |
struct data { | |
data() = default; | |
data(int a) : a(a) {} | |
~data() { printf("destructed\n"); } | |
int a; | |
}; | |
int main() { | |
// Storage to hold the vector itself. | |
std::aligned_storage_t<sizeof(std::pmr::vector<data>), alignof(std::pmr::vector<data>)> vector_holder; | |
// Storage for the first 128 vector elements. | |
std::aligned_storage_t<sizeof(data), alignof(data)> buffer[128]; | |
// Monotonic allocator. | |
std::pmr::monotonic_buffer_resource alloc(buffer, 128*sizeof(data)); | |
// Placement new the vector in its local storage. | |
std::pmr::vector<data>* v = new (&vector_holder) std::pmr::vector<data>(&alloc); | |
v->reserve(128); | |
v->emplace_back(1); | |
v->emplace_back(1); | |
v->emplace_back(1); | |
v->emplace_back(1); | |
alloc.release(); | |
v = new (&vector_holder) std::pmr::vector<data>(&alloc); | |
v->reserve(128); | |
v->emplace_back(2); | |
v->emplace_back(2); | |
v->emplace_back(2); | |
v->emplace_back(2); | |
} |
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
#include <cstdio> | |
#include <memory_resource> | |
#include <new> | |
#include <type_traits> | |
#include <vector> | |
struct data { | |
data() = default; | |
data(int a) : a(a) {} | |
// Never gets printed. | |
~data() { printf("destructed\n"); } | |
int a; | |
}; | |
int main() { | |
// Storage to hold the vector itself. | |
std::aligned_storage_t<sizeof(std::pmr::vector<data>), | |
alignof(std::pmr::vector<data>)> | |
vector_holder; | |
// Storage for the first 128 vector elements. | |
std::aligned_storage_t<sizeof(data), alignof(data)> buffer[128]; | |
// Monotonic allocator. | |
std::pmr::monotonic_buffer_resource alloc(buffer, 128 * sizeof(data)); | |
// Placement new the vector in its local storage. | |
std::pmr::vector<data> *v = | |
new (&vector_holder) std::pmr::vector<data>(&alloc); | |
// Reserve all 128 elements. Their space is already allocated in buffer, | |
// so no extra memory is required and prevents the vector from | |
// reallocating when populating it. This allows us to take maximum | |
// advantage of the buffer and avoids any moves... | |
// untill thet 129th element is pushed. | |
v->reserve(128); | |
// Use emplace_back() to avoid temporaries. | |
v->emplace_back(1); | |
v->emplace_back(1); | |
v->emplace_back(1); | |
v->emplace_back(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment