diff --git a/source/libs/tdb/src/db/tdb_mpool.c b/source/libs/tdb/src/db/tdb_mpool.c index ac4018d348..6d4f7c4bdc 100644 --- a/source/libs/tdb/src/db/tdb_mpool.c +++ b/source/libs/tdb/src/db/tdb_mpool.c @@ -13,4 +13,59 @@ * along with this program. If not, see . */ -#include "tdb_mpool.h" \ No newline at end of file +#include "tdb_mp.h" + +int tdbOpenMP(TDB_MPOOL **mpp, uint64_t cachesize, pgsize_t pgsize) { + TDB_MPOOL *mp; + size_t tsize; + MP_PAGE * pagep; + + // check parameters + if (!TDB_IS_PGSIZE_VLD(pgsize)) { + tdbError("invalid page size"); + return -1; + } + + // allocate handle + mp = (TDB_MPOOL *)calloc(1, sizeof(*mp)); + if (mp == NULL) { + tdbError("failed to malloc memory pool handle"); + return -1; + } + + // initialize the handle + mp->cachesize = cachesize; + mp->pgsize = pgsize; + mp->npages = cachesize / pgsize; + mp->pages = (MP_PAGE *)calloc(mp->npages, MP_PAGE_SIZE(pgsize)); + if (mp->pages == NULL) { + tdbError("failed to malloc memory pool pages"); + free(mp); + return -1; + } + + TD_DLIST_INIT(&(mp->freeList)); + + mp->nbucket = mp->npages; + mp->hashtab = (MP_PAGE_LIST *)calloc(mp->nbucket, sizeof(MP_PAGE_LIST)); + if (mp->hashtab == NULL) { + tdbError("failed to malloc memory pool hash table"); + free(mp->pages); + free(mp); + return -1; + } + + for (int i = 0; i < mp->npages; i++) { + pagep = (MP_PAGE *)MP_PAGE_AT(mp, i); + TD_DLIST_APPEND(&mp->freeList, pagep); + } + + // return + *mpp = mp; + return 0; +} + +int tdbCloseMP(TDB_MPOOL *mp) { + // TODO + return 0; +} diff --git a/source/libs/tdb/src/inc/tdb_inc.h b/source/libs/tdb/src/inc/tdb_inc.h index cce6bc3bda..2eed9a8dc7 100644 --- a/source/libs/tdb/src/inc/tdb_inc.h +++ b/source/libs/tdb/src/inc/tdb_inc.h @@ -35,7 +35,13 @@ typedef int32_t pgsize_t; #define TDB_MIN_PGSIZE 512 #define TDB_MAX_PGSIZE 16384 #define TDB_DEFAULT_PGSIZE 4096 -#define TDB_IS_PGSIZE_VLD(s) (((s) >= TKV_MIN_PGSIZE) && (TKV_MAX_PGSIZE <= TKV_MAX_PGSIZE)) +#define TDB_IS_PGSIZE_VLD(s) (((s) >= TDB_MIN_PGSIZE) && ((s) <= TDB_MAX_PGSIZE)) + +// fileid +#define TDB_FILE_UID_LEN 20 + +// tdb_log +#define tdbError(var) #ifdef __cplusplus } diff --git a/source/libs/tdb/src/inc/tdb_mpool.h b/source/libs/tdb/src/inc/tdb_mp.h similarity index 68% rename from source/libs/tdb/src/inc/tdb_mpool.h rename to source/libs/tdb/src/inc/tdb_mp.h index 8d113e5edc..d4e7457b05 100644 --- a/source/libs/tdb/src/inc/tdb_mpool.h +++ b/source/libs/tdb/src/inc/tdb_mp.h @@ -25,39 +25,44 @@ extern "C" { // Exposed handle typedef struct TDB_MPOOL TDB_MPOOL; -#define TDB_FILE_UID_LEN 20 typedef struct { uint8_t fuid[TDB_FILE_UID_LEN]; pgid_t pgid; } mp_pgid_t; -// Exposed apis -int tdbOpenMP(TDB_MPOOL **mpp, uint64_t cachesize, pgsize_t pgsize); -int tdbCloseMP(TDB_MPOOL *mp); -int tdbMPFetchPage(TDB_MPOOL *mp, mp_pgid_t mpgid, void *p); -int tdbMpUnfetchPage(TDB_MPOOL *mp, mp_pgid_t mpgid, void *p); - -// Hidden impls - typedef struct MP_PAGE { // SRWLatch rwLatch; mp_pgid_t mpgid; uint8_t dirty; int32_t pinRef; - // TD_DLIST_NODE(MP_PAGE); // The free list handle + TD_DLIST_NODE(MP_PAGE); char *page[]; } MP_PAGE; +#define MP_PAGE_SIZE(pgsize) (sizeof(MP_PAGE) + (pgsize)) + +typedef TD_DLIST(MP_PAGE) MP_PAGE_LIST; struct TDB_MPOOL { - pthread_mutex_t mutex; - int64_t cachesize; - pgsize_t pgsize; - MP_PAGE * pages; - // TD_DBLIST(MP_PAGE) freeList; - // TD_DLIST(TD_MPFILE) mpfList; // MPFILE registered on this memory pool - // Hash hash; + int64_t cachesize; + pgsize_t pgsize; + int32_t npages; + MP_PAGE * pages; + MP_PAGE_LIST freeList; + // Hash + int32_t nbucket; + MP_PAGE_LIST *hashtab; + // TODO: TD_DLIST(TD_MPFILE) mpfList; // MPFILE registered on this memory pool }; +#define MP_PAGE_AT(mp, idx) ((char *)((mp)->pages) + MP_PAGE_SIZE((mp)->pgsize) * (idx)) + +// Exposed apis ===================================================================================================== + +int tdbOpenMP(TDB_MPOOL **mpp, uint64_t cachesize, pgsize_t pgsize); +int tdbCloseMP(TDB_MPOOL *mp); +int tdbMPFetchPage(TDB_MPOOL *mp, mp_pgid_t mpgid, void *p); +int tdbMpUnfetchPage(TDB_MPOOL *mp, mp_pgid_t mpgid, void *p); + #ifdef __cplusplus } #endif