Line 0
Link Here
|
|
|
1 |
--- deps/v8/src/platform-freebsd.cc.orig 2011-10-25 19:44:21.000000000 +0800 |
2 |
+++ deps/v8/src/platform-freebsd.cc 2011-10-25 20:08:08.000000000 +0800 |
3 |
@@ -333,32 +333,96 @@ int OS::StackWalk(Vector<OS::StackFrame> |
4 |
static const int kMmapFd = -1; |
5 |
static const int kMmapFdOffset = 0; |
6 |
|
7 |
+VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { } |
8 |
+ |
9 |
+ |
10 |
+VirtualMemory::VirtualMemory(size_t size) |
11 |
+ : address_(ReserveRegion(size)), size_(size) { } |
12 |
|
13 |
-VirtualMemory::VirtualMemory(size_t size) { |
14 |
- address_ = mmap(NULL, size, PROT_NONE, |
15 |
- MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, |
16 |
- kMmapFd, kMmapFdOffset); |
17 |
- size_ = size; |
18 |
-} |
19 |
|
20 |
+VirtualMemory::VirtualMemory(size_t size, size_t alignment) |
21 |
+ : address_(NULL), size_(0) { |
22 |
+ ASSERT(IsAligned(alignment, static_cast<intptr_t>(OS::AllocateAlignment()))); |
23 |
+ size_t request_size = RoundUp(size + alignment, |
24 |
+ static_cast<intptr_t>(OS::AllocateAlignment())); |
25 |
+ void* reservation = mmap(OS::GetRandomMmapAddr(), |
26 |
+ request_size, |
27 |
+ PROT_NONE, |
28 |
+ MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, |
29 |
+ kMmapFd, |
30 |
+ kMmapFdOffset); |
31 |
+ if (reservation == MAP_FAILED) return; |
32 |
+ |
33 |
+ Address base = static_cast<Address>(reservation); |
34 |
+ Address aligned_base = RoundUp(base, alignment); |
35 |
+ ASSERT_LE(base, aligned_base); |
36 |
+ |
37 |
+ // Unmap extra memory reserved before and after the desired block. |
38 |
+ if (aligned_base != base) { |
39 |
+ size_t prefix_size = static_cast<size_t>(aligned_base - base); |
40 |
+ OS::Free(base, prefix_size); |
41 |
+ request_size -= prefix_size; |
42 |
+ } |
43 |
+ |
44 |
+ size_t aligned_size = RoundUp(size, OS::AllocateAlignment()); |
45 |
+ ASSERT_LE(aligned_size, request_size); |
46 |
+ |
47 |
+ if (aligned_size != request_size) { |
48 |
+ size_t suffix_size = request_size - aligned_size; |
49 |
+ OS::Free(aligned_base + aligned_size, suffix_size); |
50 |
+ request_size -= suffix_size; |
51 |
+ } |
52 |
+ |
53 |
+ ASSERT(aligned_size == request_size); |
54 |
+ |
55 |
+ address_ = static_cast<void*>(aligned_base); |
56 |
+ size_ = aligned_size; |
57 |
+} |
58 |
|
59 |
VirtualMemory::~VirtualMemory() { |
60 |
if (IsReserved()) { |
61 |
- if (0 == munmap(address(), size())) address_ = MAP_FAILED; |
62 |
+ bool result = ReleaseRegion(address(), size()); |
63 |
+ ASSERT(result); |
64 |
+ USE(result); |
65 |
} |
66 |
} |
67 |
|
68 |
+void VirtualMemory::Reset() { |
69 |
+ address_ = NULL; |
70 |
+ size_ = 0; |
71 |
+} |
72 |
+ |
73 |
+void* VirtualMemory::ReserveRegion(size_t size) { |
74 |
+ void* result = mmap(OS::GetRandomMmapAddr(), |
75 |
+ size, |
76 |
+ PROT_NONE, |
77 |
+ MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, |
78 |
+ kMmapFd, |
79 |
+ kMmapFdOffset); |
80 |
+ |
81 |
+ if (result == MAP_FAILED) return NULL; |
82 |
+ |
83 |
+ return result; |
84 |
+} |
85 |
|
86 |
bool VirtualMemory::IsReserved() { |
87 |
return address_ != MAP_FAILED; |
88 |
} |
89 |
|
90 |
+bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) { |
91 |
+ return CommitRegion(address, size, is_executable); |
92 |
+} |
93 |
|
94 |
-bool VirtualMemory::Commit(void* address, size_t size, bool executable) { |
95 |
- int prot = PROT_READ | PROT_WRITE | (executable ? PROT_EXEC : 0); |
96 |
- if (MAP_FAILED == mmap(address, size, prot, |
97 |
+bool VirtualMemory::CommitRegion(void* address, |
98 |
+ size_t size, |
99 |
+ bool is_executable) { |
100 |
+ int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0); |
101 |
+ if (MAP_FAILED == mmap(address, |
102 |
+ size, |
103 |
+ prot, |
104 |
MAP_PRIVATE | MAP_ANON | MAP_FIXED, |
105 |
- kMmapFd, kMmapFdOffset)) { |
106 |
+ kMmapFd, |
107 |
+ kMmapFdOffset)) { |
108 |
return false; |
109 |
} |
110 |
|
111 |
@@ -366,13 +430,22 @@ bool VirtualMemory::Commit(void* address |
112 |
return true; |
113 |
} |
114 |
|
115 |
- |
116 |
bool VirtualMemory::Uncommit(void* address, size_t size) { |
117 |
- return mmap(address, size, PROT_NONE, |
118 |
+ return UncommitRegion(address, size); |
119 |
+} |
120 |
+ |
121 |
+bool VirtualMemory::UncommitRegion(void* address, size_t size) { |
122 |
+ return mmap(address, |
123 |
+ size, |
124 |
+ PROT_NONE, |
125 |
MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | MAP_FIXED, |
126 |
- kMmapFd, kMmapFdOffset) != MAP_FAILED; |
127 |
+ kMmapFd, |
128 |
+ kMmapFdOffset) != MAP_FAILED; |
129 |
} |
130 |
|
131 |
+bool VirtualMemory::ReleaseRegion(void* address, size_t size) { |
132 |
+ return munmap(address, size) == 0; |
133 |
+} |
134 |
|
135 |
class Thread::PlatformData : public Malloced { |
136 |
public: |