Program Listing for File SizeClassAllocator.h

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

/*--------------------------------------------------------------------------*/
/*----------------------- File SizeClassAllocator.h ------------------------*/
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/*----------------------------- DEFINITIONS --------------------------------*/
/*--------------------------------------------------------------------------*/

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

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

#include "ADS/Contract.h"

#include <bit>
#include <cstddef>
#include <cstdint>
#include <new>
#include <utility>

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

namespace ADS {

/*--------------------------------------------------------------------------*/
/*----------------------- CLASS SizeClassAllocator -------------------------*/
/*--------------------------------------------------------------------------*/

class SizeClassAllocator {

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

 static constexpr std::size_t LARGEST = 32768;

 static constexpr std::size_t PAGE = std::size_t( 1 ) << 16;

 SizeClassAllocator( void ) = default;
 SizeClassAllocator( const SizeClassAllocator & ) = delete;
 SizeClassAllocator & operator=( const SizeClassAllocator & ) = delete;

 SizeClassAllocator( SizeClassAllocator && other ) noexcept;

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

 ~SizeClassAllocator() { release(); }

 [[nodiscard]] void * allocate( std::size_t n );

 void deallocate( void * p , std::size_t n ) noexcept;

 [[nodiscard]] static unsigned sizeClass( std::size_t n );
 [[nodiscard]] static std::size_t classSize( unsigned c );

 [[nodiscard]] std::size_t bytes( void ) const { return( f_bytes ); }

 void release( void ) noexcept;

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

 static constexpr unsigned CLASSES = 9 + 4 * 8;

 struct Free {
  Free * next;
  };

 struct Page {
  Page * prev;
  std::size_t bytes;
  };

 void * fresh( unsigned c );

 Free * f_free[ CLASSES ] = {};
 char * f_next[ CLASSES ] = {};
 char * f_end[ CLASSES ] = {};
 Page * f_pages = nullptr;
 std::size_t f_bytes = 0;

 };  // end( class SizeClassAllocator )

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

}  // end( namespace ADS )

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

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

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

#endif  /* SizeClassAllocator.h included */

/*--------------------------------------------------------------------------*/
/*---------------------- End File SizeClassAllocator.h ---------------------*/
/*--------------------------------------------------------------------------*/