ttimer.c 16.5 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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
/*
 * 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 <assert.h>
#include <errno.h>
#include <pthread.h>
#include <semaphore.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <unistd.h>

#include "tidpool.h"
#include "tlog.h"
#include "tsched.h"
#include "ttimer.h"
#include "tutil.h"

// special mempool without mutex
#define mpool_h void *

typedef struct {
  int   numOfFree;  /* number of free slots */
  int   first;      /* the first free slot  */
  int   numOfBlock; /* the number of blocks */
  int   blockSize;  /* block size in bytes  */
  int * freeList;   /* the index list       */
  char *pool;       /* the actual mem block */
} pool_t;

mpool_h tmrMemPoolInit(int maxNum, int blockSize);
char *tmrMemPoolMalloc(mpool_h handle);
void tmrMemPoolFree(mpool_h handle, char *p);
void tmrMemPoolCleanUp(mpool_h handle);

#define tmrError(...)                                 \
  if (tmrDebugFlag & DEBUG_ERROR) {                   \
    tprintf("ERROR TMR ", tmrDebugFlag, __VA_ARGS__); \
  }
#define tmrWarn(...)                                  \
  if (tmrDebugFlag & DEBUG_WARN) {                    \
    tprintf("WARN  TMR ", tmrDebugFlag, __VA_ARGS__); \
  }
#define tmrTrace(...)                           \
  if (tmrDebugFlag & DEBUG_TRACE) {             \
    tprintf("TMR ", tmrDebugFlag, __VA_ARGS__); \
  }

#define maxNumOfTmrCtrl 512
#define MSECONDS_PER_TICK 5

typedef struct _tmr_obj {
  void *param1;
  void (*fp)(void *, void *);
  tmr_h               timerId;
  short               cycle;
  struct _tmr_obj *   prev;
  struct _tmr_obj *   next;
  int                 index;
  struct _tmr_ctrl_t *pCtrl;
} tmr_obj_t;

typedef struct {
  tmr_obj_t *head;
  int        count;
} tmr_list_t;

typedef struct _tmr_ctrl_t {
  void *          signature;
  pthread_mutex_t mutex;            /* mutex to protect critical resource */
  int             resolution;       /* resolution in mseconds */
  int             numOfPeriods;     /* total number of periods */
  int64_t         periodsFromStart; /* count number of periods since start */
  pthread_t       thread;           /* timer thread ID */
  tmr_list_t *    tmrList;
  mpool_h         poolHandle;
  char            label[12];
  int             maxNumOfTmrs;
  int             numOfTmrs;
  int             ticks;
  int             maxTicks;
  int             tmrCtrlId;
} tmr_ctrl_t;

int  tmrDebugFlag = DEBUG_ERROR | DEBUG_WARN | DEBUG_FILE;
void taosTmrProcessList(tmr_ctrl_t *);

tmr_ctrl_t tmrCtrl[maxNumOfTmrCtrl];
int        numOfTmrCtrl = 0;
void *     tmrIdPool = NULL;
void *     tmrQhandle;
int        taosTmrThreads = 1;

void *taosTimerLoopFunc(int signo) {
  tmr_ctrl_t *pCtrl;
  int         count = 0;

  for (int i = 1; i < maxNumOfTmrCtrl; ++i) {
    pCtrl = tmrCtrl + i;
    if (pCtrl->signature) {
      count++;
      pCtrl->ticks++;
      if (pCtrl->ticks >= pCtrl->maxTicks) {
        taosTmrProcessList(pCtrl);
        pCtrl->ticks = 0;
      }
      if (count >= numOfTmrCtrl) break;
    }
  }

  return NULL;
}

void *taosProcessAlarmSignal(void *tharg) {
  // Block the signal
  sigset_t sigset;
  sigemptyset(&sigset);
  sigaddset(&sigset, SIGALRM);
  sigprocmask(SIG_BLOCK, &sigset, NULL);

  timer_t         timerId;
  struct sigevent sevent;
  sevent.sigev_notify = SIGEV_THREAD_ID;
  sevent._sigev_un._tid = syscall(__NR_gettid);
  sevent.sigev_signo = SIGALRM;

  if (timer_create(CLOCK_REALTIME, &sevent, &timerId) == -1) {
    tmrError("Failed to create timer");
  }

  struct itimerspec ts;
  ts.it_value.tv_sec = 0;
  ts.it_value.tv_nsec = 1000000 * MSECONDS_PER_TICK;
  ts.it_interval.tv_sec = 0;
  ts.it_interval.tv_nsec = 1000000 * MSECONDS_PER_TICK;

  if (timer_settime(timerId, 0, &ts, NULL)) {
    tmrError("Failed to init timer");
    return NULL;
  }

  int signo;
  while (1) {
    if (sigwait(&sigset, &signo)) {
      tmrError("Failed to wait signal: number %d", signo);
      continue;
    }
    /* printf("Signal handling: number %d ......\n", signo); */

    taosTimerLoopFunc(0);
  }

  assert(0);
  return NULL;
}

void taosTmrModuleInit(void) {
  tmrIdPool = taosInitIdPool(maxNumOfTmrCtrl);
  memset(tmrCtrl, 0, sizeof(tmrCtrl));

  pthread_t      thread;
  pthread_attr_t tattr;
  pthread_attr_init(&tattr);
  pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
  if (pthread_create(&thread, &tattr, taosProcessAlarmSignal, NULL) != 0) {
    tmrError("failed to create timer thread");
    return;
  }

  pthread_attr_destroy(&tattr);

  tmrQhandle = taosInitScheduler(10000, taosTmrThreads, "tmr");
  tmrTrace("timer module is initialized, thread:%d", taosTmrThreads);
}

void *taosTmrInit(int maxNumOfTmrs, int resolution, int longest, char *label) {
  static pthread_once_t tmrInit = PTHREAD_ONCE_INIT;
  tmr_ctrl_t *          pCtrl;

  pthread_once(&tmrInit, taosTmrModuleInit);

  int tmrCtrlId = taosAllocateId(tmrIdPool);

  if (tmrCtrlId < 0) {
    tmrError("%s bug!!! too many timers!!!", label);
    return NULL;
  }

  pCtrl = tmrCtrl + tmrCtrlId;
  tfree(pCtrl->tmrList);
  tmrMemPoolCleanUp(pCtrl->poolHandle);

  memset(pCtrl, 0, sizeof(tmr_ctrl_t));

  pCtrl->tmrCtrlId = tmrCtrlId;
  strcpy(pCtrl->label, label);
  pCtrl->maxNumOfTmrs = maxNumOfTmrs;

  if ((pCtrl->poolHandle = tmrMemPoolInit(maxNumOfTmrs + 10, sizeof(tmr_obj_t))) == NULL) {
    tmrError("%s failed to allocate mem pool", label);
    tmrMemPoolCleanUp(pCtrl->poolHandle);
    return NULL;
  }

  if (resolution < MSECONDS_PER_TICK) resolution = MSECONDS_PER_TICK;
  pCtrl->resolution = resolution;
  pCtrl->maxTicks = resolution / MSECONDS_PER_TICK;
  pCtrl->ticks = rand() / pCtrl->maxTicks;
  pCtrl->numOfPeriods = longest / resolution;
  if (pCtrl->numOfPeriods < 10) pCtrl->numOfPeriods = 10;

  pCtrl->tmrList = (tmr_list_t *)malloc(sizeof(tmr_list_t) * pCtrl->numOfPeriods);
  for (int i = 0; i < pCtrl->numOfPeriods; i++) {
    pCtrl->tmrList[i].head = NULL;
    pCtrl->tmrList[i].count = 0;
  }

  if (pthread_mutex_init(&pCtrl->mutex, NULL) < 0) {
    tmrError("%s failed to create the mutex, reason:%s", label, strerror(errno));
    taosTmrCleanUp(pCtrl);
    return NULL;
  }

  pCtrl->signature = pCtrl;
  numOfTmrCtrl++;
  tmrTrace("%s timer ctrl is initialized, index:%d", label, tmrCtrlId);
  return pCtrl;
}

void taosTmrProcessList(tmr_ctrl_t *pCtrl) {
  unsigned int index;
  tmr_list_t * pList;
  tmr_obj_t *  pObj, *header;

  pthread_mutex_lock(&pCtrl->mutex);
  index = pCtrl->periodsFromStart % pCtrl->numOfPeriods;
  pList = &pCtrl->tmrList[index];

  while (1) {
    header = pList->head;
    if (header == NULL) break;

    if (header->cycle > 0) {
      pObj = header;
      while (pObj) {
        pObj->cycle--;
        pObj = pObj->next;
      }
      break;
    }

    pCtrl->numOfTmrs--;
    tmrTrace("%s %p, timer expired, fp:%p, tmr_h:%p, index:%d, total:%d", pCtrl->label, header->param1, header->fp,
             header, index, pCtrl->numOfTmrs);

    pList->head = header->next;
    if (header->next) header->next->prev = NULL;
    pList->count--;
    header->timerId = NULL;

    SSchedMsg schedMsg;
    schedMsg.fp = NULL;
    schedMsg.tfp = header->fp;
    schedMsg.ahandle = header->param1;
    schedMsg.thandle = header;
    taosScheduleTask(tmrQhandle, &schedMsg);

    tmrMemPoolFree(pCtrl->poolHandle, (char *)header);
  }

  pCtrl->periodsFromStart++;
  pthread_mutex_unlock(&pCtrl->mutex);
}

void taosTmrCleanUp(void *handle) {
  tmr_ctrl_t *pCtrl = (tmr_ctrl_t *)handle;
  if (pCtrl == NULL || pCtrl->signature != pCtrl) return;

  pCtrl->signature = NULL;
  taosFreeId(tmrIdPool, pCtrl->tmrCtrlId);
  numOfTmrCtrl--;
  tmrTrace("%s is cleaned up, numOfTmrs:%d", pCtrl->label, numOfTmrCtrl);
}

tmr_h taosTmrStart(void (*fp)(void *, void *), int mseconds, void *param1, void *handle) {
  tmr_obj_t * pObj, *cNode, *pNode;
  tmr_list_t *pList;
  int         index, period;
  tmr_ctrl_t *pCtrl = (tmr_ctrl_t *)handle;

  if (handle == NULL) return NULL;

  period = mseconds / pCtrl->resolution;
  if (pthread_mutex_lock(&pCtrl->mutex) != 0)
    tmrError("%s mutex lock failed, reason:%s", pCtrl->label, strerror(errno));

  pObj = (tmr_obj_t *)tmrMemPoolMalloc(pCtrl->poolHandle);
  if (pObj == NULL) {
    tmrError("%s reach max number of timers:%d", pCtrl->label, pCtrl->maxNumOfTmrs);
    pthread_mutex_unlock(&pCtrl->mutex);
    return NULL;
  }

  pObj->cycle = period / pCtrl->numOfPeriods;
  pObj->param1 = param1;
  pObj->fp = fp;
  pObj->timerId = pObj;
  pObj->pCtrl = pCtrl;

  index = (period + pCtrl->periodsFromStart) % pCtrl->numOfPeriods;
  int cindex = (pCtrl->periodsFromStart) % pCtrl->numOfPeriods;
  pList = &(pCtrl->tmrList[index]);

  pObj->index = index;
  cNode = pList->head;
  pNode = NULL;

  while (cNode != NULL) {
    if (cNode->cycle < pObj->cycle) {
      pNode = cNode;
      cNode = cNode->next;
    } else {
      break;
    }
  }

  pObj->next = cNode;
  pObj->prev = pNode;

  if (cNode != NULL) {
    cNode->prev = pObj;
  }

  if (pNode != NULL) {
    pNode->next = pObj;
  } else {
    pList->head = pObj;
  }

  pList->count++;
  pCtrl->numOfTmrs++;

  if (pthread_mutex_unlock(&pCtrl->mutex) != 0)
    tmrError("%s mutex unlock failed, reason:%s", pCtrl->label, strerror(errno));

  tmrTrace("%s %p, timer started, fp:%p, tmr_h:%p, index:%d, total:%d cindex:%d", pCtrl->label, param1, fp, pObj, index,
           pCtrl->numOfTmrs, cindex);

  return (tmr_h)pObj;
}

void taosTmrStop(tmr_h timerId) {
  tmr_obj_t * pObj;
  tmr_list_t *pList;
  tmr_ctrl_t *pCtrl;

  pObj = (tmr_obj_t *)timerId;
  if (pObj == NULL) return;

  pCtrl = pObj->pCtrl;
  if (pCtrl == NULL) return;

  if (pthread_mutex_lock(&pCtrl->mutex) != 0)
    tmrError("%s mutex lock failed, reason:%s", pCtrl->label, strerror(errno));

  if (pObj->timerId == timerId) {
    pList = &(pCtrl->tmrList[pObj->index]);
    if (pObj->prev) {
      pObj->prev->next = pObj->next;
    } else {
      pList->head = pObj->next;
    }

    if (pObj->next) {
      pObj->next->prev = pObj->prev;
    }

    pList->count--;
    pObj->timerId = NULL;
    pCtrl->numOfTmrs--;

    tmrTrace("%s %p, timer stopped, fp:%p, tmr_h:%p, total:%d", pCtrl->label, pObj->param1, pObj->fp, pObj,
             pCtrl->numOfTmrs);
    tmrMemPoolFree(pCtrl->poolHandle, (char *)(pObj));
  }

  pthread_mutex_unlock(&pCtrl->mutex);
}

void taosTmrStopA(tmr_h *timerId) {
  tmr_obj_t * pObj;
  tmr_list_t *pList;
  tmr_ctrl_t *pCtrl;

  pObj = *(tmr_obj_t **)timerId;
  if (pObj == NULL) return;

  pCtrl = pObj->pCtrl;
  if (pCtrl == NULL) return;

  if (pthread_mutex_lock(&pCtrl->mutex) != 0)
    tmrError("%s mutex lock failed, reason:%s", pCtrl->label, strerror(errno));

  if (pObj->timerId == pObj) {
    pList = &(pCtrl->tmrList[pObj->index]);
    if (pObj->prev) {
      pObj->prev->next = pObj->next;
    } else {
      pList->head = pObj->next;
    }

    if (pObj->next) {
      pObj->next->prev = pObj->prev;
    }

    pList->count--;
    pObj->timerId = NULL;
    pCtrl->numOfTmrs--;

    tmrTrace("%s %p, timer stopped atomiclly, fp:%p, tmr_h:%p, total:%d", pCtrl->label, pObj->param1, pObj->fp, pObj,
             pCtrl->numOfTmrs);
    tmrMemPoolFree(pCtrl->poolHandle, (char *)(pObj));

    *(tmr_obj_t **)timerId = NULL;
  } else {
    tmrTrace("%s %p, timer stopped atomiclly, fp:%p, tmr_h:%p, total:%d", pCtrl->label, pObj->param1, pObj->fp, pObj,
             pCtrl->numOfTmrs);
  }

  pthread_mutex_unlock(&pCtrl->mutex);
}

void taosTmrReset(void (*fp)(void *, void *), int mseconds, void *param1, void *handle, tmr_h *pTmrId) {
  tmr_obj_t * pObj, *cNode, *pNode;
  tmr_list_t *pList;
  int         index, period;
  tmr_ctrl_t *pCtrl = (tmr_ctrl_t *)handle;

  if (handle == NULL) return;
  if (pTmrId == NULL) return;

  period = mseconds / pCtrl->resolution;
  if (pthread_mutex_lock(&pCtrl->mutex) != 0)
    tmrError("%s mutex lock failed, reason:%s", pCtrl->label, strerror(errno));

  pObj = (tmr_obj_t *)(*pTmrId);

  if (pObj && pObj->timerId == *pTmrId) {
    // exist, stop it first
    pList = &(pCtrl->tmrList[pObj->index]);
    if (pObj->prev) {
      pObj->prev->next = pObj->next;
    } else {
      pList->head = pObj->next;
    }

    if (pObj->next) {
      pObj->next->prev = pObj->prev;
    }

    pList->count--;
    pObj->timerId = NULL;
    pCtrl->numOfTmrs--;
  } else {
    // timer not there, or already expired
    pObj = (tmr_obj_t *)tmrMemPoolMalloc(pCtrl->poolHandle);
    *pTmrId = pObj;

    if (pObj == NULL) {
      tmrError("%s failed to allocate timer, max:%d allocated:%d", pCtrl->label, pCtrl->maxNumOfTmrs, pCtrl->numOfTmrs);
      pthread_mutex_unlock(&pCtrl->mutex);
      return;
    }
  }

  pObj->cycle = period / pCtrl->numOfPeriods;
  pObj->param1 = param1;
  pObj->fp = fp;
  pObj->timerId = pObj;
  pObj->pCtrl = pCtrl;

  index = (period + pCtrl->periodsFromStart) % pCtrl->numOfPeriods;
  pList = &(pCtrl->tmrList[index]);

  pObj->index = index;
  cNode = pList->head;
  pNode = NULL;

  while (cNode != NULL) {
    if (cNode->cycle < pObj->cycle) {
      pNode = cNode;
      cNode = cNode->next;
    } else {
      break;
    }
  }

  pObj->next = cNode;
  pObj->prev = pNode;

  if (cNode != NULL) {
    cNode->prev = pObj;
  }

  if (pNode != NULL) {
    pNode->next = pObj;
  } else {
    pList->head = pObj;
  }

  pList->count++;
  pCtrl->numOfTmrs++;

  if (pthread_mutex_unlock(&pCtrl->mutex) != 0)
    tmrError("%s mutex unlock failed, reason:%s", pCtrl->label, strerror(errno));

  tmrTrace("%s %p, timer is reset, fp:%p, tmr_h:%p, index:%d, total:%d numOfFree:%d", pCtrl->label, param1, fp, pObj,
           index, pCtrl->numOfTmrs, ((pool_t *)pCtrl->poolHandle)->numOfFree);

  return;
}

void taosTmrList(void *handle) {
  int         i;
  tmr_list_t *pList;
  tmr_obj_t * pObj;
  tmr_ctrl_t *pCtrl = (tmr_ctrl_t *)handle;

  for (i = 0; i < pCtrl->numOfPeriods; ++i) {
    pList = &(pCtrl->tmrList[i]);
    pObj = pList->head;
    if (!pObj) continue;
    printf("\nindex=%d count:%d\n", i, pList->count);
    while (pObj) {
      pObj = pObj->next;
    }
  }
}

mpool_h tmrMemPoolInit(int numOfBlock, int blockSize) {
  int     i;
  pool_t *pool_p;

  if (numOfBlock <= 1 || blockSize <= 1) {
    tmrError("invalid parameter in memPoolInit\n");
    return NULL;
  }

  pool_p = (pool_t *)malloc(sizeof(pool_t));
  if (pool_p == NULL) {
    tmrError("mempool malloc failed\n");
    return NULL;
  } else {
    memset(pool_p, 0, sizeof(pool_t));
  }

  pool_p->blockSize = blockSize;
  pool_p->numOfBlock = numOfBlock;
  pool_p->pool = (char *)malloc(blockSize * numOfBlock);
  memset(pool_p->pool, 0, blockSize * numOfBlock);
  pool_p->freeList = (int *)malloc(sizeof(int) * numOfBlock);

  if (pool_p->pool == NULL || pool_p->freeList == NULL) {
    tmrError("failed to allocate memory\n");
    free(pool_p->freeList);
    free(pool_p->pool);
    free(pool_p);
    return NULL;
  }

  for (i = 0; i < pool_p->numOfBlock; ++i) pool_p->freeList[i] = i;

  pool_p->first = 0;
  pool_p->numOfFree = pool_p->numOfBlock;

  return (mpool_h)pool_p;
}

char *tmrMemPoolMalloc(mpool_h handle) {
  char *  pos = NULL;
  pool_t *pool_p = (pool_t *)handle;

  if (pool_p->numOfFree <= 0 || pool_p->numOfFree > pool_p->numOfBlock) {
    tmrError("mempool: out of memory, numOfFree:%d, numOfBlock:%d", pool_p->numOfFree, pool_p->numOfBlock);
  } else {
    pos = pool_p->pool + pool_p->blockSize * (pool_p->freeList[pool_p->first]);
    pool_p->first++;
    pool_p->first = pool_p->first % pool_p->numOfBlock;
    pool_p->numOfFree--;
  }

  return pos;
}

void tmrMemPoolFree(mpool_h handle, char *pMem) {
  int     index;
  pool_t *pool_p = (pool_t *)handle;

  if (pMem == NULL) return;

  index = (int)(pMem - pool_p->pool) / pool_p->blockSize;

  if (index < 0 || index >= pool_p->numOfBlock) {
    tmrError("tmr mempool: error, invalid address:%p\n", pMem);
  } else {
    memset(pMem, 0, pool_p->blockSize);
    pool_p->freeList[(pool_p->first + pool_p->numOfFree) % pool_p->numOfBlock] = index;
    pool_p->numOfFree++;
  }
}

void tmrMemPoolCleanUp(mpool_h handle) {
  pool_t *pool_p = (pool_t *)handle;
  if (pool_p == NULL) return;

  if (pool_p->pool) free(pool_p->pool);
  if (pool_p->freeList) free(pool_p->freeList);
  memset(&pool_p, 0, sizeof(pool_p));
  free(pool_p);
}