Protium
Math and Design Features
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
SmallObject.h
Go to the documentation of this file.
1 #ifndef Protium_SmallObject_h_
2 #define Protium_SmallObject_h_
3 
7 
8 
9 namespace Protium{
10 
11  namespace Allocation{
12 
25  template< template <class, class> class ThreadPolicy,
26  std::size_t chunkSize,
27  std::size_t maxSmallObjectSize,
28  std::size_t objectAlignSize,
29  template <class> class LifetimePolicy,
30  class MutexPolicy>
32  public:
34  typedef Protium::Allocation::SmallObjectAllocator< ThreadPolicy, chunkSize,
35  maxSmallObjectSize, objectAlignSize, LifetimePolicy > ObjAllocator;
36 
37  private:
38 
39  typedef ThreadPolicy< ObjAllocator, MutexPolicy > ThreadType;
41 
42  public:
43 
44  static void * operator new ( std::size_t size ) throw ( std::bad_alloc ){
45  typename ThreadType::Lock lock;
46  (void)lock;
47  return AllocatorSingleton::Instance().Allocate( size, true );
48  }
49 
50  static void * operator new ( std::size_t size, const std::nothrow_t & ) throw () {
51  typename ThreadType::Lock lock;
52  (void)lock;
53  return AllocatorSingleton::Instance().Allocate( size, false );
54  }
55 
56  inline static void * operator new ( std::size_t size, void * place ) {
57  return ::operator new( size, place );
58  }
59 
60  static void operator delete ( void * p, std::size_t size ) throw () {
61  typename ThreadType::Lock lock;
62  (void)lock;
63  AllocatorSingleton::Instance().Deallocate( p, size );
64  }
65 
66  static void operator delete ( void * p, const std::nothrow_t & ) throw() {
67  typename ThreadType::Lock lock;
68  (void)lock; // get rid of warning
69  AllocatorSingleton::Instance().Deallocate( p );
70  }
71 
72  inline static void operator delete ( void * p, void * place ) {
73  ::operator delete ( p, place );
74  }
75 
76  static void * operator new [] ( std::size_t size ) throw ( std::bad_alloc ) {
77  typename ThreadType::Lock lock;
78  (void)lock; // get rid of warning
79  return AllocatorSingleton::Instance().Allocate( size, true );
80  }
81 
82  static void * operator new [] ( std::size_t size, const std::nothrow_t & ) throw () {
83  typename ThreadType::Lock lock;
84  (void)lock; // get rid of warning
85  return AllocatorSingleton::Instance().Allocate( size, false );
86  }
87 
88  inline static void * operator new [] ( std::size_t size, void * place ) {
89  return ::operator new( size, place );
90  }
91 
92  static void operator delete [] ( void * p, std::size_t size ) throw () {
93  typename ThreadType::Lock lock;
94  (void)lock; // get rid of warning
95  AllocatorSingleton::Instance().Deallocate( p, size );
96  }
97 
98  static void operator delete [] ( void * p, const std::nothrow_t & ) throw(){
99  typename ThreadType::Lock lock;
100  (void)lock; // get rid of warning
101  AllocatorSingleton::Instance().Deallocate( p );
102  }
103 
104  inline static void operator delete [] ( void * p, void * place ) {
105  ::operator delete ( p, place );
106  }
107 
108  protected:
109  inline SmallObjectBase( void ) {}
110  inline SmallObjectBase( const SmallObjectBase & ) {}
112  { return *this; }
113  inline ~SmallObjectBase() {}
114  };
115 
116 
122  template
123  <
124  template <class, class> class ThreadingModel = Protium::Threads::InSingleThread,
125  std::size_t chunkSize = 4096,
126  std::size_t maxSmallObjectSize = 256,
127  std::size_t objectAlignSize = 4,
128  template <class> class LifetimePolicy = Protium::Singleton::DeleteLast,
129  class MutexPolicy = Protium::Threads::Mutex
130  >
131  class SmallObject : public SmallObjectBase< ThreadingModel, chunkSize,
132  maxSmallObjectSize, objectAlignSize, LifetimePolicy, MutexPolicy >
133  {
134 
135  public:
136  virtual ~SmallObject() {}
137  protected:
138  inline SmallObject( void ) {}
139 
140  private:
141  SmallObject( const SmallObject & );
142  SmallObject & operator = ( const SmallObject & );
143  }; //end class small object
144 
145 
152  template< template <class, class> class ThreadingModel = Protium::Threads::InSingleThread,
153  std::size_t chunkSize = 4096,
154  std::size_t maxSmallObjectSize = 256,
155  std::size_t objectAlignSize = 4,
156  template <class> class LifetimePolicy = Protium::Singleton::DeleteLast,
157  class MutexPolicy = Protium::Threads::Mutex >
158  class SmallValueObject : public SmallObjectBase< ThreadingModel, chunkSize,
159  maxSmallObjectSize, objectAlignSize, LifetimePolicy, MutexPolicy >
160  {
161  protected:
162  inline SmallValueObject( void ) {}
163  inline SmallValueObject( const SmallValueObject & ) {}
165  { return *this; }
166  inline ~SmallValueObject() {}
167  }; // end class SmallValueObject
168 
170 
175 
176  }
177 }
178 
180 
181 #endif //Protium_SmallObject_h_
static Host & Instance()
Use to get instance.
Definition: Singleton.h:83
ThreadPolicy< ObjAllocator, MutexPolicy > ThreadType
Definition: SmallObject.h:39
Protium::Allocation::SmallObjectAllocator< ThreadPolicy, chunkSize, maxSmallObjectSize, objectAlignSize, LifetimePolicy > ObjAllocator
Type of allocator to host the allocator singleton.
Definition: SmallObject.h:35
Base class for small objects to be used in the small object allocation scheme.
Definition: SmallObject.h:31
ObjAllocator::AllocatorSingleton AllocatorSingleton
Definition: SmallObject.h:40
SmallObject & operator=(const SmallObject &)
SmallObjectBase & operator=(const SmallObjectBase &)
Definition: SmallObject.h:111
SmallValueObject DefaultSmallValueObject
Definition: SmallObject.h:174
Use to specify non-thread safe classes.
SmallObjectBase(const SmallObjectBase &)
Definition: SmallObject.h:110
SmallObject DefaultSmallObject
A default small obect type.
Definition: SmallObject.h:173
Wrapper class for pthreads-style mutexes.
Definition: Mutex.h:22
SmallValueObject(const SmallValueObject &)
Definition: SmallObject.h:163
Singleton small object allocator.
Inherit from this in order to benefit from small object allocation.
Definition: SmallObject.h:158
Inherit from this in order to benefit from small object allocation.
Definition: SmallObject.h:131
SmallValueObject & operator=(const SmallValueObject &)
Definition: SmallObject.h:164