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 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 static_cast<PropertyAttributes>(0)); | 144 static_cast<PropertyAttributes>(0)); |
145 map->set_instance_descriptors(*instance_descriptors); | 145 map->set_instance_descriptors(*instance_descriptors); |
146 // Add the Foo constructor the global object. | 146 // Add the Foo constructor the global object. |
147 env->Global()->Set(v8::String::New("Foo"), v8::Utils::ToLocal(function)); | 147 env->Global()->Set(v8::String::New("Foo"), v8::Utils::ToLocal(function)); |
148 // Call the accessor through JavaScript. | 148 // Call the accessor through JavaScript. |
149 v8::Handle<v8::Value> result = | 149 v8::Handle<v8::Value> result = |
150 v8::Script::Compile(v8::String::New("(new Foo).get"))->Run(); | 150 v8::Script::Compile(v8::String::New("(new Foo).get"))->Run(); |
151 CHECK_EQ(42, result->Int32Value()); | 151 CHECK_EQ(42, result->Int32Value()); |
152 env->Exit(); | 152 env->Exit(); |
153 } | 153 } |
| 154 |
| 155 |
| 156 // CodeRange test. |
| 157 // Tests memory management in a CodeRange by allocating and freeing blocks, |
| 158 // using a pseudorandom generator to choose block sizes geometrically |
| 159 // distributed between 2 * Page::kPageSize and 2^5 + 1 * Page::kPageSize. |
| 160 // Ensure that the freed chunks are collected and reused by allocating (in |
| 161 // total) more than the size of the CodeRange. |
| 162 |
| 163 // This pseudorandom generator does not need to be particularly good. |
| 164 // Use the lower half of the V8::Random() generator. |
| 165 unsigned int Pseudorandom() { |
| 166 static uint32_t lo = 2345; |
| 167 lo = 18273 * (lo & 0xFFFF) + (lo >> 16); // Provably not 0. |
| 168 return lo & 0xFFFF; |
| 169 } |
| 170 |
| 171 |
| 172 // Plain old data class. Represents a block of allocated memory. |
| 173 class Block { |
| 174 public: |
| 175 Block(void* base_arg, int size_arg) |
| 176 : base(base_arg), size(size_arg) {} |
| 177 |
| 178 void *base; |
| 179 int size; |
| 180 }; |
| 181 |
| 182 |
| 183 TEST(CodeRange) { |
| 184 const int code_range_size = 16*MB; |
| 185 CodeRange::Setup(code_range_size); |
| 186 int current_allocated = 0; |
| 187 int total_allocated = 0; |
| 188 List<Block> blocks(1000); |
| 189 |
| 190 while (total_allocated < 5 * code_range_size) { |
| 191 if (current_allocated < code_range_size / 10) { |
| 192 // Allocate a block. |
| 193 // Geometrically distributed sizes, greater than Page::kPageSize. |
| 194 size_t requested = (Page::kPageSize << (Pseudorandom() % 6)) + |
| 195 Pseudorandom() % 5000 + 1; |
| 196 size_t allocated = 0; |
| 197 void* base = CodeRange::AllocateRawMemory(requested, &allocated); |
| 198 blocks.Add(Block(base, allocated)); |
| 199 current_allocated += allocated; |
| 200 total_allocated += allocated; |
| 201 } else { |
| 202 // Free a block. |
| 203 int index = Pseudorandom() % blocks.length(); |
| 204 CodeRange::FreeRawMemory(blocks[index].base, blocks[index].size); |
| 205 current_allocated -= blocks[index].size; |
| 206 if (index < blocks.length() - 1) { |
| 207 blocks[index] = blocks.RemoveLast(); |
| 208 } else { |
| 209 blocks.RemoveLast(); |
| 210 } |
| 211 } |
| 212 } |
| 213 |
| 214 CodeRange::TearDown(); |
| 215 } |
OLD | NEW |