Triggering build... again.
1 #ifndef __SH_PAGEALLOC_H__
2 #define __SH_PAGEALLOC_H__
7 # if SH_XP == SH_XP_WINAPI
9 # elif SH_XP == SH_XP_POSIX
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 platforms 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;
52 struct AllocatedRegion
56 bool isolated; // may contain only one AU
59 bool isRE; // true: RE, otherwise: RW
61 void CheckGap(size_t gap_begin, size_t gap_end, size_t reqsize,
62 size_t &smallestgap_pos, size_t &smallestgap_size, size_t &outAlignBytes)
64 size_t gapsize = gap_end - gap_begin;
65 // How many bytes do we actually need here?
66 // = requested size + alignment bytes
67 size_t neededSize = reqsize;
68 size_t alignBytes = minAlignment - ((reinterpret_cast<intptr_t>(startPtr) + gap_begin) % minAlignment);
70 alignBytes %= minAlignment;
71 neededSize += alignBytes;
73 if (gapsize >= neededSize)
75 if (gapsize < smallestgap_size)
77 smallestgap_size = gapsize;
78 smallestgap_pos = gap_begin;
79 outAlignBytes = alignBytes;
84 bool TryAlloc(size_t reqsize, void * &outAddr)
87 if (isolated && !allocUnits.empty())
90 // Find the smallest gap where req fits
92 size_t smallestgap_pos = size + 1;
93 size_t smallestgap_size = size + 1;
94 size_t alignmentbytes = 0;
96 for (AUList::iterator iter = allocUnits.begin(); iter != allocUnits.end(); ++iter)
98 CheckGap(lastend, iter->begin_offset, reqsize, smallestgap_pos, smallestgap_size, alignmentbytes);
99 lastend = iter->begin_offset + iter->size;
102 CheckGap(lastend, size, reqsize, smallestgap_pos, smallestgap_size, alignmentbytes);
104 if (smallestgap_pos < size)
106 outAddr = reinterpret_cast<void*>(reinterpret_cast<char*>(startPtr) + smallestgap_pos + alignmentbytes);
107 allocUnits.push_sorted( AllocationUnit(smallestgap_pos, reqsize + alignmentbytes) );
116 bool TryFree(void *addr)
118 if (addr < startPtr || addr >= reinterpret_cast<void*>(reinterpret_cast<char*>(startPtr) + size))
121 intptr_t start = reinterpret_cast<intptr_t>(startPtr);
123 for (AUList::iterator iter = allocUnits.begin(); iter != allocUnits.end(); ++iter)
125 size_t AUBegin = start + iter->begin_offset;
126 void *alignedAUBegin = reinterpret_cast<void*>(
127 AUBegin + ((minAlignment - AUBegin % minAlignment) % minAlignment)
130 if (addr == alignedAUBegin)
132 DebugCleanMemory(reinterpret_cast<unsigned char*>(startPtr) + iter->begin_offset,
134 allocUnits.erase(iter);
142 void DebugCleanMemory(unsigned char* start, size_t size)
150 unsigned char* end = start + size;
151 for (unsigned char* p = start; p != end; ++p)
162 bool Contains(void *addr)
164 return addr >= startPtr && addr < reinterpret_cast<void*>(reinterpret_cast<char*>(startPtr) + size);
169 #if SH_XP == SH_XP_POSIX
170 munmap(startPtr, size);
171 #elif SH_XP == SH_XP_WINAPI
172 VirtualFree(startPtr, 0, MEM_RELEASE);
178 SetMemAccess(startPtr, size, SH_MEM_READ | SH_MEM_EXEC);
184 SetMemAccess(startPtr, size, SH_MEM_READ | SH_MEM_WRITE);
189 typedef List<AllocatedRegion> ARList;
191 size_t m_MinAlignment;
195 bool AddRegion(size_t minSize, bool isolated)
197 AllocatedRegion newRegion;
198 newRegion.startPtr = 0;
199 newRegion.isolated = isolated;
200 newRegion.minAlignment = m_MinAlignment;
202 // Compute real size -> align up to m_PageSize boundary
204 newRegion.size = minSize - (minSize % m_PageSize);
205 if (newRegion.size < minSize)
206 newRegion.size += m_PageSize;
208 #if SH_XP == SH_XP_POSIX
209 # if !defined MAP_ANONYMOUS
210 # define MAP_ANONYMOUS MAP_ANON
212 newRegion.startPtr = mmap(0, newRegion.size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
213 #elif SH_XP == SH_XP_WINAPI
214 newRegion.startPtr = VirtualAlloc(NULL, newRegion.size, MEM_COMMIT, PAGE_READWRITE);
217 if (newRegion.startPtr)
220 m_Regions.push_back(newRegion);
230 void *AllocPriv(size_t size, bool isolated)
236 for (ARList::iterator iter = m_Regions.begin(); iter != m_Regions.end(); ++iter)
238 if (iter->TryAlloc(size, addr))
243 if (!AddRegion(size, isolated))
246 bool tmp = m_Regions.back().TryAlloc(size, addr);
247 SH_ASSERT(tmp, ("TryAlloc fails after AddRegion"));
248 return tmp ? addr : NULL;
252 CPageAlloc(size_t minAlignment = 4 /* power of 2 */ ) : m_MinAlignment(minAlignment)
254 #if SH_XP == SH_XP_POSIX
255 m_PageSize = sysconf(_SC_PAGESIZE);
256 #elif SH_XP == SH_XP_WINAPI
258 GetSystemInfo(&sysInfo);
259 m_PageSize = sysInfo.dwPageSize;
266 for (ARList::iterator iter = m_Regions.begin(); iter != m_Regions.end(); ++iter)
272 void *Alloc(size_t size)
274 return AllocPriv(size, false);
277 void *AllocIsolated(size_t size)
279 return AllocPriv(size, true);
284 for (ARList::iterator iter = m_Regions.begin(); iter != m_Regions.end(); ++iter)
286 if (iter->TryFree(ptr))
288 if (iter->allocUnits.empty())
291 m_Regions.erase(iter);
298 void SetRE(void *ptr)
300 for (ARList::iterator iter = m_Regions.begin(); iter != m_Regions.end(); ++iter)
302 if (iter->Contains(ptr))
310 void SetRW(void *ptr)
312 for (ARList::iterator iter = m_Regions.begin(); iter != m_Regions.end(); ++iter)
314 if (iter->Contains(ptr))