00001 /* 00002 * PROPRIETARY INFORMATION. This software is proprietary to 00003 * Side Effects Software Inc., and is not to be reproduced, 00004 * transmitted, or disclosed in any way without written permission. 00005 * 00006 * Produced by: 00007 * Andrew Clinton 00008 * Side Effects Software Inc 00009 * 123 Front Street West, Suite 1401 00010 * Toronto, Ontario 00011 * Canada M5J 2M2 00012 * 416-504-9876 00013 * 00014 * NAME: UT_StackArray.h ( UT Library, C++ ) 00015 * 00016 * COMMENTS: A class to allocate small, dynamic arrays on the stack from 00017 * a static memory pool 00018 */ 00019 00020 #ifndef __UT_StackArray__ 00021 #define __UT_StackArray__ 00022 00023 #include "UT_API.h" 00024 #include "UT_PtrArray.h" 00025 #include "UT_RefArray.h" 00026 #include "UT_IntArray.h" 00027 #include "UT_FloatArray.h" 00028 #include "UT_ThreadSpecificValue.h" 00029 00030 template <typename Array> 00031 class UT_StackArrayData { 00032 public: 00033 int alloc() 00034 { 00035 int idx; 00036 00037 if (myFreeList.entries()) 00038 { 00039 idx = myFreeList.last(); 00040 myFreeList.removeLast(); 00041 myValue(idx)->entries(0); 00042 } 00043 else 00044 { 00045 idx = myValue.entries(); 00046 myValue.append(new Array); 00047 } 00048 return idx; 00049 } 00050 void release(int idx) 00051 { 00052 myFreeList.append(idx); 00053 } 00054 Array &get(int idx) 00055 { 00056 return *myValue(idx); 00057 } 00058 private: 00059 UT_PtrArray<Array *> myValue; 00060 UT_IntArray myFreeList; 00061 }; 00062 00063 #ifndef WIN32 00064 00065 class UT_StackPtrArray { 00066 public: 00067 UT_StackPtrArray() 00068 { 00069 myIdx = myData.get().alloc(); 00070 } 00071 ~UT_StackPtrArray() 00072 { 00073 myData.get().release(myIdx); 00074 } 00075 template <typename T> 00076 UT_PtrArray<T> &get() 00077 { 00078 return *(UT_PtrArray<T> *)&myData.get().get(myIdx); 00079 } 00080 00081 private: 00082 static UT_ThreadSpecificValue< 00083 UT_StackArrayData<UT_PtrArray<void *> > > myData; 00084 int myIdx; 00085 }; 00086 00087 template <typename Array> 00088 class UT_StackArray { 00089 public: 00090 UT_StackArray() 00091 { 00092 myIdx = myData.get().alloc(); 00093 } 00094 ~UT_StackArray() 00095 { 00096 myData.get().release(myIdx); 00097 } 00098 Array &get() 00099 { 00100 return myData.get().get(myIdx); 00101 } 00102 00103 private: 00104 static UT_ThreadSpecificValue<UT_StackArrayData<Array> > myData; 00105 int myIdx; 00106 }; 00107 00108 #else 00109 00110 class UT_StackPtrArray { 00111 public: 00112 template <typename T> 00113 UT_PtrArray<T> &get() 00114 { 00115 return *(UT_PtrArray<T> *)&myData; 00116 } 00117 00118 private: 00119 UT_PtrArray<void *> myData; 00120 }; 00121 00122 template <typename Array> 00123 class UT_StackArray { 00124 public: 00125 Array &get() 00126 { 00127 return myData; 00128 } 00129 00130 private: 00131 Array myData; 00132 }; 00133 00134 #endif 00135 00136 // Macros to simplify creation of stack arrays 00137 00138 #define UT_STACK_PTRARRAY(type, name) \ 00139 UT_StackPtrArray __##name##_stack__; \ 00140 UT_PtrArray<type> &name = __##name##_stack__.get<type>() 00141 00142 #define UT_STACK_INTARRAY(name) \ 00143 UT_StackArray<UT_IntArray> __##name##_stack__; \ 00144 UT_IntArray &name = __##name##_stack__.get() 00145 00146 #define UT_STACK_FLTARRAY(name) \ 00147 UT_StackArray<UT_FloatArray> __##name##_stack__; \ 00148 UT_FloatArray &name = __##name##_stack__.get() 00149 00150 #endif
1.5.9