CPageAlloc now executes before TestHookmanGen, newline at the end of sh_pagealloc.h
1 #ifndef __SH_PAGEALLOC_H__
2 #define __SH_PAGEALLOC_H__
4 # if /********/ defined _WIN32
7 # define SH_MEM_WRITE 2
9 # elif /******/ defined __linux__
10 # include <sys/mman.h>
13 # error Unsupported OS/Compiler
21 Class which lets us allocate memory regions in special pages only meant for on the fly code generation.
23 If we alloc with malloc and then set the page access type to read/exec only, other regions returned by
24 malloc that are in the same page would lose their write access as well and the process could crash.
26 Allocating one page per code generation session is usually a waste of memory and on some plattforms also
27 a waste of virtual address space (Windows� VirtualAlloc has a granularity of 64K).
30 IMPORTANT: the memory that Alloc() returns is not a in a defined state!
31 It could be in read+exec OR read+write mode.
32 -> call SetRE() or SetRW() before using allocated memory!
41 AllocationUnit(size_t p_offs, size_t p_size) : begin_offset(p_offs), size(p_size)
45 bool operator < (const AllocationUnit &other) const
47 return begin_offset < other.begin_offset;
51 typedef List<AllocationUnit> AUList;
53 struct AllocatedRegion
57 bool isolated; // may contain only one AU
60 bool TryAlloc(size_t reqsize, void * &outAddr)
63 if (isolated && !allocUnits.empty())
66 // Find the smallest gap where req fits
68 size_t smallestgap_pos = size + 1;
69 size_t smallestgap_size = size + 1;
71 for (AUList::iterator iter = allocUnits.begin(); iter != allocUnits.end(); ++iter)
73 if (iter->begin_offset - lastend >= reqsize)
75 if (iter->begin_offset - lastend < smallestgap_size)
77 smallestgap_size = iter->begin_offset - lastend;
78 smallestgap_pos = lastend;
81 lastend = iter->begin_offset + iter->size;
84 if (size - lastend >= reqsize)
86 if (size - lastend < smallestgap_size)
88 smallestgap_size = size - lastend;
89 smallestgap_pos = lastend;
93 if (smallestgap_pos < size)
95 outAddr = reinterpret_cast<void*>(reinterpret_cast<char*>(startPtr) + smallestgap_pos);
96 allocUnits.push_sorted( AllocationUnit(smallestgap_pos, reqsize) );
105 bool TryFree(void *addr)
107 if (addr < startPtr || addr >= reinterpret_cast<void*>(reinterpret_cast<char*>(startPtr) + size))
110 size_t offs = reinterpret_cast<char*>(addr) - reinterpret_cast<char*>(startPtr);
112 for (AUList::iterator iter = allocUnits.begin(); iter != allocUnits.end(); ++iter)
114 if (iter->begin_offset == offs)
116 allocUnits.erase(iter);
124 bool Contains(void *addr)
126 return addr >= startPtr && addr < reinterpret_cast<void*>(reinterpret_cast<char*>(startPtr) + size);
132 munmap(startPtr, size);
134 VirtualFree(startPtr, 0, MEM_RELEASE);
139 typedef List<AllocatedRegion> ARList;
144 bool AddRegion(size_t minSize, bool isolated)
146 AllocatedRegion newRegion;
147 newRegion.startPtr = 0;
148 newRegion.isolated = isolated;
150 // Compute real size -> align up to m_PageSize boundary
152 newRegion.size = minSize - (minSize % m_PageSize);
153 if (newRegion.size < minSize)
154 newRegion.size += m_PageSize;
157 newRegion.startPtr = mmap(0, newRegion.size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
159 newRegion.startPtr = VirtualAlloc(NULL, newRegion.size, MEM_COMMIT, PAGE_READWRITE);
162 if (newRegion.startPtr)
164 m_Regions.push_back(newRegion);
174 void *AllocPriv(size_t size, bool isolated)
180 for (ARList::iterator iter = m_Regions.begin(); iter != m_Regions.end(); ++iter)
182 if (iter->TryAlloc(size, addr))
187 if (!AddRegion(size, isolated))
190 bool tmp = m_Regions.back().TryAlloc(size, addr);
191 SH_ASSERT(tmp, ("TryAlloc fails after AddRegion"));
199 m_PageSize = sysconf(_SC_PAGESIZE);
202 GetSystemInfo(&sysInfo);
203 m_PageSize = sysInfo.dwPageSize;
210 for (ARList::iterator iter = m_Regions.begin(); iter != m_Regions.end(); ++iter)
216 void *Alloc(size_t size)
218 return AllocPriv(size, false);
221 void *AllocIsolated(size_t size)
223 return AllocPriv(size, true);
228 for (ARList::iterator iter = m_Regions.begin(); iter != m_Regions.end(); ++iter)
230 if (iter->TryFree(ptr))
232 if (iter->allocUnits.empty())
235 m_Regions.erase(iter);
242 void SetRE(void *ptr)
244 for (ARList::iterator iter = m_Regions.begin(); iter != m_Regions.end(); ++iter)
246 if (iter->Contains(ptr))
248 SetMemAccess(iter->startPtr, iter->size, SH_MEM_READ | SH_MEM_EXEC);
254 void SetRW(void *ptr)
256 for (ARList::iterator iter = m_Regions.begin(); iter != m_Regions.end(); ++iter)
258 if (iter->Contains(ptr))
260 SetMemAccess(iter->startPtr, iter->size, SH_MEM_READ | SH_MEM_WRITE);