00001
#if !defined(__ABSTRACTALLOCATOR_HPP)
00002
#define __ABSTRACTALLOCATOR_HPP
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
#if !defined(__COMMON_HPP)
00025
#include <Common.hpp>
00026
#endif
00027
00028
#if !defined(__ALLOCATOR_HPP)
00029
#include <Allocator.hpp>
00030
#endif
00031
00032 namespace corelinux
00033 {
00043
template<
class TypeImpl >
00044 class AbstractAllocator :
public Allocator
00045 {
00046
public:
00047
00048
00049
00050
00051
00053
00054 AbstractAllocator(
void )
00055 :
00056
Allocator( )
00057 {
00058 ;
00059 }
00060
00066
AbstractAllocator
00067 (
00068
const AbstractAllocator & aRef
00069 )
00070 :
00071
Allocator( aRef )
00072 {
00073 ;
00074 }
00075
00077
00078 virtual ~AbstractAllocator(
void )
00079 {
00080 ;
00081 }
00082
00083
00084
00085
00086
00093
AbstractAllocator &
operator=
00094 (
00095
const AbstractAllocator & aRef
00096 )
00097 {
00098 Allocator::operator=(aRef);
00099
return (*this);
00100 }
00101
00108 bool operator==(
const AbstractAllocator & aRef )
const
00109
{
00110
return Allocator::operator==(aRef);
00111 }
00112
00113
00114
00115
00116
00117
00128 TypeImpl *
createType(
void )
00129 {
00130 TypeImpl *aPtr( NULLPTR );
00131
try
00132 {
00133 aPtr =
allocateObject();
00134
incrementAllocates();
00135 }
00136
catch( ... )
00137 {
00138
decrementAllocates();
00139
throw;
00140 }
00141
return aPtr;
00142 }
00143
00154 void destroyType( TypeImpl *aPtr )
00155 {
00156
try
00157 {
00158 deallocateObject( aPtr );
00159
incrementDeallocates();
00160 }
00161
catch( ... )
00162 {
00163
decrementDeallocates();
00164
throw;
00165 }
00166 }
00167
00168
protected:
00169
00170
00171
00172
00173
00179
virtual TypeImpl *
allocateObject(
void ) = 0;
00180
00186
virtual void deallocateObject( TypeImpl * ) = 0;
00187
00188
00189 };
00190
00191 }
00192
00200
#define CORELINUX_DEFAULT_ALLOCATOR( nameTag, typeTag ) \
00201
class nameTag : public CORELINUX(AbstractAllocator<typeTag>) \
00202
{ \
00203
public: \
00204
nameTag( void ) \
00205
: \
00206
CORELINUX(AbstractAllocator<typeTag>)()\
00207
{ \
00208
; \
00209
} \
00210
\
00211
virtual ~nameTag( void ) \
00212
{ \
00213
; \
00214
} \
00215
protected: \
00216
\
00217
\
00218
virtual typeTag *allocateObject( void ) \
00219
{ \
00220
return ::new typeTag; \
00221
} \
00222
\
00223
virtual void deallocateObject( typeTag *aPtr ) \
00224
{ \
00225
::delete aPtr; \
00226
} \
00227
}; \
00228
typedef nameTag * nameTag ## Ptr; \
00229
typedef const nameTag * nameTag ## Cptr; \
00230
typedef nameTag & nameTag ## Ref; \
00231
typedef const nameTag & nameTag ## Cref;
00232
00233
#endif // if !defined(__ABSTRACTALLOCATOR_HPP)
00234
00235
00236
00237
00238
00239
00240
00241
00242