tidpool.c 3.7 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
/*
 * 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/>.
 */

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tlog.h"

typedef struct {
  int             maxId;
  int             numOfFree;
  int             freeSlot;
  int *           freeList;
  pthread_mutex_t mutex;
} id_pool_t;

void *taosInitIdPool(int maxId) {
  id_pool_t *pIdPool;
  int *      idList, i;

  if (maxId < 3) maxId = 3;

  pIdPool = (id_pool_t *)malloc(sizeof(id_pool_t));
  if (pIdPool == NULL) return NULL;

  idList = (int *)malloc(sizeof(int) * (size_t)maxId);
  if (idList == NULL) {
    free(pIdPool);
    return NULL;
  }

  memset(pIdPool, 0, sizeof(id_pool_t));
  pIdPool->maxId = maxId;
  pIdPool->numOfFree = maxId - 1;
  pIdPool->freeSlot = 0;
  pIdPool->freeList = idList;

  pthread_mutex_init(&pIdPool->mutex, NULL);

  for (i = 1; i < maxId; ++i) idList[i - 1] = i;

  pTrace("pool:%p is setup, maxId:%d", pIdPool, pIdPool->maxId);

  return (void *)pIdPool;
}

int taosAllocateId(void *handle) {
  id_pool_t *pIdPool;
  int        id = -1;
  if (handle == NULL) return id;

  pIdPool = (id_pool_t *)handle;

  if (pIdPool->maxId < 3) pError("pool:%p is messed up, maxId:%d", pIdPool, pIdPool->maxId);

  if (pthread_mutex_lock(&pIdPool->mutex) != 0) perror("lock pIdPool Mutex");

  if (pIdPool->numOfFree > 0) {
    id = pIdPool->freeList[pIdPool->freeSlot];
    pIdPool->freeSlot = (pIdPool->freeSlot + 1) % pIdPool->maxId;
    pIdPool->numOfFree--;
  }

  if (pthread_mutex_unlock(&pIdPool->mutex) != 0) perror("unlock pIdPool Mutex");

  return id;
}

void taosFreeId(void *handle, int id) {
  id_pool_t *pIdPool;
  int        slot;

  pIdPool = (id_pool_t *)handle;
  if (pIdPool->freeList == NULL || pIdPool->maxId == 0) return;
  if (id <= 0 || id >= pIdPool->maxId) return;
  if (pthread_mutex_lock(&pIdPool->mutex) != 0) perror("lock pIdPool Mutex");

  slot = (pIdPool->freeSlot + pIdPool->numOfFree) % pIdPool->maxId;
  pIdPool->freeList[slot] = id;
  pIdPool->numOfFree++;

  if (pthread_mutex_unlock(&pIdPool->mutex) != 0) perror("unlock pIdPool Mutex");
}

void taosIdPoolCleanUp(void *handle) {
  id_pool_t *pIdPool;

  if (handle == NULL) return;
  pIdPool = (id_pool_t *)handle;

  pTrace("pool:%p is cleaned", pIdPool);

  if (pIdPool->freeList) free(pIdPool->freeList);

  pthread_mutex_destroy(&pIdPool->mutex);

  memset(pIdPool, 0, sizeof(id_pool_t));

  free(pIdPool);
}

int taosIdPoolNumOfUsed(void *handle) {
  id_pool_t *pIdPool = (id_pool_t *)handle;

  return pIdPool->maxId - pIdPool->numOfFree - 1;
}

void taosIdPoolReinit(void *handle) {
  id_pool_t *pIdPool;

  pIdPool = (id_pool_t *)handle;
  pIdPool->numOfFree = 0;
  pIdPool->freeSlot = 0;

  for (int i = 0; i < pIdPool->maxId; ++i) pIdPool->freeList[i] = 0;
}

void taosIdPoolMarkStatus(void *handle, int id, int status) {
  id_pool_t *pIdPool = (id_pool_t *)handle;

  pIdPool->freeList[id] = status;
}

void taosIdPoolSetFreeList(void *handle) {
  id_pool_t *pIdPool;
  int        pos = 0;

  pIdPool = (id_pool_t *)handle;
  pIdPool->numOfFree = 0;
  pIdPool->freeSlot = 0;

  for (int i = 1; i < pIdPool->maxId; ++i) {
    if (pIdPool->freeList[i] == 0) {
      pIdPool->freeList[pos] = i;
      pIdPool->numOfFree++;
      pos++;
    }
  }
}