OSAllocator.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (C) 2010 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef OSAllocator_h
  26. #define OSAllocator_h
  27. #include <algorithm>
  28. #include <wtf/VMTags.h>
  29. namespace WTF {
  30. class OSAllocator {
  31. public:
  32. enum Usage {
  33. UnknownUsage = -1,
  34. FastMallocPages = VM_TAG_FOR_TCMALLOC_MEMORY,
  35. JSGCHeapPages = VM_TAG_FOR_COLLECTOR_MEMORY,
  36. JSVMStackPages = VM_TAG_FOR_REGISTERFILE_MEMORY,
  37. JSJITCodePages = VM_TAG_FOR_EXECUTABLEALLOCATOR_MEMORY,
  38. };
  39. // These methods are symmetric; reserveUncommitted allocates VM in an uncommitted state,
  40. // releaseDecommitted should be called on a region of VM allocated by a single reservation,
  41. // the memory must all currently be in a decommitted state.
  42. WTF_EXPORT_PRIVATE static void* reserveUncommitted(size_t, Usage = UnknownUsage, bool writable = true, bool executable = false, bool includesGuardPages = false);
  43. WTF_EXPORT_PRIVATE static void releaseDecommitted(void*, size_t);
  44. // These methods are symmetric; they commit or decommit a region of VM (uncommitted VM should
  45. // never be accessed, since the OS may not have attached physical memory for these regions).
  46. // Clients should only call commit on uncommitted regions and decommit on committed regions.
  47. WTF_EXPORT_PRIVATE static void commit(void*, size_t, bool writable, bool executable);
  48. WTF_EXPORT_PRIVATE static void decommit(void*, size_t);
  49. // These methods are symmetric; reserveAndCommit allocates VM in an committed state,
  50. // decommitAndRelease should be called on a region of VM allocated by a single reservation,
  51. // the memory must all currently be in a committed state.
  52. WTF_EXPORT_PRIVATE static void* reserveAndCommit(size_t, Usage = UnknownUsage, bool writable = true, bool executable = false, bool includesGuardPages = false);
  53. static void decommitAndRelease(void* base, size_t size);
  54. // These methods are akin to reserveAndCommit/decommitAndRelease, above - however rather than
  55. // committing/decommitting the entire region additional parameters allow a subregion to be
  56. // specified.
  57. WTF_EXPORT_PRIVATE static void* reserveAndCommit(size_t reserveSize, size_t commitSize, Usage = UnknownUsage, bool writable = true, bool executable = false);
  58. static void decommitAndRelease(void* releaseBase, size_t releaseSize, void* decommitBase, size_t decommitSize);
  59. // Reallocate an existing, committed allocation.
  60. // The prior allocation must be fully comitted, and the new size will also be fully committed.
  61. // This interface is provided since it may be possible to optimize this operation on some platforms.
  62. template<typename T>
  63. static T* reallocateCommitted(T*, size_t oldSize, size_t newSize, Usage = UnknownUsage, bool writable = true, bool executable = false);
  64. };
  65. inline void* OSAllocator::reserveAndCommit(size_t reserveSize, size_t commitSize, Usage usage, bool writable, bool executable)
  66. {
  67. void* base = reserveUncommitted(reserveSize, usage, writable, executable);
  68. commit(base, commitSize, writable, executable);
  69. return base;
  70. }
  71. inline void OSAllocator::decommitAndRelease(void* releaseBase, size_t releaseSize, void* decommitBase, size_t decommitSize)
  72. {
  73. ASSERT(decommitBase >= releaseBase && (static_cast<char*>(decommitBase) + decommitSize) <= (static_cast<char*>(releaseBase) + releaseSize));
  74. #if OS(WINCE)
  75. // On most platforms we can actually skip this final decommit; releasing the VM will
  76. // implicitly decommit any physical memory in the region. This is not true on WINCE.
  77. decommit(decommitBase, decommitSize);
  78. #else
  79. UNUSED_PARAM(decommitBase);
  80. UNUSED_PARAM(decommitSize);
  81. #endif
  82. releaseDecommitted(releaseBase, releaseSize);
  83. }
  84. inline void OSAllocator::decommitAndRelease(void* base, size_t size)
  85. {
  86. decommitAndRelease(base, size, base, size);
  87. }
  88. template<typename T>
  89. inline T* OSAllocator::reallocateCommitted(T* oldBase, size_t oldSize, size_t newSize, Usage usage, bool writable, bool executable)
  90. {
  91. void* newBase = reserveAndCommit(newSize, usage, writable, executable);
  92. memcpy(newBase, oldBase, std::min(oldSize, newSize));
  93. decommitAndRelease(oldBase, oldSize);
  94. return static_cast<T*>(newBase);
  95. }
  96. } // namespace WTF
  97. using WTF::OSAllocator;
  98. #endif // OSAllocator_h