Protium
Math and Design Features
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
SmallObjectAllocator.h
Go to the documentation of this file.
1 #ifndef Protium_SmallObjectAllocator_h_
2 #define Protium_SmallObjectAllocator_h_
3 
9 
10 #include <cstddef>
11 
12 namespace Protium{
13 
14  namespace Allocation{
15 
16  class FixedAllocator;
17 
20  private:
22  const std::size_t fMaxObjectSize;
23  const std::size_t fAlignSize;
24  protected:
25  SmallObjectAllocatorImplementation( std::size_t pageSize, std::size_t maxObjectSize, std::size_t objectAlignSize );
27  public:
28  void * Allocate( std::size_t size, bool doThrow );
29  void Deallocate( void * p, std::size_t size );
30  void Deallocate( void * p );
31  inline std::size_t GetMaxObjectSize() const { return fMaxObjectSize; }
32  inline std::size_t GetAlignment() const { return fAlignSize; }
33  bool TrimExcessMemory( void );
34  bool IsCorrupt( void ) const;
35  private:
39  };
40 
42  template < template <class, class> class ThreadingModel = Protium::Threads::InSingleThread,
43  std::size_t chunkSize = 4096,
44  std::size_t maxSmallObjectSize = 256,
45  std::size_t objectAlignSize = 4,
46  template <class> class LifetimePolicy = Protium::Singleton::DeleteLast,
47  class MutexPolicy = Protium::Threads::Mutex
48  >
50  public:
51 
53  typedef ThreadingModel< AllocatorModel, MutexPolicy > ThreadModel;
54 
56  LifetimePolicy, ThreadingModel > AllocatorSingleton;
57 
58  inline static SmallObjectAllocator & Instance( void )
59  {
61  }
62 
64  SmallObjectAllocatorImplementation( chunkSize, maxSmallObjectSize, objectAlignSize )
65  {}
66 
67  inline ~SmallObjectAllocator( void ) {}
68  static void ClearExtraMemory( void ){
69  typename ThreadModel::Lock lock;
70  (void)lock; // get rid of warning
72  }
73 
74  static bool IsCorrupted( void ) {
75  typename ThreadModel::Lock lock;
76  (void)lock; // get rid of warning
77  return Instance().IsCorrupt();
78  }
79 
80  private:
83  };
84 
85  template
86  <
87  template <class, class> class T,
88  std::size_t C,
89  std::size_t M,
90  std::size_t O,
91  template <class> class L,
92  class X
93  >
94  inline unsigned int GetPriority(
96  {
97  return 0xFFFFFFFF;
98  }
99 
100  }
101 }
102 
103 
104 #endif //File Guardian
static Host & Instance()
Use to get instance.
Definition: Singleton.h:83
Use to specify non-thread safe classes.
ThreadingModel< AllocatorModel, MutexPolicy > ThreadModel
Pool of allocation chunks.
Protium::Singleton::Singleton< AllocatorModel, Protium::Singleton::CreateStatic, LifetimePolicy, ThreadingModel > AllocatorSingleton
Helper struct to create static instances of classes.
Wrapper class for pthreads-style mutexes.
Definition: Mutex.h:22
SmallObjectAllocator & operator=(const SmallObjectAllocator &)
static SmallObjectAllocator & Instance(void)
Singleton small object allocator.
SmallObjectAllocatorImplementation & operator=(const SmallObjectAllocatorImplementation &)
Defines mutexes and policies to be used by threaded objects.
unsigned int GetPriority(SmallObjectAllocator< T, C, M, O, L, X > *)
SmallObjectAllocator< ThreadingModel, chunkSize, maxSmallObjectSize, objectAlignSize, LifetimePolicy > AllocatorModel