test-alloc.cc revision 5913587db4c6bab03d97bfe44b06289fd6d7270d
1a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Copyright 2007-2008 the V8 project authors. All rights reserved.
2a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Redistribution and use in source and binary forms, with or without
3a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// modification, are permitted provided that the following conditions are
4a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// met:
5a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//
6a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//     * Redistributions of source code must retain the above copyright
7a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       notice, this list of conditions and the following disclaimer.
8a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//     * Redistributions in binary form must reproduce the above
9a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       copyright notice, this list of conditions and the following
10a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       disclaimer in the documentation and/or other materials provided
11a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       with the distribution.
12a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//     * Neither the name of Google Inc. nor the names of its
13a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       contributors may be used to endorse or promote products derived
14a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       from this software without specific prior written permission.
15a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//
16a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
28a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#include "v8.h"
29a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#include "accessors.h"
30a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#include "top.h"
31a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
32a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#include "cctest.h"
33a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
34a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
35a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockusing namespace v8::internal;
36a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
37a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
385913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reckstatic MaybeObject* AllocateAfterFailures() {
39a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  static int attempts = 0;
40f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  if (++attempts < 3) return Failure::RetryAfterGC();
41a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
42a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // New space.
43a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  NewSpace* new_space = Heap::new_space();
44a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  static const int kNewSpaceFillerSize = ByteArray::SizeFor(0);
45a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  while (new_space->Available() > kNewSpaceFillerSize) {
46f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch    int available_before = static_cast<int>(new_space->Available());
47a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    CHECK(!Heap::AllocateByteArray(0)->IsFailure());
48a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    if (available_before == new_space->Available()) {
49a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      // It seems that we are avoiding new space allocations when
50a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      // allocation is forced, so no need to fill up new space
51a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      // in order to make the test harder.
52a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      break;
53a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    }
54a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
55a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  CHECK(!Heap::AllocateByteArray(100)->IsFailure());
56a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  CHECK(!Heap::AllocateFixedArray(100, NOT_TENURED)->IsFailure());
57a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
58a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Make sure we can allocate through optimized allocation functions
59a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // for specific kinds.
60a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  CHECK(!Heap::AllocateFixedArray(100)->IsFailure());
61a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  CHECK(!Heap::AllocateHeapNumber(0.42)->IsFailure());
62a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  CHECK(!Heap::AllocateArgumentsObject(Smi::FromInt(87), 10)->IsFailure());
635913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  Object* object =
645913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck      Heap::AllocateJSObject(*Top::object_function())->ToObjectChecked();
65a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  CHECK(!Heap::CopyJSObject(JSObject::cast(object))->IsFailure());
66a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
67a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Old data space.
68a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  OldSpace* old_data_space = Heap::old_data_space();
69d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block  static const int kOldDataSpaceFillerSize = ByteArray::SizeFor(0);
70a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  while (old_data_space->Available() > kOldDataSpaceFillerSize) {
71d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block    CHECK(!Heap::AllocateByteArray(0, TENURED)->IsFailure());
72a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
73a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  CHECK(!Heap::AllocateRawAsciiString(100, TENURED)->IsFailure());
74a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
75a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Large object space.
76a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  while (!Heap::OldGenerationAllocationLimitReached()) {
77a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    CHECK(!Heap::AllocateFixedArray(10000, TENURED)->IsFailure());
78a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
79a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  CHECK(!Heap::AllocateFixedArray(10000, TENURED)->IsFailure());
80a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
81a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Map space.
82a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  MapSpace* map_space = Heap::map_space();
83a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  static const int kMapSpaceFillerSize = Map::kSize;
84a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  InstanceType instance_type = JS_OBJECT_TYPE;
85a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  int instance_size = JSObject::kHeaderSize;
86a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  while (map_space->Available() > kMapSpaceFillerSize) {
87a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    CHECK(!Heap::AllocateMap(instance_type, instance_size)->IsFailure());
88a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
89a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  CHECK(!Heap::AllocateMap(instance_type, instance_size)->IsFailure());
90a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
91a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Test that we can allocate in old pointer space and code space.
92a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  CHECK(!Heap::AllocateFixedArray(100, TENURED)->IsFailure());
93a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  CHECK(!Heap::CopyCode(Builtins::builtin(Builtins::Illegal))->IsFailure());
94a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
95a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Return success.
96a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  return Smi::FromInt(42);
97a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
98a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
99a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
100a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockstatic Handle<Object> Test() {
101a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  CALL_HEAP_FUNCTION(AllocateAfterFailures(), Object);
102a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
103a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
104a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
105a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockTEST(StressHandles) {
106a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  v8::Persistent<v8::Context> env = v8::Context::New();
107a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  v8::HandleScope scope;
108a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  env->Enter();
109a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  Handle<Object> o = Test();
110a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  CHECK(o->IsSmi() && Smi::cast(*o)->value() == 42);
111a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  env->Exit();
112a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
113a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
114a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
1155913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reckstatic MaybeObject* TestAccessorGet(Object* object, void*) {
116a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  return AllocateAfterFailures();
117a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
118a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
119a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
120a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockconst AccessorDescriptor kDescriptor = {
121a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  TestAccessorGet,
122a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  0,
123a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  0
124a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block};
125a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
126a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
127a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockTEST(StressJS) {
128a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  v8::Persistent<v8::Context> env = v8::Context::New();
129a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  v8::HandleScope scope;
130a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  env->Enter();
131a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  Handle<JSFunction> function =
132a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      Factory::NewFunction(Factory::function_symbol(), Factory::null_value());
133a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Force the creation of an initial map and set the code to
134a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // something empty.
135a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  Factory::NewJSObject(function);
136a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  function->set_code(Builtins::builtin(Builtins::EmptyFunction));
137a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Patch the map to have an accessor for "get".
138a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  Handle<Map> map(function->initial_map());
139a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  Handle<DescriptorArray> instance_descriptors(map->instance_descriptors());
140a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  Handle<Proxy> proxy = Factory::NewProxy(&kDescriptor);
141a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  instance_descriptors = Factory::CopyAppendProxyDescriptor(
142a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      instance_descriptors,
143a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      Factory::NewStringFromAscii(Vector<const char>("get", 3)),
144a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      proxy,
145a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      static_cast<PropertyAttributes>(0));
146a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  map->set_instance_descriptors(*instance_descriptors);
147a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Add the Foo constructor the global object.
148a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  env->Global()->Set(v8::String::New("Foo"), v8::Utils::ToLocal(function));
149a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Call the accessor through JavaScript.
150a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  v8::Handle<v8::Value> result =
151a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      v8::Script::Compile(v8::String::New("(new Foo).get"))->Run();
152a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  CHECK_EQ(42, result->Int32Value());
153a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  env->Exit();
154a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
155a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
156a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
157a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// CodeRange test.
158a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Tests memory management in a CodeRange by allocating and freeing blocks,
159a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// using a pseudorandom generator to choose block sizes geometrically
160a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// distributed between 2 * Page::kPageSize and 2^5 + 1 * Page::kPageSize.
161a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Ensure that the freed chunks are collected and reused by allocating (in
162a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// total) more than the size of the CodeRange.
163a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
164a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// This pseudorandom generator does not need to be particularly good.
165a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Use the lower half of the V8::Random() generator.
166a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockunsigned int Pseudorandom() {
167a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  static uint32_t lo = 2345;
168a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  lo = 18273 * (lo & 0xFFFF) + (lo >> 16);  // Provably not 0.
169a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  return lo & 0xFFFF;
170a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
171a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
172a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
173a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Plain old data class.  Represents a block of allocated memory.
174a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockclass Block {
175a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block public:
176a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  Block(void* base_arg, int size_arg)
177a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      : base(base_arg), size(size_arg) {}
178a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
179a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  void *base;
180a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  int size;
181a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block};
182a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
183a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
184a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockTEST(CodeRange) {
185a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  const int code_range_size = 16*MB;
186a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  CodeRange::Setup(code_range_size);
187a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  int current_allocated = 0;
188a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  int total_allocated = 0;
189a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  List<Block> blocks(1000);
190a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
191a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  while (total_allocated < 5 * code_range_size) {
192a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    if (current_allocated < code_range_size / 10) {
193a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      // Allocate a block.
194a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      // Geometrically distributed sizes, greater than Page::kPageSize.
195a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      size_t requested = (Page::kPageSize << (Pseudorandom() % 6)) +
196a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block           Pseudorandom() % 5000 + 1;
197a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      size_t allocated = 0;
198a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      void* base = CodeRange::AllocateRawMemory(requested, &allocated);
199d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block      blocks.Add(Block(base, static_cast<int>(allocated)));
200d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block      current_allocated += static_cast<int>(allocated);
201d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block      total_allocated += static_cast<int>(allocated);
202a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    } else {
203a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      // Free a block.
204a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      int index = Pseudorandom() % blocks.length();
205a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      CodeRange::FreeRawMemory(blocks[index].base, blocks[index].size);
206a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      current_allocated -= blocks[index].size;
207a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      if (index < blocks.length() - 1) {
208a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block        blocks[index] = blocks.RemoveLast();
209a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      } else {
210a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block        blocks.RemoveLast();
211a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      }
212a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    }
213a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
214a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
215a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  CodeRange::TearDown();
216a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
217