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 #if defined(WIN32) || defined(MBSD)
00066 #include <tbb/scalable_allocator.h>
00067 template <class SubClass,
00068 bool cleanPages = UT_SMALLOBJECT_CLEANPAGES_DEFAULT,
00069 int pageSize = UT_SMALLOBJECT_PAGESIZE_DEFAULT,
00070 bool threadSafe = UT_SMALLOBJECT_THREADSAFE_DEFAULT,
00071 size_t maxObjectSize = UT_SMALLOBJECT_MAXOBJECTSIZE_DEFAULT,
00072 class SuperClass = UT_SmallObjectDefaultSuperClass>
00073 class UT_SmallObject : public SuperClass
00074 {
00075 public:
00076 static void *operator new(size_t size)
00077 {
00078 return scalable_malloc(size);
00079 }
00080 static void *operator new(size_t , void *p)
00081 {
00082 return p;
00083 }
00084 static void operator delete(void *p, size_t )
00085 {
00086 scalable_free(p);
00087 }
00088 };
00089 #else
00090 template <class SubClass,
00091 bool cleanPages = UT_SMALLOBJECT_CLEANPAGES_DEFAULT,
00092 int pageSize = UT_SMALLOBJECT_PAGESIZE_DEFAULT,
00093 bool threadSafe = UT_SMALLOBJECT_THREADSAFE_DEFAULT,
00094 size_t maxObjectSize = UT_SMALLOBJECT_MAXOBJECTSIZE_DEFAULT,
00095 class SuperClass = UT_SmallObjectDefaultSuperClass>
00096 class UT_SmallObject : public SuperClass
00097 {
00098 public:
00099 static void *operator new(size_t size)
00100 {
00101 return getAllocator()->allocate(size);
00102 }
00103 static void *operator new(size_t , void *p)
00104 {
00105 return p;
00106 }
00107 static void operator delete(void *p, size_t size)
00108 {
00109 getAllocator()->deallocate(p, size);
00110 }
00111
00112 private:
00113 static UT_API UT_SmallObjectAllocator *getAllocator()
00114 {
00115 static UT_SmallObjectAllocator *theAllocator = 0;
00116
00117 if( !theAllocator )
00118 theAllocator = new UT_SmallObjectAllocator(
00119 cleanPages, pageSize, maxObjectSize, threadSafe);
00120
00121 return theAllocator;
00122 }
00123 };
00124 #endif
00125
00126
00127 #endif