From 6b4f5f67ae40b042b7e18cee04fe2148fbe6a8a5 Mon Sep 17 00:00:00 2001 From: Owen Green Date: Wed, 23 Jun 2021 12:06:38 +0100 Subject: [PATCH] Add fluid::SCWorldAllocator --- include/wrapper/SCWorldAllocator.hpp | 57 ++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 include/wrapper/SCWorldAllocator.hpp diff --git a/include/wrapper/SCWorldAllocator.hpp b/include/wrapper/SCWorldAllocator.hpp new file mode 100644 index 0000000..a53d1fd --- /dev/null +++ b/include/wrapper/SCWorldAllocator.hpp @@ -0,0 +1,57 @@ +/* + Part of the Fluid Corpus Manipulation Project (http://www.flucoma.org/) + Copyright 2017-2019 University of Huddersfield. + Licensed under the BSD-3 License. + See license.md file in the project root for full license information. + This project has received funding from the European Research Council (ERC) + under the European Union’s Horizon 2020 research and innovation programme + (grant agreement No 725899). + */ +#pragma once + +#include +#include +#include +#include + +namespace fluid { + +template +class SCWorldAllocator +{ + World* mWorld{nullptr}; + InterfaceTable* mInterface{nullptr}; + +public: + using propagate_on_container_move_assignment = std::true_type; + using value_type = T; + + template + friend class SCWorldAllocator; + + SCWorldAllocator(World* w, InterfaceTable* ft) : mWorld{w}, mInterface{ft} {} + + template + SCWorldAllocator(const SCWorldAllocator& other) noexcept + : mWorld{other.mWorld}, mInterface{other.mInterface} + {} + + T* allocate(std::size_t n) + { + if (n > std::numeric_limits::max() / sizeof(T)) + throw std::bad_array_new_length(); + + if (mWorld && mInterface) + if (auto p = static_cast(mInterface->fRTAlloc(mWorld, n * sizeof(T)))) + return p; + + throw std::bad_alloc(); + } + + void deallocate(T* p, std::size_t n) noexcept + { + assert(mWorld && mInterface); + mInterface->fRTFree(mWorld, p); + } +}; +} // namespace fluid