Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
move pooltable into a separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinNowak committed Jan 18, 2015
1 parent 4919de8 commit 0848b2f
Show file tree
Hide file tree
Showing 4 changed files with 296 additions and 279 deletions.
7 changes: 4 additions & 3 deletions mak/MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,13 @@ MANIFEST=\
src\core\sys\windows\windows.d \
src\core\sys\windows\winsock2.d \
\
src\gc\gc.d \
src\gc\os.d \
src\gc\bits.d \
src\gc\config.d \
src\gc\stats.d \
src\gc\gc.d \
src\gc\os.d \
src\gc\pooltable.d \
src\gc\proxy.d \
src\gc\stats.d \
\
src\gcstub\gc.d \
\
Expand Down
7 changes: 4 additions & 3 deletions mak/SRCS
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,13 @@ SRCS=\
src\core\sys\windows\windows.d \
src\core\sys\windows\winsock2.d \
\
src\gc\gc.d \
src\gc\os.d \
src\gc\bits.d \
src\gc\config.d \
src\gc\stats.d \
src\gc\gc.d \
src\gc\os.d \
src\gc\pooltable.d \
src\gc\proxy.d \
src\gc\stats.d \
\
src\rt\aApply.d \
src\rt\aApplyR.d \
Expand Down
274 changes: 1 addition & 273 deletions src/gc/gc.d
Original file line number Diff line number Diff line change
Expand Up @@ -1348,6 +1348,7 @@ struct Gcx
uint running;
int disabled; // turn off collections if >0

import gc.pooltable;
@property size_t npools() pure const nothrow { return pooltable.length; }
PoolTable!Pool pooltable;

Expand Down Expand Up @@ -2819,279 +2820,6 @@ struct Gcx
}
}

/* ========================= PoolTable ============================ */

struct PoolTable(Pool)
{
nothrow:
void reset()
{
cstdlib.free(pools);
pools = null;
npools = 0;
}

bool insert(Pool* pool)
{
auto newpools = cast(Pool **)cstdlib.realloc(pools, (npools + 1) * pools[0].sizeof);
if (!newpools)
return false;

pools = newpools;

// Sort pool into newpooltable[]
size_t i;
for (; i < npools; ++i)
{
if (pool.baseAddr < pools[i].baseAddr)
break;
}
if (i != npools)
memmove(pools + i + 1, pools + i, (npools - i) * pools[0].sizeof);
pools[i] = pool;

++npools;

minAddr = pools[0].baseAddr;
maxAddr = pools[npools - 1].topAddr;

return true;
}

@property size_t length() pure const
{
return npools;
}

ref inout(Pool*) opIndex(size_t idx) inout pure
in { assert(idx < length); }
body
{
return pools[idx];
}

inout(Pool*)[] opSlice(size_t a, size_t b) inout pure
in { assert(a <= length && b <= length); }
body
{
return pools[a .. b];
}

alias opDollar = length;

/**
* Find Pool that pointer is in.
* Return null if not in a Pool.
* Assume pooltable[] is sorted.
*/
Pool *findPool(void *p) nothrow
{
if (p >= minAddr && p < maxAddr)
{
assert(npools);

// let dmd allocate a register for this.pools
auto pools = this.pools;

if (npools == 1)
return pools[0];

/* The pooltable[] is sorted by address, so do a binary search
*/
size_t low = 0;
size_t high = npools - 1;
while (low <= high)
{
size_t mid = (low + high) >> 1;
auto pool = pools[mid];
if (p < pool.baseAddr)
high = mid - 1;
else if (p >= pool.topAddr)
low = mid + 1;
else
return pool;
}
}
return null;
}

// semi-stable partition, returns right half for which pred is false
Pool*[] minimize(alias isFree)() pure
{
static void swap(ref Pool* a, ref Pool* b)
{
auto c = a; a = b; b = c;
}

size_t i;
// find first bad entry
for (; i < npools; ++i)
if (isFree(pools[i])) break;

// move good in front of bad entries
size_t j = i + 1;
for (; j < npools; ++j)
{
if (!isFree(pools[j])) // keep
swap(pools[i++], pools[j]);
}
// npooltable[0 .. i] => used pools
// npooltable[i .. npools] => free pools

if (i)
{
minAddr = pools[0].baseAddr;
maxAddr = pools[i - 1].topAddr;
}
else
{
minAddr = maxAddr = null;
}

immutable len = npools;
npools = i;
// return freed pools to the caller
return pools[npools .. len];
}

debug (INVARIANT)
void Invariant() const
{
if (!npools) return;

foreach (i, pool; pools[0 .. npools - 1])
assert(pool.baseAddr < pools[i + 1].baseAddr);

assert(minAddr == pools[0].baseAddr);
assert(maxAddr == pools[npools - 1].topAddr);
}

private:
Pool** pools;
size_t npools;
byte* minAddr, maxAddr;
}

unittest
{
enum NPOOLS = 6;
enum NPAGES = 10;

static struct MockPool
{
byte* baseAddr, topAddr;
size_t freepages, npages;
}
PoolTable!MockPool pooltable;

static bool isFree(MockPool* mp) pure nothrow { return mp.freepages == mp.npages; }

void reset()
{
foreach(ref pool; pooltable[0 .. $])
pool.freepages = pool.npages;
pooltable.minimize!isFree();
assert(pooltable.length == 0);

foreach(i; 0 .. NPOOLS)
{
auto pool = cast(MockPool*)cstdlib.malloc(MockPool.sizeof);
*pool = MockPool.init;
assert(pooltable.insert(pool));
}
}

void usePools()
{
foreach(pool; pooltable[0 .. $])
{
pool.npages = NPAGES;
pool.freepages = NPAGES / 2;
}
}

// all pools are free
reset();
assert(pooltable.length == NPOOLS);
auto freed = pooltable.minimize!isFree();
assert(freed.length == NPOOLS);
assert(pooltable.length == 0);

// all pools used
reset();
usePools();
assert(pooltable.length == NPOOLS);
freed = pooltable.minimize!isFree();
assert(freed.length == 0);
assert(pooltable.length == NPOOLS);

// preserves order of used pools
reset();
usePools();

{
MockPool*[NPOOLS] opools = pooltable[0 .. NPOOLS];
// make the 2nd pool free
pooltable[2].freepages = NPAGES;

pooltable.minimize!isFree();
assert(pooltable.length == NPOOLS - 1);
assert(pooltable[0] == opools[0]);
assert(pooltable[1] == opools[1]);
assert(pooltable[2] == opools[3]);
}

// test that PoolTable reduces min/max address span
reset();
usePools();

byte* base, top;

{
// fill with fake addresses
size_t i;
foreach(pool; pooltable[0 .. NPOOLS])
{
pool.baseAddr = cast(byte*)(i++ * NPAGES * PAGESIZE);
pool.topAddr = pool.baseAddr + NPAGES * PAGESIZE;
}
base = pooltable[0].baseAddr;
top = pooltable[NPOOLS - 1].topAddr;
}

freed = pooltable.minimize!isFree();
assert(freed.length == 0);
assert(pooltable.length == NPOOLS);
assert(pooltable.minAddr == base);
assert(pooltable.maxAddr == top);

pooltable[NPOOLS - 1].freepages = NPAGES;
pooltable[NPOOLS - 2].freepages = NPAGES;

freed = pooltable.minimize!isFree();
assert(freed.length == 2);
assert(pooltable.length == NPOOLS - 2);
assert(pooltable.minAddr == base);
assert(pooltable.maxAddr == pooltable[NPOOLS - 3].topAddr);

pooltable[0].freepages = NPAGES;

freed = pooltable.minimize!isFree();
assert(freed.length == 1);
assert(pooltable.length == NPOOLS - 3);
assert(pooltable.minAddr != base);
assert(pooltable.minAddr == pooltable[0].baseAddr);
assert(pooltable.maxAddr == pooltable[NPOOLS - 4].topAddr);

// free all
foreach(pool; pooltable[0 .. $])
pool.freepages = NPAGES;
freed = pooltable.minimize!isFree();
assert(freed.length == NPOOLS - 3);
assert(pooltable.length == 0);
pooltable.reset();
}

/* ============================ Pool =============================== */


Expand Down
Loading

0 comments on commit 0848b2f

Please sign in to comment.