OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 the V8 project authors. All rights reserved. | |
Jakob Kummerow
2016/09/07 12:49:03
nit: 2016
| |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include <cmath> | |
6 #include <limits> | |
7 | |
8 #include "src/objects-inl.h" | |
9 #include "src/objects.h" | |
Jakob Kummerow
2016/09/07 12:49:03
nit: when you #include foo-inl.h, you don't need t
| |
10 | |
Jakob Kummerow
2016/09/07 12:49:02
nit: no empty line necessary here. Instead, sort #
| |
11 #include "src/handles-inl.h" | |
12 #include "src/handles.h" | |
Jakob Kummerow
2016/09/07 12:49:03
again
| |
13 | |
14 #include "src/base/utils/random-number-generator.h" | |
15 #include "src/zone.h" | |
16 #include "testing/gtest/include/gtest/gtest.h" | |
17 | |
18 namespace v8 { | |
19 namespace internal { | |
20 | |
21 void generate_data(byte* data, const uint32_t length) { | |
22 for (uint32_t i = 0; i < length; i++) { | |
23 data[i] = static_cast<byte>(i % 100); | |
Jakob Kummerow
2016/09/07 12:49:02
how about s/i % 100/i & 0xFF/ ?
| |
24 } | |
25 } | |
26 | |
27 bool validate_data(const byte* data, const uint32_t length) { | |
28 for (uint32_t i = 0; i < length; i++) { | |
29 if (data[i] != static_cast<byte>(i % 100)) return false; | |
30 } | |
31 return true; | |
32 } | |
33 | |
34 void empty_alloc() { | |
Jakob Kummerow
2016/09/07 12:49:03
Any reason this is a separate function? I'd prefer
| |
35 base::AccountingAllocator allocator; | |
36 Zone zone(&allocator); | |
37 | |
38 CHECK_EQ(zone.allocation_size(), 0); | |
39 | |
40 void* ptr = zone.New(0); | |
41 | |
42 CHECK_EQ(zone.allocation_size(), 0); | |
43 CHECK_EQ(Zone::GetZoneFromPointer(ptr), &zone); | |
44 } | |
45 | |
46 TEST(Zone, EmptyAlloc) { empty_alloc(); } | |
47 | |
48 void simple_alloc() { | |
49 const size_t kNumberOfChunks = 10000; | |
Jakob Kummerow
2016/09/07 12:49:03
How long does it take to run these tests?
| |
50 const size_t kMaxChunkSize = 1000; | |
51 | |
52 base::RandomNumberGenerator rnd(::v8::internal::FLAG_random_seed); | |
53 base::AccountingAllocator allocator; | |
54 Zone zone(&allocator); | |
55 | |
56 CHECK_EQ(zone.allocation_size(), 0); | |
57 | |
58 byte* chunks[kNumberOfChunks]; | |
59 uint16_t sizes[kNumberOfChunks]; | |
60 | |
61 for (int i = 0; i < kNumberOfChunks; i++) { | |
62 sizes[i] = 1 + (rnd.NextInt() % kMaxChunkSize); | |
63 chunks[i] = reinterpret_cast<byte*>(zone.New(sizes[i])); | |
64 | |
65 generate_data(chunks[i], sizes[i]); | |
66 } | |
67 | |
68 CHECK_GE(zone.allocation_size(), kNumberOfChunks); | |
69 | |
70 for (int i = 0; i < kNumberOfChunks; i++) { | |
71 CHECK(validate_data(chunks[i], sizes[i])); | |
72 } | |
73 } | |
74 | |
75 TEST(Zone, SimpleAlloc) { simple_alloc(); } | |
76 | |
77 void large_alloc() { | |
78 const size_t kNumberOfChunks = 50; | |
79 const size_t kMaxChunkSize = 5000000; | |
80 | |
81 base::RandomNumberGenerator rnd(::v8::internal::FLAG_random_seed); | |
82 base::AccountingAllocator allocator; | |
83 Zone zone(&allocator); | |
84 | |
85 CHECK_EQ(zone.allocation_size(), 0); | |
86 | |
87 byte* chunks[kNumberOfChunks]; | |
88 uint32_t sizes[kNumberOfChunks]; | |
89 | |
90 for (int i = 0; i < kNumberOfChunks; i++) { | |
91 sizes[i] = 1 + (rnd.NextInt() % kMaxChunkSize); | |
92 chunks[i] = reinterpret_cast<byte*>(zone.New(sizes[i])); | |
93 | |
94 generate_data(chunks[i], sizes[i]); | |
95 } | |
96 | |
97 CHECK_GE(zone.allocation_size(), kNumberOfChunks); | |
98 | |
99 for (int i = 0; i < kNumberOfChunks; i++) { | |
100 CHECK(validate_data(chunks[i], sizes[i])); | |
101 } | |
102 } | |
103 | |
104 TEST(Zone, LargeAlloc) { large_alloc(); } | |
105 | |
106 void zone_realloc() { | |
107 const size_t kDataAmount = 5000000; | |
108 | |
109 base::AccountingAllocator allocator; | |
110 Zone zone(&allocator); | |
111 | |
112 CHECK_EQ(zone.allocation_size(), 0); | |
113 | |
114 auto list = new (&zone) ZoneList<byte>(0, &zone); | |
Jakob Kummerow
2016/09/07 12:49:03
s/auto/ZoneList<byte>/
| |
115 | |
116 for (uint32_t i = 0; i < kDataAmount; i++) { | |
117 list->Add(i % 100, &zone); | |
118 } | |
119 | |
120 CHECK_EQ(static_cast<size_t>(list->length()), kDataAmount); | |
121 CHECK_GE(static_cast<size_t>(list->capacity()), kDataAmount); | |
122 CHECK_GE(zone.allocation_size(), kDataAmount); | |
123 | |
124 for (uint32_t i = 0; i < kDataAmount; i++) { | |
125 CHECK_EQ((*list)[i], i % 100); | |
126 } | |
127 } | |
128 | |
129 TEST(Zone, ZoneRealloc) { zone_realloc(); } | |
130 | |
131 } // namespace internal | |
132 } // namespace v8 | |
OLD | NEW |