| 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 |  | 
| 26 | #ifndef OSAllocator_h | 
| 27 | #define OSAllocator_h | 
| 28 |  | 
| 29 | #include <algorithm> | 
| 30 | #include <wtf/UnusedParam.h> | 
| 31 | #include <wtf/VMTags.h> | 
| 32 |  | 
| 33 | namespace WTF { | 
| 34 |  | 
| 35 | class OSAllocator { | 
| 36 | public: | 
| 37 |     enum Usage { | 
| 38 |         UnknownUsage = -1, | 
| 39 |         FastMallocPages = VM_TAG_FOR_TCMALLOC_MEMORY, | 
| 40 |         JSGCHeapPages = VM_TAG_FOR_COLLECTOR_MEMORY, | 
| 41 |         JSVMStackPages = VM_TAG_FOR_REGISTERFILE_MEMORY, | 
| 42 |         JSJITCodePages = VM_TAG_FOR_EXECUTABLEALLOCATOR_MEMORY, | 
| 43 |     }; | 
| 44 |  | 
| 45 |     // These methods are symmetric; reserveUncommitted allocates VM in an uncommitted state, | 
| 46 |     // releaseDecommitted should be called on a region of VM allocated by a single reservation, | 
| 47 |     // the memory must all currently be in a decommitted state. | 
| 48 |     static void* reserveUncommitted(size_t, Usage = UnknownUsage, bool writable = true, bool executable = false); | 
| 49 |     WTF_EXPORT_PRIVATE static void releaseDecommitted(void*, size_t); | 
| 50 |  | 
| 51 |     // These methods are symmetric; they commit or decommit a region of VM (uncommitted VM should | 
| 52 |     // never be accessed, since the OS may not have attached physical memory for these regions). | 
| 53 |     // Clients should only call commit on uncommitted regions and decommit on committed regions. | 
| 54 |     static void commit(void*, size_t, bool writable, bool executable); | 
| 55 |     static void decommit(void*, size_t); | 
| 56 |  | 
| 57 |     // These methods are symmetric; reserveAndCommit allocates VM in an committed state, | 
| 58 |     // decommitAndRelease should be called on a region of VM allocated by a single reservation, | 
| 59 |     // the memory must all currently be in a committed state. | 
| 60 |     WTF_EXPORT_PRIVATE static void* reserveAndCommit(size_t, Usage = UnknownUsage, bool writable = true, bool executable = false, bool includesGuardPages = false); | 
| 61 |     static void decommitAndRelease(void* base, size_t size); | 
| 62 |  | 
| 63 |     // These methods are akin to reserveAndCommit/decommitAndRelease, above - however rather than | 
| 64 |     // committing/decommitting the entire region additional parameters allow a subregion to be | 
| 65 |     // specified. | 
| 66 |     static void* reserveAndCommit(size_t reserveSize, size_t commitSize, Usage = UnknownUsage, bool writable = true, bool executable = false); | 
| 67 |     static void decommitAndRelease(void* releaseBase, size_t releaseSize, void* decommitBase, size_t decommitSize); | 
| 68 |  | 
| 69 |     // Reallocate an existing, committed allocation. | 
| 70 |     // The prior allocation must be fully comitted, and the new size will also be fully committed. | 
| 71 |     // This interface is provided since it may be possible to optimize this operation on some platforms. | 
| 72 |     template<typename T> | 
| 73 |     static T* reallocateCommitted(T*, size_t oldSize, size_t newSize, Usage = UnknownUsage, bool writable = true, bool executable = false); | 
| 74 |  | 
| 75 |     static bool canAllocateExecutableMemory(); | 
| 76 |  | 
| 77 | #if defined(Q_OS_INTEGRITY) | 
| 78 |     static void setMemoryAttributes(void* addr, size_t size, bool writable, bool executable); | 
| 79 | #endif | 
| 80 | }; | 
| 81 |  | 
| 82 | inline void* OSAllocator::reserveAndCommit(size_t reserveSize, size_t commitSize, Usage usage, bool writable, bool executable) | 
| 83 | { | 
| 84 |     void* base = reserveUncommitted(reserveSize, usage, writable, executable); | 
| 85 |     commit(base, commitSize, writable, executable); | 
| 86 |     return base; | 
| 87 | } | 
| 88 |  | 
| 89 | inline void OSAllocator::decommitAndRelease(void* releaseBase, size_t releaseSize, void* decommitBase, size_t decommitSize) | 
| 90 | { | 
| 91 |     ASSERT(decommitBase >= releaseBase && (static_cast<char*>(decommitBase) + decommitSize) <= (static_cast<char*>(releaseBase) + releaseSize)); | 
| 92 | #if OS(WINCE) | 
| 93 |     // On most platforms we can actually skip this final decommit; releasing the VM will | 
| 94 |     // implicitly decommit any physical memory in the region. This is not true on WINCE. | 
| 95 |     decommit(decommitBase, decommitSize); | 
| 96 | #else | 
| 97 |     UNUSED_PARAM(decommitBase); | 
| 98 |     UNUSED_PARAM(decommitSize); | 
| 99 | #endif | 
| 100 |     releaseDecommitted(releaseBase, releaseSize); | 
| 101 | } | 
| 102 |  | 
| 103 | inline void OSAllocator::decommitAndRelease(void* base, size_t size) | 
| 104 | { | 
| 105 |     decommitAndRelease(releaseBase: base, releaseSize: size, decommitBase: base, decommitSize: size); | 
| 106 | } | 
| 107 |  | 
| 108 | template<typename T> | 
| 109 | inline T* OSAllocator::reallocateCommitted(T* oldBase, size_t oldSize, size_t newSize, Usage usage, bool writable, bool executable) | 
| 110 | { | 
| 111 |     void* newBase = reserveAndCommit(newSize, usage, writable, executable); | 
| 112 |     memcpy(newBase, oldBase, std::min(a: oldSize, b: newSize)); | 
| 113 |     decommitAndRelease(oldBase, oldSize); | 
| 114 |     return static_cast<T*>(newBase); | 
| 115 | } | 
| 116 |  | 
| 117 | } // namespace WTF | 
| 118 |  | 
| 119 | using WTF::OSAllocator; | 
| 120 |  | 
| 121 | #endif // OSAllocator_h | 
| 122 |  |