tdb_mpool.c 2.0 KB
Newer Older
H
refact  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

H
Hongze Cheng 已提交
16
#include "tdb_mpool.h"
H
Hongze Cheng 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

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;
}
H
Hongze Cheng 已提交
72 73 74 75 76 77 78 79 80 81 82

int tdbMPFetchPage(TDB_MPOOL *mp, mp_pgid_t mpgid, void *p) {
  // Search the hash
  // TODO
  return 0;
}

int tdbMpUnfetchPage(TDB_MPOOL *mp, mp_pgid_t mpgid, void *p) {
  // TODO
  return 0;
}