fix: free cell decoder's pVal when resetting

This commit is contained in:
Minglei Jin 2022-08-04 19:03:53 +08:00
parent 8dbbd8362e
commit d5c36c6536
1 changed files with 29 additions and 3 deletions

View File

@ -176,6 +176,8 @@ int tdbBtreeInsert(SBTree *pBt, const void *pKey, int kLen, const void *pVal, in
tdbBtcOpen(&btc, pBt, pTxn); tdbBtcOpen(&btc, pBt, pTxn);
tdbTrace("tdb insert, btc: %p, pTxn: %p", &btc, pTxn);
// move to the position to insert // move to the position to insert
ret = tdbBtcMoveTo(&btc, pKey, kLen, &c); ret = tdbBtcMoveTo(&btc, pKey, kLen, &c);
if (ret < 0) { if (ret < 0) {
@ -214,6 +216,8 @@ int tdbBtreeDelete(SBTree *pBt, const void *pKey, int kLen, TXN *pTxn) {
tdbBtcOpen(&btc, pBt, pTxn); tdbBtcOpen(&btc, pBt, pTxn);
tdbTrace("tdb delete, btc: %p, pTxn: %p", &btc, pTxn);
// move the cursor // move the cursor
ret = tdbBtcMoveTo(&btc, pKey, kLen, &c); ret = tdbBtcMoveTo(&btc, pKey, kLen, &c);
if (ret < 0) { if (ret < 0) {
@ -244,6 +248,8 @@ int tdbBtreeUpsert(SBTree *pBt, const void *pKey, int nKey, const void *pData, i
tdbBtcOpen(&btc, pBt, pTxn); tdbBtcOpen(&btc, pBt, pTxn);
tdbTrace("tdb upsert, btc: %p, pTxn: %p", &btc, pTxn);
// move the cursor // move the cursor
ret = tdbBtcMoveTo(&btc, pKey, nKey, &c); ret = tdbBtcMoveTo(&btc, pKey, nKey, &c);
if (ret < 0) { if (ret < 0) {
@ -283,10 +289,12 @@ int tdbBtreePGet(SBTree *pBt, const void *pKey, int kLen, void **ppKey, int *pkL
int ret; int ret;
void *pTKey = NULL; void *pTKey = NULL;
void *pTVal = NULL; void *pTVal = NULL;
SCellDecoder cd; SCellDecoder cd = {0};
tdbBtcOpen(&btc, pBt, NULL); tdbBtcOpen(&btc, pBt, NULL);
tdbTrace("tdb pget, btc: %p", &btc);
ret = tdbBtcMoveTo(&btc, pKey, kLen, &cret); ret = tdbBtcMoveTo(&btc, pKey, kLen, &cret);
if (ret < 0) { if (ret < 0) {
tdbBtcClose(&btc); tdbBtcClose(&btc);
@ -295,6 +303,7 @@ int tdbBtreePGet(SBTree *pBt, const void *pKey, int kLen, void **ppKey, int *pkL
if (btc.idx < 0 || cret) { if (btc.idx < 0 || cret) {
tdbBtcClose(&btc); tdbBtcClose(&btc);
return -1; return -1;
} }
@ -330,9 +339,13 @@ int tdbBtreePGet(SBTree *pBt, const void *pKey, int kLen, void **ppKey, int *pkL
} }
if (TDB_CELLDECODER_FREE_VAL(&cd)) { if (TDB_CELLDECODER_FREE_VAL(&cd)) {
tdbDebug("tdb btc/pget/2 decoder: %p pVal free: %p", &cd, cd.pVal);
tdbFree(cd.pVal); tdbFree(cd.pVal);
} }
tdbTrace("tdb pget end, btc decoder: %p/0x%x, local decoder:%p", &btc.coder, btc.coder.freeKV, &cd);
tdbBtcClose(&btc); tdbBtcClose(&btc);
return 0; return 0;
@ -1235,6 +1248,8 @@ static int tdbBtreeDecodePayload(SPage *pPage, const SCell *pCell, int nHeader,
} }
TDB_CELLDECODER_SET_FREE_VAL(pDecoder); TDB_CELLDECODER_SET_FREE_VAL(pDecoder);
tdbDebug("tdb btc decoder: %p/0x%x pVal: %p ", pDecoder, pDecoder->freeKV, pDecoder->pVal);
memcpy(pDecoder->pVal, pCell + nHeader + kLen, nLocal - kLen - sizeof(SPgno)); memcpy(pDecoder->pVal, pCell + nHeader + kLen, nLocal - kLen - sizeof(SPgno));
nLeft -= nLocal - kLen - sizeof(SPgno); nLeft -= nLocal - kLen - sizeof(SPgno);
@ -1376,6 +1391,9 @@ static int tdbBtreeDecodeCell(SPage *pPage, const SCell *pCell, SCellDecoder *pD
leaf = TDB_BTREE_PAGE_IS_LEAF(pPage); leaf = TDB_BTREE_PAGE_IS_LEAF(pPage);
// Clear the state of decoder // Clear the state of decoder
if (TDB_CELLDECODER_FREE_VAL(pDecoder)) {
tdbFree(pDecoder->pVal);
}
pDecoder->kLen = -1; pDecoder->kLen = -1;
pDecoder->pKey = NULL; pDecoder->pKey = NULL;
pDecoder->vLen = -1; pDecoder->vLen = -1;
@ -1383,6 +1401,8 @@ static int tdbBtreeDecodeCell(SPage *pPage, const SCell *pCell, SCellDecoder *pD
pDecoder->pgno = 0; pDecoder->pgno = 0;
TDB_CELLDECODER_SET_FREE_NIL(pDecoder); TDB_CELLDECODER_SET_FREE_NIL(pDecoder);
tdbDebug("tdb btc decoder set nil: %p/0x%x ", pDecoder, pDecoder->freeKV);
// 1. Decode header part // 1. Decode header part
if (!leaf) { if (!leaf) {
ASSERT(pPage->vLen == sizeof(SPgno)); ASSERT(pPage->vLen == sizeof(SPgno));
@ -1650,7 +1670,7 @@ int tdbBtcMoveToLast(SBTC *pBtc) {
int tdbBtreeNext(SBTC *pBtc, void **ppKey, int *kLen, void **ppVal, int *vLen) { int tdbBtreeNext(SBTC *pBtc, void **ppKey, int *kLen, void **ppVal, int *vLen) {
SCell *pCell; SCell *pCell;
SCellDecoder cd; SCellDecoder cd = {0};
void *pKey, *pVal; void *pKey, *pVal;
int ret; int ret;
@ -2037,7 +2057,7 @@ int tdbBtcMoveTo(SBTC *pBtc, const void *pKey, int kLen, int *pCRst) {
const void *pTKey; const void *pTKey;
int tkLen; int tkLen;
tdbTrace("ttl moveto, pager:%p, ipage:%d", pPager, pBtc->iPage); tdbTrace("tdb moveto, pager:%p, ipage:%d", pPager, pBtc->iPage);
if (pBtc->iPage < 0) { if (pBtc->iPage < 0) {
// move from a clear cursor // move from a clear cursor
ret = tdbPagerFetchPage(pPager, &pBt->root, &(pBtc->pPage), tdbBtreeInitPage, ret = tdbPagerFetchPage(pPager, &pBt->root, &(pBtc->pPage), tdbBtreeInitPage,
@ -2093,6 +2113,7 @@ int tdbBtcMoveTo(SBTC *pBtc, const void *pKey, int kLen, int *pCRst) {
} }
// search downward to the leaf // search downward to the leaf
tdbTrace("tdb search downward, pager:%p, ipage:%d", pPager, pBtc->iPage);
for (;;) { for (;;) {
int lidx, ridx; int lidx, ridx;
SPage *pPage; SPage *pPage;
@ -2127,6 +2148,7 @@ int tdbBtcMoveTo(SBTC *pBtc, const void *pKey, int kLen, int *pCRst) {
} }
// binary search // binary search
tdbTrace("tdb binary search, pager:%p, ipage:%d", pPager, pBtc->iPage);
for (;;) { for (;;) {
if (lidx > ridx) break; if (lidx > ridx) break;
@ -2157,6 +2179,8 @@ int tdbBtcMoveTo(SBTC *pBtc, const void *pKey, int kLen, int *pCRst) {
} }
} }
tdbTrace("tdb moveto end, pager:%p, ipage:%d", pPager, pBtc->iPage);
return 0; return 0;
} }
@ -2176,6 +2200,8 @@ int tdbBtcClose(SBTC *pBtc) {
} }
if (TDB_CELLDECODER_FREE_VAL(&pBtc->coder)) { if (TDB_CELLDECODER_FREE_VAL(&pBtc->coder)) {
tdbDebug("tdb btc/close decoder: %p pVal free: %p", &pBtc->coder, pBtc->coder.pVal);
tdbFree(pBtc->coder.pVal); tdbFree(pBtc->coder.pVal);
} }