PageAllocation.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef PageAllocation_h
  26. #define PageAllocation_h
  27. #include <wtf/Assertions.h>
  28. #include <wtf/OSAllocator.h>
  29. #include <wtf/PageBlock.h>
  30. #include <wtf/VMTags.h>
  31. #include <algorithm>
  32. #if OS(DARWIN)
  33. #include <mach/mach_init.h>
  34. #include <mach/vm_map.h>
  35. #endif
  36. #if OS(WINDOWS)
  37. #include <malloc.h>
  38. #include <windows.h>
  39. #endif
  40. #if HAVE(ERRNO_H)
  41. #include <errno.h>
  42. #endif
  43. #if HAVE(MMAP)
  44. #include <sys/mman.h>
  45. #include <unistd.h>
  46. #endif
  47. namespace WTF {
  48. /*
  49. PageAllocation
  50. The PageAllocation class provides a cross-platform memory allocation interface
  51. with similar capabilities to posix mmap/munmap. Memory is allocated by calling
  52. PageAllocation::allocate, and deallocated by calling deallocate on the
  53. PageAllocation object. The PageAllocation holds the allocation's base pointer
  54. and size.
  55. The allocate method is passed the size required (which must be a multiple of
  56. the system page size, which can be accessed using PageAllocation::pageSize).
  57. Callers may also optinally provide a flag indicating the usage (for use by
  58. system memory usage tracking tools, where implemented), and boolean values
  59. specifying the required protection (defaulting to writable, non-executable).
  60. */
  61. class PageAllocation : private PageBlock {
  62. public:
  63. PageAllocation()
  64. {
  65. }
  66. using PageBlock::size;
  67. using PageBlock::base;
  68. #ifndef __clang__
  69. using PageBlock::operator bool;
  70. #else
  71. // FIXME: This is a workaround for <rdar://problem/8876150>, wherein Clang incorrectly emits an access
  72. // control warning when a client tries to use operator bool exposed above via "using PageBlock::operator bool".
  73. operator bool() const { return PageBlock::operator bool(); }
  74. #endif
  75. static PageAllocation allocate(size_t size, OSAllocator::Usage usage = OSAllocator::UnknownUsage, bool writable = true, bool executable = false)
  76. {
  77. ASSERT(isPageAligned(size));
  78. return PageAllocation(OSAllocator::reserveAndCommit(size, usage, writable, executable), size);
  79. }
  80. void deallocate()
  81. {
  82. // Clear base & size before calling release; if this is *inside* allocation
  83. // then we won't be able to clear then after deallocating the memory.
  84. PageAllocation tmp;
  85. std::swap(tmp, *this);
  86. ASSERT(tmp);
  87. ASSERT(!*this);
  88. OSAllocator::decommitAndRelease(tmp.base(), tmp.size());
  89. }
  90. private:
  91. PageAllocation(void* base, size_t size)
  92. : PageBlock(base, size, false)
  93. {
  94. }
  95. };
  96. } // namespace WTF
  97. using WTF::PageAllocation;
  98. #endif // PageAllocation_h