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 | #pragma once |
27 | |
28 | #include <algorithm> |
29 | #include <wtf/VMTags.h> |
30 | |
31 | namespace WTF { |
32 | |
33 | class OSAllocator { |
34 | public: |
35 | enum Usage { |
36 | UnknownUsage = -1, |
37 | FastMallocPages = VM_TAG_FOR_TCMALLOC_MEMORY, |
38 | JSVMStackPages = VM_TAG_FOR_REGISTERFILE_MEMORY, |
39 | JSJITCodePages = VM_TAG_FOR_EXECUTABLEALLOCATOR_MEMORY, |
40 | }; |
41 | |
42 | // These methods are symmetric; reserveUncommitted allocates VM in an uncommitted state, |
43 | // releaseDecommitted should be called on a region of VM allocated by a single reservation, |
44 | // the memory must all currently be in a decommitted state. reserveUncommitted returns to |
45 | // you memory that is zeroed. |
46 | WTF_EXPORT_PRIVATE static void* reserveUncommitted(size_t, Usage = UnknownUsage, bool writable = true, bool executable = false, bool includesGuardPages = false); |
47 | WTF_EXPORT_PRIVATE static void releaseDecommitted(void*, size_t); |
48 | |
49 | // These methods are symmetric; they commit or decommit a region of VM (uncommitted VM should |
50 | // never be accessed, since the OS may not have attached physical memory for these regions). |
51 | // Clients should only call commit on uncommitted regions and decommit on committed regions. |
52 | WTF_EXPORT_PRIVATE static void commit(void*, size_t, bool writable, bool executable); |
53 | WTF_EXPORT_PRIVATE static void decommit(void*, size_t); |
54 | |
55 | // These methods are symmetric; reserveAndCommit allocates VM in an committed state, |
56 | // decommitAndRelease should be called on a region of VM allocated by a single reservation, |
57 | // the memory must all currently be in a committed state. |
58 | WTF_EXPORT_PRIVATE static void* reserveAndCommit(size_t, Usage = UnknownUsage, bool writable = true, bool executable = false, bool includesGuardPages = false); |
59 | static void decommitAndRelease(void* base, size_t size); |
60 | |
61 | // These methods are akin to reserveAndCommit/decommitAndRelease, above - however rather than |
62 | // committing/decommitting the entire region additional parameters allow a subregion to be |
63 | // specified. |
64 | WTF_EXPORT_PRIVATE static void* reserveAndCommit(size_t reserveSize, size_t commitSize, Usage = UnknownUsage, bool writable = true, bool executable = false); |
65 | |
66 | // Reallocate an existing, committed allocation. |
67 | // The prior allocation must be fully comitted, and the new size will also be fully committed. |
68 | // This interface is provided since it may be possible to optimize this operation on some platforms. |
69 | template<typename T> |
70 | static T* reallocateCommitted(T*, size_t oldSize, size_t newSize, Usage = UnknownUsage, bool writable = true, bool executable = false); |
71 | |
72 | // Hint to the OS that an address range is not expected to be accessed anytime soon. |
73 | WTF_EXPORT_PRIVATE static void hintMemoryNotNeededSoon(void*, size_t); |
74 | }; |
75 | |
76 | inline void* OSAllocator::reserveAndCommit(size_t reserveSize, size_t commitSize, Usage usage, bool writable, bool executable) |
77 | { |
78 | void* base = reserveUncommitted(reserveSize, usage, writable, executable); |
79 | commit(base, commitSize, writable, executable); |
80 | return base; |
81 | } |
82 | |
83 | inline void OSAllocator::decommitAndRelease(void* releaseBase, size_t releaseSize) |
84 | { |
85 | releaseDecommitted(releaseBase, releaseSize); |
86 | } |
87 | |
88 | template<typename T> |
89 | inline T* OSAllocator::reallocateCommitted(T* oldBase, size_t oldSize, size_t newSize, Usage usage, bool writable, bool executable) |
90 | { |
91 | void* newBase = reserveAndCommit(newSize, usage, writable, executable); |
92 | memcpy(newBase, oldBase, std::min(oldSize, newSize)); |
93 | decommitAndRelease(oldBase, oldSize); |
94 | return static_cast<T*>(newBase); |
95 | } |
96 | |
97 | } // namespace WTF |
98 | |
99 | using WTF::OSAllocator; |
100 | |