OLD | NEW |
1 // Copyright 2007-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2007-2008 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 static_cast<PropertyAttributes>(0)); | 137 static_cast<PropertyAttributes>(0)); |
138 map->set_instance_descriptors(*instance_descriptors); | 138 map->set_instance_descriptors(*instance_descriptors); |
139 // Add the Foo constructor the global object. | 139 // Add the Foo constructor the global object. |
140 env->Global()->Set(v8::String::New("Foo"), v8::Utils::ToLocal(function)); | 140 env->Global()->Set(v8::String::New("Foo"), v8::Utils::ToLocal(function)); |
141 // Call the accessor through JavaScript. | 141 // Call the accessor through JavaScript. |
142 v8::Handle<v8::Value> result = | 142 v8::Handle<v8::Value> result = |
143 v8::Script::Compile(v8::String::New("(new Foo).get"))->Run(); | 143 v8::Script::Compile(v8::String::New("(new Foo).get"))->Run(); |
144 CHECK_EQ(42, result->Int32Value()); | 144 CHECK_EQ(42, result->Int32Value()); |
145 env->Exit(); | 145 env->Exit(); |
146 } | 146 } |
| 147 |
| 148 |
| 149 // CodeRange test. |
| 150 // Tests memory management in a CodeRange by allocating and freeing blocks, |
| 151 // using a pseudorandom generator to choose block sizes geometrically |
| 152 // distributed between 2 * Page::kPageSize and 2^5 + 1 * Page::kPageSize. |
| 153 // Ensure that the freed chunks are collected and reused by allocating (in |
| 154 // total) more than the size of the CodeRange. |
| 155 |
| 156 // This pseudorandom generator does not need to be particularly good. |
| 157 // Use the lower half of the V8::Random() generator. |
| 158 unsigned int Pseudorandom() { |
| 159 static uint32_t lo = 2345; |
| 160 lo = 18273 * (lo & 0xFFFF) + (lo >> 16); // Provably not 0. |
| 161 return lo & 0xFFFF; |
| 162 } |
| 163 |
| 164 |
| 165 // Plain old data class. Represents a block of allocated memory. |
| 166 class Block { |
| 167 public: |
| 168 Block(void* base_arg, int size_arg) |
| 169 : base(base_arg), size(size_arg) {} |
| 170 |
| 171 void *base; |
| 172 int size; |
| 173 }; |
| 174 |
| 175 |
| 176 TEST(CodeRange) { |
| 177 const int code_range_size = 16*MB; |
| 178 CodeRange::Setup(code_range_size); |
| 179 int current_allocated = 0; |
| 180 int total_allocated = 0; |
| 181 List<Block> blocks(1000); |
| 182 |
| 183 while (total_allocated < 5 * code_range_size) { |
| 184 if (current_allocated < code_range_size / 10) { |
| 185 // Allocate a block. |
| 186 // Geometrically distributed sizes, greater than Page::kPageSize. |
| 187 size_t requested = (Page::kPageSize << (Pseudorandom() % 6)) + |
| 188 Pseudorandom() % 5000 + 1; |
| 189 size_t allocated = 0; |
| 190 void* base = CodeRange::AllocateRawMemory(requested, &allocated); |
| 191 blocks.Add(Block(base, allocated)); |
| 192 current_allocated += allocated; |
| 193 total_allocated += allocated; |
| 194 } else { |
| 195 // Free a block. |
| 196 int index = Pseudorandom() % blocks.length(); |
| 197 CodeRange::FreeRawMemory(blocks[index].base, blocks[index].size); |
| 198 current_allocated -= blocks[index].size; |
| 199 if (index < blocks.length() - 1) { |
| 200 blocks[index] = blocks.RemoveLast(); |
| 201 } else { |
| 202 blocks.RemoveLast(); |
| 203 } |
| 204 } |
| 205 } |
| 206 |
| 207 CodeRange::TearDown(); |
| 208 } |
OLD | NEW |