Skip to content

Instantly share code, notes, and snippets.

@kaoskorobase
Created January 20, 2019 11:31
Show Gist options
  • Save kaoskorobase/38e15df544dd9b9866dcc2e28191f77c to your computer and use it in GitHub Desktop.
Save kaoskorobase/38e15df544dd9b9866dcc2e28191f77c to your computer and use it in GitHub Desktop.
Faust DSP factory table using custom allocator
#include <iostream>
#include <list>
#include <map>
class memory_manager {
public:
void* allocate(size_t n) { return new char[n]; }
void destroy(void* ptr) { delete[] static_cast<char*>(ptr); }
};
template <class T>
class custom_allocator {
memory_manager* m_manager;
public:
typedef T value_type;
template <class U>
struct rebind {
typedef custom_allocator<U> other;
};
custom_allocator(memory_manager* manager) : m_manager(manager) {}
custom_allocator(const custom_allocator<T>& other) : m_manager(other.m_manager) {}
template <class U>
custom_allocator(const custom_allocator<U>& other) : m_manager(other.manager())
{
}
memory_manager* manager() const { return m_manager; }
T* allocate(std::size_t n)
{
void* ptr = m_manager->allocate(n * sizeof(T));
std::cout << "allocate " << ptr << " (" << n * sizeof(T) << ")" << std::endl;
return static_cast<T*>(ptr);
}
void deallocate(T* ptr, std::size_t n)
{
std::cout << "deallocate " << ptr << " (" << n * sizeof(T) << ")" << std::endl;
m_manager->destroy(ptr);
}
};
template <class T, class U>
constexpr bool operator==(const custom_allocator<T>& a, const custom_allocator<U>& b) noexcept
{
return a.manager() == b.manager();
}
template <class T, class U>
constexpr bool operator!=(const custom_allocator<T>& a, const custom_allocator<U>& b) noexcept
{
return !(a == b);
}
struct dsp {
};
typedef std::list<dsp*, custom_allocator<dsp*>> dsp_list;
typedef std::map<int, dsp_list, std::less<int>, custom_allocator<std::pair<const int, dsp_list>>> factory_map;
int main()
{
memory_manager* m = new memory_manager();
factory_map fm((factory_map::allocator_type(m)));
fm.emplace(0, dsp_list(dsp_list::allocator_type(m)));
fm.at(0).push_back(new dsp());
// This doesn't compile:
// fm[0] = dsp_list(dsp_list::allocator_type(m));
// fm[0].push_back(new dsp());
}
@kaoskorobase
Copy link
Author

Uncommenting line 73 fails with:

In file included from allocator.cpp:1:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/iostream:38:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/ios:216:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/__locale:15:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:470:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string_view:171:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/__string:56:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/algorithm:640:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2101:54: error: no
      matching constructor for initialization of 'custom_allocator<std::__1::__list_node<dsp *, void *> >'
        : __first_(_VSTD::forward<_T1_param>(__t1)), __second_() {}
                                                     ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2317:11: note: in
      instantiation of member function 'std::__1::__libcpp_compressed_pair_imp<unsigned long,
      custom_allocator<std::__1::__list_node<dsp *, void *> >, 0>::__libcpp_compressed_pair_imp' requested here
        : base(_VSTD::forward<_T1_param>(__t1)) {}
          ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/list:696:7: note: in
      instantiation of member function 'std::__1::__compressed_pair<unsigned long, custom_allocator<std::__1::__list_node<dsp *,
      void *> > >::__compressed_pair' requested here
    : __size_alloc_(0)
      ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/list:832:5: note: in
      instantiation of member function 'std::__1::__list_imp<dsp *, custom_allocator<dsp *> >::__list_imp' requested here
    list()
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/tuple:1356:7: note: in
      instantiation of member function 'std::__1::list<dsp *, custom_allocator<dsp *> >::list' requested here
      second(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
      ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/utility:484:11: note: in
      instantiation of function template specialization 'std::__1::pair<const int, std::__1::list<dsp *, custom_allocator<dsp *> >
      >::pair<int &&, 0>' requested here
        : pair(__pc, __first_args, __second_args,
          ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:1699:36: note: (skipping
      1 context in backtrace; use -ftemplate-backtrace-limit=0 to see all)
                ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
                                   ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:1540:14: note: in
      instantiation of function template specialization
      'std::__1::allocator_traits<custom_allocator<std::__1::__tree_node<std::__1::__value_type<int, std::__1::list<dsp *,
      custom_allocator<dsp *> > >, void *> > >::__construct<std::__1::pair<const int, std::__1::list<dsp *, custom_allocator<dsp
      *> > >, const std::__1::piecewise_construct_t &, std::__1::tuple<int &&>, std::__1::tuple<> >' requested here
            {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
             ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/__tree:2163:20: note: in
      instantiation of function template specialization
      'std::__1::allocator_traits<custom_allocator<std::__1::__tree_node<std::__1::__value_type<int, std::__1::list<dsp *,
      custom_allocator<dsp *> > >, void *> > >::construct<std::__1::pair<const int, std::__1::list<dsp *, custom_allocator<dsp *>
      > >, const std::__1::piecewise_construct_t &, std::__1::tuple<int &&>, std::__1::tuple<> >' requested here
    __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), _VSTD::forward<_Args>(__args)...);
                   ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/__tree:2108:29: note: in
      instantiation of function template specialization 'std::__1::__tree<std::__1::__value_type<int, std::__1::list<dsp *,
      custom_allocator<dsp *> > >, std::__1::__map_value_compare<int, std::__1::__value_type<int, std::__1::list<dsp *,
      custom_allocator<dsp *> > >, std::__1::less<int>, true>, custom_allocator<std::__1::__value_type<int, std::__1::list<dsp *,
      custom_allocator<dsp *> > > > >::__construct_node<const std::__1::piecewise_construct_t &, std::__1::tuple<int &&>,
      std::__1::tuple<> >' requested here
        __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
                            ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/map:1376:20: note: in
      instantiation of function template specialization 'std::__1::__tree<std::__1::__value_type<int, std::__1::list<dsp *,
      custom_allocator<dsp *> > >, std::__1::__map_value_compare<int, std::__1::__value_type<int, std::__1::list<dsp *,
      custom_allocator<dsp *> > >, std::__1::less<int>, true>, custom_allocator<std::__1::__value_type<int, std::__1::list<dsp *,
      custom_allocator<dsp *> > > > >::__emplace_unique_key_args<int, const std::__1::piecewise_construct_t &, std::__1::tuple<int
      &&>, std::__1::tuple<> >' requested here
    return __tree_.__emplace_unique_key_args(__k,
                   ^
allocator.cpp:73:7: note: in instantiation of member function 'std::__1::map<int, std::__1::list<dsp *, custom_allocator<dsp *> >,
      std::__1::less<int>, custom_allocator<std::__1::pair<const int, std::__1::list<dsp *, custom_allocator<dsp *> > > >
      >::operator[]' requested here
    fm[0] = dsp_list(dsp_list::allocator_type(m));
      ^
allocator.cpp:28:5: note: candidate constructor template not viable: requires single argument 'other', but no arguments were
      provided
    custom_allocator(const custom_allocator<U>& other) : m_manager(other.manager())
    ^
allocator.cpp:23:5: note: candidate constructor not viable: requires single argument 'manager', but no arguments were provided
    custom_allocator(memory_manager* manager) : m_manager(manager) {}
    ^
allocator.cpp:25:5: note: candidate constructor not viable: requires single argument 'other', but no arguments were provided
    custom_allocator(const custom_allocator<T>& other) : m_manager(other.m_manager) {}
    ^
1 error generated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment