2 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
30 #include <wtf/UnusedParam.h>
31 #include <wtf/VMTags.h>
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,
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, bool includesGuardPages = false);
49 WTF_EXPORT_PRIVATE static void releaseDecommitted(void*, size_t);
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);
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);
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
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);
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.
73 static T* reallocateCommitted(T*, size_t oldSize, size_t newSize, Usage = UnknownUsage, bool writable = true, bool executable = false);
76 inline void* OSAllocator::reserveAndCommit(size_t reserveSize, size_t commitSize, Usage usage, bool writable, bool executable)
78 void* base = reserveUncommitted(reserveSize, usage, writable, executable);
79 commit(base, commitSize, writable, executable);
83 inline void OSAllocator::decommitAndRelease(void* releaseBase, size_t releaseSize, void* decommitBase, size_t decommitSize)
85 ASSERT(decommitBase >= releaseBase && (static_cast<char*>(decommitBase) + decommitSize) <= (static_cast<char*>(releaseBase) + releaseSize));
87 // On most platforms we can actually skip this final decommit; releasing the VM will
88 // implicitly decommit any physical memory in the region. This is not true on WINCE.
89 decommit(decommitBase, decommitSize);
91 UNUSED_PARAM(decommitBase);
92 UNUSED_PARAM(decommitSize);
94 releaseDecommitted(releaseBase, releaseSize);
97 inline void OSAllocator::decommitAndRelease(void* base, size_t size)
99 decommitAndRelease(base, size, base, size);
103 inline T* OSAllocator::reallocateCommitted(T* oldBase, size_t oldSize, size_t newSize, Usage usage, bool writable, bool executable)
105 void* newBase = reserveAndCommit(newSize, usage, writable, executable);
106 memcpy(newBase, oldBase, std::min(oldSize, newSize));
107 decommitAndRelease(oldBase, oldSize);
108 return static_cast<T*>(newBase);
113 using WTF::OSAllocator;
115 #endif // OSAllocator_h