From d4df7d9d6064f34d605d33d965d9c4c3c2d00b42 Mon Sep 17 00:00:00 2001 From: Teon Banek Date: Thu, 11 Jul 2019 13:28:06 +0200 Subject: [PATCH] Add helper methods for new/delete to Allocator Summary: The newly added methods are modeled after the ones being added in C++20 to `std::pmr::polymorphic_allocator`. Reviewers: mtomic, mferencevic Reviewed By: mferencevic Subscribers: pullbot Differential Revision: https://phabricator.memgraph.io/D2197 --- src/utils/memory.hpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/utils/memory.hpp b/src/utils/memory.hpp index 990b99811..c8f447994 100644 --- a/src/utils/memory.hpp +++ b/src/utils/memory.hpp @@ -197,6 +197,29 @@ class Allocator { std::forward_as_tuple(std::forward(xy.second))); } + template + void destroy(U *p) { + p->~U(); + } + + template + U *new_object(TArgs &&... args) { + U *p = static_cast(memory_->Allocate(sizeof(U), alignof(U))); + try { + construct(p, std::forward(args)...); + } catch (...) { + memory_->Deallocate(p, sizeof(U), alignof(U)); + throw; + } + return p; + } + + template + void delete_object(U *p) { + destroy(p); + memory_->Deallocate(p, sizeof(U), alignof(U)); + } + private: MemoryResource *memory_;