You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.2 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
Part of the Fluid Corpus Manipulation Project (http://www.flucoma.org/)
Copyright 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 Unions Horizon 2020 research and innovation programme
(grant agreement No 725899).
*/
#pragma once
#include <SC_PlugIn.hpp>
#include <cstdlib>
#include <limits>
#include <new>
namespace fluid {
template <typename T, typename Wrapper>
class SCWorldAllocator
{
World* mWorld;
InterfaceTable* mInterface;
public:
using propagate_on_container_move_assignment = std::true_type;
using value_type = T;
template <typename U, typename W>
friend class SCWorldAllocator;
SCWorldAllocator(World* w, InterfaceTable* interface)
: mWorld{w}, mInterface{interface}
{}
template <typename U, typename W>
SCWorldAllocator(const SCWorldAllocator<U, W>& other) noexcept
{
mWorld = other.mWorld;
mInterface = other.mInterface;
}
T* allocate(std::size_t n)
{
if (n > std::numeric_limits<std::size_t>::max() / sizeof(T))
throw std::bad_array_new_length();
if (mWorld && mInterface)
if (auto p = static_cast<T*>(mInterface->fRTAlloc(mWorld, n * sizeof(T))))
return p;
throw std::bad_alloc();
}
void deallocate(T* p, std::size_t /*n*/) noexcept
{
if (mWorld && mInterface) mInterface->fRTFree(mWorld, p);
}
};
//foonathan::memory RawAllocator with SC rtalloc
struct SCRawAllocator
{
using is_stateful = std::true_type;
SCRawAllocator(World* w, InterfaceTable* interface)
: mWorld{w}, mInterface{interface}
{}
void* allocate_node(std::size_t size, std::size_t)
{
if(auto res = mInterface->fRTAlloc(mWorld,size))
{
// std::cout << "Allocated " << res << " with " << size << '\n';
return res;
}
throw std::bad_alloc();
}
void deallocate_node(void* node, std::size_t /*size*/, std::size_t) noexcept
{
mInterface->fRTFree(mWorld, node);
// std::cout << "Freed " << node << " with " << size << '\n';
}
private:
World* mWorld;
InterfaceTable* mInterface;
};
} // namespace fluid