| OLD | NEW |
| (Empty) |
| 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 | |
| 26 #ifndef PageAllocation_h | |
| 27 #define PageAllocation_h | |
| 28 | |
| 29 #include <wtf/Assertions.h> | |
| 30 #include <wtf/OSAllocator.h> | |
| 31 #include <wtf/PageBlock.h> | |
| 32 #include <wtf/UnusedParam.h> | |
| 33 #include <wtf/VMTags.h> | |
| 34 #include <algorithm> | |
| 35 | |
| 36 #if OS(DARWIN) | |
| 37 #include <mach/mach_init.h> | |
| 38 #include <mach/vm_map.h> | |
| 39 #endif | |
| 40 | |
| 41 #if OS(WINDOWS) | |
| 42 #include <malloc.h> | |
| 43 #include <windows.h> | |
| 44 #endif | |
| 45 | |
| 46 #if HAVE(ERRNO_H) | |
| 47 #include <errno.h> | |
| 48 #endif | |
| 49 | |
| 50 #if HAVE(MMAP) | |
| 51 #include <sys/mman.h> | |
| 52 #include <unistd.h> | |
| 53 #endif | |
| 54 | |
| 55 namespace WTF { | |
| 56 | |
| 57 /* | |
| 58 PageAllocation | |
| 59 | |
| 60 The PageAllocation class provides a cross-platform memory allocation interfa
ce | |
| 61 with similar capabilities to posix mmap/munmap. Memory is allocated by call
ing | |
| 62 PageAllocation::allocate, and deallocated by calling deallocate on the | |
| 63 PageAllocation object. The PageAllocation holds the allocation's base point
er | |
| 64 and size. | |
| 65 | |
| 66 The allocate method is passed the size required (which must be a multiple of | |
| 67 the system page size, which can be accessed using PageAllocation::pageSize). | |
| 68 Callers may also optinally provide a flag indicating the usage (for use by | |
| 69 system memory usage tracking tools, where implemented), and boolean values | |
| 70 specifying the required protection (defaulting to writable, non-executable). | |
| 71 */ | |
| 72 | |
| 73 class PageAllocation : private PageBlock { | |
| 74 public: | |
| 75 PageAllocation() | |
| 76 { | |
| 77 } | |
| 78 | |
| 79 using PageBlock::size; | |
| 80 using PageBlock::base; | |
| 81 | |
| 82 #ifndef __clang__ | |
| 83 using PageBlock::operator bool; | |
| 84 #else | |
| 85 // FIXME: This is a workaround for <rdar://problem/8876150>, wherein Clang i
ncorrectly emits an access | |
| 86 // control warning when a client tries to use operator bool exposed above vi
a "using PageBlock::operator bool". | |
| 87 operator bool() const { return PageBlock::operator bool(); } | |
| 88 #endif | |
| 89 | |
| 90 static PageAllocation allocate(size_t size, OSAllocator::Usage usage = OSAll
ocator::UnknownUsage, bool writable = true, bool executable = false) | |
| 91 { | |
| 92 ASSERT(isPageAligned(size)); | |
| 93 return PageAllocation(OSAllocator::reserveAndCommit(size, usage, writabl
e, executable), size); | |
| 94 } | |
| 95 | |
| 96 void deallocate() | |
| 97 { | |
| 98 // Clear base & size before calling release; if this is *inside* allocat
ion | |
| 99 // then we won't be able to clear then after deallocating the memory. | |
| 100 PageAllocation tmp; | |
| 101 std::swap(tmp, *this); | |
| 102 | |
| 103 ASSERT(tmp); | |
| 104 ASSERT(!*this); | |
| 105 | |
| 106 OSAllocator::decommitAndRelease(tmp.base(), tmp.size()); | |
| 107 } | |
| 108 | |
| 109 private: | |
| 110 PageAllocation(void* base, size_t size) | |
| 111 : PageBlock(base, size, false) | |
| 112 { | |
| 113 } | |
| 114 }; | |
| 115 | |
| 116 } // namespace WTF | |
| 117 | |
| 118 using WTF::PageAllocation; | |
| 119 | |
| 120 #endif // PageAllocation_h | |
| OLD | NEW |