1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
#include "stdafx.h"
#include "Utils/Bitwise.h"
static void *allocCode(Gc &gc, size_t size, size_t refs, nat pattern) {
void *r = gc.allocCode(size, refs);
memset(r, pattern & 0xFF, size);
return r;
}
struct TestEntry {
size_t count;
size_t size;
size_t refs;
};
static const TestEntry seq[] = {
{ 1, 50, 2 },
{ 58, 20, 3 },
{ 2, 60, 2 },
{ 1, 68, 2 },
{ 2, 20, 3 },
{ 1, 44, 10 },
{ 20, 8, 2 },
};
nat align(nat v) {
return roundUp(v, Nat(sizeof(void *)));
}
#ifndef X86
size_t align(size_t v) {
return roundUp(v, sizeof(void *));
}
#endif
void verify(const RootArray<void> &data) {
nat pos = 0;
for (nat i = 0; i < ARRAY_COUNT(seq); i++) {
const TestEntry &e = seq[i];
for (size_t j = 0; j < e.count; j++) {
byte *d = (byte *)data[pos];
if (!d)
return;
if (Gc::codeSize(d) != align(e.size))
PLN(pos << L": SIZE CHECK FAILED: is " << Gc::codeSize(d) << L", should be " << align(e.size));
if (Gc::codeRefs(d)->refCount != e.refs)
PLN(pos << L": REF COUNT CHECK FAILED!");
for (nat q = 0; q < e.size; q++) {
if (d[q] != (pos & 0xFF)) {
PLN(pos << L": VERIFICATION ERROR IN ALLOC #" << pos);
}
}
pos++;
}
}
}
BEGIN_TEST(CodeFmtTest, GcScan) {
Gc &gc = ::gc();
RootArray<void> data(gc);
data.resize(1000);
// Allocate some objects, verify everything all the time!
nat pos = 0;
for (nat i = 0; i < ARRAY_COUNT(seq); i++) {
const TestEntry &e = seq[i];
for (size_t j = 0; j < e.count; j++) {
data[pos] = allocCode(gc, e.size, e.refs, pos);
pos++;
verify(data);
}
// Try to force a garbage collection.
gc.test(1);
// NOTE: Calling 'gc.collect' with a small heap seems to make the GC consume
// a lot of time during shutdown for some reason. Possibly, a lot of small areas
// are allocated, which causes the problems.
// gc.collect();
}
} END_TEST
|