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_zone_sizes); | 13 DECLARE_DEBUG_FLAG(bool, trace_zone_sizes); |
14 | 14 |
15 UNIT_TEST_CASE(AllocateZone) { | 15 UNIT_TEST_CASE(AllocateZone) { |
16 #if defined(DEBUG) | 16 #if defined(DEBUG) |
17 FLAG_trace_zone_sizes = true; | 17 FLAG_trace_zone_sizes = true; |
18 #endif | 18 #endif |
19 Isolate* isolate = Isolate::Init(NULL); | 19 Isolate* isolate = Isolate::Init(NULL); |
20 EXPECT(Isolate::Current() == isolate); | 20 EXPECT(Isolate::Current() == isolate); |
21 EXPECT(isolate->current_zone() == NULL); | 21 EXPECT(isolate->current_zone() == NULL); |
22 { | 22 { |
23 StackZone zone(isolate); | 23 StackZone stack_zone(isolate); |
24 EXPECT(isolate->current_zone() != NULL); | 24 EXPECT(isolate->current_zone() != NULL); |
| 25 Zone* zone = stack_zone.GetZone(); |
25 intptr_t allocated_size = 0; | 26 intptr_t allocated_size = 0; |
26 | 27 |
27 // The loop is to make sure we overflow one segment and go on | 28 // The loop is to make sure we overflow one segment and go on |
28 // to the next segment. | 29 // to the next segment. |
29 for (int i = 0; i < 1000; i++) { | 30 for (int i = 0; i < 1000; i++) { |
30 uword first = zone.AllocUnsafe(2 * kWordSize); | 31 uword first = zone->AllocUnsafe(2 * kWordSize); |
31 uword second = zone.AllocUnsafe(3 * kWordSize); | 32 uword second = zone->AllocUnsafe(3 * kWordSize); |
32 EXPECT(first != second); | 33 EXPECT(first != second); |
33 allocated_size = ((2 + 3) * kWordSize); | 34 allocated_size = ((2 + 3) * kWordSize); |
34 } | 35 } |
35 EXPECT_LE(allocated_size, zone.SizeInBytes()); | 36 EXPECT_LE(allocated_size, zone->SizeInBytes()); |
36 | 37 |
37 // Test for allocation of large segments. | 38 // Test for allocation of large segments. |
38 const uword kLargeSize = 1 * MB; | 39 const uword kLargeSize = 1 * MB; |
39 const uword kSegmentSize = 64 * KB; | 40 const uword kSegmentSize = 64 * KB; |
40 ASSERT(kLargeSize > kSegmentSize); | 41 ASSERT(kLargeSize > kSegmentSize); |
41 for (int i = 0; i < 10; i++) { | 42 for (int i = 0; i < 10; i++) { |
42 EXPECT(zone.AllocUnsafe(kLargeSize) != 0); | 43 EXPECT(zone->AllocUnsafe(kLargeSize) != 0); |
43 allocated_size += kLargeSize; | 44 allocated_size += kLargeSize; |
44 } | 45 } |
45 EXPECT_LE(allocated_size, zone.SizeInBytes()); | 46 EXPECT_LE(allocated_size, zone->SizeInBytes()); |
46 | 47 |
47 // Test corner cases of kSegmentSize. | 48 // Test corner cases of kSegmentSize. |
48 uint8_t* buffer = NULL; | 49 uint8_t* buffer = NULL; |
49 buffer = reinterpret_cast<uint8_t*>( | 50 buffer = reinterpret_cast<uint8_t*>( |
50 zone.AllocUnsafe(kSegmentSize - kWordSize)); | 51 zone->AllocUnsafe(kSegmentSize - kWordSize)); |
51 EXPECT(buffer != NULL); | 52 EXPECT(buffer != NULL); |
52 buffer[(kSegmentSize - kWordSize) - 1] = 0; | 53 buffer[(kSegmentSize - kWordSize) - 1] = 0; |
53 allocated_size += (kSegmentSize - kWordSize); | 54 allocated_size += (kSegmentSize - kWordSize); |
54 EXPECT_LE(allocated_size, zone.SizeInBytes()); | 55 EXPECT_LE(allocated_size, zone->SizeInBytes()); |
55 | 56 |
56 buffer = reinterpret_cast<uint8_t*>( | 57 buffer = reinterpret_cast<uint8_t*>( |
57 zone.AllocUnsafe(kSegmentSize - (2 * kWordSize))); | 58 zone->AllocUnsafe(kSegmentSize - (2 * kWordSize))); |
58 EXPECT(buffer != NULL); | 59 EXPECT(buffer != NULL); |
59 buffer[(kSegmentSize - (2 * kWordSize)) - 1] = 0; | 60 buffer[(kSegmentSize - (2 * kWordSize)) - 1] = 0; |
60 allocated_size += (kSegmentSize - (2 * kWordSize)); | 61 allocated_size += (kSegmentSize - (2 * kWordSize)); |
61 EXPECT_LE(allocated_size, zone.SizeInBytes()); | 62 EXPECT_LE(allocated_size, zone->SizeInBytes()); |
62 | 63 |
63 buffer = reinterpret_cast<uint8_t*>( | 64 buffer = reinterpret_cast<uint8_t*>( |
64 zone.AllocUnsafe(kSegmentSize + kWordSize)); | 65 zone->AllocUnsafe(kSegmentSize + kWordSize)); |
65 EXPECT(buffer != NULL); | 66 EXPECT(buffer != NULL); |
66 buffer[(kSegmentSize + kWordSize) - 1] = 0; | 67 buffer[(kSegmentSize + kWordSize) - 1] = 0; |
67 allocated_size += (kSegmentSize + kWordSize); | 68 allocated_size += (kSegmentSize + kWordSize); |
68 EXPECT_LE(allocated_size, zone.SizeInBytes()); | 69 EXPECT_LE(allocated_size, zone->SizeInBytes()); |
69 } | 70 } |
70 EXPECT(isolate->current_zone() == NULL); | 71 EXPECT(isolate->current_zone() == NULL); |
71 isolate->Shutdown(); | 72 isolate->Shutdown(); |
72 delete isolate; | 73 delete isolate; |
73 } | 74 } |
74 | 75 |
75 | 76 |
76 UNIT_TEST_CASE(AllocGeneric_Success) { | 77 UNIT_TEST_CASE(AllocGeneric_Success) { |
77 #if defined(DEBUG) | 78 #if defined(DEBUG) |
78 FLAG_trace_zone_sizes = true; | 79 FLAG_trace_zone_sizes = true; |
79 #endif | 80 #endif |
80 Isolate* isolate = Isolate::Init(NULL); | 81 Isolate* isolate = Isolate::Init(NULL); |
81 EXPECT(Isolate::Current() == isolate); | 82 EXPECT(Isolate::Current() == isolate); |
82 EXPECT(isolate->current_zone() == NULL); | 83 EXPECT(isolate->current_zone() == NULL); |
83 { | 84 { |
84 StackZone zone(isolate); | 85 StackZone zone(isolate); |
85 EXPECT(isolate->current_zone() != NULL); | 86 EXPECT(isolate->current_zone() != NULL); |
86 intptr_t allocated_size = 0; | 87 intptr_t allocated_size = 0; |
87 | 88 |
88 const intptr_t kNumElements = 1000; | 89 const intptr_t kNumElements = 1000; |
89 zone.Alloc<uint32_t>(kNumElements); | 90 zone.GetZone()->Alloc<uint32_t>(kNumElements); |
90 allocated_size += sizeof(uint32_t) * kNumElements; | 91 allocated_size += sizeof(uint32_t) * kNumElements; |
91 EXPECT_LE(allocated_size, zone.SizeInBytes()); | 92 EXPECT_LE(allocated_size, zone.SizeInBytes()); |
92 } | 93 } |
93 EXPECT(isolate->current_zone() == NULL); | 94 EXPECT(isolate->current_zone() == NULL); |
94 isolate->Shutdown(); | 95 isolate->Shutdown(); |
95 delete isolate; | 96 delete isolate; |
96 } | 97 } |
97 | 98 |
98 | 99 |
99 // This test is expected to crash. | 100 // This test is expected to crash. |
100 UNIT_TEST_CASE(AllocGeneric_Overflow) { | 101 UNIT_TEST_CASE(AllocGeneric_Overflow) { |
101 #if defined(DEBUG) | 102 #if defined(DEBUG) |
102 FLAG_trace_zone_sizes = true; | 103 FLAG_trace_zone_sizes = true; |
103 #endif | 104 #endif |
104 Isolate* isolate = Isolate::Init(NULL); | 105 Isolate* isolate = Isolate::Init(NULL); |
105 EXPECT(Isolate::Current() == isolate); | 106 EXPECT(Isolate::Current() == isolate); |
106 EXPECT(isolate->current_zone() == NULL); | 107 EXPECT(isolate->current_zone() == NULL); |
107 { | 108 { |
108 StackZone zone(isolate); | 109 StackZone zone(isolate); |
109 EXPECT(isolate->current_zone() != NULL); | 110 EXPECT(isolate->current_zone() != NULL); |
110 | 111 |
111 const intptr_t kNumElements = (kIntptrMax / sizeof(uint32_t)) + 1; | 112 const intptr_t kNumElements = (kIntptrMax / sizeof(uint32_t)) + 1; |
112 zone.Alloc<uint32_t>(kNumElements); | 113 zone.GetZone()->Alloc<uint32_t>(kNumElements); |
113 } | 114 } |
114 isolate->Shutdown(); | 115 isolate->Shutdown(); |
115 delete isolate; | 116 delete isolate; |
116 } | 117 } |
117 | 118 |
118 | 119 |
119 UNIT_TEST_CASE(ZoneAllocated) { | 120 UNIT_TEST_CASE(ZoneAllocated) { |
120 #if defined(DEBUG) | 121 #if defined(DEBUG) |
121 FLAG_trace_zone_sizes = true; | 122 FLAG_trace_zone_sizes = true; |
122 #endif | 123 #endif |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 EXPECT_EQ(87, second->slot); | 159 EXPECT_EQ(87, second->slot); |
159 } | 160 } |
160 EXPECT(isolate->current_zone() == NULL); | 161 EXPECT(isolate->current_zone() == NULL); |
161 isolate->Shutdown(); | 162 isolate->Shutdown(); |
162 delete isolate; | 163 delete isolate; |
163 } | 164 } |
164 | 165 |
165 | 166 |
166 TEST_CASE(PrintToString) { | 167 TEST_CASE(PrintToString) { |
167 StackZone zone(Isolate::Current()); | 168 StackZone zone(Isolate::Current()); |
168 const char* result = zone.PrintToString("Hello %s!", "World"); | 169 const char* result = zone.GetZone()->PrintToString("Hello %s!", "World"); |
169 EXPECT_STREQ("Hello World!", result); | 170 EXPECT_STREQ("Hello World!", result); |
170 } | 171 } |
171 | 172 |
172 } // namespace dart | 173 } // namespace dart |
OLD | NEW |