提交 a751c97f 编写于 作者: H Hongze Cheng

more tdb

上级 a243752b
......@@ -13,4 +13,59 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#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;
}
......@@ -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
}
......
......@@ -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<mp_pgid_t, frameid> hash;
int64_t cachesize;
pgsize_t pgsize;
int32_t npages;
MP_PAGE * pages;
MP_PAGE_LIST freeList;
// Hash<mp_pgid_t, frame_id_t>
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册