Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
michael@0 | 2 | * vim: set ts=8 sts=4 et sw=4 tw=99: |
michael@0 | 3 | * |
michael@0 | 4 | * ***** BEGIN LICENSE BLOCK ***** |
michael@0 | 5 | * Copyright (C) 2010 Apple Inc. All rights reserved. |
michael@0 | 6 | * |
michael@0 | 7 | * Redistribution and use in source and binary forms, with or without |
michael@0 | 8 | * modification, are permitted provided that the following conditions |
michael@0 | 9 | * are met: |
michael@0 | 10 | * 1. Redistributions of source code must retain the above copyright |
michael@0 | 11 | * notice, this list of conditions and the following disclaimer. |
michael@0 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
michael@0 | 13 | * notice, this list of conditions and the following disclaimer in the |
michael@0 | 14 | * documentation and/or other materials provided with the distribution. |
michael@0 | 15 | * |
michael@0 | 16 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
michael@0 | 17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
michael@0 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
michael@0 | 19 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
michael@0 | 20 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
michael@0 | 21 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
michael@0 | 22 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
michael@0 | 23 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
michael@0 | 24 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 26 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 27 | * |
michael@0 | 28 | * ***** END LICENSE BLOCK ***** */ |
michael@0 | 29 | |
michael@0 | 30 | #ifndef yarr_PageAllocation_h |
michael@0 | 31 | #define yarr_PageAllocation_h |
michael@0 | 32 | |
michael@0 | 33 | #include "yarr/wtfbridge.h" |
michael@0 | 34 | #include "yarr/OSAllocator.h" |
michael@0 | 35 | #include "yarr/PageBlock.h" |
michael@0 | 36 | #include "assembler/wtf/VMTags.h" |
michael@0 | 37 | |
michael@0 | 38 | #if WTF_OS_DARWIN |
michael@0 | 39 | #include <mach/mach_init.h> |
michael@0 | 40 | #include <mach/vm_map.h> |
michael@0 | 41 | #endif |
michael@0 | 42 | |
michael@0 | 43 | #if WTF_OS_HAIKU |
michael@0 | 44 | #include <OS.h> |
michael@0 | 45 | #endif |
michael@0 | 46 | |
michael@0 | 47 | #if WTF_OS_WINDOWS |
michael@0 | 48 | #include <malloc.h> |
michael@0 | 49 | #include <windows.h> |
michael@0 | 50 | #endif |
michael@0 | 51 | |
michael@0 | 52 | #if WTF_OS_SYMBIAN |
michael@0 | 53 | #include <e32hal.h> |
michael@0 | 54 | #include <e32std.h> |
michael@0 | 55 | #endif |
michael@0 | 56 | |
michael@0 | 57 | #if WTF_HAVE_ERRNO_H |
michael@0 | 58 | #include <errno.h> |
michael@0 | 59 | #endif |
michael@0 | 60 | |
michael@0 | 61 | #if WTF_HAVE_MMAP |
michael@0 | 62 | #include <sys/mman.h> |
michael@0 | 63 | #include <unistd.h> |
michael@0 | 64 | #endif |
michael@0 | 65 | |
michael@0 | 66 | namespace WTF { |
michael@0 | 67 | |
michael@0 | 68 | /* |
michael@0 | 69 | PageAllocation |
michael@0 | 70 | |
michael@0 | 71 | The PageAllocation class provides a cross-platform memory allocation interface |
michael@0 | 72 | with similar capabilities to posix mmap/munmap. Memory is allocated by calling |
michael@0 | 73 | PageAllocation::allocate, and deallocated by calling deallocate on the |
michael@0 | 74 | PageAllocation object. The PageAllocation holds the allocation's base pointer |
michael@0 | 75 | and size. |
michael@0 | 76 | |
michael@0 | 77 | The allocate method is passed the size required (which must be a multiple of |
michael@0 | 78 | the system page size, which can be accessed using PageAllocation::pageSize). |
michael@0 | 79 | Callers may also optinally provide a flag indicating the usage (for use by |
michael@0 | 80 | system memory usage tracking tools, where implemented), and boolean values |
michael@0 | 81 | specifying the required protection (defaulting to writable, non-executable). |
michael@0 | 82 | */ |
michael@0 | 83 | |
michael@0 | 84 | class PageAllocation : private PageBlock { |
michael@0 | 85 | public: |
michael@0 | 86 | PageAllocation() |
michael@0 | 87 | { |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | using PageBlock::size; |
michael@0 | 91 | using PageBlock::base; |
michael@0 | 92 | |
michael@0 | 93 | #ifndef __clang__ |
michael@0 | 94 | using PageBlock::operator bool; |
michael@0 | 95 | #else |
michael@0 | 96 | // FIXME: This is a workaround for <rdar://problem/8876150>, wherein Clang incorrectly emits an access |
michael@0 | 97 | // control warning when a client tries to use operator bool exposed above via "using PageBlock::operator bool". |
michael@0 | 98 | operator bool() const { return PageBlock::operator bool(); } |
michael@0 | 99 | #endif |
michael@0 | 100 | |
michael@0 | 101 | static PageAllocation allocate(size_t size, OSAllocator::Usage usage = OSAllocator::UnknownUsage, bool writable = true, bool executable = false) |
michael@0 | 102 | { |
michael@0 | 103 | ASSERT(isPageAligned(size)); |
michael@0 | 104 | return PageAllocation(OSAllocator::reserveAndCommit(size, usage, writable, executable), size); |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | void deallocate() |
michael@0 | 108 | { |
michael@0 | 109 | // Clear base & size before calling release; if this is *inside* allocation |
michael@0 | 110 | // then we won't be able to clear then after deallocating the memory. |
michael@0 | 111 | PageAllocation tmp; |
michael@0 | 112 | JSC::std::swap(tmp, *this); |
michael@0 | 113 | |
michael@0 | 114 | ASSERT(tmp); |
michael@0 | 115 | ASSERT(!*this); |
michael@0 | 116 | |
michael@0 | 117 | OSAllocator::decommitAndRelease(tmp.base(), tmp.size()); |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | private: |
michael@0 | 121 | PageAllocation(void* base, size_t size) |
michael@0 | 122 | : PageBlock(base, size) |
michael@0 | 123 | { |
michael@0 | 124 | } |
michael@0 | 125 | }; |
michael@0 | 126 | |
michael@0 | 127 | } // namespace WTF |
michael@0 | 128 | |
michael@0 | 129 | using WTF::PageAllocation; |
michael@0 | 130 | |
michael@0 | 131 | #endif /* yarr_PageAllocation_h */ |