| 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 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 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)); |
| 32 EXPECT(vm->Contains(vm->start() + (kVirtualMemoryBlockSize / 2))); | 32 EXPECT(vm->Contains(vm->start() + (kVirtualMemoryBlockSize / 2))); |
| 33 EXPECT(!vm->Contains(vm->start() - 1)); | 33 EXPECT(!vm->Contains(vm->start() - 1)); |
| 34 EXPECT(!vm->Contains(vm->end())); | 34 EXPECT(!vm->Contains(vm->end())); |
| 35 EXPECT(!vm->Contains(vm->end() + 1)); | 35 EXPECT(!vm->Contains(vm->end() + 1)); |
| 36 EXPECT(!vm->Contains(0)); | 36 EXPECT(!vm->Contains(0)); |
| 37 EXPECT(!vm->Contains(static_cast<uword>(-1))); | 37 EXPECT(!vm->Contains(static_cast<uword>(-1))); |
| 38 | 38 |
| 39 vm->Commit(false); | 39 vm->Commit(false, NULL); |
| 40 | 40 |
| 41 char* buf = reinterpret_cast<char*>(vm->address()); | 41 char* buf = reinterpret_cast<char*>(vm->address()); |
| 42 EXPECT(IsZero(buf, buf + vm->size())); | 42 EXPECT(IsZero(buf, buf + vm->size())); |
| 43 buf[0] = 'a'; | 43 buf[0] = 'a'; |
| 44 buf[1] = 'c'; | 44 buf[1] = 'c'; |
| 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 VM_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, NULL); |
| 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) { |
| 66 VirtualMemory* vm = VirtualMemory::Reserve(kVirtualMemoryBlockSize); | 66 VirtualMemory* vm = VirtualMemory::Reserve(kVirtualMemoryBlockSize); |
| 67 vm->Commit(false); | 67 vm->Commit(false, NULL); |
| 68 vm->Truncate(kVirtualMemoryBlockSize / 2, true); | 68 vm->Truncate(kVirtualMemoryBlockSize / 2, true); |
| 69 delete vm; | 69 delete vm; |
| 70 } | 70 } |
| 71 for (intptr_t i = 0; i < kIterations; ++i) { | 71 for (intptr_t i = 0; i < kIterations; ++i) { |
| 72 VirtualMemory* vm = VirtualMemory::Reserve(kVirtualMemoryBlockSize); | 72 VirtualMemory* vm = VirtualMemory::Reserve(kVirtualMemoryBlockSize); |
| 73 vm->Commit(true); | 73 vm->Commit(true, NULL); |
| 74 vm->Truncate(kVirtualMemoryBlockSize / 2, false); | 74 vm->Truncate(kVirtualMemoryBlockSize / 2, false); |
| 75 delete vm; | 75 delete vm; |
| 76 } | 76 } |
| 77 for (intptr_t i = 0; i < kIterations; ++i) { | 77 for (intptr_t i = 0; i < kIterations; ++i) { |
| 78 VirtualMemory* vm = VirtualMemory::Reserve(kVirtualMemoryBlockSize); | 78 VirtualMemory* vm = VirtualMemory::Reserve(kVirtualMemoryBlockSize); |
| 79 vm->Commit(true); | 79 vm->Commit(true, NULL); |
| 80 vm->Truncate(0, true); | 80 vm->Truncate(0, true); |
| 81 delete vm; | 81 delete vm; |
| 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, NULL); |
| 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 VM_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, NULL); |
| 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 |