OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "platform/assert.h" | 5 #include "platform/assert.h" |
6 #include "vm/unit_test.h" | 6 #include "vm/unit_test.h" |
7 #include "vm/virtual_memory.h" | 7 #include "vm/virtual_memory.h" |
8 | 8 |
9 namespace dart { | 9 namespace dart { |
10 | 10 |
11 bool IsZero(char* begin, char* end) { | 11 bool IsZero(char* begin, char* end) { |
12 for (char* current = begin; current < end; ++current) { | 12 for (char* current = begin; current < end; ++current) { |
13 if (*current != 0) { | 13 if (*current != 0) { |
14 return false; | 14 return false; |
15 } | 15 } |
16 } | 16 } |
17 return true; | 17 return true; |
18 } | 18 } |
19 | 19 |
20 | 20 |
21 UNIT_TEST_CASE(AllocateVirtualMemory) { | 21 VM_UNIT_TEST_CASE(AllocateVirtualMemory) { |
22 const intptr_t kVirtualMemoryBlockSize = 64 * KB; | 22 const intptr_t kVirtualMemoryBlockSize = 64 * KB; |
23 VirtualMemory* vm = VirtualMemory::Reserve(kVirtualMemoryBlockSize); | 23 VirtualMemory* vm = VirtualMemory::Reserve(kVirtualMemoryBlockSize); |
24 EXPECT(vm != NULL); | 24 EXPECT(vm != NULL); |
25 EXPECT(vm->address() != NULL); | 25 EXPECT(vm->address() != NULL); |
26 EXPECT_EQ(kVirtualMemoryBlockSize, vm->size()); | 26 EXPECT_EQ(kVirtualMemoryBlockSize, vm->size()); |
27 EXPECT_EQ(vm->start(), reinterpret_cast<uword>(vm->address())); | 27 EXPECT_EQ(vm->start(), reinterpret_cast<uword>(vm->address())); |
28 EXPECT_EQ(vm->start() + kVirtualMemoryBlockSize, vm->end()); | 28 EXPECT_EQ(vm->start() + kVirtualMemoryBlockSize, vm->end()); |
29 EXPECT(vm->Contains(vm->start())); | 29 EXPECT(vm->Contains(vm->start())); |
30 EXPECT(vm->Contains(vm->start() + 1)); | 30 EXPECT(vm->Contains(vm->start() + 1)); |
31 EXPECT(vm->Contains(vm->start() + kVirtualMemoryBlockSize - 1)); | 31 EXPECT(vm->Contains(vm->start() + kVirtualMemoryBlockSize - 1)); |
(...skipping 13 matching lines...) Expand all Loading... |
45 buf[2] = '/'; | 45 buf[2] = '/'; |
46 buf[3] = 'd'; | 46 buf[3] = 'd'; |
47 buf[4] = 'c'; | 47 buf[4] = 'c'; |
48 buf[5] = 0; | 48 buf[5] = 0; |
49 EXPECT_STREQ("ac/dc", buf); | 49 EXPECT_STREQ("ac/dc", buf); |
50 | 50 |
51 delete vm; | 51 delete vm; |
52 } | 52 } |
53 | 53 |
54 | 54 |
55 UNIT_TEST_CASE(FreeVirtualMemory) { | 55 VM_UNIT_TEST_CASE(FreeVirtualMemory) { |
56 // Reservations should always be handed back to OS upon destruction. | 56 // Reservations should always be handed back to OS upon destruction. |
57 const intptr_t kVirtualMemoryBlockSize = 10 * MB; | 57 const intptr_t kVirtualMemoryBlockSize = 10 * MB; |
58 const intptr_t kIterations = 900; // Enough to exhaust 32-bit address space. | 58 const intptr_t kIterations = 900; // Enough to exhaust 32-bit address space. |
59 for (intptr_t i = 0; i < kIterations; ++i) { | 59 for (intptr_t i = 0; i < kIterations; ++i) { |
60 VirtualMemory* vm = VirtualMemory::Reserve(kVirtualMemoryBlockSize); | 60 VirtualMemory* vm = VirtualMemory::Reserve(kVirtualMemoryBlockSize); |
61 vm->Commit(false); | 61 vm->Commit(false); |
62 delete vm; | 62 delete vm; |
63 } | 63 } |
64 // Check that truncation does not introduce leaks. | 64 // Check that truncation does not introduce leaks. |
65 for (intptr_t i = 0; i < kIterations; ++i) { | 65 for (intptr_t i = 0; i < kIterations; ++i) { |
(...skipping 16 matching lines...) Expand all Loading... |
82 } | 82 } |
83 for (intptr_t i = 0; i < kIterations; ++i) { | 83 for (intptr_t i = 0; i < kIterations; ++i) { |
84 VirtualMemory* vm = VirtualMemory::Reserve(kVirtualMemoryBlockSize); | 84 VirtualMemory* vm = VirtualMemory::Reserve(kVirtualMemoryBlockSize); |
85 vm->Commit(false); | 85 vm->Commit(false); |
86 vm->Truncate(0, false); | 86 vm->Truncate(0, false); |
87 delete vm; | 87 delete vm; |
88 } | 88 } |
89 } | 89 } |
90 | 90 |
91 | 91 |
92 UNIT_TEST_CASE(VirtualMemoryCommitPartial) { | 92 VM_UNIT_TEST_CASE(VirtualMemoryCommitPartial) { |
93 const intptr_t kVirtualMemoryBlockSize = 3 * MB; | 93 const intptr_t kVirtualMemoryBlockSize = 3 * MB; |
94 VirtualMemory* vm = VirtualMemory::Reserve(kVirtualMemoryBlockSize); | 94 VirtualMemory* vm = VirtualMemory::Reserve(kVirtualMemoryBlockSize); |
95 EXPECT(vm != NULL); | 95 EXPECT(vm != NULL); |
96 // Commit only the middle MB and write to it. | 96 // Commit only the middle MB and write to it. |
97 const uword commit_start = vm->start() + (1 * MB); | 97 const uword commit_start = vm->start() + (1 * MB); |
98 const intptr_t kCommitSize = 1 * MB; | 98 const intptr_t kCommitSize = 1 * MB; |
99 vm->Commit(commit_start, kCommitSize, false); | 99 vm->Commit(commit_start, kCommitSize, false); |
100 char* buf = reinterpret_cast<char*>(commit_start); | 100 char* buf = reinterpret_cast<char*>(commit_start); |
101 EXPECT(IsZero(buf, buf + kCommitSize)); | 101 EXPECT(IsZero(buf, buf + kCommitSize)); |
102 buf[0] = 'f'; | 102 buf[0] = 'f'; |
103 buf[1] = 'o'; | 103 buf[1] = 'o'; |
104 buf[2] = 'o'; | 104 buf[2] = 'o'; |
105 buf[3] = 0; | 105 buf[3] = 0; |
106 EXPECT_STREQ("foo", buf); | 106 EXPECT_STREQ("foo", buf); |
107 delete vm; | 107 delete vm; |
108 } | 108 } |
109 | 109 |
110 } // namespace dart | 110 } // namespace dart |
OLD | NEW |