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 <wtf/UnusedParam.h> 30 #include <wtf/VMTags.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 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 static void* reserveAndCommit(size_t, Usage = UnknownUsage, bool writable = true, bool executable = 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 70 inline void* OSAllocator::reserveAndCommit(size_t reserveSize, size_t commitSize, Usage usage, bool writable, bool executable) 71 { 72 void* base = reserveUncommitted(reserveSize, usage, writable, executable); 73 commit(base, commitSize, writable, executable); 74 return base; 75 } 76 77 inline void OSAllocator::decommitAndRelease(void* releaseBase, size_t releaseSize, void* decommitBase, size_t decommitSize) 78 { 79 ASSERT(decommitBase >= releaseBase && (static_cast<char*>(decommitBase) + decommitSize) <= (static_cast<char*>(releaseBase) + releaseSize)); 80 #if OS(WINCE) || OS(SYMBIAN) 81 // On most platforms we can actually skip this final decommit; releasing the VM will 82 // implicitly decommit any physical memory in the region. This is not true on WINCE. 83 // On Symbian, this makes implementation simpler and better aligned with the RChunk API 84 decommit(decommitBase, decommitSize); 85 #else 86 UNUSED_PARAM(decommitBase); 87 UNUSED_PARAM(decommitSize); 88 #endif 89 releaseDecommitted(releaseBase, releaseSize); 90 } 91 92 inline void OSAllocator::decommitAndRelease(void* base, size_t size) 93 { 94 decommitAndRelease(base, size, base, size); 95 } 96 97 } // namespace WTF 98 99 using WTF::OSAllocator; 100 101 #endif // OSAllocator_h 102