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 Zone zone(isolate); | 23 Zone zone(isolate); |
24 EXPECT(isolate->current_zone() != NULL); | 24 EXPECT(isolate->current_zone() != NULL); |
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. |
29 for (int i = 0; i < 1000; i++) { | 29 for (int i = 0; i < 1000; i++) { |
30 uword first = zone.Allocate(2 * kWordSize); | 30 uword first = zone.AllocUnsafe(2 * kWordSize); |
31 uword second = zone.Allocate(3 * kWordSize); | 31 uword second = zone.AllocUnsafe(3 * kWordSize); |
32 EXPECT(first != second); | 32 EXPECT(first != second); |
33 allocated_size = ((2 + 3) * kWordSize); | 33 allocated_size = ((2 + 3) * kWordSize); |
34 } | 34 } |
35 EXPECT_LE(allocated_size, zone.SizeInBytes()); | 35 EXPECT_LE(allocated_size, zone.SizeInBytes()); |
36 | 36 |
37 // Test for allocation of large segments. | 37 // Test for allocation of large segments. |
38 const uword kLargeSize = 1 * MB; | 38 const uword kLargeSize = 1 * MB; |
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.Allocate(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 = reinterpret_cast<uint8_t*>( |
50 zone.Allocate(kSegmentSize - kWordSize)); | 50 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.Allocate(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 = reinterpret_cast<uint8_t*>( |
64 zone.Allocate(kSegmentSize + kWordSize)); | 64 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(isolate->current_zone() == NULL); | 70 EXPECT(isolate->current_zone() == NULL); |
71 isolate->Shutdown(); | 71 isolate->Shutdown(); |
72 delete isolate; | 72 delete isolate; |
73 } | 73 } |
74 | 74 |
75 | 75 |
| 76 UNIT_TEST_CASE(AllocGeneric_Success) { |
| 77 #if defined(DEBUG) |
| 78 FLAG_trace_zone_sizes = true; |
| 79 #endif |
| 80 Isolate* isolate = Isolate::Init(NULL); |
| 81 EXPECT(Isolate::Current() == isolate); |
| 82 EXPECT(isolate->current_zone() == NULL); |
| 83 { |
| 84 Zone zone(isolate); |
| 85 EXPECT(isolate->current_zone() != NULL); |
| 86 intptr_t allocated_size = 0; |
| 87 |
| 88 const intptr_t kNumElements = 1000; |
| 89 zone.Alloc<uint32_t>(kNumElements); |
| 90 allocated_size += sizeof(uint32_t) * kNumElements; |
| 91 EXPECT_LE(allocated_size, zone.SizeInBytes()); |
| 92 } |
| 93 EXPECT(isolate->current_zone() == NULL); |
| 94 isolate->Shutdown(); |
| 95 delete isolate; |
| 96 } |
| 97 |
| 98 |
| 99 // This test is expected to crash. |
| 100 UNIT_TEST_CASE(AllocGeneric_Overflow) { |
| 101 #if defined(DEBUG) |
| 102 FLAG_trace_zone_sizes = true; |
| 103 #endif |
| 104 Isolate* isolate = Isolate::Init(NULL); |
| 105 EXPECT(Isolate::Current() == isolate); |
| 106 EXPECT(isolate->current_zone() == NULL); |
| 107 { |
| 108 Zone zone(isolate); |
| 109 EXPECT(isolate->current_zone() != NULL); |
| 110 |
| 111 const intptr_t kNumElements = (kIntptrMax / sizeof(uint32_t)) + 1; |
| 112 zone.Alloc<uint32_t>(kNumElements); |
| 113 } |
| 114 isolate->Shutdown(); |
| 115 delete isolate; |
| 116 } |
| 117 |
| 118 |
76 UNIT_TEST_CASE(ZoneAllocated) { | 119 UNIT_TEST_CASE(ZoneAllocated) { |
77 #if defined(DEBUG) | 120 #if defined(DEBUG) |
78 FLAG_trace_zone_sizes = true; | 121 FLAG_trace_zone_sizes = true; |
79 #endif | 122 #endif |
80 Isolate* isolate = Isolate::Init(NULL); | 123 Isolate* isolate = Isolate::Init(NULL); |
81 EXPECT(Isolate::Current() == isolate); | 124 EXPECT(Isolate::Current() == isolate); |
82 EXPECT(isolate->current_zone() == NULL); | 125 EXPECT(isolate->current_zone() == NULL); |
83 static int marker; | 126 static int marker; |
84 | 127 |
85 class SimpleZoneObject : public ZoneAllocated { | 128 class SimpleZoneObject : public ZoneAllocated { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 } | 163 } |
121 | 164 |
122 | 165 |
123 TEST_CASE(PrintToString) { | 166 TEST_CASE(PrintToString) { |
124 Zone zone(Isolate::Current()); | 167 Zone zone(Isolate::Current()); |
125 const char* result = zone.PrintToString("Hello %s!", "World"); | 168 const char* result = zone.PrintToString("Hello %s!", "World"); |
126 EXPECT_STREQ("Hello World!", result); | 169 EXPECT_STREQ("Hello World!", result); |
127 } | 170 } |
128 | 171 |
129 } // namespace dart | 172 } // namespace dart |
OLD | NEW |