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
30namespace 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
56class PageReservation : private PageBlock {
57public:
58 PageReservation()
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 commit(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 decommit(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 committed()
92 {
93 return m_committed;
94 }
95
96 static PageReservation reserve(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 reserveWithGuardPages(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 deallocate()
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
129private:
130 PageReservation(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 m_committed;
139 bool m_writable;
140 bool m_executable;
141};
142
143}
144
145using WTF::PageReservation;
146