| 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 DECLARE_DEBUG_FLAG(bool, trace_zones); | 13 DECLARE_DEBUG_FLAG(bool, trace_zones); |
| 14 | 14 |
| 15 UNIT_TEST_CASE(AllocateZone) { | 15 UNIT_TEST_CASE(AllocateZone) { |
| 16 #if defined(DEBUG) | 16 #if defined(DEBUG) |
| 17 FLAG_trace_zones = true; | 17 FLAG_trace_zones = true; |
| 18 #endif | 18 #endif |
| 19 Dart_CreateIsolate( | 19 Dart_CreateIsolate( |
| 20 NULL, NULL, bin::isolate_snapshot_buffer, NULL, NULL, NULL); | 20 NULL, NULL, bin::isolate_snapshot_buffer, NULL, NULL, NULL); |
| 21 Isolate* isolate = Isolate::Current(); | 21 Thread* thread = Thread::Current(); |
| 22 EXPECT(isolate->current_zone() == NULL); | 22 EXPECT(thread->zone() == NULL); |
| 23 { | 23 { |
| 24 StackZone stack_zone(isolate); | 24 StackZone stack_zone(thread); |
| 25 EXPECT(isolate->current_zone() != NULL); | 25 EXPECT(thread->zone() != NULL); |
| 26 Zone* zone = stack_zone.GetZone(); | 26 Zone* zone = stack_zone.GetZone(); |
| 27 intptr_t allocated_size = 0; | 27 intptr_t allocated_size = 0; |
| 28 | 28 |
| 29 // The loop is to make sure we overflow one segment and go on | 29 // The loop is to make sure we overflow one segment and go on |
| 30 // to the next segment. | 30 // to the next segment. |
| 31 for (int i = 0; i < 1000; i++) { | 31 for (int i = 0; i < 1000; i++) { |
| 32 uword first = zone->AllocUnsafe(2 * kWordSize); | 32 uword first = zone->AllocUnsafe(2 * kWordSize); |
| 33 uword second = zone->AllocUnsafe(3 * kWordSize); | 33 uword second = zone->AllocUnsafe(3 * kWordSize); |
| 34 EXPECT(first != second); | 34 EXPECT(first != second); |
| 35 allocated_size = ((2 + 3) * kWordSize); | 35 allocated_size = ((2 + 3) * kWordSize); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 62 allocated_size += (kSegmentSize - (2 * kWordSize)); | 62 allocated_size += (kSegmentSize - (2 * kWordSize)); |
| 63 EXPECT_LE(allocated_size, zone->SizeInBytes()); | 63 EXPECT_LE(allocated_size, zone->SizeInBytes()); |
| 64 | 64 |
| 65 buffer = reinterpret_cast<uint8_t*>( | 65 buffer = reinterpret_cast<uint8_t*>( |
| 66 zone->AllocUnsafe(kSegmentSize + kWordSize)); | 66 zone->AllocUnsafe(kSegmentSize + kWordSize)); |
| 67 EXPECT(buffer != NULL); | 67 EXPECT(buffer != NULL); |
| 68 buffer[(kSegmentSize + kWordSize) - 1] = 0; | 68 buffer[(kSegmentSize + kWordSize) - 1] = 0; |
| 69 allocated_size += (kSegmentSize + kWordSize); | 69 allocated_size += (kSegmentSize + kWordSize); |
| 70 EXPECT_LE(allocated_size, zone->SizeInBytes()); | 70 EXPECT_LE(allocated_size, zone->SizeInBytes()); |
| 71 } | 71 } |
| 72 EXPECT(isolate->current_zone() == NULL); | 72 EXPECT(thread->zone() == NULL); |
| 73 Dart_ShutdownIsolate(); | 73 Dart_ShutdownIsolate(); |
| 74 } | 74 } |
| 75 | 75 |
| 76 | 76 |
| 77 UNIT_TEST_CASE(AllocGeneric_Success) { | 77 UNIT_TEST_CASE(AllocGeneric_Success) { |
| 78 #if defined(DEBUG) | 78 #if defined(DEBUG) |
| 79 FLAG_trace_zones = true; | 79 FLAG_trace_zones = true; |
| 80 #endif | 80 #endif |
| 81 Dart_CreateIsolate( | 81 Dart_CreateIsolate( |
| 82 NULL, NULL, bin::isolate_snapshot_buffer, NULL, NULL, NULL); | 82 NULL, NULL, bin::isolate_snapshot_buffer, NULL, NULL, NULL); |
| 83 Isolate* isolate = Isolate::Current(); | 83 Thread* thread = Thread::Current(); |
| 84 EXPECT(isolate->current_zone() == NULL); | 84 EXPECT(thread->zone() == NULL); |
| 85 { | 85 { |
| 86 StackZone zone(isolate); | 86 StackZone zone(thread); |
| 87 EXPECT(isolate->current_zone() != NULL); | 87 EXPECT(thread->zone() != NULL); |
| 88 intptr_t allocated_size = 0; | 88 intptr_t allocated_size = 0; |
| 89 | 89 |
| 90 const intptr_t kNumElements = 1000; | 90 const intptr_t kNumElements = 1000; |
| 91 zone.GetZone()->Alloc<uint32_t>(kNumElements); | 91 zone.GetZone()->Alloc<uint32_t>(kNumElements); |
| 92 allocated_size += sizeof(uint32_t) * kNumElements; | 92 allocated_size += sizeof(uint32_t) * kNumElements; |
| 93 EXPECT_LE(allocated_size, zone.SizeInBytes()); | 93 EXPECT_LE(allocated_size, zone.SizeInBytes()); |
| 94 } | 94 } |
| 95 EXPECT(isolate->current_zone() == NULL); | 95 EXPECT(thread->zone() == NULL); |
| 96 Dart_ShutdownIsolate(); | 96 Dart_ShutdownIsolate(); |
| 97 } | 97 } |
| 98 | 98 |
| 99 | 99 |
| 100 // This test is expected to crash. | 100 // This test is expected to crash. |
| 101 UNIT_TEST_CASE(AllocGeneric_Overflow) { | 101 UNIT_TEST_CASE(AllocGeneric_Overflow) { |
| 102 #if defined(DEBUG) | 102 #if defined(DEBUG) |
| 103 FLAG_trace_zones = true; | 103 FLAG_trace_zones = true; |
| 104 #endif | 104 #endif |
| 105 Dart_CreateIsolate( | 105 Dart_CreateIsolate( |
| 106 NULL, NULL, bin::isolate_snapshot_buffer, NULL, NULL, NULL); | 106 NULL, NULL, bin::isolate_snapshot_buffer, NULL, NULL, NULL); |
| 107 Isolate* isolate = Isolate::Current(); | 107 Thread* thread = Thread::Current(); |
| 108 EXPECT(isolate->current_zone() == NULL); | 108 EXPECT(thread->zone() == NULL); |
| 109 { | 109 { |
| 110 StackZone zone(isolate); | 110 StackZone zone(thread); |
| 111 EXPECT(isolate->current_zone() != NULL); | 111 EXPECT(thread->zone() != NULL); |
| 112 | 112 |
| 113 const intptr_t kNumElements = (kIntptrMax / sizeof(uint32_t)) + 1; | 113 const intptr_t kNumElements = (kIntptrMax / sizeof(uint32_t)) + 1; |
| 114 zone.GetZone()->Alloc<uint32_t>(kNumElements); | 114 zone.GetZone()->Alloc<uint32_t>(kNumElements); |
| 115 } | 115 } |
| 116 Dart_ShutdownIsolate(); | 116 Dart_ShutdownIsolate(); |
| 117 } | 117 } |
| 118 | 118 |
| 119 | 119 |
| 120 UNIT_TEST_CASE(ZoneAllocated) { | 120 UNIT_TEST_CASE(ZoneAllocated) { |
| 121 #if defined(DEBUG) | 121 #if defined(DEBUG) |
| 122 FLAG_trace_zones = true; | 122 FLAG_trace_zones = true; |
| 123 #endif | 123 #endif |
| 124 Dart_CreateIsolate( | 124 Dart_CreateIsolate( |
| 125 NULL, NULL, bin::isolate_snapshot_buffer, NULL, NULL, NULL); | 125 NULL, NULL, bin::isolate_snapshot_buffer, NULL, NULL, NULL); |
| 126 Isolate* isolate = Isolate::Current(); | 126 Thread* thread = Thread::Current(); |
| 127 EXPECT(isolate->current_zone() == NULL); | 127 EXPECT(thread->zone() == NULL); |
| 128 static int marker; | 128 static int marker; |
| 129 | 129 |
| 130 class SimpleZoneObject : public ZoneAllocated { | 130 class SimpleZoneObject : public ZoneAllocated { |
| 131 public: | 131 public: |
| 132 SimpleZoneObject() : slot(marker++) { } | 132 SimpleZoneObject() : slot(marker++) { } |
| 133 virtual ~SimpleZoneObject() { } | 133 virtual ~SimpleZoneObject() { } |
| 134 virtual int GetSlot() { return slot; } | 134 virtual int GetSlot() { return slot; } |
| 135 int slot; | 135 int slot; |
| 136 }; | 136 }; |
| 137 | 137 |
| 138 // Reset the marker. | 138 // Reset the marker. |
| 139 marker = 0; | 139 marker = 0; |
| 140 | 140 |
| 141 // Create a few zone allocated objects. | 141 // Create a few zone allocated objects. |
| 142 { | 142 { |
| 143 StackZone zone(isolate); | 143 StackZone zone(thread); |
| 144 EXPECT_EQ(0, zone.SizeInBytes()); | 144 EXPECT_EQ(0, zone.SizeInBytes()); |
| 145 SimpleZoneObject* first = new SimpleZoneObject(); | 145 SimpleZoneObject* first = new SimpleZoneObject(); |
| 146 EXPECT(first != NULL); | 146 EXPECT(first != NULL); |
| 147 SimpleZoneObject* second = new SimpleZoneObject(); | 147 SimpleZoneObject* second = new SimpleZoneObject(); |
| 148 EXPECT(second != NULL); | 148 EXPECT(second != NULL); |
| 149 EXPECT(first != second); | 149 EXPECT(first != second); |
| 150 intptr_t expected_size = (2 * sizeof(SimpleZoneObject)); | 150 intptr_t expected_size = (2 * sizeof(SimpleZoneObject)); |
| 151 EXPECT_LE(expected_size, zone.SizeInBytes()); | 151 EXPECT_LE(expected_size, zone.SizeInBytes()); |
| 152 | 152 |
| 153 // Make sure the constructors were invoked. | 153 // Make sure the constructors were invoked. |
| 154 EXPECT_EQ(0, first->slot); | 154 EXPECT_EQ(0, first->slot); |
| 155 EXPECT_EQ(1, second->slot); | 155 EXPECT_EQ(1, second->slot); |
| 156 | 156 |
| 157 // Make sure we can write to the members of the zone objects. | 157 // Make sure we can write to the members of the zone objects. |
| 158 first->slot = 42; | 158 first->slot = 42; |
| 159 second->slot = 87; | 159 second->slot = 87; |
| 160 EXPECT_EQ(42, first->slot); | 160 EXPECT_EQ(42, first->slot); |
| 161 EXPECT_EQ(87, second->slot); | 161 EXPECT_EQ(87, second->slot); |
| 162 } | 162 } |
| 163 EXPECT(isolate->current_zone() == NULL); | 163 EXPECT(thread->zone() == NULL); |
| 164 Dart_ShutdownIsolate(); | 164 Dart_ShutdownIsolate(); |
| 165 } | 165 } |
| 166 | 166 |
| 167 | 167 |
| 168 TEST_CASE(PrintToString) { | 168 TEST_CASE(PrintToString) { |
| 169 StackZone zone(Thread::Current()); | 169 StackZone zone(Thread::Current()); |
| 170 const char* result = zone.GetZone()->PrintToString("Hello %s!", "World"); | 170 const char* result = zone.GetZone()->PrintToString("Hello %s!", "World"); |
| 171 EXPECT_STREQ("Hello World!", result); | 171 EXPECT_STREQ("Hello World!", result); |
| 172 } | 172 } |
| 173 | 173 |
| 174 } // namespace dart | 174 } // namespace dart |
| OLD | NEW |