michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * vim: set ts=8 sts=4 et sw=4 tw=99: michael@0: * michael@0: * ***** BEGIN LICENSE BLOCK ***** michael@0: * Copyright (C) 2010 Apple Inc. All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' michael@0: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, michael@0: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR michael@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS michael@0: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR michael@0: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF michael@0: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS michael@0: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN michael@0: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) michael@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF michael@0: * THE POSSIBILITY OF SUCH DAMAGE. michael@0: * michael@0: * ***** END LICENSE BLOCK ***** */ michael@0: michael@0: #ifndef yarr_OSAllocator_h michael@0: #define yarr_OSAllocator_h michael@0: michael@0: #include michael@0: #include "yarr/wtfbridge.h" michael@0: #include "assembler/wtf/VMTags.h" michael@0: #include "assembler/wtf/Assertions.h" michael@0: michael@0: namespace WTF { michael@0: michael@0: class OSAllocator { michael@0: public: michael@0: enum Usage { michael@0: UnknownUsage = -1, michael@0: FastMallocPages = VM_TAG_FOR_TCMALLOC_MEMORY, michael@0: JSGCHeapPages = VM_TAG_FOR_COLLECTOR_MEMORY, michael@0: JSVMStackPages = VM_TAG_FOR_REGISTERFILE_MEMORY, michael@0: JSJITCodePages = VM_TAG_FOR_EXECUTABLEALLOCATOR_MEMORY michael@0: }; michael@0: michael@0: // These methods are symmetric; reserveUncommitted allocates VM in an uncommitted state, michael@0: // releaseDecommitted should be called on a region of VM allocated by a single reservation, michael@0: // the memory must all currently be in a decommitted state. michael@0: static void* reserveUncommitted(size_t, Usage = UnknownUsage, bool writable = true, bool executable = false); michael@0: static void releaseDecommitted(void*, size_t); michael@0: michael@0: // These methods are symmetric; they commit or decommit a region of VM (uncommitted VM should michael@0: // never be accessed, since the OS may not have attached physical memory for these regions). michael@0: // Clients should only call commit on uncommitted regions and decommit on committed regions. michael@0: static void commit(void*, size_t, bool writable, bool executable); michael@0: static void decommit(void*, size_t); michael@0: michael@0: // These methods are symmetric; reserveAndCommit allocates VM in an committed state, michael@0: // decommitAndRelease should be called on a region of VM allocated by a single reservation, michael@0: // the memory must all currently be in a committed state. michael@0: static void* reserveAndCommit(size_t, Usage = UnknownUsage, bool writable = true, bool executable = false); michael@0: static void decommitAndRelease(void* base, size_t size); michael@0: michael@0: // These methods are akin to reserveAndCommit/decommitAndRelease, above - however rather than michael@0: // committing/decommitting the entire region additional parameters allow a subregion to be michael@0: // specified. michael@0: static void* reserveAndCommit(size_t reserveSize, size_t commitSize, Usage = UnknownUsage, bool writable = true, bool executable = false); michael@0: static void decommitAndRelease(void* releaseBase, size_t releaseSize, void* decommitBase, size_t decommitSize); michael@0: }; michael@0: michael@0: inline void* OSAllocator::reserveAndCommit(size_t reserveSize, size_t commitSize, Usage usage, bool writable, bool executable) michael@0: { michael@0: void* base = reserveUncommitted(reserveSize, usage, writable, executable); michael@0: commit(base, commitSize, writable, executable); michael@0: return base; michael@0: } michael@0: michael@0: inline void OSAllocator::decommitAndRelease(void* releaseBase, size_t releaseSize, void* decommitBase, size_t decommitSize) michael@0: { michael@0: ASSERT(decommitBase >= releaseBase && (static_cast(decommitBase) + decommitSize) <= (static_cast(releaseBase) + releaseSize)); michael@0: #if WTF_OS_WINCE || WTF_OS_SYMBIAN michael@0: // On most platforms we can actually skip this final decommit; releasing the VM will michael@0: // implicitly decommit any physical memory in the region. This is not true on WINCE. michael@0: // On Symbian, this makes implementation simpler and better aligned with the RChunk API michael@0: decommit(decommitBase, decommitSize); michael@0: #endif michael@0: releaseDecommitted(releaseBase, releaseSize); michael@0: } michael@0: michael@0: inline void OSAllocator::decommitAndRelease(void* base, size_t size) michael@0: { michael@0: decommitAndRelease(base, size, base, size); michael@0: } michael@0: michael@0: } // namespace WTF michael@0: michael@0: using WTF::OSAllocator; michael@0: michael@0: #endif /* yarr_OSAllocator_h */