From 86cb9e3d6e47aa16df3d0f9492e25cf027960ae5 Mon Sep 17 00:00:00 2001 From: keldu Date: Wed, 17 Mar 2021 11:24:18 +0100 Subject: [PATCH] oom safety implementations --- driver/io-unix.cpp | 4 ++-- source/async.cpp | 11 ++++++++--- source/async.h | 2 +- source/common.h | 45 ++++----------------------------------------- source/error.cpp | 18 ++++++++++++++---- source/error.h | 8 ++++---- 6 files changed, 33 insertions(+), 55 deletions(-) diff --git a/driver/io-unix.cpp b/driver/io-unix.cpp index 6021230..fcf1f8c 100644 --- a/driver/io-unix.cpp +++ b/driver/io-unix.cpp @@ -313,9 +313,9 @@ ErrorOr setupAsyncIo() { EventLoop &loop_ref = io_provider->eventLoop(); - return {std::move(io_provider), loop_ref, prt_ref}; + return {{std::move(io_provider), loop_ref, prt_ref}}; } catch (std::bad_alloc &) { - return criticalError(""); + return criticalError("Out of memory"); } } } // namespace gin diff --git a/source/async.cpp b/source/async.cpp index b6816f5..6e74838 100644 --- a/source/async.cpp +++ b/source/async.cpp @@ -276,13 +276,18 @@ void ConveyorSinks::fail(Error &&error) { void ConveyorSinks::add(Conveyor &&sink) { auto nas = Conveyor::fromConveyor(std::move(sink)); - Own sink_node = - heap(std::move(nas.first), *this); + + Own sink_node = nullptr; + try { + sink_node = heap(std::move(nas.first), *this); + }catch(std::bad_alloc&){ + return; + } if (nas.second) { nas.second->setParent(sink_node.get()); } - sink_nodes.push_back(std::move(sink_node)); + sink_nodes.emplace_back(std::move(sink_node)); } void ConveyorSinks::fire() { diff --git a/source/async.h b/source/async.h index d359d70..77ea5bd 100644 --- a/source/async.h +++ b/source/async.h @@ -668,4 +668,4 @@ public: } // namespace gin -#include "async.tmpl.h" \ No newline at end of file +#include "async.tmpl.h" diff --git a/source/common.h b/source/common.h index aa1054c..7489547 100644 --- a/source/common.h +++ b/source/common.h @@ -15,53 +15,16 @@ namespace gin { classname(const classname &) = delete; \ classname &operator=(const classname &) = delete -template using Maybe = std::optional; +template using Maybe = std::optional; -template class Own { -private: - T *data = nullptr; - -public: - Own() = default; - Own(T *d) : data{d} {} - ~Own() { - if (data) { - delete data; - } - } - - Own(Own &&rhs) : data{rhs.data} { rhs.data = nullptr; } - - Own &operator=(Own &&rhs) { - if (data) { - delete data; - } - - data = rhs.data; - rhs.data = nullptr; - - return *this; - } - - T *operator->() const noexcept { return data; } - - explicit operator bool() const noexcept { return data; } - - T *get() noexcept { return data; } - - typename std::add_lvalue_reference::type operator*() const { - return *data; - } - - GIN_FORBID_COPY(Own); -}; +template using Own = std::unique_ptr; template using Our = std::shared_ptr; template using Lent = std::weak_ptr; template Own heap(Args &&...args) { - return Own(new T{std::forward(args)...}); + return Own(new (std::nothrow) T(std::forward(args)...)); } template Our share(Args &&...args) { @@ -90,4 +53,4 @@ template using UnfixVoid = typename VoidUnfix::Type; template using ReturnType = typename ReturnTypeHelper::Type; -} // namespace gin \ No newline at end of file +} // namespace gin diff --git a/source/error.cpp b/source/error.cpp index e9441a7..f0a839d 100644 --- a/source/error.cpp +++ b/source/error.cpp @@ -9,14 +9,24 @@ Error::Error(const std::string_view &msg, int8_t code) Error::Error(std::string &&msg, int8_t code) : error_message{std::move(msg)}, error_{code} {} -Error::Error(const Error &error) - : error_message{error.error_message}, error_{error.error_} {} - Error::Error(Error &&error) : error_message{std::move(error.error_message)}, error_{std::move( error.error_)} {} -const std::string_view Error::message() const { return error_message; } +const std::string_view Error::message() const { + + return std::visit([this](auto&& arg) -> const std::string_view { + using T = std::decay_t; + + if constexpr (std::is_same_v){ + return std::string_view{arg}; + }else if constexpr (std::is_same_v){ + return arg; + }else{ + return "Error. Good luck :)"; + } + }, error_message); +} bool Error::failed() const { return error_ != 0; } diff --git a/source/error.h b/source/error.h index f147aa1..8b46944 100644 --- a/source/error.h +++ b/source/error.h @@ -38,7 +38,7 @@ public: template Error makeError(const Formatter &formatter, int8_t code, - const std::string_view &generic = "") { + const std::string_view &generic) { try { std::string error_msg = formatter(); return Error{std::move(error_msg), code}; @@ -53,7 +53,7 @@ Error criticalError(const std::string_view &generic) { template Error criticalError(const Formatter &formatter, - const std::string_view &generic = "") { + const std::string_view &generic) { return makeError(formatter, -1, generic); } @@ -63,7 +63,7 @@ Error recoverableError(const std::string_view &generic) { template Error recoverableError(const Formatter &formatter, - const std::string_view &generic = "") { + const std::string_view &generic) { return makeError(formatter, -1, generic); } @@ -116,4 +116,4 @@ public: const T &value() const { return std::get(value_or_error); } }; -} // namespace gin \ No newline at end of file +} // namespace gin