| 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 "mozilla/Attributes.h" |
| 9 |
|
| 10 |
#include "gc/GCLock.h" |
| 11 |
#include "jsapi-tests/tests.h" |
| 12 |
|
| 13 |
#include "gc/ArenaList-inl.h" |
| 14 |
#include "gc/Heap-inl.h" |
| 15 |
|
| 16 |
using namespace js; |
| 17 |
using namespace js::gc; |
| 18 |
|
| 19 |
// Automatically allocate and free an Arena for testing purposes. |
| 20 |
class MOZ_RAII AutoTestArena { |
| 21 |
Arena* arena = nullptr; |
| 22 |
|
| 23 |
public: |
| 24 |
explicit AutoTestArena(JSContext* cx, AllocKind kind, size_t nfree) { |
| 25 |
// For testing purposes only. Don't do this in real code! |
| 26 |
arena = js_pod_calloc<Arena>(1); |
| 27 |
MOZ_RELEASE_ASSERT(arena); |
| 28 |
|
| 29 |
arena->init(&cx->runtime()->gc, cx->zone(), kind); |
| 30 |
|
| 31 |
size_t nallocs = Arena::thingsPerArena(kind) - nfree; |
| 32 |
size_t thingSize = Arena::thingSize(kind); |
| 33 |
for (size_t i = 0; i < nallocs; i++) { |
| 34 |
MOZ_RELEASE_ASSERT(arena->getFirstFreeSpan()->allocate(thingSize)); |
| 35 |
} |
| 36 |
MOZ_RELEASE_ASSERT(arena->countFreeCells() == nfree); |
| 37 |
} |
| 38 |
|
| 39 |
~AutoTestArena() { js_free(arena); } |
| 40 |
|
| 41 |
Arena* get() { return arena; } |
| 42 |
operator Arena*() { return arena; } |
| 43 |
Arena* operator->() { return arena; } |
| 44 |
}; |
| 45 |
|
| 46 |
BEGIN_TEST(testSortedArenaList) { |
| 47 |
const AllocKind kind = AllocKind::OBJECT0; |
| 48 |
|
| 49 |
// Test empty list. |
| 50 |
|
| 51 |
SortedArenaList sortedList(kind); |
| 52 |
CHECK(sortedList.thingsPerArena() == Arena::thingsPerArena(kind)); |
| 53 |
|
| 54 |
CHECK(ConvertToArenaList(kind, sortedList, 0)); |
| 55 |
|
| 56 |
// Test with single non-empty arena. |
| 57 |
|
| 58 |
size_t thingsPerArena = Arena::thingsPerArena(kind); |
| 59 |
for (size_t i = 0; i != thingsPerArena; i++) { |
| 60 |
AutoTestArena arena(cx, kind, i); |
| 61 |
sortedList.insertAt(arena.get(), i); |
| 62 |
|
| 63 |
CHECK(ConvertToArenaList(kind, sortedList, 1, nullptr, arena.get())); |
| 64 |
} |
| 65 |
|
| 66 |
// Test with single empty arena. |
| 67 |
|
| 68 |
AutoTestArena arena(cx, kind, thingsPerArena); |
| 69 |
sortedList.insertAt(arena.get(), thingsPerArena); |
| 70 |
|
| 71 |
CHECK(ConvertToArenaList(kind, sortedList, 0, arena.get())); |
| 72 |
|
| 73 |
// Test with full and non-full arenas. |
| 74 |
|
| 75 |
AutoTestArena fullArena(cx, kind, 0); |
| 76 |
AutoTestArena nonFullArena(cx, kind, 1); |
| 77 |
sortedList.insertAt(fullArena.get(), 0); |
| 78 |
sortedList.insertAt(nonFullArena.get(), 1); |
| 79 |
|
| 80 |
CHECK(ConvertToArenaList(kind, sortedList, 2, nullptr, nonFullArena.get(), |
| 81 |
fullArena.get())); |
| 82 |
|
| 83 |
return true; |
| 84 |
} |
| 85 |
|
| 86 |
bool ConvertToArenaList(AllocKind kind, SortedArenaList& sortedList, |
| 87 |
size_t expectedBucketCount, |
| 88 |
Arena* expectedEmpty = nullptr, |
| 89 |
Arena* expectedFirst = nullptr, |
| 90 |
Arena* expectedLast = nullptr) { |
| 91 |
CHECK(ConvertToArenaListOnce(sortedList, expectedBucketCount, expectedEmpty, |
| 92 |
expectedFirst, expectedLast)); |
| 93 |
|
| 94 |
// Check again to test restoreFromArenaList restored the original state |
| 95 |
// (except for the empty arenas which are not restored). |
| 96 |
CHECK(ConvertToArenaListOnce(sortedList, expectedBucketCount, nullptr, |
| 97 |
expectedFirst, expectedLast)); |
| 98 |
|
| 99 |
// Clear the list on exit. |
| 100 |
new (&sortedList) SortedArenaList(kind); |
| 101 |
|
| 102 |
return true; |
| 103 |
} |
| 104 |
|
| 105 |
bool ConvertToArenaListOnce(SortedArenaList& sortedList, |
| 106 |
size_t expectedBucketCount, Arena* expectedEmpty, |
| 107 |
Arena* expectedFirst, Arena* expectedLast) { |
| 108 |
Arena* emptyArenas = nullptr; |
| 109 |
sortedList.extractEmptyTo(&emptyArenas); |
| 110 |
CHECK(emptyArenas == expectedEmpty); |
| 111 |
|
| 112 |
Arena* bucketLast[SortedArenaList::BucketCount]; |
| 113 |
ArenaList list = sortedList.convertToArenaList(bucketLast); |
| 114 |
CHECK(list.isEmpty() == (expectedBucketCount == 0)); |
| 115 |
if (expectedFirst) { |
| 116 |
CHECK(list.first() == expectedFirst); |
| 117 |
} |
| 118 |
if (expectedLast) { |
| 119 |
CHECK(list.last() == expectedLast); |
| 120 |
} |
| 121 |
|
| 122 |
size_t count = 0; |
| 123 |
for (size_t j = 0; j < SortedArenaList::BucketCount; j++) { |
| 124 |
if (bucketLast[j]) { |
| 125 |
count++; |
| 126 |
} |
| 127 |
} |
| 128 |
CHECK(count == expectedBucketCount); |
| 129 |
|
| 130 |
sortedList.restoreFromArenaList(list, bucketLast); |
| 131 |
CHECK(list.isEmpty()); |
| 132 |
|
| 133 |
return true; |
| 134 |
} |
| 135 |
END_TEST(testSortedArenaList) |