| 150 | } |
| 151 | |
| 152 | bool Free(void* ptr, bool leaveAllocated = false) |
| 153 | { |
| 154 | uint8* memPtr = (uint8*)ptr; |
| 155 | if ((memPtr < mMemory) || (memPtr >= mMemory + PAGE_SIZE * NUM_PAGES)) |
| 156 | return false; |
| 157 | |
| 158 | int pageStart = (int)(memPtr - mMemory) / PAGE_SIZE; |
| 159 | int pageOfs = (int)(memPtr - mMemory) % PAGE_SIZE; |
| 160 | if (pageOfs < sizeof(SA_AllocHeader)) |
| 161 | pageStart--; // We actually allocated on the previous page |
| 162 | |
| 163 | SA_AllocHeader* allocHeader = (SA_AllocHeader*)(mMemory + pageStart*PAGE_SIZE); |
| 164 | assert(allocHeader->mMagic == STOMP_MAGIC); |
| 165 | if (leaveAllocated) |
| 166 | { |
| 167 | DWORD oldProtect; |
| 168 | ::VirtualProtect(allocHeader, allocHeader->mNumPages * PAGE_SIZE, /*PAGE_NOACCESS*/PAGE_READONLY, &oldProtect); |
| 169 | } |
| 170 | else |
| 171 | { |
| 172 | for (int pageIdx = pageStart; pageIdx < pageStart + allocHeader->mNumPages; pageIdx++) |
| 173 | { |
| 174 | assert(mUsedBits.IsSet(pageIdx)); |
| 175 | mUsedBits.Clear(pageIdx); |
| 176 | } |
| 177 | ::VirtualFree(allocHeader, allocHeader->mNumPages * PAGE_SIZE, MEM_DECOMMIT); |
| 178 | } |
| 179 | return true; |
| 180 | } |
| 181 | }; |
| 182 | |
| 183 | static std::list<SA_AllocRange>* gAllocRanges = NULL; |