1 #ifndef __SH_PAGEALLOC_H__
2 #define __SH_PAGEALLOC_H__
4 # if SH_XP == SH_XP_WINAPI
6 # elif SH_XP == SH_XP_POSIX
10 # error Unsupported OS/Compiler
18 Class which lets us allocate memory regions in special pages only meant for on the fly code generation.
20 If we alloc with malloc and then set the page access type to read/exec only, other regions returned by
21 malloc that are in the same page would lose their write access as well and the process could crash.
23 Allocating one page per code generation session is usually a waste of memory and on some platforms also
24 a waste of virtual address space (Windows� VirtualAlloc has a granularity of 64K).
27 IMPORTANT: the memory that Alloc() returns is not a in a defined state!
28 It could be in read+exec OR read+write mode.
29 -> call SetRE() or SetRW() before using allocated memory!
38 AllocationUnit(size_t p_offs, size_t p_size) : begin_offset(p_offs), size(p_size)
42 bool operator < (const AllocationUnit &other) const
44 return begin_offset < other.begin_offset;
48 typedef List<AllocationUnit> AUList;
49 struct AllocatedRegion
53 bool isolated; // may contain only one AU
57 void CheckGap(size_t gap_begin, size_t gap_end, size_t reqsize,
58 size_t &smallestgap_pos, size_t &smallestgap_size, size_t &outAlignBytes)
60 size_t gapsize = gap_end - gap_begin;
61 // How many bytes do we actually need here?
62 // = requested size + alignment bytes
63 size_t neededSize = reqsize;
64 size_t alignBytes = minAlignment - ((reinterpret_cast<intptr_t>(startPtr) + gap_begin) % minAlignment);
66 alignBytes %= minAlignment;
67 neededSize += alignBytes;
69 if (gapsize >= neededSize)
71 if (gapsize < smallestgap_size)
73 smallestgap_size = gapsize;
74 smallestgap_pos = gap_begin;
75 outAlignBytes = alignBytes;
80 bool TryAlloc(size_t reqsize, void * &outAddr)
83 if (isolated && !allocUnits.empty())
86 // Find the smallest gap where req fits
88 size_t smallestgap_pos = size + 1;
89 size_t smallestgap_size = size + 1;
90 size_t alignmentbytes = 0;
92 for (AUList::iterator iter = allocUnits.begin(); iter != allocUnits.end(); ++iter)
94 CheckGap(lastend, iter->begin_offset, reqsize, smallestgap_pos, smallestgap_size, alignmentbytes);
95 lastend = iter->begin_offset + iter->size;
98 CheckGap(lastend, size, reqsize, smallestgap_pos, smallestgap_size, alignmentbytes);
100 if (smallestgap_pos < size)
102 outAddr = reinterpret_cast<void*>(reinterpret_cast<char*>(startPtr) + smallestgap_pos + alignmentbytes);
103 allocUnits.push_sorted( AllocationUnit(smallestgap_pos, reqsize + alignmentbytes) );
112 bool TryFree(void *addr)
114 if (addr < startPtr || addr >= reinterpret_cast<void*>(reinterpret_cast<char*>(startPtr) + size))
117 intptr_t start = reinterpret_cast<intptr_t>(startPtr);
119 for (AUList::iterator iter = allocUnits.begin(); iter != allocUnits.end(); ++iter)
121 size_t AUBegin = start + iter->begin_offset;
122 void *alignedAUBegin = reinterpret_cast<void*>(
123 AUBegin + ((minAlignment - AUBegin % minAlignment) % minAlignment)
126 if (addr == alignedAUBegin)
128 allocUnits.erase(iter);
136 bool Contains(void *addr)
138 return addr >= startPtr && addr < reinterpret_cast<void*>(reinterpret_cast<char*>(startPtr) + size);
143 #if SH_XP == SH_XP_POSIX
144 munmap(startPtr, size);
145 #elif SH_XP == SH_XP_WINAPI
146 VirtualFree(startPtr, 0, MEM_RELEASE);
151 typedef List<AllocatedRegion> ARList;
153 size_t m_MinAlignment;
157 bool AddRegion(size_t minSize, bool isolated)
159 AllocatedRegion newRegion;
160 newRegion.startPtr = 0;
161 newRegion.isolated = isolated;
162 newRegion.minAlignment = m_MinAlignment;
164 // Compute real size -> align up to m_PageSize boundary
166 newRegion.size = minSize - (minSize % m_PageSize);
167 if (newRegion.size < minSize)
168 newRegion.size += m_PageSize;
170 #if SH_XP == SH_XP_POSIX
171 # if !defined MAP_ANONYMOUS
172 # define MAP_ANONYMOUS MAP_ANON
174 newRegion.startPtr = mmap(0, newRegion.size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
175 #elif SH_XP == SH_XP_WINAPI
176 newRegion.startPtr = VirtualAlloc(NULL, newRegion.size, MEM_COMMIT, PAGE_READWRITE);
179 if (newRegion.startPtr)
181 m_Regions.push_back(newRegion);
191 void *AllocPriv(size_t size, bool isolated)
197 for (ARList::iterator iter = m_Regions.begin(); iter != m_Regions.end(); ++iter)
199 if (iter->TryAlloc(size, addr))
204 if (!AddRegion(size, isolated))
207 bool tmp = m_Regions.back().TryAlloc(size, addr);
208 SH_ASSERT(tmp, ("TryAlloc fails after AddRegion"));
209 return tmp ? addr : NULL;
213 CPageAlloc(size_t minAlignment = 1 /* power of 2 */ ) : m_MinAlignment(minAlignment)
215 #if SH_XP == SH_XP_POSIX
216 m_PageSize = sysconf(_SC_PAGESIZE);
217 #elif SH_XP == SH_XP_WINAPI
219 GetSystemInfo(&sysInfo);
220 m_PageSize = sysInfo.dwPageSize;
227 for (ARList::iterator iter = m_Regions.begin(); iter != m_Regions.end(); ++iter)
233 void *Alloc(size_t size)
235 return AllocPriv(size, false);
238 void *AllocIsolated(size_t size)
240 return AllocPriv(size, true);
245 for (ARList::iterator iter = m_Regions.begin(); iter != m_Regions.end(); ++iter)
247 if (iter->TryFree(ptr))
249 if (iter->allocUnits.empty())
252 m_Regions.erase(iter);
259 void SetRE(void *ptr)
261 for (ARList::iterator iter = m_Regions.begin(); iter != m_Regions.end(); ++iter)
263 if (iter->Contains(ptr))
265 SetMemAccess(iter->startPtr, iter->size, SH_MEM_READ | SH_MEM_EXEC);
271 void SetRW(void *ptr)
273 for (ARList::iterator iter = m_Regions.begin(); iter != m_Regions.end(); ++iter)
275 if (iter->Contains(ptr))
277 SetMemAccess(iter->startPtr, iter->size, SH_MEM_READ | SH_MEM_WRITE);