diff options
Diffstat (limited to 'src/3rdparty/webkit/JavaScriptCore/wtf/TCSystemAlloc.cpp')
-rw-r--r-- | src/3rdparty/webkit/JavaScriptCore/wtf/TCSystemAlloc.cpp | 64 |
1 files changed, 58 insertions, 6 deletions
diff --git a/src/3rdparty/webkit/JavaScriptCore/wtf/TCSystemAlloc.cpp b/src/3rdparty/webkit/JavaScriptCore/wtf/TCSystemAlloc.cpp index 478ce63..659bb0e 100644 --- a/src/3rdparty/webkit/JavaScriptCore/wtf/TCSystemAlloc.cpp +++ b/src/3rdparty/webkit/JavaScriptCore/wtf/TCSystemAlloc.cpp @@ -31,6 +31,14 @@ // Author: Sanjay Ghemawat #include "config.h" +#include "TCSystemAlloc.h" + +#include <algorithm> +#include <fcntl.h> +#include "Assertions.h" +#include "TCSpinLock.h" +#include "UnusedParam.h" + #if HAVE(STDINT_H) #include <stdint.h> #elif HAVE(INTTYPES_H) @@ -38,6 +46,7 @@ #else #include <sys/types.h> #endif + #if PLATFORM(WIN_OS) #include "windows.h" #else @@ -45,16 +54,13 @@ #include <unistd.h> #include <sys/mman.h> #endif -#include <fcntl.h> -#include "Assertions.h" -#include "TCSystemAlloc.h" -#include "TCSpinLock.h" -#include "UnusedParam.h" #ifndef MAP_ANONYMOUS #define MAP_ANONYMOUS MAP_ANON #endif +using namespace std; + // Structure for discovering alignment union MemoryAligner { void* p; @@ -441,6 +447,32 @@ void TCMalloc_SystemRelease(void* start, size_t length) ASSERT_UNUSED(newAddress, newAddress == start || newAddress == reinterpret_cast<void*>(MAP_FAILED)); } +#elif HAVE(VIRTUALALLOC) + +void TCMalloc_SystemRelease(void* start, size_t length) +{ + if (VirtualFree(start, length, MEM_DECOMMIT)) + return; + + // The decommit may fail if the memory region consists of allocations + // from more than one call to VirtualAlloc. In this case, fall back to + // using VirtualQuery to retrieve the allocation boundaries and decommit + // them each individually. + + char* ptr = static_cast<char*>(start); + char* end = ptr + length; + MEMORY_BASIC_INFORMATION info; + while (ptr < end) { + size_t resultSize = VirtualQuery(ptr, &info, sizeof(info)); + ASSERT_UNUSED(resultSize, resultSize == sizeof(info)); + + size_t decommitSize = min<size_t>(info.RegionSize, end - ptr); + BOOL success = VirtualFree(ptr, decommitSize, MEM_DECOMMIT); + ASSERT_UNUSED(success, success); + ptr += decommitSize; + } +} + #else // Platforms that don't support returning memory use an empty inline version of TCMalloc_SystemRelease @@ -457,8 +489,28 @@ void TCMalloc_SystemCommit(void* start, size_t length) #elif HAVE(VIRTUALALLOC) -void TCMalloc_SystemCommit(void*, size_t) +void TCMalloc_SystemCommit(void* start, size_t length) { + if (VirtualAlloc(start, length, MEM_COMMIT, PAGE_READWRITE) == start) + return; + + // The commit may fail if the memory region consists of allocations + // from more than one call to VirtualAlloc. In this case, fall back to + // using VirtualQuery to retrieve the allocation boundaries and commit them + // each individually. + + char* ptr = static_cast<char*>(start); + char* end = ptr + length; + MEMORY_BASIC_INFORMATION info; + while (ptr < end) { + size_t resultSize = VirtualQuery(ptr, &info, sizeof(info)); + ASSERT_UNUSED(resultSize, resultSize == sizeof(info)); + + size_t commitSize = min<size_t>(info.RegionSize, end - ptr); + void* newAddress = VirtualAlloc(ptr, commitSize, MEM_COMMIT, PAGE_READWRITE); + ASSERT_UNUSED(newAddress, newAddress == ptr); + ptr += commitSize; + } } #else |