"L. Spiro Engine"
|
00001 00016 #ifndef __LSA_OSHEAP_H__ 00017 #define __LSA_OSHEAP_H__ 00018 00019 // Include the standard library. 00020 #include "LSSTDStandardLib.h" 00021 using namespace ::lsstd; 00022 00023 // Include the thread library. 00024 #include "LSHThreadLib.h" 00025 using namespace ::lsh; 00026 00027 #include "CriticalSection/LSHCriticalSection.h" 00028 00029 #include <cassert> 00030 00031 // Special definitions. 00032 // For Windows. 00033 #if defined( LSE_WINDOWS ) 00034 #include "Windows/LSSTDWindows.h" 00035 00036 #define LSA_SUPERSIZE LSUINT64 00037 #define LSA_MEMHANDLE HANDLE 00038 #endif // #if defined( LSE_WINDOWS ) 00039 00040 // For Macintosh OS X and Linux. 00041 #if defined( LSE_MAC ) || defined( LSE_LINUX ) 00042 // Define __LSA_64BIT__ if you want to support 64-bit machines. 00043 //#define __LSA_64BIT__ 00044 00045 #define LSA_SUPERSIZE LSUINT64 00046 #define LSA_MEMHANDLE LSVOID * 00047 #endif // #if defined( LSE_MAC ) 00048 00049 // For iOS. 00050 #ifdef LSE_IPHONE 00051 #include <cstdio> 00052 #endif // #ifdef LSE_IPHONE 00053 00054 00055 00056 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 00057 // TUNING 00058 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 00059 // Number of hash buckets used to quickly find addresses for deleting. 00060 // Higher number takes more RAM but runs faster. Must be a power of 2. 00061 #ifdef LSE_IPHONE 00062 #define LSA_BUCKETS 32 00063 #else 00064 #define LSA_BUCKETS 64 00065 #endif // #ifdef LSE_IPHONE 00066 00067 // The minimum alignment. 00068 #define LSA_MIN_ALIGN_BITS 4UL 00069 #define LSA_MIN_ALIGN (1UL << LSA_MIN_ALIGN_BITS) 00070 00071 // On systems that allow efficient floating-point operations we can use some 00072 // trickier math to disperse addresses through a hash table less evenly. By 00073 // default, addresses are dispersed linearly across the range of addresses 00074 // covered by a heap block. This means that all of the first allocations go 00075 // to the low hash indices and slowly build up. In most cases, however, the 00076 // indices higher into the hash table never even get used, and performance 00077 // is lost. 00078 // With floating-point math we can change the hash-key generation to 00079 // distribute addresses very widely along the low range, widely along the 00080 // middle range, and tightly at the high range, where it is less likely for 00081 // any allocations ever to be made. 00082 #define LSA_USE_FLOAT_OPTIMIZATIONS 00083 00084 // To enable debugging, uncomment the following macro. 00085 // Defaults to disabled on iPhone because the iPhone OS starts 00086 // using our override new/delete and then does not free its memory, which 00087 // fills our output screen with unreleased allocations during shutdown, 00088 // making the debugging functionality of this library mostly useless. 00089 #ifndef LSE_IPHONE 00090 #ifdef _DEBUG 00091 #define LSA_DEBUG 00092 #endif // #ifdef _DEBUG 00093 #endif // #ifndef LSE_IPHONE 00094 00095 00096 00097 #ifdef LSA_DEBUG 00098 #define LSA_DEBUGPARMSDECL , const LSCHAR * _pcFile = NULL, LSUINT32 _ui32Line = 0 00099 #define LSA_DEBUGPARMSDEF , const LSCHAR * _pcFile, LSUINT32 _ui32Line 00100 #define LSA_DEBUGPARMSPASS , _pcFile, _ui32Line 00101 #define LSA_DEBUGPARMS , __FILE__, __LINE__ 00102 #else // #ifdef LSA_DEBUG 00103 #define LSA_DEBUGPARMSDECL 00104 #define LSA_DEBUGPARMSDEF 00105 #define LSA_DEBUGPARMSPASS 00106 #define LSA_DEBUGPARMS 00107 #endif // #ifdef LSA_DEBUG 00108 00109 00110 namespace lsa { 00111 00112 typedef LSUINTPTR LSA_SIZE; 00113 00114 00121 class COsHeap { 00122 friend class CMemLib; 00123 friend class CStackAllocator; 00124 friend class CStdAllocator; 00125 00126 00127 // Protect everything to deny unauthorized access. 00128 protected : 00129 // == Functions. 00136 static LSVOID LSE_CALL Init( LSA_SIZE _sSize ); 00137 00141 static LSVOID LSE_CALL Destroy(); 00142 00149 static LSVOID * LSE_CALL Alloc( LSA_SIZE _sSize ); 00150 00157 static LSBOOL LSE_CALL Free( LSVOID * _pvAddr ); 00158 00159 00160 private : 00161 #ifdef LSE_IPHONE 00162 // == Types. 00166 typedef struct LSA_FILE_MAP { 00170 FILE * pfFile; 00171 00175 LSVOID * pvAddr; 00176 00180 LSA_SIZE sSize; 00181 00185 LSUINT32 ui32FileNumber; 00186 } * LPLSA_FILE_MAP, * const LPCLSA_FILE_MAP; 00187 #endif // #ifdef LSE_IPHONE 00188 00189 00190 // == Members. 00194 static LSA_MEMHANDLE m_mhHeapHandle; 00195 00199 static volatile LSBOOL m_bInit; 00200 00204 static CCriticalSection m_csCrit; 00205 00206 #ifdef LSE_IPHONE 00207 00210 static LSA_FILE_MAP * m_pfmFileMaps; 00211 00215 static LSUINT32 m_ui32TotalMaps; 00216 00220 static LSUINT32 m_ui32AllocMaps; 00221 00225 static LSUINT32 m_ui32FileId; 00226 00227 00228 // == Functions. 00235 static LSVOID LSE_CALL MakeMemMapPath( LSUINT32 _ui32Index, LSCHAR * _pcReturn ); 00236 #endif // #ifdef LSE_IPHONE 00237 }; 00238 00239 } // namespace lsa 00240 00241 #endif // __LSA_OSHEAP_H__