/js/src/yarr/OSAllocatorWin.cpp

http://github.com/zpao/v8monkey · C++ · 89 lines · 46 code · 13 blank · 30 comment · 6 complexity · a2a761449789716656b4d854a62ffe71 MD5 · raw file

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. * vim: set ts=8 sw=4 et tw=99 ft=cpp:
  3. *
  4. * ***** BEGIN LICENSE BLOCK *****
  5. * Copyright (C) 2010 Apple Inc. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  18. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  19. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  20. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  26. * THE POSSIBILITY OF SUCH DAMAGE.
  27. *
  28. * ***** END LICENSE BLOCK ***** */
  29. #include "assembler/wtf/Platform.h"
  30. #if ENABLE_ASSEMBLER && WTF_OS_WINDOWS
  31. #include "windows.h"
  32. #include "wtf/Assertions.h"
  33. #include "OSAllocator.h"
  34. namespace WTF {
  35. static inline DWORD protection(bool writable, bool executable)
  36. {
  37. return executable ?
  38. (writable ? PAGE_EXECUTE_READWRITE : PAGE_EXECUTE_READ) :
  39. (writable ? PAGE_READWRITE : PAGE_READONLY);
  40. }
  41. void* OSAllocator::reserveUncommitted(size_t bytes, Usage, bool writable, bool executable)
  42. {
  43. void* result = VirtualAlloc(0, bytes, MEM_RESERVE, protection(writable, executable));
  44. if (!result)
  45. CRASH();
  46. return result;
  47. }
  48. void* OSAllocator::reserveAndCommit(size_t bytes, Usage, bool writable, bool executable)
  49. {
  50. void* result = VirtualAlloc(0, bytes, MEM_RESERVE | MEM_COMMIT, protection(writable, executable));
  51. if (!result)
  52. CRASH();
  53. return result;
  54. }
  55. void OSAllocator::commit(void* address, size_t bytes, bool writable, bool executable)
  56. {
  57. void* result = VirtualAlloc(address, bytes, MEM_COMMIT, protection(writable, executable));
  58. if (!result)
  59. CRASH();
  60. }
  61. void OSAllocator::decommit(void* address, size_t bytes)
  62. {
  63. bool result = VirtualFree(address, bytes, MEM_DECOMMIT);
  64. if (!result)
  65. CRASH();
  66. }
  67. void OSAllocator::releaseDecommitted(void* address, size_t bytes)
  68. {
  69. // According to http://msdn.microsoft.com/en-us/library/aa366892(VS.85).aspx,
  70. // dwSize must be 0 if dwFreeType is MEM_RELEASE.
  71. bool result = VirtualFree(address, 0, MEM_RELEASE);
  72. if (!result)
  73. CRASH();
  74. }
  75. } // namespace WTF
  76. #endif