This commit is contained in:
Hongze Cheng 2022-03-11 02:58:36 +00:00
parent d907e44863
commit 69352daf97
3 changed files with 20 additions and 11 deletions

View File

@ -45,6 +45,8 @@ static void tdbPCachePinPage(SPage *pPage);
static void tdbPCacheRemovePageFromHash(SPage *pPage); static void tdbPCacheRemovePageFromHash(SPage *pPage);
static void tdbPCacheAddPageToHash(SPage *pPage); static void tdbPCacheAddPageToHash(SPage *pPage);
static void tdbPCacheUnpinPage(SPage *pPage); static void tdbPCacheUnpinPage(SPage *pPage);
static void *tdbOsMalloc(void *arg, size_t size);
static void tdbOsFree(void *arg, void *ptr);
int tdbPCacheOpen(int pageSize, int cacheSize, SPCache **ppCache) { int tdbPCacheOpen(int pageSize, int cacheSize, SPCache **ppCache) {
SPCache *pCache; SPCache *pCache;
@ -235,6 +237,7 @@ static int tdbPCacheOpenImpl(SPCache *pCache) {
SPage *pPage; SPage *pPage;
u8 *pPtr; u8 *pPtr;
int tsize; int tsize;
int ret;
tdbPCacheInitLock(pCache); tdbPCacheInitLock(pCache);
@ -242,16 +245,12 @@ static int tdbPCacheOpenImpl(SPCache *pCache) {
pCache->nFree = 0; pCache->nFree = 0;
pCache->pFree = NULL; pCache->pFree = NULL;
for (int i = 0; i < pCache->cacheSize; i++) { for (int i = 0; i < pCache->cacheSize; i++) {
tsize = pCache->pageSize + sizeof(SPage); ret = tdbPageCreate(pCache->pageSize, &pPage, tdbOsMalloc, NULL);
pPtr = (u8 *)calloc(1, tsize); if (ret < 0) {
if (pPtr == NULL) { // TODO: handle error
// TODO
return -1; return -1;
} }
pPage = (SPage *)(&(pPtr[pCache->pageSize]));
TDB_INIT_PAGE_LOCK(pPage);
pPage->pData = (void *)pPtr;
// pPage->pgid = 0; // pPage->pgid = 0;
pPage->isAnchor = 0; pPage->isAnchor = 0;
pPage->isLocalPage = 1; pPage->isLocalPage = 1;
@ -285,4 +284,14 @@ static int tdbPCacheOpenImpl(SPCache *pCache) {
return 0; return 0;
} }
int tdbPCacheGetPageSize(SPCache *pCache) { return pCache->pageSize; } int tdbPCacheGetPageSize(SPCache *pCache) { return pCache->pageSize; }
static void *tdbOsMalloc(void *arg, size_t size) {
void *ptr;
ptr = malloc(size);
return ptr;
}
static void tdbOsFree(void *arg, void *ptr) { free(ptr); }

View File

@ -41,11 +41,11 @@ int tdbPageCreate(int pageSize, SPage **ppPage, void *(*xMalloc)(void *, size_t)
return 0; return 0;
} }
int tdbPageDestroy(SPage *pPage, void (*xFree)(void *)) { int tdbPageDestroy(SPage *pPage, void (*xFree)(void *arg, void *ptr), void *arg) {
u8 *ptr; u8 *ptr;
ptr = pPage->pData; ptr = pPage->pData;
(*xFree)(ptr); (*xFree)(arg, ptr);
return 0; return 0;
} }

View File

@ -95,7 +95,7 @@ struct SPage {
// APIs // APIs
int tdbPageCreate(int pageSize, SPage **ppPage, void *(*xMalloc)(void *, size_t), void *arg); int tdbPageCreate(int pageSize, SPage **ppPage, void *(*xMalloc)(void *, size_t), void *arg);
int tdbPageDestroy(SPage *pPage, void (*xFree)(void *)); int tdbPageDestroy(SPage *pPage, void (*xFree)(void *arg, void *ptr), void *arg);
int tdbPageInsertCell(SPage *pPage, int idx, SCell *pCell, int szCell); int tdbPageInsertCell(SPage *pPage, int idx, SCell *pCell, int szCell);
int tdbPageDropCell(SPage *pPage, int idx); int tdbPageDropCell(SPage *pPage, int idx);