tdb_mpool.c 1.8 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

H
Hongze Cheng 已提交
18
int tdbMPoolOpen(TDB_MPOOL **mpp, uint64_t cachesize, pgsize_t pgsize) {
H
Hongze Cheng 已提交
19 20
  TDB_MPOOL *mp;
  size_t     tsize;
H
refact  
Hongze Cheng 已提交
21
  pg_t *     pagep;
H
Hongze Cheng 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

  // 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;
H
refact  
Hongze Cheng 已提交
40
  mp->pages = (pg_t *)calloc(mp->npages, MP_PAGE_SIZE(pgsize));
H
Hongze Cheng 已提交
41 42 43 44 45 46 47 48 49
  if (mp->pages == NULL) {
    tdbError("failed to malloc memory pool pages");
    free(mp);
    return -1;
  }

  TD_DLIST_INIT(&(mp->freeList));

  mp->nbucket = mp->npages;
H
refact  
Hongze Cheng 已提交
50
  mp->hashtab = (pg_list_t *)calloc(mp->nbucket, sizeof(pg_list_t));
H
Hongze Cheng 已提交
51 52 53 54 55 56 57 58
  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++) {
H
refact  
Hongze Cheng 已提交
59
    pagep = (pg_t *)MP_PAGE_AT(mp, i);
H
Hongze Cheng 已提交
60 61 62 63 64 65 66 67
    TD_DLIST_APPEND(&mp->freeList, pagep);
  }

  // return
  *mpp = mp;
  return 0;
}

H
Hongze Cheng 已提交
68
int tdbMPoolClose(TDB_MPOOL *mp) {
H
Hongze Cheng 已提交
69 70 71
  // TODO
  return 0;
}