static Bool checkCallback(Range range, void *closureP, Size closureS) { Addr base, limit; CheckFBMClosure cl = (CheckFBMClosure)closureP; UNUSED(closureS); Insist(cl != NULL); base = RangeBase(range); limit = RangeLimit(range); if (base > cl->oldLimit) { Insist(BTIsSetRange(cl->state->allocTable, indexOfAddr(cl->state, cl->oldLimit), indexOfAddr(cl->state, base))); } else { /* must be at start of table */ Insist(base == cl->oldLimit); Insist(cl->oldLimit == cl->state->block); } Insist(BTIsResRange(cl->state->allocTable, indexOfAddr(cl->state, base), indexOfAddr(cl->state, limit))); cl->oldLimit = limit; return TRUE; }
static void check(FBMState state) { CheckFBMClosureStruct closure; closure.state = state; closure.limit = addrOfIndex(state, ArraySize); closure.oldLimit = state->block; switch (state->type) { case FBMTypeCBS: CBSIterate(state->the.cbs, checkCBSCallback, (void *)&closure, 0); break; case FBMTypeFreelist: FreelistIterate(state->the.fl, checkFLCallback, (void *)&closure, 0); break; default: fail(); return; } if (closure.oldLimit == state->block) Insist(BTIsSetRange(state->allocTable, 0, indexOfAddr(state, closure.limit))); else if (closure.limit > closure.oldLimit) Insist(BTIsSetRange(state->allocTable, indexOfAddr(state, closure.oldLimit), indexOfAddr(state, closure.limit))); else Insist(closure.oldLimit == closure.limit); }
static Bool checkCBSAction(CBS cbs, CBSBlock cbsBlock, void *p) { Addr base, limit; CheckCBSClosure closure = (CheckCBSClosure)p; /* Don't need to check cbs every time */ UNUSED(cbs); Insist(closure != NULL); base = CBSBlockBase(cbsBlock); limit = CBSBlockLimit(cbsBlock); if (base > closure->oldLimit) { Insist(BTIsSetRange(closure->allocTable, indexOfAddr(closure->base, closure->oldLimit), indexOfAddr(closure->base, base))); } else { /* must be at start of table */ Insist(base == closure->oldLimit); Insist(closure->oldLimit == closure->base); } Insist(BTIsResRange(closure->allocTable, indexOfAddr(closure->base, base), indexOfAddr(closure->base, limit))); closure->oldLimit = limit; return TRUE; }
static void checkCBS(CBS cbs, BT allocTable, Addr dummyBlock) { CheckCBSClosureStruct closure; closure.allocTable = allocTable; closure.base = dummyBlock; closure.limit = addrOfIndex(closure.base, ArraySize); closure.oldLimit = closure.base; CBSIterate(cbs, checkCBSAction, (void *)&closure); if (closure.oldLimit == closure.base) Insist(BTIsSetRange(allocTable, 0, indexOfAddr(dummyBlock, closure.limit))); else if (closure.limit > closure.oldLimit) Insist(BTIsSetRange(allocTable, indexOfAddr(dummyBlock, closure.oldLimit), indexOfAddr(dummyBlock, closure.limit))); else Insist(closure.oldLimit == closure.limit); }
static void find(FBMState state, Size size, Bool high, FindDelete findDelete) { Bool expected, found; Index expectedBase, expectedLimit; RangeStruct foundRange, oldRange; Addr remainderBase, remainderLimit; Addr origBase, origLimit; Size oldSize, newSize; origBase = origLimit = NULL; expected = (high ? BTFindLongResRangeHigh : BTFindLongResRange) (&expectedBase, &expectedLimit, state->allocTable, (Index)0, (Index)ArraySize, (Count)size); if (expected) { oldSize = (expectedLimit - expectedBase) * state->align; remainderBase = origBase = addrOfIndex(state, expectedBase); remainderLimit = origLimit = addrOfIndex(state, expectedLimit); switch(findDelete) { case FindDeleteNONE: { /* do nothing */ } break; case FindDeleteENTIRE: { remainderBase = remainderLimit; } break; case FindDeleteLOW: { expectedLimit = expectedBase + size; remainderBase = addrOfIndex(state, expectedLimit); } break; case FindDeleteHIGH: { expectedBase = expectedLimit - size; remainderLimit = addrOfIndex(state, expectedBase); } break; } if (findDelete != FindDeleteNONE) { newSize = AddrOffset(remainderBase, remainderLimit); } /* TODO: check these values */ UNUSED(oldSize); UNUSED(newSize); } switch (state->type) { case FBMTypeCBS: found = (high ? CBSFindLast : CBSFindFirst) (&foundRange, &oldRange, state->the.cbs, size * state->align, findDelete); break; case FBMTypeFreelist: found = (high ? FreelistFindLast : FreelistFindFirst) (&foundRange, &oldRange, state->the.fl, size * state->align, findDelete); break; default: fail(); return; } if (verbose) { printf("find %s %lu: ", high ? "last" : "first", (unsigned long)(size * state->align)); if (expected) { printf("expecting [%p,%p)\n", (void *)addrOfIndex(state, expectedBase), (void *)addrOfIndex(state, expectedLimit)); } else { printf("expecting this not to be found\n"); } if (found) { printf(" found [%p,%p)\n", (void *)RangeBase(&foundRange), (void *)RangeLimit(&foundRange)); } else { printf(" not found\n"); } } Insist(found == expected); if (found) { Insist(expectedBase == indexOfAddr(state, RangeBase(&foundRange))); Insist(expectedLimit == indexOfAddr(state, RangeLimit(&foundRange))); if (findDelete != FindDeleteNONE) { Insist(RangeBase(&oldRange) == origBase); Insist(RangeLimit(&oldRange) == origLimit); BTSetRange(state->allocTable, expectedBase, expectedLimit); } } return; }
static void deallocate(FBMState state, Addr base, Addr limit) { Res res; Index ib, il; Bool isAllocated; Addr outerBase = base, outerLimit = limit; /* interval containing [ib, il) */ RangeStruct range, freeRange; /* interval returned by the manager */ ib = indexOfAddr(state, base); il = indexOfAddr(state, limit); isAllocated = BTIsSetRange(state->allocTable, ib, il); NDeallocateTried++; if (isAllocated) { Size left, right, total; /* Sizes of block and two fragments */ /* Find the free blocks adjacent to the allocated block */ if (ib > 0 && !BTGet(state->allocTable, ib - 1)) { outerBase = addrOfIndex(state, lastEdge(state->allocTable, ArraySize, ib - 1)); } else { outerBase = base; } if (il < ArraySize && !BTGet(state->allocTable, il)) { outerLimit = addrOfIndex(state, nextEdge(state->allocTable, ArraySize, il)); } else { outerLimit = limit; } left = AddrOffset(outerBase, base); right = AddrOffset(limit, outerLimit); total = AddrOffset(outerBase, outerLimit); /* TODO: check these values */ UNUSED(left); UNUSED(right); UNUSED(total); } RangeInit(&range, base, limit); switch (state->type) { case FBMTypeCBS: res = CBSInsert(&freeRange, state->the.cbs, &range); break; case FBMTypeFreelist: res = FreelistInsert(&freeRange, state->the.fl, &range); break; default: fail(); return; } if (verbose) { printf("deallocate: [%p,%p) -- %s\n", (void *)base, (void *)limit, isAllocated ? "succeed" : "fail"); describe(state); } if (!isAllocated) { die_expect((mps_res_t)res, MPS_RES_FAIL, "succeeded in inserting non-allocated block"); } else { /* isAllocated */ die_expect((mps_res_t)res, MPS_RES_OK, "failed to insert allocated block"); NDeallocateSucceeded++; BTResRange(state->allocTable, ib, il); Insist(RangeBase(&freeRange) == outerBase); Insist(RangeLimit(&freeRange) == outerLimit); } }
static void allocate(FBMState state, Addr base, Addr limit) { Res res; Index ib, il; /* Indexed for base and limit */ Bool isFree; RangeStruct range, oldRange; Addr outerBase, outerLimit; /* interval containing [ib, il) */ ib = indexOfAddr(state, base); il = indexOfAddr(state, limit); isFree = BTIsResRange(state->allocTable, ib, il); NAllocateTried++; if (isFree) { Size left, right, total; /* Sizes of block and two fragments */ outerBase = addrOfIndex(state, lastEdge(state->allocTable, ArraySize, ib)); outerLimit = addrOfIndex(state, nextEdge(state->allocTable, ArraySize, il - 1)); left = AddrOffset(outerBase, base); right = AddrOffset(limit, outerLimit); total = AddrOffset(outerBase, outerLimit); /* TODO: check these values */ UNUSED(left); UNUSED(right); UNUSED(total); } else { outerBase = outerLimit = NULL; } RangeInit(&range, base, limit); switch (state->type) { case FBMTypeCBS: res = CBSDelete(&oldRange, state->the.cbs, &range); break; case FBMTypeFreelist: res = FreelistDelete(&oldRange, state->the.fl, &range); break; default: fail(); return; } if (verbose) { printf("allocate: [%p,%p) -- %s\n", (void *)base, (void *)limit, isFree ? "succeed" : "fail"); describe(state); } if (!isFree) { die_expect((mps_res_t)res, MPS_RES_FAIL, "Succeeded in deleting allocated block"); } else { /* isFree */ die_expect((mps_res_t)res, MPS_RES_OK, "failed to delete free block"); Insist(RangeBase(&oldRange) == outerBase); Insist(RangeLimit(&oldRange) == outerLimit); NAllocateSucceeded++; BTSetRange(state->allocTable, ib, il); } }
static void find(CBS cbs, void *block, BT alloc, Size size, Bool high, CBSFindDelete findDelete) { Bool expected, found; Index expectedBase, expectedLimit; Addr foundBase, foundLimit, remainderBase, remainderLimit; Size oldSize, newSize; checkExpectations(); expected = (high ? BTFindLongResRangeHigh : BTFindLongResRange) (&expectedBase, &expectedLimit, alloc, (Index)0, (Index)ArraySize, (Count)size); if (expected) { oldSize = (expectedLimit - expectedBase) * Alignment; remainderBase = addrOfIndex(block, expectedBase); remainderLimit = addrOfIndex(block, expectedLimit); switch(findDelete) { case CBSFindDeleteNONE: { /* do nothing */ } break; case CBSFindDeleteENTIRE: { remainderBase = remainderLimit; } break; case CBSFindDeleteLOW: { expectedLimit = expectedBase + size; remainderBase = addrOfIndex(block, expectedLimit); } break; case CBSFindDeleteHIGH: { expectedBase = expectedLimit - size; remainderLimit = addrOfIndex(block, expectedBase); } break; } if (findDelete != CBSFindDeleteNONE) { newSize = AddrOffset(remainderBase, remainderLimit); if (oldSize >= MinSize) { if (newSize == 0) expectCallback(&CallbackDelete, oldSize, (Addr)0, (Addr)0); else if (newSize < MinSize) expectCallback(&CallbackDelete, oldSize, remainderBase, remainderLimit); else expectCallback(&CallbackShrink, oldSize, remainderBase, remainderLimit); } } } found = (high ? CBSFindLast : CBSFindFirst) (&foundBase, &foundLimit, cbs, size * Alignment, findDelete); Insist(found == expected); if (found) { Insist(expectedBase == indexOfAddr(block, foundBase)); Insist(expectedLimit == indexOfAddr(block, foundLimit)); checkExpectations(); if (findDelete != CBSFindDeleteNONE) BTSetRange(alloc, expectedBase, expectedLimit); } return; }
static void deallocate(CBS cbs, Addr block, BT allocTable, Addr base, Addr limit) { Res res; Index ib, il; Bool isAllocated; Addr outerBase = base, outerLimit = limit; /* interval containing [ib, il) */ Addr freeBase, freeLimit; /* interval returned by CBS */ ib = indexOfAddr(block, base); il = indexOfAddr(block, limit); isAllocated = BTIsSetRange(allocTable, ib, il); /* printf("deallocate: [%p, %p) -- %s\n", base, limit, isAllocated ? "succeed" : "fail"); */ NDeallocateTried++; if (isAllocated) { Size left, right, total; /* Sizes of block and two fragments */ /* Find the free blocks adjacent to the allocated block */ if (ib > 0 && !BTGet(allocTable, ib - 1)) { outerBase = addrOfIndex(block, lastEdge(allocTable, ArraySize, ib - 1)); } else { outerBase = base; } if (il < ArraySize && !BTGet(allocTable, il)) { outerLimit = addrOfIndex(block, nextEdge(allocTable, ArraySize, il)); } else { outerLimit = limit; } left = AddrOffset(outerBase, base); right = AddrOffset(limit, outerLimit); total = AddrOffset(outerBase, outerLimit); /* based on detailed knowledge of CBS behaviour */ checkExpectations(); if (total >= MinSize && left < MinSize && right < MinSize) { if (left >= right) expectCallback(&CallbackNew, left, outerBase, outerLimit); else expectCallback(&CallbackNew, right, outerBase, outerLimit); } else if (left >= MinSize && right >= MinSize) { if (left >= right) { expectCallback(&CallbackDelete, right, (Addr)0, (Addr)0); expectCallback(&CallbackGrow, left, outerBase, outerLimit); } else { expectCallback(&CallbackDelete, left, (Addr)0, (Addr)0); expectCallback(&CallbackGrow, right, outerBase, outerLimit); } } else if (total >= MinSize) { if (left >= right) { Insist(left >= MinSize); Insist(right < MinSize); expectCallback(&CallbackGrow, left, outerBase, outerLimit); } else { Insist(left < MinSize); Insist(right >= MinSize); expectCallback(&CallbackGrow, right, outerBase, outerLimit); } } } res = CBSInsertReturningRange(&freeBase, &freeLimit, cbs, base, limit); if (!isAllocated) { die_expect((mps_res_t)res, MPS_RES_FAIL, "succeeded in inserting non-allocated block"); } else { /* isAllocated */ die_expect((mps_res_t)res, MPS_RES_OK, "failed to insert allocated block"); NDeallocateSucceeded++; BTResRange(allocTable, ib, il); checkExpectations(); Insist(freeBase == outerBase); Insist(freeLimit == outerLimit); } }
static void allocate(CBS cbs, Addr block, BT allocTable, Addr base, Addr limit) { Res res; Index ib, il; /* Indexed for base and limit */ Bool isFree; ib = indexOfAddr(block, base); il = indexOfAddr(block, limit); isFree = BTIsResRange(allocTable, ib, il); /* printf("allocate: [%p, %p) -- %s\n", base, limit, isFree ? "succeed" : "fail"); */ NAllocateTried++; if (isFree) { Addr outerBase, outerLimit; /* interval containing [ib, il) */ Size left, right, total; /* Sizes of block and two fragments */ outerBase = addrOfIndex(block, lastEdge(allocTable, ArraySize, ib)); outerLimit = addrOfIndex(block, nextEdge(allocTable, ArraySize, il - 1)); left = AddrOffset(outerBase, base); right = AddrOffset(limit, outerLimit); total = AddrOffset(outerBase, outerLimit); /* based on detailed knowledge of CBS behaviour */ checkExpectations(); if (total >= MinSize && left < MinSize && right < MinSize) { if (left == (Size)0 && right == (Size)0) { expectCallback(&CallbackDelete, total, (Addr)0, (Addr)0); } else if (left >= right) { expectCallback(&CallbackDelete, total, outerBase, base); } else { expectCallback(&CallbackDelete, total, limit, outerLimit); } } else if (left >= MinSize && right >= MinSize) { if (left >= right) { expectCallback(&CallbackShrink, total, outerBase, base); expectCallback(&CallbackNew, (Size)0, limit, outerLimit); } else { expectCallback(&CallbackNew, (Size)0, outerBase, base); expectCallback(&CallbackShrink, total, limit, outerLimit); } } else if (total >= MinSize) { if (left >= right) { Insist(left >= MinSize); Insist(right < MinSize); expectCallback(&CallbackShrink, total, outerBase, base); } else { Insist(left < MinSize); Insist(right >= MinSize); expectCallback(&CallbackShrink, total, limit, outerLimit); } } } res = CBSDelete(cbs, base, limit); if (!isFree) { die_expect((mps_res_t)res, MPS_RES_FAIL, "Succeeded in deleting allocated block"); } else { /* isFree */ die_expect((mps_res_t)res, MPS_RES_OK, "failed to delete free block"); NAllocateSucceeded++; BTSetRange(allocTable, ib, il); checkExpectations(); } }