core/sourcehook/sh_pagealloc.h
changeset 739 968e67c7780c
parent 733 2365240861a2
     1.1 --- a/core/sourcehook/sh_pagealloc.h	Fri Dec 17 22:20:10 2010 -0500
     1.2 +++ b/core/sourcehook/sh_pagealloc.h	Fri Dec 17 22:22:44 2010 -0500
     1.3 @@ -1,6 +1,9 @@
     1.4  #ifndef __SH_PAGEALLOC_H__
     1.5  #define __SH_PAGEALLOC_H__
     1.6  
     1.7 +#include "sh_list.h"
     1.8 +#include "sh_memory.h"
     1.9 +
    1.10  # if SH_XP == SH_XP_WINAPI
    1.11  #		include <windows.h>
    1.12  # elif SH_XP == SH_XP_POSIX
    1.13 @@ -53,6 +56,7 @@
    1.14  			bool isolated;					// may contain only one AU
    1.15  			size_t minAlignment;
    1.16  			AUList allocUnits;
    1.17 +			bool isRE;						// true: RE, otherwise: RW
    1.18  
    1.19  			void CheckGap(size_t gap_begin, size_t gap_end, size_t reqsize,
    1.20  				size_t &smallestgap_pos, size_t &smallestgap_size, size_t &outAlignBytes)
    1.21 @@ -122,9 +126,11 @@
    1.22  					void *alignedAUBegin = reinterpret_cast<void*>(
    1.23  						AUBegin + ((minAlignment - AUBegin % minAlignment) % minAlignment)
    1.24  						);
    1.25 -					
    1.26 +
    1.27  					if (addr == alignedAUBegin)
    1.28  					{
    1.29 +						DebugCleanMemory(reinterpret_cast<unsigned char*>(startPtr) + iter->begin_offset,
    1.30 +							iter->size);
    1.31  						allocUnits.erase(iter);
    1.32  						return true;
    1.33  					}
    1.34 @@ -133,6 +139,26 @@
    1.35  				return false;
    1.36  			}
    1.37  
    1.38 +			void DebugCleanMemory(unsigned char* start, size_t size)
    1.39 +			{
    1.40 +				bool wasRE = isRE;
    1.41 +				if (isRE)
    1.42 +				{
    1.43 +					SetRW();
    1.44 +				}
    1.45 +
    1.46 +				unsigned char* end = start + size;
    1.47 +				for (unsigned char* p = start; p != end; ++p)
    1.48 +				{
    1.49 +					*p = 0xCC;
    1.50 +				}
    1.51 +
    1.52 +				if (wasRE)
    1.53 +				{
    1.54 +					SetRE();
    1.55 +				}
    1.56 +			}
    1.57 +
    1.58  			bool Contains(void *addr)
    1.59  			{
    1.60  				return addr >= startPtr && addr < reinterpret_cast<void*>(reinterpret_cast<char*>(startPtr) + size);
    1.61 @@ -146,6 +172,18 @@
    1.62  				VirtualFree(startPtr, 0, MEM_RELEASE);
    1.63  #endif
    1.64  			}
    1.65 +
    1.66 +			void SetRE()
    1.67 +			{
    1.68 +				SetMemAccess(startPtr, size, SH_MEM_READ | SH_MEM_EXEC);
    1.69 +				isRE = true;
    1.70 +			}
    1.71 +
    1.72 +			void SetRW()
    1.73 +			{
    1.74 +				SetMemAccess(startPtr, size, SH_MEM_READ | SH_MEM_WRITE);
    1.75 +				isRE = false;
    1.76 +			}
    1.77  		};
    1.78  
    1.79  		typedef List<AllocatedRegion> ARList;
    1.80 @@ -178,6 +216,7 @@
    1.81  
    1.82  			if (newRegion.startPtr)
    1.83  			{
    1.84 +				newRegion.SetRW();
    1.85  				m_Regions.push_back(newRegion);
    1.86  				return true;
    1.87  			}
    1.88 @@ -210,7 +249,7 @@
    1.89  		}
    1.90  
    1.91  	public:
    1.92 -		CPageAlloc(size_t minAlignment = 1 /* power of 2 */ ) : m_MinAlignment(minAlignment)
    1.93 +		CPageAlloc(size_t minAlignment = 4 /* power of 2 */ ) : m_MinAlignment(minAlignment)
    1.94  		{
    1.95  #if SH_XP == SH_XP_POSIX
    1.96  			m_PageSize = sysconf(_SC_PAGESIZE);
    1.97 @@ -262,7 +301,7 @@
    1.98  			{
    1.99  				if (iter->Contains(ptr))
   1.100  				{
   1.101 -					SetMemAccess(iter->startPtr, iter->size, SH_MEM_READ | SH_MEM_EXEC);
   1.102 +					iter->SetRE();
   1.103  					break;
   1.104  				}
   1.105  			}
   1.106 @@ -274,7 +313,7 @@
   1.107  			{
   1.108  				if (iter->Contains(ptr))
   1.109  				{
   1.110 -					SetMemAccess(iter->startPtr, iter->size, SH_MEM_READ | SH_MEM_WRITE);
   1.111 +					iter->SetRW();
   1.112  					break;
   1.113  				}
   1.114  			}