vnodeArenaMAImpl.c 2.9 KB
Newer Older
H
more  
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
refact  
Hongze Cheng 已提交
16
#include "vnd.h"
H
more  
Hongze Cheng 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29

static SVArenaNode *vArenaNodeNew(uint64_t capacity);
static void         vArenaNodeFree(SVArenaNode *pNode);

SVMemAllocator *vmaCreate(uint64_t capacity, uint64_t ssize, uint64_t lsize) {
  SVMemAllocator *pVMA = (SVMemAllocator *)malloc(sizeof(*pVMA));
  if (pVMA == NULL) {
    return NULL;
  }

  pVMA->capacity = capacity;
  pVMA->ssize = ssize;
  pVMA->lsize = lsize;
H
Hongze Cheng 已提交
30
  TD_SLIST_INIT(&(pVMA->nlist));
H
more  
Hongze Cheng 已提交
31

H
more  
Hongze Cheng 已提交
32 33
  pVMA->pNode = vArenaNodeNew(capacity);
  if (pVMA->pNode == NULL) {
H
more  
Hongze Cheng 已提交
34
    free(pVMA);
H
more  
Hongze Cheng 已提交
35 36 37
    return NULL;
  }

H
Hongze Cheng 已提交
38
  TD_SLIST_PUSH(&(pVMA->nlist), pVMA->pNode);
H
more  
Hongze Cheng 已提交
39 40 41 42 43

  return pVMA;
}

void vmaDestroy(SVMemAllocator *pVMA) {
H
more  
Hongze Cheng 已提交
44
  if (pVMA) {
H
more  
Hongze Cheng 已提交
45 46
    while (TD_SLIST_NELES(&(pVMA->nlist)) > 1) {
      SVArenaNode *pNode = TD_SLIST_HEAD(&(pVMA->nlist));
H
Hongze Cheng 已提交
47
      TD_SLIST_POP(&(pVMA->nlist));
H
more  
Hongze Cheng 已提交
48
      vArenaNodeFree(pNode);
H
more  
Hongze Cheng 已提交
49 50 51 52
    }

    free(pVMA);
  }
H
more  
Hongze Cheng 已提交
53 54 55
}

void vmaReset(SVMemAllocator *pVMA) {
H
more  
Hongze Cheng 已提交
56 57
  while (TD_SLIST_NELES(&(pVMA->nlist)) > 1) {
    SVArenaNode *pNode = TD_SLIST_HEAD(&(pVMA->nlist));
H
Hongze Cheng 已提交
58
    TD_SLIST_POP(&(pVMA->nlist));
H
more  
Hongze Cheng 已提交
59 60 61
    vArenaNodeFree(pNode);
  }

H
more  
Hongze Cheng 已提交
62
  SVArenaNode *pNode = TD_SLIST_HEAD(&(pVMA->nlist));
H
more  
Hongze Cheng 已提交
63
  pNode->ptr = pNode->data;
H
more  
Hongze Cheng 已提交
64 65 66
}

void *vmaMalloc(SVMemAllocator *pVMA, uint64_t size) {
H
more  
Hongze Cheng 已提交
67
  SVArenaNode *pNode = TD_SLIST_HEAD(&(pVMA->nlist));
H
more  
Hongze Cheng 已提交
68 69 70
  void *       ptr;

  if (pNode->size < POINTER_DISTANCE(pNode->ptr, pNode->data) + size) {
dengyihao's avatar
dengyihao 已提交
71
    uint64_t capacity = TMAX(pVMA->ssize, size);
H
more  
Hongze Cheng 已提交
72 73 74 75 76 77
    pNode = vArenaNodeNew(capacity);
    if (pNode == NULL) {
      // TODO: handle error
      return NULL;
    }

H
Hongze Cheng 已提交
78
    TD_SLIST_PUSH(&(pVMA->nlist), pNode);
H
more  
Hongze Cheng 已提交
79 80 81 82 83 84
  }

  ptr = pNode->ptr;
  pNode->ptr = POINTER_SHIFT(ptr, size);

  return ptr;
H
more  
Hongze Cheng 已提交
85 86 87 88 89 90
}

void vmaFree(SVMemAllocator *pVMA, void *ptr) {
  // TODO
}

H
more  
Hongze Cheng 已提交
91
bool vmaIsFull(SVMemAllocator *pVMA) {
H
more  
Hongze Cheng 已提交
92
  SVArenaNode *pNode = TD_SLIST_HEAD(&(pVMA->nlist));
H
more  
Hongze Cheng 已提交
93

H
more  
Hongze Cheng 已提交
94
  return (TD_SLIST_NELES(&(pVMA->nlist)) > 1) ||
H
more  
Hongze Cheng 已提交
95
         (pNode->size < POINTER_DISTANCE(pNode->ptr, pNode->data) + pVMA->lsize);
H
more  
Hongze Cheng 已提交
96 97
}

H
more  
Hongze Cheng 已提交
98 99 100
/* ------------------------ STATIC METHODS ------------------------ */
static SVArenaNode *vArenaNodeNew(uint64_t capacity) {
  SVArenaNode *pNode = NULL;
H
more  
Hongze Cheng 已提交
101 102 103 104 105 106 107 108 109

  pNode = (SVArenaNode *)malloc(sizeof(*pNode) + capacity);
  if (pNode == NULL) {
    return NULL;
  }

  pNode->size = capacity;
  pNode->ptr = pNode->data;

H
more  
Hongze Cheng 已提交
110 111 112 113
  return pNode;
}

static void vArenaNodeFree(SVArenaNode *pNode) {
H
more  
Hongze Cheng 已提交
114 115 116
  if (pNode) {
    free(pNode);
  }
H
more  
Hongze Cheng 已提交
117
}