OLD | NEW |
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 | 151 |
152 // static | 152 // static |
153 void* VirtualMemory::ReserveRegion(size_t size, size_t* size_return) { | 153 void* VirtualMemory::ReserveRegion(size_t size, size_t* size_return) { |
154 ASSERT_LT(0, size); | 154 ASSERT_LT(0, size); |
155 ASSERT_NE(NULL, size_return); | 155 ASSERT_NE(NULL, size_return); |
156 // The minimum size that can be reserved is 64KiB, see | 156 // The minimum size that can be reserved is 64KiB, see |
157 // http://msdn.microsoft.com/en-us/library/ms810627.aspx | 157 // http://msdn.microsoft.com/en-us/library/ms810627.aspx |
158 if (size < 64 * KB) { | 158 if (size < 64 * KB) { |
159 size = 64 * KB; | 159 size = 64 * KB; |
160 } | 160 } |
161 size = RoundUp(size, GetAllocationGranularity()); | 161 size = RoundUp(size, GetPageSize()); |
162 LPVOID address = NULL; | 162 LPVOID address = NULL; |
163 // Try and randomize the allocation address (up to three attempts). | 163 // Try and randomize the allocation address (up to three attempts). |
164 for (unsigned attempts = 0; address == NULL && attempts < 3; ++attempts) { | 164 for (unsigned attempts = 0; address == NULL && attempts < 3; ++attempts) { |
165 address = VirtualAlloc(GenerateRandomAddress(), | 165 address = VirtualAlloc(GenerateRandomAddress(), |
166 size, | 166 size, |
167 MEM_RESERVE, | 167 MEM_RESERVE, |
168 PAGE_NOACCESS); | 168 PAGE_NOACCESS); |
169 } | 169 } |
170 if (address == NULL) { | 170 if (address == NULL) { |
171 // After three attempts give up and let the kernel find an address. | 171 // After three attempts give up and let the kernel find an address. |
(...skipping 10 matching lines...) Expand all Loading... |
182 | 182 |
183 | 183 |
184 // static | 184 // static |
185 void* VirtualMemory::ReserveRegion(size_t size, | 185 void* VirtualMemory::ReserveRegion(size_t size, |
186 size_t* size_return, | 186 size_t* size_return, |
187 size_t alignment) { | 187 size_t alignment) { |
188 ASSERT_LT(0, size); | 188 ASSERT_LT(0, size); |
189 ASSERT_NE(NULL, size_return); | 189 ASSERT_NE(NULL, size_return); |
190 ASSERT(IsAligned(alignment, GetAllocationGranularity())); | 190 ASSERT(IsAligned(alignment, GetAllocationGranularity())); |
191 | 191 |
192 size_t reserved_size; | 192 size_t reserved_size = RoundUp(size + alignment, GetAllocationGranularity()); |
193 Address reserved_base = static_cast<Address>( | 193 Address reserved_base = static_cast<Address>( |
194 ReserveRegion(size + alignment, &reserved_size)); | 194 ReserveRegion(reserved_size, &reserved_size)); |
195 if (reserved_base == NULL) { | 195 if (reserved_base == NULL) { |
196 return NULL; | 196 return NULL; |
197 } | 197 } |
198 ASSERT_LE(size, reserved_size); | 198 ASSERT_LE(size, reserved_size); |
| 199 ASSERT_LE(size + alignment, reserved_size); |
199 ASSERT(IsAligned(reserved_size, GetPageSize())); | 200 ASSERT(IsAligned(reserved_size, GetPageSize())); |
200 | 201 |
201 // Try reducing the size by freeing and then reallocating a specific area. | 202 // Try reducing the size by freeing and then reallocating a specific area. |
202 bool result = ReleaseRegion(reserved_base, reserved_size); | 203 bool result = ReleaseRegion(reserved_base, reserved_size); |
203 USE(result); | 204 USE(result); |
204 ASSERT(result); | 205 ASSERT(result); |
205 size_t aligned_size = RoundUp(size, GetPageSize()); | 206 size_t aligned_size = RoundUp(size, GetPageSize()); |
206 Address aligned_base = static_cast<Address>( | 207 Address aligned_base = static_cast<Address>( |
207 VirtualAlloc(RoundUp(reserved_base, alignment), | 208 VirtualAlloc(RoundUp(reserved_base, alignment), |
208 aligned_size, | 209 aligned_size, |
209 MEM_RESERVE, | 210 MEM_RESERVE, |
210 PAGE_NOACCESS)); | 211 PAGE_NOACCESS)); |
211 if (aligned_base != NULL) { | 212 if (aligned_base != NULL) { |
212 ASSERT(aligned_base == RoundUp(reserved_base, alignment)); | 213 ASSERT(aligned_base == RoundUp(reserved_base, alignment)); |
213 ASSERT(IsAligned(reinterpret_cast<uintptr_t>(aligned_base), | 214 ASSERT(IsAligned(reinterpret_cast<uintptr_t>(aligned_base), |
214 GetAllocationGranularity())); | 215 GetAllocationGranularity())); |
215 ASSERT(IsAligned(aligned_size, GetPageSize())); | 216 ASSERT(IsAligned(aligned_size, GetPageSize())); |
216 *size_return = aligned_size; | 217 *size_return = aligned_size; |
217 return aligned_base; | 218 return aligned_base; |
218 } | 219 } |
219 | 220 |
220 // Resizing failed, just go with a bigger area. | 221 // Resizing failed, just go with a bigger area. |
| 222 ASSERT(IsAligned(reserved_size, GetAllocationGranularity())); |
221 return ReserveRegion(reserved_size, size_return); | 223 return ReserveRegion(reserved_size, size_return); |
222 } | 224 } |
223 | 225 |
224 | 226 |
225 // static | 227 // static |
226 bool VirtualMemory::CommitRegion(void* address, | 228 bool VirtualMemory::CommitRegion(void* address, |
227 size_t size, | 229 size_t size, |
228 Executability executability) { | 230 Executability executability) { |
229 ASSERT_NE(NULL, address); | 231 ASSERT_NE(NULL, address); |
230 ASSERT_LT(0, size); | 232 ASSERT_LT(0, size); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 | 286 |
285 // static | 287 // static |
286 size_t VirtualMemory::GetAllocationGranularity() { | 288 size_t VirtualMemory::GetAllocationGranularity() { |
287 static size_t allocation_granularity = 0; | 289 static size_t allocation_granularity = 0; |
288 if (allocation_granularity == 0) { | 290 if (allocation_granularity == 0) { |
289 SYSTEM_INFO system_info; | 291 SYSTEM_INFO system_info; |
290 GetSystemInfo(&system_info); | 292 GetSystemInfo(&system_info); |
291 allocation_granularity = system_info.dwAllocationGranularity; | 293 allocation_granularity = system_info.dwAllocationGranularity; |
292 MemoryBarrier(); | 294 MemoryBarrier(); |
293 } | 295 } |
| 296 ASSERT_GE(allocation_granularity, GetPageSize()); |
294 return allocation_granularity; | 297 return allocation_granularity; |
295 } | 298 } |
296 | 299 |
297 | 300 |
298 // static | 301 // static |
299 size_t VirtualMemory::GetLimit() { | 302 size_t VirtualMemory::GetLimit() { |
300 return 0; | 303 return 0; |
301 } | 304 } |
302 | 305 |
303 | 306 |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
501 | 504 |
502 // static | 505 // static |
503 size_t VirtualMemory::GetPageSize() { | 506 size_t VirtualMemory::GetPageSize() { |
504 static const size_t kPageSize = getpagesize(); | 507 static const size_t kPageSize = getpagesize(); |
505 return kPageSize; | 508 return kPageSize; |
506 } | 509 } |
507 | 510 |
508 #endif // V8_OS_CYGWIN || V8_OS_WIN | 511 #endif // V8_OS_CYGWIN || V8_OS_WIN |
509 | 512 |
510 } } // namespace v8::internal | 513 } } // namespace v8::internal |
OLD | NEW |