1.1 --- a/sourcehook/sh_pagealloc.h Sun Nov 25 00:12:56 2007 +0000
1.2 +++ b/sourcehook/sh_pagealloc.h Sun Nov 25 01:20:00 2007 +0000
1.3 @@ -46,14 +46,37 @@
1.4 };
1.5
1.6 typedef List<AllocationUnit> AUList;
1.7 -
1.8 struct AllocatedRegion
1.9 {
1.10 void *startPtr;
1.11 size_t size;
1.12 bool isolated; // may contain only one AU
1.13 + size_t minAlignment;
1.14 AUList allocUnits;
1.15
1.16 + void CheckGap(size_t gap_begin, size_t gap_end, size_t reqsize,
1.17 + size_t &smallestgap_pos, size_t &smallestgap_size, size_t &outAlignBytes)
1.18 + {
1.19 + size_t gapsize = gap_end - gap_begin;
1.20 + // How many bytes do we actually need here?
1.21 + // = requested size + alignment bytes
1.22 + size_t neededSize = reqsize;
1.23 + size_t alignBytes = minAlignment - ((reinterpret_cast<intptr_t>(startPtr) + gap_begin) % minAlignment);
1.24 +
1.25 + alignBytes %= minAlignment;
1.26 + neededSize += alignBytes;
1.27 +
1.28 + if (gap_end - gap_begin >= neededSize)
1.29 + {
1.30 + if (gap_end - gap_begin < smallestgap_size)
1.31 + {
1.32 + smallestgap_size = gap_end - gap_begin;
1.33 + smallestgap_pos = gap_begin;
1.34 + outAlignBytes = alignBytes;
1.35 + }
1.36 + }
1.37 + }
1.38 +
1.39 bool TryAlloc(size_t reqsize, void * &outAddr)
1.40 {
1.41 // Check for isolated
1.42 @@ -64,33 +87,20 @@
1.43 size_t lastend = 0;
1.44 size_t smallestgap_pos = size + 1;
1.45 size_t smallestgap_size = size + 1;
1.46 + size_t alignmentbytes = 0;
1.47
1.48 for (AUList::iterator iter = allocUnits.begin(); iter != allocUnits.end(); ++iter)
1.49 {
1.50 - if (iter->begin_offset - lastend >= reqsize)
1.51 - {
1.52 - if (iter->begin_offset - lastend < smallestgap_size)
1.53 - {
1.54 - smallestgap_size = iter->begin_offset - lastend;
1.55 - smallestgap_pos = lastend;
1.56 - }
1.57 - }
1.58 + CheckGap(lastend, iter->begin_offset, reqsize, smallestgap_pos, smallestgap_size, alignmentbytes);
1.59 lastend = iter->begin_offset + iter->size;
1.60 }
1.61
1.62 - if (size - lastend >= reqsize)
1.63 - {
1.64 - if (size - lastend < smallestgap_size)
1.65 - {
1.66 - smallestgap_size = size - lastend;
1.67 - smallestgap_pos = lastend;
1.68 - }
1.69 - }
1.70 + CheckGap(lastend, size, reqsize, smallestgap_pos, smallestgap_size, alignmentbytes);
1.71
1.72 if (smallestgap_pos < size)
1.73 {
1.74 - outAddr = reinterpret_cast<void*>(reinterpret_cast<char*>(startPtr) + smallestgap_pos);
1.75 - allocUnits.push_sorted( AllocationUnit(smallestgap_pos, reqsize) );
1.76 + outAddr = reinterpret_cast<void*>(reinterpret_cast<char*>(startPtr) + smallestgap_pos + alignmentbytes);
1.77 + allocUnits.push_sorted( AllocationUnit(smallestgap_pos, reqsize + alignmentbytes) );
1.78 return true;
1.79 }
1.80 else
1.81 @@ -104,11 +114,16 @@
1.82 if (addr < startPtr || addr >= reinterpret_cast<void*>(reinterpret_cast<char*>(startPtr) + size))
1.83 return false;
1.84
1.85 - size_t offs = reinterpret_cast<char*>(addr) - reinterpret_cast<char*>(startPtr);
1.86 + intptr_t start = reinterpret_cast<intptr_t>(startPtr);
1.87
1.88 for (AUList::iterator iter = allocUnits.begin(); iter != allocUnits.end(); ++iter)
1.89 {
1.90 - if (iter->begin_offset == offs)
1.91 + size_t AUBegin = start + iter->begin_offset;
1.92 + void *alignedAUBegin = reinterpret_cast<void*>(
1.93 + AUBegin + ((minAlignment - AUBegin % minAlignment) % minAlignment)
1.94 + );
1.95 +
1.96 + if (addr == alignedAUBegin)
1.97 {
1.98 allocUnits.erase(iter);
1.99 return true;
1.100 @@ -135,6 +150,7 @@
1.101
1.102 typedef List<AllocatedRegion> ARList;
1.103
1.104 + size_t m_MinAlignment;
1.105 size_t m_PageSize;
1.106 ARList m_Regions;
1.107
1.108 @@ -143,6 +159,7 @@
1.109 AllocatedRegion newRegion;
1.110 newRegion.startPtr = 0;
1.111 newRegion.isolated = isolated;
1.112 + newRegion.minAlignment = m_MinAlignment;
1.113
1.114 // Compute real size -> align up to m_PageSize boundary
1.115
1.116 @@ -190,7 +207,7 @@
1.117 }
1.118
1.119 public:
1.120 - CPageAlloc()
1.121 + CPageAlloc(size_t minAlignment = 1 /* power of 2 */ ) : m_MinAlignment(minAlignment)
1.122 {
1.123 #ifdef __linux__
1.124 m_PageSize = sysconf(_SC_PAGESIZE);