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. ``AS IS'' AND ANY |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #pragma once |
27 | |
28 | #include <wtf/PageAllocation.h> |
29 | |
30 | namespace WTF { |
31 | |
32 | /* |
33 | PageReservation |
34 | |
35 | Like PageAllocation, the PageReservation class provides a cross-platform memory |
36 | allocation interface, but with a set of capabilities more similar to that of |
37 | VirtualAlloc than posix mmap. PageReservation can be used to allocate virtual |
38 | memory without committing physical memory pages using PageReservation::reserve. |
39 | Following a call to reserve all memory in the region is in a decommited state, |
40 | in which the memory should not be used (accessing the memory may cause a fault). |
41 | |
42 | Before using memory it must be committed by calling commit, which is passed start |
43 | and size values (both of which require system page size granularity). One the |
44 | committed memory is no longer needed 'decommit' may be called to return the |
45 | memory to its devommitted state. Commit should only be called on memory that is |
46 | currently decommitted, and decommit should only be called on memory regions that |
47 | are currently committed. All memory should be decommited before the reservation |
48 | is deallocated. Values in memory may not be retained accross a pair of calls if |
49 | the region of memory is decommitted and then committed again. |
50 | |
51 | Memory protection should not be changed on decommitted memory, and if protection |
52 | is changed on memory while it is committed it should be returned to the orignal |
53 | protection before decommit is called. |
54 | */ |
55 | |
56 | class : private PageBlock { |
57 | public: |
58 | () |
59 | : m_committed(0) |
60 | , m_writable(false) |
61 | , m_executable(false) |
62 | { |
63 | } |
64 | |
65 | using PageBlock::base; |
66 | using PageBlock::size; |
67 | using PageBlock::operator bool; |
68 | |
69 | void (void* start, size_t size) |
70 | { |
71 | ASSERT(*this); |
72 | ASSERT(isPageAligned(start)); |
73 | ASSERT(isPageAligned(size)); |
74 | ASSERT(contains(start, size)); |
75 | |
76 | m_committed += size; |
77 | OSAllocator::commit(start, size, m_writable, m_executable); |
78 | } |
79 | |
80 | void (void* start, size_t size) |
81 | { |
82 | ASSERT(*this); |
83 | ASSERT(isPageAligned(start)); |
84 | ASSERT(isPageAligned(size)); |
85 | ASSERT(contains(start, size)); |
86 | |
87 | m_committed -= size; |
88 | OSAllocator::decommit(start, size); |
89 | } |
90 | |
91 | size_t () |
92 | { |
93 | return m_committed; |
94 | } |
95 | |
96 | static PageReservation (size_t size, OSAllocator::Usage usage = OSAllocator::UnknownUsage, bool writable = true, bool executable = false) |
97 | { |
98 | ASSERT(isPageAligned(size)); |
99 | return PageReservation(OSAllocator::reserveUncommitted(size, usage, writable, executable), size, writable, executable, false); |
100 | } |
101 | |
102 | static PageReservation (size_t size, OSAllocator::Usage usage = OSAllocator::UnknownUsage, bool writable = true, bool executable = false) |
103 | { |
104 | ASSERT(isPageAligned(size)); |
105 | return PageReservation(OSAllocator::reserveUncommitted(size + pageSize() * 2, usage, writable, executable, true), size, writable, executable, true); |
106 | } |
107 | |
108 | static PageReservation reserveAndCommitWithGuardPages(size_t size, OSAllocator::Usage usage = OSAllocator::UnknownUsage, bool writable = true, bool executable = false) |
109 | { |
110 | ASSERT(isPageAligned(size)); |
111 | return PageReservation(OSAllocator::reserveAndCommit(size + pageSize() * 2, usage, writable, executable, true), size, writable, executable, true); |
112 | } |
113 | |
114 | void () |
115 | { |
116 | ASSERT(!m_committed); |
117 | |
118 | // Clear base & size before calling release; if this is *inside* allocation |
119 | // then we won't be able to clear then after deallocating the memory. |
120 | PageReservation tmp; |
121 | std::swap(tmp, *this); |
122 | |
123 | ASSERT(tmp); |
124 | ASSERT(!*this); |
125 | |
126 | OSAllocator::releaseDecommitted(tmp.base(), tmp.size()); |
127 | } |
128 | |
129 | private: |
130 | (void* base, size_t size, bool writable, bool executable, bool hasGuardPages) |
131 | : PageBlock(base, size, hasGuardPages) |
132 | , m_committed(0) |
133 | , m_writable(writable) |
134 | , m_executable(executable) |
135 | { |
136 | } |
137 | |
138 | size_t ; |
139 | bool ; |
140 | bool ; |
141 | }; |
142 | |
143 | } |
144 | |
145 | using WTF::PageReservation; |
146 | |