fix logical bug with memory leak
This commit is contained in:
parent
0b12117fb2
commit
dbec5f1fa4
|
@ -746,6 +746,7 @@ static int tdbBtreeBalanceNonRoot(SBTree *pBt, SPage *pParent, int idx) {
|
||||||
pgno = TDB_PAGE_PGNO(pNews[iNew]);
|
pgno = TDB_PAGE_PGNO(pNews[iNew]);
|
||||||
tdbBtreeEncodeCell(pParent, cd.pKey, cd.kLen, (void *)&pgno, sizeof(SPgno), pNewCell, &szNewCell);
|
tdbBtreeEncodeCell(pParent, cd.pKey, cd.kLen, (void *)&pgno, sizeof(SPgno), pNewCell, &szNewCell);
|
||||||
tdbPageInsertCell(pParent, sIdx++, pNewCell, szNewCell, 0);
|
tdbPageInsertCell(pParent, sIdx++, pNewCell, szNewCell, 0);
|
||||||
|
free(pNewCell);
|
||||||
}
|
}
|
||||||
|
|
||||||
// move to next new page
|
// move to next new page
|
||||||
|
@ -795,12 +796,11 @@ static int tdbBtreeBalanceNonRoot(SBTree *pBt, SPage *pParent, int idx) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: here has memory leak
|
for (int i = 0; i < 3; i++) {
|
||||||
// for (int i = 0; i < 3; i++) {
|
if (pDivCell[i]) {
|
||||||
// if (pDivCell[i]) {
|
free(pDivCell[i]);
|
||||||
// free(pDivCell[i]);
|
}
|
||||||
// }
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -143,7 +143,11 @@ int tdbPageInsertCell(SPage *pPage, int idx, SCell *pCell, int szCell, u8 asOvfl
|
||||||
pPage->aiOvfl[i] = pPage->aiOvfl[i - 1];
|
pPage->aiOvfl[i] = pPage->aiOvfl[i - 1];
|
||||||
}
|
}
|
||||||
|
|
||||||
pPage->apOvfl[iOvfl] = pCell;
|
// TODO: here has memory leak
|
||||||
|
pNewCell = (SCell *)malloc(szCell);
|
||||||
|
memcpy(pNewCell, pCell, szCell);
|
||||||
|
|
||||||
|
pPage->apOvfl[iOvfl] = pNewCell;
|
||||||
pPage->aiOvfl[iOvfl] = idx;
|
pPage->aiOvfl[iOvfl] = idx;
|
||||||
pPage->nOverflow++;
|
pPage->nOverflow++;
|
||||||
iOvfl++;
|
iOvfl++;
|
||||||
|
|
|
@ -4,8 +4,112 @@
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
static int tKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
|
typedef struct SPoolMem {
|
||||||
static int tDefaultKeyCmpr(const void *pKey1, int keyLen1, const void *pKey2, int keyLen2);
|
int64_t size;
|
||||||
|
struct SPoolMem *prev;
|
||||||
|
struct SPoolMem *next;
|
||||||
|
} SPoolMem;
|
||||||
|
|
||||||
|
static SPoolMem *openPool() {
|
||||||
|
SPoolMem *pPool = (SPoolMem *)malloc(sizeof(*pPool));
|
||||||
|
|
||||||
|
pPool->prev = pPool->next = pPool;
|
||||||
|
pPool->size = 0;
|
||||||
|
|
||||||
|
return pPool;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void closePool(SPoolMem *pPool) {
|
||||||
|
SPoolMem *pMem;
|
||||||
|
|
||||||
|
do {
|
||||||
|
pMem = pPool->next;
|
||||||
|
|
||||||
|
if (pMem == pPool) break;
|
||||||
|
|
||||||
|
pMem->next->prev = pMem->prev;
|
||||||
|
pMem->prev->next = pMem->next;
|
||||||
|
pPool->size -= pMem->size;
|
||||||
|
|
||||||
|
free(pMem);
|
||||||
|
} while (1);
|
||||||
|
|
||||||
|
assert(pPool->size == 0);
|
||||||
|
|
||||||
|
free(pPool);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void *poolMalloc(void *arg, int size) {
|
||||||
|
void *ptr = NULL;
|
||||||
|
SPoolMem *pPool = (SPoolMem *)arg;
|
||||||
|
SPoolMem *pMem;
|
||||||
|
|
||||||
|
pMem = (SPoolMem *)malloc(sizeof(*pMem) + size);
|
||||||
|
if (pMem == NULL) {
|
||||||
|
assert(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
pMem->size = sizeof(*pMem) + size;
|
||||||
|
pMem->next = pPool->next;
|
||||||
|
pMem->prev = pPool;
|
||||||
|
|
||||||
|
pPool->next->prev = pMem;
|
||||||
|
pPool->next = pMem;
|
||||||
|
pPool->size += pMem->size;
|
||||||
|
|
||||||
|
ptr = (void *)(&pMem[1]);
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void poolFree(void *arg, void *ptr) {
|
||||||
|
SPoolMem *pPool = (SPoolMem *)arg;
|
||||||
|
SPoolMem *pMem;
|
||||||
|
|
||||||
|
pMem = &(((SPoolMem *)ptr)[-1]);
|
||||||
|
|
||||||
|
pMem->next->prev = pMem->prev;
|
||||||
|
pMem->prev->next = pMem->next;
|
||||||
|
pPool->size -= pMem->size;
|
||||||
|
|
||||||
|
free(pMem);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int tKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
|
||||||
|
int k1, k2;
|
||||||
|
|
||||||
|
std::string s1((char *)pKey1 + 3, kLen1 - 3);
|
||||||
|
std::string s2((char *)pKey2 + 3, kLen2 - 3);
|
||||||
|
k1 = stoi(s1);
|
||||||
|
k2 = stoi(s2);
|
||||||
|
|
||||||
|
if (k1 < k2) {
|
||||||
|
return -1;
|
||||||
|
} else if (k1 > k2) {
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int tDefaultKeyCmpr(const void *pKey1, int keyLen1, const void *pKey2, int keyLen2) {
|
||||||
|
int mlen;
|
||||||
|
int cret;
|
||||||
|
|
||||||
|
ASSERT(keyLen1 > 0 && keyLen2 > 0 && pKey1 != NULL && pKey2 != NULL);
|
||||||
|
|
||||||
|
mlen = keyLen1 < keyLen2 ? keyLen1 : keyLen2;
|
||||||
|
cret = memcmp(pKey1, pKey2, mlen);
|
||||||
|
if (cret == 0) {
|
||||||
|
if (keyLen1 < keyLen2) {
|
||||||
|
cret = -1;
|
||||||
|
} else if (keyLen1 > keyLen2) {
|
||||||
|
cret = 1;
|
||||||
|
} else {
|
||||||
|
cret = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return cret;
|
||||||
|
}
|
||||||
|
|
||||||
TEST(tdb_test, simple_test) {
|
TEST(tdb_test, simple_test) {
|
||||||
int ret;
|
int ret;
|
||||||
|
@ -94,41 +198,4 @@ TEST(tdb_test, simple_test) {
|
||||||
// Close Env
|
// Close Env
|
||||||
ret = tdbEnvClose(pEnv);
|
ret = tdbEnvClose(pEnv);
|
||||||
GTEST_ASSERT_EQ(ret, 0);
|
GTEST_ASSERT_EQ(ret, 0);
|
||||||
}
|
|
||||||
|
|
||||||
static int tKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
|
|
||||||
int k1, k2;
|
|
||||||
|
|
||||||
std::string s1((char *)pKey1 + 3, kLen1 - 3);
|
|
||||||
std::string s2((char *)pKey2 + 3, kLen2 - 3);
|
|
||||||
k1 = stoi(s1);
|
|
||||||
k2 = stoi(s2);
|
|
||||||
|
|
||||||
if (k1 < k2) {
|
|
||||||
return -1;
|
|
||||||
} else if (k1 > k2) {
|
|
||||||
return 1;
|
|
||||||
} else {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static int tDefaultKeyCmpr(const void *pKey1, int keyLen1, const void *pKey2, int keyLen2) {
|
|
||||||
int mlen;
|
|
||||||
int cret;
|
|
||||||
|
|
||||||
ASSERT(keyLen1 > 0 && keyLen2 > 0 && pKey1 != NULL && pKey2 != NULL);
|
|
||||||
|
|
||||||
mlen = keyLen1 < keyLen2 ? keyLen1 : keyLen2;
|
|
||||||
cret = memcmp(pKey1, pKey2, mlen);
|
|
||||||
if (cret == 0) {
|
|
||||||
if (keyLen1 < keyLen2) {
|
|
||||||
cret = -1;
|
|
||||||
} else if (keyLen1 > keyLen2) {
|
|
||||||
cret = 1;
|
|
||||||
} else {
|
|
||||||
cret = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return cret;
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue