00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef __UT_SmallObject__
00020 #define __UT_SmallObject__
00021
00022 #include "UT_SmallObjectAllocator.h"
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 #define UT_SMALLOBJECT_CLEANPAGES_DEFAULT true
00046 #define UT_SMALLOBJECT_CLEANPAGES_ON true
00047 #define UT_SMALLOBJECT_CLEANPAGES_OFF false
00048
00049 #define UT_SMALLOBJECT_PAGESIZE_DEFAULT 1024
00050
00051 #define UT_SMALLOBJECT_THREADSAFE_DEFAULT true
00052 #define UT_SMALLOBJECT_THREADSAFE_ON true
00053 #define UT_SMALLOBJECT_THREADSAFE_OFF false
00054
00055 #define UT_SMALLOBJECT_MAXOBJECTSIZE_DEFAULT 128
00056
00057
00058
00059 class UT_SmallObjectDefaultSuperClass
00060 {
00061 public:
00062 };
00063
00064
00065 template <class SubClass,
00066 bool cleanPages = UT_SMALLOBJECT_CLEANPAGES_DEFAULT,
00067 int pageSize = UT_SMALLOBJECT_PAGESIZE_DEFAULT,
00068 bool threadSafe = UT_SMALLOBJECT_THREADSAFE_DEFAULT,
00069 size_t maxObjectSize = UT_SMALLOBJECT_MAXOBJECTSIZE_DEFAULT,
00070 class SuperClass = UT_SmallObjectDefaultSuperClass>
00071 class UT_SmallObject : public SuperClass
00072 {
00073 public:
00074 static void *operator new(size_t size)
00075 {
00076 return UT_MySmallObjectAllocator::
00077 getAllocator()->allocate(size);
00078 }
00079 static void *operator new(size_t , void *p)
00080 {
00081 return p;
00082 }
00083 static void operator delete(void *p, size_t size)
00084 {
00085 UT_MySmallObjectAllocator::
00086 getAllocator()->deallocate(p, size);
00087 }
00088
00089 private:
00090 class UT_MySmallObjectAllocator : public UT_SmallObjectAllocator
00091 {
00092 public:
00093 UT_MySmallObjectAllocator()
00094 : UT_SmallObjectAllocator(cleanPages, pageSize,
00095 maxObjectSize, threadSafe)
00096 { }
00097
00098 static UT_MySmallObjectAllocator *getAllocator()
00099 {
00100 static UT_MySmallObjectAllocator *theAllocator = 0;
00101
00102 if( !theAllocator )
00103 theAllocator = new UT_MySmallObjectAllocator();
00104
00105 return theAllocator;
00106 }
00107 };
00108 };
00109
00110
00111 #endif