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(HAIKU) 42 #include <OS.h> 43 #endif 44 45 #if OS(WINDOWS) 46 #include <malloc.h> 47 #include <windows.h> 48 #endif 49 50 #if OS(SYMBIAN) 51 #include <e32hal.h> 52 #include <e32std.h> 53 #endif 54 55 #if HAVE(ERRNO_H) 56 #include <errno.h> 57 #endif 58 59 #if HAVE(MMAP) 60 #include <sys/mman.h> 61 #include <unistd.h> 62 #endif 63 64 namespace WTF { 65 66 /* 67 PageAllocation 68 69 The PageAllocation class provides a cross-platform memory allocation interface 70 with similar capabilities to posix mmap/munmap. Memory is allocated by calling 71 PageAllocation::allocate, and deallocated by calling deallocate on the 72 PageAllocation object. The PageAllocation holds the allocation's base pointer 73 and size. 74 75 The allocate method is passed the size required (which must be a multiple of 76 the system page size, which can be accessed using PageAllocation::pageSize). 77 Callers may also optinally provide a flag indicating the usage (for use by 78 system memory usage tracking tools, where implemented), and boolean values 79 specifying the required protection (defaulting to writable, non-executable). 80 */ 81 82 class PageAllocation : private PageBlock { 83 public: 84 PageAllocation() 85 { 86 } 87 88 using PageBlock::size; 89 using PageBlock::base; 90 91 #ifndef __clang__ 92 using PageBlock::operator bool; 93 #else 94 // FIXME: This is a workaround for <rdar://problem/8876150>, wherein Clang incorrectly emits an access 95 // control warning when a client tries to use operator bool exposed above via "using PageBlock::operator bool". 96 operator bool() const { return PageBlock::operator bool(); } 97 #endif 98 99 static PageAllocation allocate(size_t size, OSAllocator::Usage usage = OSAllocator::UnknownUsage, bool writable = true, bool executable = false) 100 { 101 ASSERT(isPageAligned(size)); 102 return PageAllocation(OSAllocator::reserveAndCommit(size, usage, writable, executable), size); 103 } 104 105 void deallocate() 106 { 107 // Clear base & size before calling release; if this is *inside* allocation 108 // then we won't be able to clear then after deallocating the memory. 109 PageAllocation tmp; 110 std::swap(tmp, *this); 111 112 ASSERT(tmp); 113 ASSERT(!*this); 114 115 OSAllocator::decommitAndRelease(tmp.base(), tmp.size()); 116 } 117 118 private: 119 PageAllocation(void* base, size_t size) 120 : PageBlock(base, size) 121 { 122 } 123 }; 124 125 } // namespace WTF 126 127 using WTF::PageAllocation; 128 129 #endif // PageAllocation_h 130