This commit is contained in:
Hongze Cheng 2022-03-20 09:45:49 +00:00
parent d3446a5c7a
commit 57a92869ae
1 changed files with 17 additions and 29 deletions

View File

@ -141,6 +141,7 @@ static int tdbPageAllocate(SPage *pPage, int szCell, SCell **ppCell) {
int nFree; int nFree;
int ret; int ret;
int cellFree; int cellFree;
SCell *pCell = NULL;
*ppCell = NULL; *ppCell = NULL;
nFree = TDB_PAGE_NFREE(pPage); nFree = TDB_PAGE_NFREE(pPage);
@ -150,14 +151,10 @@ static int tdbPageAllocate(SPage *pPage, int szCell, SCell **ppCell) {
// 1. Try to allocate from the free space block area // 1. Try to allocate from the free space block area
if (pPage->pFreeEnd - pPage->pFreeStart >= szCell + TDB_PAGE_OFFSET_SIZE(pPage)) { if (pPage->pFreeEnd - pPage->pFreeStart >= szCell + TDB_PAGE_OFFSET_SIZE(pPage)) {
pPage->pFreeStart += TDB_PAGE_OFFSET_SIZE(pPage);
pPage->pFreeEnd -= szCell; pPage->pFreeEnd -= szCell;
pCell = pPage->pFreeEnd;
TDB_PAGE_CCELLS_SET(pPage, pPage->pFreeEnd - pPage->pData); TDB_PAGE_CCELLS_SET(pPage, pPage->pFreeEnd - pPage->pData);
TDB_PAGE_NFREE_SET(pPage, nFree - szCell - TDB_PAGE_OFFSET_SIZE(pPage)); goto _alloc_finish;
*ppCell = pPage->pFreeEnd;
return 0;
} }
// 2. Try to allocate from the page free list // 2. Try to allocate from the page free list
@ -177,9 +174,7 @@ static int tdbPageAllocate(SPage *pPage, int szCell, SCell **ppCell) {
pPage->pPageMethods->getFreeCellInfo(pFreeCell, &szFreeCell, &nxFreeCell); pPage->pPageMethods->getFreeCellInfo(pFreeCell, &szFreeCell, &nxFreeCell);
if (szFreeCell >= szCell) { if (szFreeCell >= szCell) {
pPage->pFreeStart += TDB_PAGE_OFFSET_SIZE(pPage); pCell = pFreeCell;
TDB_PAGE_NFREE_SET(pPage, nFree - TDB_PAGE_OFFSET_SIZE(pPage) - szCell);
*ppCell = pFreeCell;
newSize = szFreeCell - szCell; newSize = szFreeCell - szCell;
pFreeCell += szCell; pFreeCell += szCell;
@ -191,7 +186,7 @@ static int tdbPageAllocate(SPage *pPage, int szCell, SCell **ppCell) {
TDB_PAGE_FCELL_SET(pPage, pFreeCell - pPage->pData); TDB_PAGE_FCELL_SET(pPage, pFreeCell - pPage->pData);
} }
return 0; goto _alloc_finish;
} else { } else {
if (pPrevFreeCell) { if (pPrevFreeCell) {
pPage->pPageMethods->setFreeCellInfo(pPrevFreeCell, szPrevFreeCell, nxFreeCell); pPage->pPageMethods->setFreeCellInfo(pPrevFreeCell, szPrevFreeCell, nxFreeCell);
@ -207,28 +202,21 @@ static int tdbPageAllocate(SPage *pPage, int szCell, SCell **ppCell) {
} }
} }
// 3. Try to dfragment and allocate again // 3. Try to dfragment and allocate again
#if 0 tdbPageDefragment(pPage);
if (pCell == NULL) { ASSERT(pPage->pFreeEnd - pPage->pFreeStart == nFree);
ret = tdbPageDefragment(pPage); ASSERT(nFree == TDB_PAGE_NFREE(pPage));
if (ret < 0) { ASSERT(pPage->pFreeEnd - pPage->pData == TDB_PAGE_CCELLS(pPage));
return -1;
}
ASSERT(pPage->pFreeEnd - pPage->pFreeStart > size + TDB_PAGE_OFFSET_SIZE(pPage)); pPage->pFreeEnd -= szCell;
// ASSERT(pPage->nFree == pPage->pFreeEnd - pPage->pFreeStart); pCell = pPage->pFreeEnd;
TDB_PAGE_CCELLS_SET(pPage, pPage->pFreeEnd - pPage->pData);
// Allocate from the free space area again _alloc_finish:
pPage->pFreeEnd -= size; ASSERT(pCell);
pPage->pFreeStart += TDB_PAGE_OFFSET_SIZE(pPage); pPage->pFreeStart += TDB_PAGE_OFFSET_SIZE(pPage);
pCell = pPage->pFreeEnd; TDB_PAGE_NFREE_SET(pPage, nFree - szCell - TDB_PAGE_OFFSET_SIZE(pPage));
}
ASSERT(pCell != NULL);
// pPage->nFree = pPage->nFree - size - TDB_PAGE_OFFSET_SIZE(pPage);
*ppCell = pCell; *ppCell = pCell;
#endif
return 0; return 0;
} }