Program Listing for File ObjectPool.h

↰ Return to documentation for file (ADS/memory/ObjectPool.h)

/*--------------------------------------------------------------------------*/
/*--------------------------- File ObjectPool.h ----------------------------*/
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/*----------------------------- DEFINITIONS --------------------------------*/
/*--------------------------------------------------------------------------*/

#ifndef __ADS_ObjectPool
 #define __ADS_ObjectPool
                      /* self-identification: #endif constant */

/*--------------------------------------------------------------------------*/
/*------------------------------ INCLUDES ----------------------------------*/
/*--------------------------------------------------------------------------*/

#include "ADS/Contract.h"
#include "ADS/memory/HugePageAllocator.h"

#include <cstddef>
#include <memory>
#include <new>
#include <type_traits>
#include <utility>

/*--------------------------------------------------------------------------*/
/*--------------------------- NAMESPACE ------------------------------------*/
/*--------------------------------------------------------------------------*/

namespace ADS {

/*--------------------------------------------------------------------------*/
/*---------------------------- CLASS ObjectPool ----------------------------*/
/*--------------------------------------------------------------------------*/

template< class T , class A = HugePageAllocator< T > >
class ObjectPool {

/*--------------------------------------------------------------------------*/
 public:

 explicit ObjectPool( std::size_t n = std::size_t( 1 ) << 24 ,
                      const A & alloc = A() );

 ObjectPool( const ObjectPool & ) = delete;
 ObjectPool & operator=( const ObjectPool & ) = delete;

 ObjectPool( ObjectPool && other ) noexcept;

 ObjectPool & operator=( ObjectPool && other ) noexcept;

 ~ObjectPool();

 void swap( ObjectPool & other ) noexcept;

 template< class... Args >
 T * make( Args &&... args );

 void free( T * x );

 [[nodiscard]] T * allocate( void );

 void deallocate( T * x );

 [[nodiscard]] std::size_t size( void ) const;

 [[nodiscard]] std::size_t bytes( void ) const;

/*--------------------------------------------------------------------------*/
 private:

 union Slot {
  Slot * next;
  alignas( T ) unsigned char object[ sizeof( T ) ];
  };

 struct Block {
  Block * prev;
  std::size_t k;
  };

 static constexpr std::size_t H =
  ( sizeof( Block ) + sizeof( Slot ) - 1 ) / sizeof( Slot );

 using SlotAlloc =
  typename std::allocator_traits< A >::template rebind_alloc< Slot >;

 void release( void ) noexcept;

 Slot * slot( void );

 Slot * f_free = nullptr;
 Slot * f_next = nullptr;
 Slot * f_end = nullptr;
 Block * f_last = nullptr;
 std::size_t f_n;
 std::size_t f_size = 0;
 [[no_unique_address]] SlotAlloc f_alloc;

 };  // end( class ObjectPool )

/*--------------------------------------------------------------------------*/

}  // end( namespace ADS )

/*--------------------------------------------------------------------------*/
/*-------------------------- IMPLEMENTATION --------------------------------*/
/*--------------------------------------------------------------------------*/

#include "ADS/memory/impl/ObjectPool.tpp"

/*--------------------------------------------------------------------------*/

#endif  /* ObjectPool.h included */

/*--------------------------------------------------------------------------*/
/*------------------------- End File ObjectPool.h --------------------------*/
/*--------------------------------------------------------------------------*/