| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
*/ |
| 4 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 5 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 7 |
|
| 8 |
#include <utility> |
| 9 |
|
| 10 |
#include "gc/GCLock.h" |
| 11 |
#include "gc/GCRuntime.h" |
| 12 |
#include "jsapi-tests/tests.h" |
| 13 |
|
| 14 |
BEGIN_TEST(testGCChunkPool) { |
| 15 |
using namespace js::gc; |
| 16 |
|
| 17 |
const int N = 10; |
| 18 |
ChunkPool pool; |
| 19 |
|
| 20 |
// Create. |
| 21 |
for (int i = 0; i < N; ++i) { |
| 22 |
void* ptr = ArenaChunk::allocate(&cx->runtime()->gc, StallAndRetry::No); |
| 23 |
CHECK(ptr); |
| 24 |
ArenaChunk* chunk = ArenaChunk::emplace(ptr, &cx->runtime()->gc, true); |
| 25 |
CHECK(chunk); |
| 26 |
pool.push(chunk); |
| 27 |
} |
| 28 |
MOZ_ASSERT(pool.verify()); |
| 29 |
|
| 30 |
// Iterate. |
| 31 |
uint32_t i = 0; |
| 32 |
for (ChunkPool::Iter iter(pool); !iter.done(); iter.next(), ++i) { |
| 33 |
CHECK(iter.get()); |
| 34 |
} |
| 35 |
CHECK(i == pool.count()); |
| 36 |
MOZ_ASSERT(pool.verify()); |
| 37 |
|
| 38 |
// Push/Pop. |
| 39 |
for (int i = 0; i < N; ++i) { |
| 40 |
ArenaChunk* chunkA = pool.pop(); |
| 41 |
ArenaChunk* chunkB = pool.pop(); |
| 42 |
ArenaChunk* chunkC = pool.pop(); |
| 43 |
pool.push(chunkA); |
| 44 |
pool.push(chunkB); |
| 45 |
pool.push(chunkC); |
| 46 |
} |
| 47 |
MOZ_ASSERT(pool.verify()); |
| 48 |
|
| 49 |
// Remove. |
| 50 |
ArenaChunk* chunk = nullptr; |
| 51 |
int offset = N / 2; |
| 52 |
for (ChunkPool::Iter iter(pool); !iter.done(); iter.next(), --offset) { |
| 53 |
if (offset == 0) { |
| 54 |
chunk = pool.remove(iter.get()); |
| 55 |
break; |
| 56 |
} |
| 57 |
} |
| 58 |
CHECK(chunk); |
| 59 |
MOZ_ASSERT(!pool.contains(chunk)); |
| 60 |
MOZ_ASSERT(pool.verify()); |
| 61 |
pool.push(chunk); |
| 62 |
|
| 63 |
// Destruct. |
| 64 |
js::AutoLockGC lock(cx->runtime()); |
| 65 |
for (ChunkPool::Iter iter(pool); !iter.done();) { |
| 66 |
ArenaChunk* chunk = iter.get(); |
| 67 |
iter.next(); |
| 68 |
pool.remove(chunk); |
| 69 |
UnmapPages(chunk, ChunkSize); |
| 70 |
} |
| 71 |
|
| 72 |
return true; |
| 73 |
} |
| 74 |
END_TEST(testGCChunkPool) |