Program Listing for File Allocators.h

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

/*--------------------------------------------------------------------------*/
/*--------------------------- File Allocators.h ----------------------------*/
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/*----------------------------- DEFINITIONS --------------------------------*/
/*--------------------------------------------------------------------------*/

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

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

#include "ADS/Contract.h"

#include <cstddef>
#include <limits>
#include <map>
#include <set>
#include <vector>

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

namespace ADS {

/*--------------------------------------------------------------------------*/
/*------------------------ CLASS BoundaryTagAllocator ----------------------*/
/*--------------------------------------------------------------------------*/

class BoundaryTagAllocator {

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

 enum class Fit { FIRST , BEST , NEXT };

 explicit BoundaryTagAllocator( long N , Fit fit = Fit::FIRST );

 long allocate( long n );

 void free( long a );

 [[nodiscard]] long freeWords( void ) const;

 [[nodiscard]] long freeBlocks( void ) const;

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

 // a block of s words at p: M[ p ] = M[ p + s - 1 ] = +s if free, -s if in
 // use; a free block has also M[ p + 1 ] = next, M[ p + 2 ] = prev on the
 // list of the free blocks, whose head is the place N (two extra words)

 void unlink( long p );
 void link( long p );

 [[nodiscard]] bool tiled( void ) const;

 std::vector< long > M;
 long N;
 Fit f_fit;
 long f_rover;
 std::set< long > f_used;

 };  // end( class BoundaryTagAllocator )

/*--------------------------------------------------------------------------*/
/*-------------------------- CLASS BuddyAllocator --------------------------*/
/*--------------------------------------------------------------------------*/

class BuddyAllocator {

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

 explicit BuddyAllocator( int m );

 long allocate( long n );

 void free( long a );

 [[nodiscard]] long freeWords( void ) const;

 [[nodiscard]] long blockSize( long a ) const;

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

 [[nodiscard]] bool merged( void ) const;

 int f_m;
 std::vector< std::set< long > > f_free;
 std::map< long , int > f_used;

 };  // end( class BuddyAllocator )

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

}  // end( namespace ADS )

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

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

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

#endif  /* Allocators.h included */

/*--------------------------------------------------------------------------*/
/*------------------------- End File Allocators.h --------------------------*/
/*--------------------------------------------------------------------------*/