Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(40)

Side by Side Diff: src/platform/virtual-memory.h

Issue 23641009: Refactor and cleanup VirtualMemory. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Addressed nits. Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/platform-win32.cc ('k') | src/platform/virtual-memory.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 #ifndef V8_PLATFORM_VIRTUAL_MEMORY_H_
29 #define V8_PLATFORM_VIRTUAL_MEMORY_H_
30
31 #include "checks.h"
32 #include "globals.h"
33
34 namespace v8 {
35 namespace internal {
36
37 // -----------------------------------------------------------------------------
38 // VirtualMemory
39 //
40 // This class represents and controls an area of reserved memory.
41 // Control of the reserved memory can be assigned to another VirtualMemory
42 // object by assignment or contructing. This removes the reserved memory from
43 // the original object.
44 class VirtualMemory V8_FINAL {
45 public:
46 // The executability of a memory region.
47 enum Executability { NOT_EXECUTABLE, EXECUTABLE };
48
49 // Empty VirtualMemory object, controlling no reserved memory.
50 VirtualMemory() : address_(NULL), size_(0) {}
51
52 // Reserves virtual memory with size.
53 explicit VirtualMemory(size_t size) : size_(0) {
54 address_ = ReserveRegion(size, &size_);
55 }
56
57 // Reserves virtual memory containing an area of the given size that
58 // is aligned per alignment. This may not be at the position returned
59 // by address().
60 VirtualMemory(size_t size, size_t alignment) : size_(0) {
61 address_ = ReserveRegion(size, &size_, alignment);
62 }
63
64 // Releases the reserved memory, if any, controlled by this VirtualMemory
65 // object.
66 ~VirtualMemory() {
67 if (IsReserved()) {
68 bool result = ReleaseRegion(address_, size_);
69 ASSERT(result);
70 USE(result);
71 }
72 }
73
74 // Returns whether the memory contains the specified address.
75 bool Contains(const void* address) const V8_WARN_UNUSED_RESULT {
76 if (!IsReserved()) return false;
77 if (address < address_) return false;
78 if (address >= reinterpret_cast<uint8_t*>(address_) + size_) return false;
79 return true;
80 }
81
82 // Returns whether the memory has been reserved.
83 bool IsReserved() const V8_WARN_UNUSED_RESULT {
84 return address_ != NULL;
85 }
86
87 // Initialize or resets an embedded VirtualMemory object.
88 void Reset() {
89 address_ = NULL;
90 size_ = 0;
91 }
92
93 // Returns the start address of the reserved memory. The returned value is
94 // only meaningful if |IsReserved()| returns true.
95 // If the memory was reserved with an alignment, this address is not
96 // necessarily aligned. The user might need to round it up to a multiple of
97 // the alignment to get the start of the aligned block.
98 void* address() const V8_WARN_UNUSED_RESULT { return address_; }
99
100 // Returns the size of the reserved memory. The returned value is only
101 // meaningful when |IsReserved()| returns true.
102 // If the memory was reserved with an alignment, this size may be larger
103 // than the requested size.
104 size_t size() const V8_WARN_UNUSED_RESULT { return size_; }
105
106 // Commits real memory. Returns whether the operation succeeded.
107 bool Commit(void* address,
108 size_t size,
109 Executability executability) V8_WARN_UNUSED_RESULT {
110 ASSERT(IsReserved());
111 ASSERT(Contains(address));
112 ASSERT(Contains(reinterpret_cast<uint8_t*>(address) + size - 1));
113 return CommitRegion(address, size, executability);
114 }
115
116 // Uncommit real memory. Returns whether the operation succeeded.
117 bool Uncommit(void* address, size_t size) V8_WARN_UNUSED_RESULT {
118 ASSERT(IsReserved());
119 ASSERT(Contains(address));
120 ASSERT(Contains(reinterpret_cast<uint8_t*>(address) + size - 1));
121 return UncommitRegion(address, size);
122 }
123
124 // Creates guard pages at the given address.
125 bool Guard(void* address, size_t size) V8_WARN_UNUSED_RESULT {
126 // We can simply uncommit the specified pages. Any access
127 // to them will cause a processor exception.
128 return Uncommit(address, size);
129 }
130
131 void Release() {
132 ASSERT(IsReserved());
133 // WARNING: Order is important here. The VirtualMemory
134 // object might live inside the allocated region.
135 void* address = address_;
136 size_t size = size_;
137 Reset();
138 bool result = ReleaseRegion(address, size);
139 USE(result);
140 ASSERT(result);
141 }
142
143 // Assign control of the reserved region to a different VirtualMemory object.
144 // The old object is no longer functional (IsReserved() returns false).
145 void TakeControl(VirtualMemory* from) {
146 ASSERT(!IsReserved());
147 address_ = from->address_;
148 size_ = from->size_;
149 from->Reset();
150 }
151
152 // Allocates a region of memory pages. The pages are readable/writable,
153 // but are not guaranteed to be executable unless explicitly requested.
154 // Returns the base address of the allocated memory region, or NULL in
155 // case of an error.
156 static void* AllocateRegion(size_t size,
157 size_t* size_return,
158 Executability executability)
159 V8_WARN_UNUSED_RESULT;
160
161 static void* ReserveRegion(size_t size,
162 size_t* size_return) V8_WARN_UNUSED_RESULT;
163
164 static void* ReserveRegion(size_t size,
165 size_t* size_return,
166 size_t alignment) V8_WARN_UNUSED_RESULT;
167
168 static bool CommitRegion(void* address,
169 size_t size,
170 Executability executability) V8_WARN_UNUSED_RESULT;
171
172 static bool UncommitRegion(void* address, size_t size) V8_WARN_UNUSED_RESULT;
173
174 // Mark code segments readable-executable.
175 static bool WriteProtectRegion(void* address,
176 size_t size) V8_WARN_UNUSED_RESULT;
177
178 // Must be called with a base pointer that has been returned by ReserveRegion
179 // and the same size it was reserved with.
180 static bool ReleaseRegion(void* address, size_t size) V8_WARN_UNUSED_RESULT;
181
182 // The granularity for the starting address at which virtual memory can be
183 // reserved (or allocated in terms of the underlying operating system).
184 static size_t GetAllocationGranularity() V8_PURE;
185
186 // The maximum size of the virtual memory. 0 means there is no artificial
187 // limit.
188 static size_t GetLimit() V8_PURE;
189
190 // The page size and the granularity of page protection and commitment.
191 static size_t GetPageSize() V8_PURE;
192
193 // Returns true if OS performs lazy commits, i.e. the memory allocation call
194 // defers actual physical memory allocation till the first memory access.
195 // Otherwise returns false.
196 static V8_INLINE(bool HasLazyCommits()) {
197 #if V8_OS_LINUX
198 return true;
199 #else
200 return false;
201 #endif
202 }
203
204 private:
205 void* address_; // Start address of the virtual memory.
206 size_t size_; // Size of the virtual memory.
207 };
208
209 } } // namespace v8::internal
210
211 #endif // V8_PLATFORM_VIRTUAL_MEMORY_H_
OLDNEW
« no previous file with comments | « src/platform-win32.cc ('k') | src/platform/virtual-memory.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698