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;
50 struct AllocatedRegion
54 bool isolated; // may contain only one AU
57 bool TryAlloc(size_t reqsize, void * &outAddr)
60 if (isolated && !allocUnits.empty())
63 // Find the smallest gap where req fits
65 size_t smallestgap_pos = size + 1;
66 size_t smallestgap_size = size + 1;
68 for (AUList::iterator iter = allocUnits.begin(); iter != allocUnits.end(); ++iter)
70 if (iter->begin_offset - lastend >= reqsize)
72 if (iter->begin_offset - lastend < smallestgap_size)
74 smallestgap_size = iter->begin_offset - lastend;
75 smallestgap_pos = lastend;
78 lastend = iter->begin_offset + iter->size;
81 if (size - lastend >= reqsize)
83 if (size - lastend < smallestgap_size)
85 smallestgap_size = size - lastend;
86 smallestgap_pos = lastend;
90 if (smallestgap_pos < size)
92 outAddr = reinterpret_cast<void*>(reinterpret_cast<char*>(startPtr) + smallestgap_pos);
93 allocUnits.push_sorted( AllocationUnit(smallestgap_pos, reqsize) );
102 bool TryFree(void *addr)
104 if (addr < startPtr || addr >= reinterpret_cast<void*>(reinterpret_cast<char*>(startPtr) + size))
107 size_t offs = reinterpret_cast<char*>(addr) - reinterpret_cast<char*>(startPtr);
109 for (AUList::iterator iter = allocUnits.begin(); iter != allocUnits.end(); ++iter)
111 if (iter->begin_offset == offs)
113 allocUnits.erase(iter);
121 bool Contains(void *addr)
123 return addr >= startPtr && addr < reinterpret_cast<void*>(reinterpret_cast<char*>(startPtr) + size);
129 munmap(startPtr, size);
131 VirtualFree(startPtr, 0, MEM_RELEASE);
136 typedef List<AllocatedRegion> ARList;
141 bool AddRegion(size_t minSize, bool isolated)
143 AllocatedRegion newRegion;
144 newRegion.startPtr = 0;
145 newRegion.isolated = isolated;
147 // Compute real size -> align up to m_PageSize boundary
149 newRegion.size = minSize - (minSize % m_PageSize);
150 if (newRegion.size < minSize)
151 newRegion.size += m_PageSize;
154 newRegion.startPtr = mmap(0, newRegion.size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
156 newRegion.startPtr = VirtualAlloc(NULL, newRegion.size, MEM_COMMIT, PAGE_READWRITE);
159 if (newRegion.startPtr)
161 m_Regions.push_back(newRegion);
171 void *AllocPriv(size_t size, bool isolated)
177 for (ARList::iterator iter = m_Regions.begin(); iter != m_Regions.end(); ++iter)
179 if (iter->TryAlloc(size, addr))
184 if (!AddRegion(size, isolated))
187 bool tmp = m_Regions.back().TryAlloc(size, addr);
188 SH_ASSERT(tmp, ("TryAlloc fails after AddRegion"));
196 m_PageSize = sysconf(_SC_PAGESIZE);
199 GetSystemInfo(&sysInfo);
200 m_PageSize = sysInfo.dwPageSize;
207 for (ARList::iterator iter = m_Regions.begin(); iter != m_Regions.end(); ++iter)
213 void *Alloc(size_t size)
215 return AllocPriv(size, false);
218 void *AllocIsolated(size_t size)
220 return AllocPriv(size, true);
225 for (ARList::iterator iter = m_Regions.begin(); iter != m_Regions.end(); ++iter)
227 if (iter->TryFree(ptr))
229 if (iter->allocUnits.empty())
232 m_Regions.erase(iter);
239 void SetRE(void *ptr)
241 for (ARList::iterator iter = m_Regions.begin(); iter != m_Regions.end(); ++iter)
243 if (iter->Contains(ptr))
245 SetMemAccess(iter->startPtr, iter->size, SH_MEM_READ | SH_MEM_EXEC);
251 void SetRW(void *ptr)
253 for (ARList::iterator iter = m_Regions.begin(); iter != m_Regions.end(); ++iter)
255 if (iter->Contains(ptr))
257 SetMemAccess(iter->startPtr, iter->size, SH_MEM_READ | SH_MEM_WRITE);