@@ -114,7 +114,21 @@ DECLARE_PERF_TIMER(MemoryPoolInitFilling)
114114 #define MEMORYPOOL_SINGLEBLOCK_GETS_STACKTRACE
115115 #endif
116116
117+ #define USE_FILLER_VALUE
118+
117119 const Int MAX_INIT_FILLER_COUNT = 8 ;
120+ #ifdef USE_FILLER_VALUE
121+ static UnsignedInt s_initFillerValue = 0xf00dcafe ; // will be replaced, should never be this value at runtime
122+ static void calcFillerValue (Int index)
123+ {
124+ s_initFillerValue = (index & 3 ) << 1 ;
125+ s_initFillerValue |= 0x01 ;
126+ s_initFillerValue |= (~(s_initFillerValue << 4 )) & 0xf0 ;
127+ s_initFillerValue |= (s_initFillerValue << 8 );
128+ s_initFillerValue |= (s_initFillerValue << 16 );
129+ // DEBUG_LOG(("Setting MemoryPool initFillerValue to %08x (index %d)",s_initFillerValue,index));
130+ }
131+ #endif
118132
119133#endif
120134
@@ -155,6 +169,8 @@ DECLARE_PERF_TIMER(MemoryPoolInitFilling)
155169
156170#ifdef MEMORYPOOL_DEBUG
157171
172+ static Int theTotalSystemAllocationInBytes = 0 ;
173+ static Int thePeakSystemAllocationInBytes = 0 ;
158174 static Int theTotalLargeBlocks = 0 ;
159175 static Int thePeakLargeBlocks = 0 ;
160176 Int theTotalDMA = 0 ;
@@ -187,7 +203,9 @@ static Bool theMainInitFlag = false;
187203#define MEM_BOUND_ALIGNMENT 4
188204
189205static Int roundUpMemBound (Int i);
190-
206+ static void *sysAllocateDoNotZero (Int numBytes);
207+ static void sysFree (void * p);
208+ static void memset32 (void * ptr, Int value, Int bytesToFill);
191209#ifdef MEMORYPOOL_STACKTRACE
192210static void doStackDumpOutput (const char * m);
193211static void doStackDump (void **stacktrace, int size);
@@ -205,6 +223,73 @@ static Int roundUpMemBound(Int i)
205223 return (i + (MEM_BOUND_ALIGNMENT-1 )) & ~(MEM_BOUND_ALIGNMENT-1 );
206224}
207225
226+ // -----------------------------------------------------------------------------
227+ /* *
228+ this is the low-level allocator that we use to request memory from the OS.
229+ all (repeat, all) memory allocations in this module should ultimately
230+ go thru this routine (or sysAllocate).
231+
232+ note: throws ERROR_OUT_OF_MEMORY on failure; never returns null
233+ */
234+ static void * sysAllocateDoNotZero (Int numBytes)
235+ {
236+ void * p = ::GlobalAlloc (GMEM_FIXED, numBytes);
237+ if (!p)
238+ throw ERROR_OUT_OF_MEMORY;
239+ #ifdef MEMORYPOOL_DEBUG
240+ {
241+ USE_PERF_TIMER (MemoryPoolDebugging)
242+ #ifdef USE_FILLER_VALUE
243+ {
244+ USE_PERF_TIMER (MemoryPoolInitFilling)
245+ ::memset32 (p, s_initFillerValue, ::GlobalSize(p));
246+ }
247+ #endif
248+ theTotalSystemAllocationInBytes += ::GlobalSize (p);
249+ if (thePeakSystemAllocationInBytes < theTotalSystemAllocationInBytes)
250+ thePeakSystemAllocationInBytes = theTotalSystemAllocationInBytes;
251+ }
252+ #endif
253+ return p;
254+ }
255+
256+ // -----------------------------------------------------------------------------
257+ /* *
258+ the counterpart to sysAllocate / sysAllocateDoNotZero; used to free blocks
259+ allocated by them. it is OK to pass null here (it will just be ignored).
260+ */
261+ static void sysFree (void * p)
262+ {
263+ if (p)
264+ {
265+ #ifdef MEMORYPOOL_DEBUG
266+ {
267+ USE_PERF_TIMER (MemoryPoolDebugging)
268+ ::memset32 (p, GARBAGE_FILL_VALUE, ::GlobalSize(p));
269+ theTotalSystemAllocationInBytes -= ::GlobalSize (p);
270+ }
271+ #endif
272+ ::GlobalFree (p);
273+ }
274+ }
275+
276+ // ----------------------------------------------------------------------------
277+ /* *
278+ fills memory with a 32-bit value (note: assumes the ptr is 4-byte-aligned)
279+ */
280+ static void memset32 (void * ptr, Int value, Int bytesToFill)
281+ {
282+ Int wordsToFill = bytesToFill>>2 ;
283+ bytesToFill -= (wordsToFill<<2 );
284+
285+ Int *p = (Int*)ptr;
286+ for (++wordsToFill; --wordsToFill; )
287+ *p++ = value;
288+
289+ Byte *b = (Byte *)p;
290+ for (++bytesToFill; --bytesToFill; )
291+ *b++ = (Byte)value;
292+ }
208293
209294#ifdef MEMORYPOOL_STACKTRACE
210295// ----------------------------------------------------------------------------
@@ -1548,20 +1633,20 @@ void* MemoryPool::allocateBlockDoNotZeroImplementation(DECLARE_LITERALSTRING_ARG
15481633 if (m_firstBlobWithFreeBlocks != NULL && !m_firstBlobWithFreeBlocks->hasAnyFreeBlocks ())
15491634 {
15501635 // hmm... the current 'free' blob has nothing available. look and see if there
1551- // are any other existing blobs with free space .
1636+ // are any other existing blobs with freespace .
15521637 MemoryPoolBlob *blob = m_firstBlob;
15531638 for (; blob != NULL ; blob = blob->getNextInList ())
15541639 {
15551640 if (blob->hasAnyFreeBlocks ())
15561641 break ;
15571642 }
15581643
1559- // note that if we walk through the list without finding anything, this will
1560- // reset m_firstBlobWithFreeBlocks to null and fall through .
1644+ // note that if we walk thru the list without finding anything, this will
1645+ // reset m_firstBlobWithFreeBlocks to null and fall thru .
15611646 m_firstBlobWithFreeBlocks = blob;
15621647 }
15631648
1564- // OK, if we are here then we have no blobs with free space ... darn.
1649+ // OK, if we are here then we have no blobs with freespace ... darn.
15651650 // allocate an overflow block.
15661651 if (m_firstBlobWithFreeBlocks == NULL )
15671652 {
0 commit comments