acidrainstudios / TrueReality

True Reality (TR) is an open source LGPL Game and Simulation Engine written entirely in Standard C++ and OpenGL. It runs on all Windows platforms and GNU/Linux. OpenSceneGraph is used as its graphics engine, along with many other open source projects for support of various features.
GNU Lesser General Public License v3.0
12 stars 7 forks source link

Custom Allocators #90

Open AODQ opened 5 years ago

AODQ commented 5 years ago

Add a custom allocator for smart pointers. Would have to research if this would even be worthwhile since OSG abuses new internally.

An example use case:

trBase::SmartPtr<trCore::Geometry, trBase::AllocationStrategy::Arena>
  myGeometry =
{
  // ...
};

// constructor implementation
template <
  typename T,
  typename AllocStrategy = NewAlloc,
  typename... ConstructorArgs
> trBase::SmartPtr<T, AllocStrategy>::SmartPtr(ConstructorArgs&&... args)
{
  internalData =
    AllocStrategy::Allocate<T>(
      std::forward<ConstructorArgs>(args)...
    );
}

template <typename T, typename trBase::AllocStrategy = NewAlloc>
trBase::SmartPtr<T, AllocStrategy>::~SmartPtr()
{
  AllocStrategy::Deallocate(internalData);
}

// new allocator impl
template <typename T, typename... ConstructorArgs>
static T* trBase::NewAlloc::Allocate(ConstructorArgs&&... args)
{
  return new T { std::forward<ConstructorArgs>(args)... };
}

template <typename T> static void trBase::NewAlloc::Deallocate(T* data)
{
  delete data;
}

More info: https://github.com/mtrebi/memory-allocators https://www.youtube.com/watch?v=LIb3L4vKZ7U https://www.youtube.com/watch?v=kSWfushlvB8 https://www.youtube.com/watch?v=d1DpVR0tw0U

VulkanSceneGraph Allocation example: https://github.com/vsg-dev/vsgExamples/blob/master/Core/vsgallocator/vsgallocator.cpp

DieSlower commented 5 years ago

This would be very useful, but 0.2 already has a long list for me to tackle. By any chance...would you like to write something like that up in your free time? ;-) Otherwise I placed it in the 0.5 release.

AODQ commented 5 years ago

If I can get TR to work on my home machine, sure, but no progress can be made until there is a way to circumvent OSG's internal new abuse.

DieSlower commented 5 years ago

TR should be easy to get up and going. If you have Linux, it just takes a bit of time, and if you have windows, it takes about 15min since we provide the external dependencies.

DieSlower commented 5 years ago

BTW, we now have external dependencies for Linux also, so now it takes about 15min to stand up on Windows and Linux