DynaMix  1.3.7
A new take on polymorphism in C++
dynamix::object_allocator Class Referenceabstract

#include <allocators.hpp>

Public Member Functions

virtual void on_set_to_object (object &owner)
 
virtual void release (object &owner) noexcept
 
virtual object_allocatoron_copy_construct (object &target, const object &source)
 
virtual object_allocatoron_move (object &target, object &source) noexcept
 
virtual char * alloc_mixin_data (size_t count, const object *obj)=0
 
virtual void dealloc_mixin_data (char *ptr, size_t count, const object *obj)=0
 
virtual std::pair< char *, size_t > alloc_mixin (const basic_mixin_type_info &info, const object *obj)=0
 
virtual void dealloc_mixin (char *ptr, size_t mixin_offset, const basic_mixin_type_info &info, const object *obj)=0
 
virtual void construct_mixin (const basic_mixin_type_info &info, void *ptr)
 
virtual bool copy_construct_mixin (const basic_mixin_type_info &info, void *ptr, const void *source)
 
virtual void destroy_mixin (const basic_mixin_type_info &info, void *ptr) noexcept
 
bool has_allocated () const
 

Static Public Member Functions

static size_t mem_size_for_mixin (size_t mixin_size, size_t mixin_alignment)
 
static size_t mixin_offset (const char *buffer, size_t mixin_alignment)
 

Static Public Attributes

static constexpr size_t mixin_data_size = sizeof(internal::mixin_data_in_object)
 

Protected Attributes

std::atomic< bool > _has_allocated
 

Detailed Description

The class should be the parent to your custom object allocators, i.e. allocators that are set to objects.

It's derived from domain_allocator and provides a number of additional virtual methods a user can override.

Member Function Documentation

virtual void dynamix::object_allocator::on_set_to_object ( object owner)
virtual

Called when an allocator is set to an object. This happens in the following cases:

  • When the object is constructed with an allocator
  • When the object is copy-constructed from another object and on_copy_construct returns non-null
  • When an object is moved onto another and on_move return non-null (This could lead to on_set_to_object being called multiple times for the same allocator and different objects)

The default implementation is empty

virtual void dynamix::object_allocator::release ( object owner)
virtualnoexcept

Override this if you want to provide some custom release logic to an object allocator. It will be called when the object allocator for a given object should logically be destroyed.

If multiple objects have the same allocator, this would be a good spot to decrement a reference counter.

The default implementation is empty

virtual object_allocator* dynamix::object_allocator::on_copy_construct ( object target,
const object source 
)
virtual

Called when an object is copy-constructed from the owner object Use it to provide custom logic so as to create an allocator for the target

WARNING: Cases which count as copy construction are also ones where an object is copied onto an empty one. This can happen with object::copy_from or the equality operator if it exists

The default implementation returns nullptr

virtual object_allocator* dynamix::object_allocator::on_move ( object target,
object source 
)
virtualnoexcept

Called when an object is moved from the owner object Use it to provide custom logic so as to create an allocator for the terget.

After this function is called the sources allocator will be set to nullptr without release being called for it. If you want to return a different allocator for the target object, you need to release the one from source here.

The default implementation returns this (ie the allocator of source)

virtual char* dynamix::domain_allocator::alloc_mixin_data ( size_t  count,
const object obj 
)
pure virtualinherited

Pure virtual. Should return a valid pointer to an array with the size of count mixin_data_in_object instances.

Use the static constant mixin_data_size to get the size of a single mixin_data_in_object

Example:
char* my_allocator::alloc_mixin_data(size_t count, const object*)
{
return new char[count * mixin_data_size];
}

Implemented in dynamix::internal::default_allocator.

virtual void dynamix::domain_allocator::dealloc_mixin_data ( char *  ptr,
size_t  count,
const object obj 
)
pure virtualinherited

Pure virtual. Should free the memory that has been obtained via a call to alloc_mixin_data. The number of elements to dealocate will correspond to the number of elements used to allocated the buffer

Implemented in dynamix::internal::default_allocator.

size_t dynamix::mixin_allocator::mem_size_for_mixin ( size_t  mixin_size,
size_t  mixin_alignment 
)
inlinestaticinherited

Calculates appropriate size for a mixin buffer so as to satisfy the requirements of mixin size and alignment AND leave a room for its owning object in front.

You may use it in your overrides of alloc_mixin to determine the appropriate memory size.

size_t dynamix::mixin_allocator::mixin_offset ( const char *  buffer,
size_t  mixin_alignment 
)
inlinestaticinherited

Calculates the appropriate offset of the mixin in the buffer so as to satisfy the requirements of its alignment AND leave a room for its owning object in front.

You may use it in your overrides of alloc_mixin to determine the correct mixin_offset.

virtual std::pair<char*, size_t> dynamix::mixin_allocator::alloc_mixin ( const basic_mixin_type_info info,
const object obj 
)
pure virtualinherited

Pure virtual. Returns a buffer of memory and the offset of the mixin within it (according to the alignment) BUT IN SUCH A WAY AS TO ALLOW A POINTER TO BE PLACED IN FRONT

You may use mem_size_for_mixin and mixin_offset if you're not sure what to do.

Example:
std::pair<char*, size_t> your_allocator::alloc_mixin(const basic_mixin_type_info& info, const object*)
{
size_t mem_size = mem_size_for_mixin(info.size, info.alignment);
auto buffer = new char[mem_size];
return make_pair(buffer, mixin_offset(buffer, info.alignment));
}

Implemented in dynamix::internal::default_allocator.

virtual void dynamix::mixin_allocator::dealloc_mixin ( char *  ptr,
size_t  mixin_offset,
const basic_mixin_type_info info,
const object obj 
)
pure virtualinherited

Pure virtual. Should free the memory that has been obtained via a call to alloc_mixin. The library will call the method with the same mixin type info which was used to allocate it previously and also with the offset which was returned by the allocation.

Implemented in dynamix::internal::default_allocator.

virtual void dynamix::mixin_allocator::construct_mixin ( const basic_mixin_type_info info,
void *  ptr 
)
virtualinherited

Virtual function, which constructs a mixin within a memory buffer. The default implementation calls the default constructor.

virtual bool dynamix::mixin_allocator::copy_construct_mixin ( const basic_mixin_type_info info,
void *  ptr,
const void *  source 
)
virtualinherited

Virtual function, which copy-constructs a mixin within a memory buffer, from a given source. Should return false if the copy-construction failed. The default implementation calls the default copy constructor and returns false if none exists.

virtual void dynamix::mixin_allocator::destroy_mixin ( const basic_mixin_type_info info,
void *  ptr 
)
virtualnoexceptinherited

Virtual function, which destroys a mixin from a given buffer. The default implementation calls the destructor.

Member Data Documentation

constexpr size_t dynamix::domain_allocator::mixin_data_size = sizeof(internal::mixin_data_in_object)
staticinherited

Size of mixin_data_in_object

Use this to determine how many bytes you'll allocate for single mixin data in alloc_mixin_data


The documentation for this class was generated from the following file: