More re-arranging. Not what we discussed in IRC but I think it works out better, Make can't deal with upward dependencies nicely.
1 #ifndef __SH_PAGEALLOC_H__
2 #define __SH_PAGEALLOC_H__
4 # if /********/ defined _WIN32
6 # elif /******/ defined __linux__
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);
144 munmap(startPtr, size);
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;
171 newRegion.startPtr = mmap(0, newRegion.size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
173 newRegion.startPtr = VirtualAlloc(NULL, newRegion.size, MEM_COMMIT, PAGE_READWRITE);
176 if (newRegion.startPtr)
178 m_Regions.push_back(newRegion);
188 void *AllocPriv(size_t size, bool isolated)
194 for (ARList::iterator iter = m_Regions.begin(); iter != m_Regions.end(); ++iter)
196 if (iter->TryAlloc(size, addr))
201 if (!AddRegion(size, isolated))
204 bool tmp = m_Regions.back().TryAlloc(size, addr);
205 SH_ASSERT(tmp, ("TryAlloc fails after AddRegion"));
206 return tmp ? addr : NULL;
210 CPageAlloc(size_t minAlignment = 1 /* power of 2 */ ) : m_MinAlignment(minAlignment)
213 m_PageSize = sysconf(_SC_PAGESIZE);
216 GetSystemInfo(&sysInfo);
217 m_PageSize = sysInfo.dwPageSize;
224 for (ARList::iterator iter = m_Regions.begin(); iter != m_Regions.end(); ++iter)
230 void *Alloc(size_t size)
232 return AllocPriv(size, false);
235 void *AllocIsolated(size_t size)
237 return AllocPriv(size, true);
242 for (ARList::iterator iter = m_Regions.begin(); iter != m_Regions.end(); ++iter)
244 if (iter->TryFree(ptr))
246 if (iter->allocUnits.empty())
249 m_Regions.erase(iter);
256 void SetRE(void *ptr)
258 for (ARList::iterator iter = m_Regions.begin(); iter != m_Regions.end(); ++iter)
260 if (iter->Contains(ptr))
262 SetMemAccess(iter->startPtr, iter->size, SH_MEM_READ | SH_MEM_EXEC);
268 void SetRW(void *ptr)
270 for (ARList::iterator iter = m_Regions.begin(); iter != m_Regions.end(); ++iter)
272 if (iter->Contains(ptr))
274 SetMemAccess(iter->startPtr, iter->size, SH_MEM_READ | SH_MEM_WRITE);