| 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/dart.h" | 6 #include "vm/dart.h" |
| 7 #include "vm/isolate.h" | 7 #include "vm/isolate.h" |
| 8 #include "vm/unit_test.h" | 8 #include "vm/unit_test.h" |
| 9 #include "vm/zone.h" | 9 #include "vm/zone.h" |
| 10 | 10 |
| 11 namespace dart { | 11 namespace dart { |
| 12 | 12 |
| 13 UNIT_TEST_CASE(AllocateZone) { | 13 UNIT_TEST_CASE(AllocateZone) { |
| 14 #if defined(DEBUG) | 14 #if defined(DEBUG) |
| 15 FLAG_trace_zones = true; | 15 FLAG_trace_zones = true; |
| 16 #endif | 16 #endif |
| 17 Dart_CreateIsolate( | 17 Dart_CreateIsolate(NULL, NULL, bin::isolate_snapshot_buffer, NULL, NULL, |
| 18 NULL, NULL, bin::isolate_snapshot_buffer, NULL, NULL, NULL); | 18 NULL); |
| 19 Thread* thread = Thread::Current(); | 19 Thread* thread = Thread::Current(); |
| 20 EXPECT(thread->zone() == NULL); | 20 EXPECT(thread->zone() == NULL); |
| 21 { | 21 { |
| 22 StackZone stack_zone(thread); | 22 StackZone stack_zone(thread); |
| 23 EXPECT(thread->zone() != NULL); | 23 EXPECT(thread->zone() != NULL); |
| 24 Zone* zone = stack_zone.GetZone(); | 24 Zone* zone = stack_zone.GetZone(); |
| 25 intptr_t allocated_size = 0; | 25 intptr_t allocated_size = 0; |
| 26 | 26 |
| 27 // The loop is to make sure we overflow one segment and go on | 27 // The loop is to make sure we overflow one segment and go on |
| 28 // to the next segment. | 28 // to the next segment. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 39 const uword kSegmentSize = 64 * KB; | 39 const uword kSegmentSize = 64 * KB; |
| 40 ASSERT(kLargeSize > kSegmentSize); | 40 ASSERT(kLargeSize > kSegmentSize); |
| 41 for (int i = 0; i < 10; i++) { | 41 for (int i = 0; i < 10; i++) { |
| 42 EXPECT(zone->AllocUnsafe(kLargeSize) != 0); | 42 EXPECT(zone->AllocUnsafe(kLargeSize) != 0); |
| 43 allocated_size += kLargeSize; | 43 allocated_size += kLargeSize; |
| 44 } | 44 } |
| 45 EXPECT_LE(allocated_size, zone->SizeInBytes()); | 45 EXPECT_LE(allocated_size, zone->SizeInBytes()); |
| 46 | 46 |
| 47 // Test corner cases of kSegmentSize. | 47 // Test corner cases of kSegmentSize. |
| 48 uint8_t* buffer = NULL; | 48 uint8_t* buffer = NULL; |
| 49 buffer = reinterpret_cast<uint8_t*>( | 49 buffer = |
| 50 zone->AllocUnsafe(kSegmentSize - kWordSize)); | 50 reinterpret_cast<uint8_t*>(zone->AllocUnsafe(kSegmentSize - kWordSize)); |
| 51 EXPECT(buffer != NULL); | 51 EXPECT(buffer != NULL); |
| 52 buffer[(kSegmentSize - kWordSize) - 1] = 0; | 52 buffer[(kSegmentSize - kWordSize) - 1] = 0; |
| 53 allocated_size += (kSegmentSize - kWordSize); | 53 allocated_size += (kSegmentSize - kWordSize); |
| 54 EXPECT_LE(allocated_size, zone->SizeInBytes()); | 54 EXPECT_LE(allocated_size, zone->SizeInBytes()); |
| 55 | 55 |
| 56 buffer = reinterpret_cast<uint8_t*>( | 56 buffer = reinterpret_cast<uint8_t*>( |
| 57 zone->AllocUnsafe(kSegmentSize - (2 * kWordSize))); | 57 zone->AllocUnsafe(kSegmentSize - (2 * kWordSize))); |
| 58 EXPECT(buffer != NULL); | 58 EXPECT(buffer != NULL); |
| 59 buffer[(kSegmentSize - (2 * kWordSize)) - 1] = 0; | 59 buffer[(kSegmentSize - (2 * kWordSize)) - 1] = 0; |
| 60 allocated_size += (kSegmentSize - (2 * kWordSize)); | 60 allocated_size += (kSegmentSize - (2 * kWordSize)); |
| 61 EXPECT_LE(allocated_size, zone->SizeInBytes()); | 61 EXPECT_LE(allocated_size, zone->SizeInBytes()); |
| 62 | 62 |
| 63 buffer = reinterpret_cast<uint8_t*>( | 63 buffer = |
| 64 zone->AllocUnsafe(kSegmentSize + kWordSize)); | 64 reinterpret_cast<uint8_t*>(zone->AllocUnsafe(kSegmentSize + kWordSize)); |
| 65 EXPECT(buffer != NULL); | 65 EXPECT(buffer != NULL); |
| 66 buffer[(kSegmentSize + kWordSize) - 1] = 0; | 66 buffer[(kSegmentSize + kWordSize) - 1] = 0; |
| 67 allocated_size += (kSegmentSize + kWordSize); | 67 allocated_size += (kSegmentSize + kWordSize); |
| 68 EXPECT_LE(allocated_size, zone->SizeInBytes()); | 68 EXPECT_LE(allocated_size, zone->SizeInBytes()); |
| 69 } | 69 } |
| 70 EXPECT(thread->zone() == NULL); | 70 EXPECT(thread->zone() == NULL); |
| 71 Dart_ShutdownIsolate(); | 71 Dart_ShutdownIsolate(); |
| 72 } | 72 } |
| 73 | 73 |
| 74 | 74 |
| 75 UNIT_TEST_CASE(AllocGeneric_Success) { | 75 UNIT_TEST_CASE(AllocGeneric_Success) { |
| 76 #if defined(DEBUG) | 76 #if defined(DEBUG) |
| 77 FLAG_trace_zones = true; | 77 FLAG_trace_zones = true; |
| 78 #endif | 78 #endif |
| 79 Dart_CreateIsolate( | 79 Dart_CreateIsolate(NULL, NULL, bin::isolate_snapshot_buffer, NULL, NULL, |
| 80 NULL, NULL, bin::isolate_snapshot_buffer, NULL, NULL, NULL); | 80 NULL); |
| 81 Thread* thread = Thread::Current(); | 81 Thread* thread = Thread::Current(); |
| 82 EXPECT(thread->zone() == NULL); | 82 EXPECT(thread->zone() == NULL); |
| 83 { | 83 { |
| 84 StackZone zone(thread); | 84 StackZone zone(thread); |
| 85 EXPECT(thread->zone() != NULL); | 85 EXPECT(thread->zone() != NULL); |
| 86 intptr_t allocated_size = 0; | 86 intptr_t allocated_size = 0; |
| 87 | 87 |
| 88 const intptr_t kNumElements = 1000; | 88 const intptr_t kNumElements = 1000; |
| 89 zone.GetZone()->Alloc<uint32_t>(kNumElements); | 89 zone.GetZone()->Alloc<uint32_t>(kNumElements); |
| 90 allocated_size += sizeof(uint32_t) * kNumElements; | 90 allocated_size += sizeof(uint32_t) * kNumElements; |
| 91 EXPECT_LE(allocated_size, zone.SizeInBytes()); | 91 EXPECT_LE(allocated_size, zone.SizeInBytes()); |
| 92 } | 92 } |
| 93 EXPECT(thread->zone() == NULL); | 93 EXPECT(thread->zone() == NULL); |
| 94 Dart_ShutdownIsolate(); | 94 Dart_ShutdownIsolate(); |
| 95 } | 95 } |
| 96 | 96 |
| 97 | 97 |
| 98 // This test is expected to crash. | 98 // This test is expected to crash. |
| 99 UNIT_TEST_CASE(AllocGeneric_Overflow) { | 99 UNIT_TEST_CASE(AllocGeneric_Overflow) { |
| 100 #if defined(DEBUG) | 100 #if defined(DEBUG) |
| 101 FLAG_trace_zones = true; | 101 FLAG_trace_zones = true; |
| 102 #endif | 102 #endif |
| 103 Dart_CreateIsolate( | 103 Dart_CreateIsolate(NULL, NULL, bin::isolate_snapshot_buffer, NULL, NULL, |
| 104 NULL, NULL, bin::isolate_snapshot_buffer, NULL, NULL, NULL); | 104 NULL); |
| 105 Thread* thread = Thread::Current(); | 105 Thread* thread = Thread::Current(); |
| 106 EXPECT(thread->zone() == NULL); | 106 EXPECT(thread->zone() == NULL); |
| 107 { | 107 { |
| 108 StackZone zone(thread); | 108 StackZone zone(thread); |
| 109 EXPECT(thread->zone() != NULL); | 109 EXPECT(thread->zone() != NULL); |
| 110 | 110 |
| 111 const intptr_t kNumElements = (kIntptrMax / sizeof(uint32_t)) + 1; | 111 const intptr_t kNumElements = (kIntptrMax / sizeof(uint32_t)) + 1; |
| 112 zone.GetZone()->Alloc<uint32_t>(kNumElements); | 112 zone.GetZone()->Alloc<uint32_t>(kNumElements); |
| 113 } | 113 } |
| 114 Dart_ShutdownIsolate(); | 114 Dart_ShutdownIsolate(); |
| 115 } | 115 } |
| 116 | 116 |
| 117 | 117 |
| 118 UNIT_TEST_CASE(ZoneAllocated) { | 118 UNIT_TEST_CASE(ZoneAllocated) { |
| 119 #if defined(DEBUG) | 119 #if defined(DEBUG) |
| 120 FLAG_trace_zones = true; | 120 FLAG_trace_zones = true; |
| 121 #endif | 121 #endif |
| 122 Dart_CreateIsolate( | 122 Dart_CreateIsolate(NULL, NULL, bin::isolate_snapshot_buffer, NULL, NULL, |
| 123 NULL, NULL, bin::isolate_snapshot_buffer, NULL, NULL, NULL); | 123 NULL); |
| 124 Thread* thread = Thread::Current(); | 124 Thread* thread = Thread::Current(); |
| 125 EXPECT(thread->zone() == NULL); | 125 EXPECT(thread->zone() == NULL); |
| 126 static int marker; | 126 static int marker; |
| 127 | 127 |
| 128 class SimpleZoneObject : public ZoneAllocated { | 128 class SimpleZoneObject : public ZoneAllocated { |
| 129 public: | 129 public: |
| 130 SimpleZoneObject() : slot(marker++) { } | 130 SimpleZoneObject() : slot(marker++) {} |
| 131 virtual ~SimpleZoneObject() { } | 131 virtual ~SimpleZoneObject() {} |
| 132 virtual int GetSlot() { return slot; } | 132 virtual int GetSlot() { return slot; } |
| 133 int slot; | 133 int slot; |
| 134 }; | 134 }; |
| 135 | 135 |
| 136 // Reset the marker. | 136 // Reset the marker. |
| 137 marker = 0; | 137 marker = 0; |
| 138 | 138 |
| 139 // Create a few zone allocated objects. | 139 // Create a few zone allocated objects. |
| 140 { | 140 { |
| 141 StackZone zone(thread); | 141 StackZone zone(thread); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 163 } | 163 } |
| 164 | 164 |
| 165 | 165 |
| 166 TEST_CASE(PrintToString) { | 166 TEST_CASE(PrintToString) { |
| 167 StackZone zone(Thread::Current()); | 167 StackZone zone(Thread::Current()); |
| 168 const char* result = zone.GetZone()->PrintToString("Hello %s!", "World"); | 168 const char* result = zone.GetZone()->PrintToString("Hello %s!", "World"); |
| 169 EXPECT_STREQ("Hello World!", result); | 169 EXPECT_STREQ("Hello World!", result); |
| 170 } | 170 } |
| 171 | 171 |
| 172 } // namespace dart | 172 } // namespace dart |
| OLD | NEW |