diff --git a/include/util/tmempool.h b/include/util/tmempool.h index 3e3db738a9f372082d785bedb28da776c23ba59f..7a5aca7b34f6393c5440f030f3c416ca18b08a0f 100644 --- a/include/util/tmempool.h +++ b/include/util/tmempool.h @@ -12,25 +12,24 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -#ifndef _TD_UTIL_MEMPOOL_H -#define _TD_UTIL_MEMPOOL_H +#ifndef _TD_UTIL_MEMPOOL_H_ +#define _TD_UTIL_MEMPOOL_H_ + +#include "os.h" #ifdef __cplusplus extern "C" { #endif -#define mpool_h void * - -mpool_h taosMemPoolInit(int maxNum, int blockSize); - -char *taosMemPoolMalloc(mpool_h handle); - -void taosMemPoolFree(mpool_h handle, char *p); +typedef void *mpool_h; -void taosMemPoolCleanUp(mpool_h handle); +mpool_h taosMemPoolInit(int32_t maxNum, int32_t blockSize); +char *taosMemPoolMalloc(mpool_h handle); +void taosMemPoolFree(mpool_h handle, char *p); +void taosMemPoolCleanUp(mpool_h handle); #ifdef __cplusplus } #endif -#endif /*_TD_UTIL_MEMPOOL_H*/ +#endif /*_TD_UTIL_MEMPOOL_H_*/ diff --git a/source/util/src/tmempool.c b/source/util/src/tmempool.c index f980a05629f5504d14bdf168b0d753d42f8b4c57..1fc9bfc7ab458dfda7d1e74123bbea2366a70e38 100644 --- a/source/util/src/tmempool.c +++ b/source/util/src/tmempool.c @@ -13,22 +13,23 @@ * along with this program. If not, see . */ -#include "tlog.h" +#define _DEFAULT_SOURCE #include "tmempool.h" +#include "tlog.h" #include "tutil.h" 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 */ + int32_t numOfFree; /* number of free slots */ + int32_t first; /* the first free slot */ + int32_t numOfBlock; /* the number of blocks */ + int32_t blockSize; /* block size in bytes */ + int32_t *freeList; /* the index list */ + char *pool; /* the actual mem block */ pthread_mutex_t mutex; } pool_t; -mpool_h taosMemPoolInit(int numOfBlock, int blockSize) { - int i; +mpool_h taosMemPoolInit(int32_t numOfBlock, int32_t blockSize) { + int32_t i; pool_t *pool_p; if (numOfBlock <= 1 || blockSize <= 1) { @@ -47,7 +48,7 @@ mpool_h taosMemPoolInit(int numOfBlock, int blockSize) { pool_p->blockSize = blockSize; pool_p->numOfBlock = numOfBlock; pool_p->pool = (char *)malloc((size_t)(blockSize * numOfBlock)); - pool_p->freeList = (int *)malloc(sizeof(int) * (size_t)numOfBlock); + pool_p->freeList = (int32_t *)malloc(sizeof(int32_t) * (size_t)numOfBlock); if (pool_p->pool == NULL || pool_p->freeList == NULL) { uError("failed to allocate memory\n"); @@ -69,7 +70,7 @@ mpool_h taosMemPoolInit(int numOfBlock, int blockSize) { } char *taosMemPoolMalloc(mpool_h handle) { - char * pos = NULL; + char *pos = NULL; pool_t *pool_p = (pool_t *)handle; pthread_mutex_lock(&(pool_p->mutex)); @@ -88,20 +89,20 @@ char *taosMemPoolMalloc(mpool_h handle) { } void taosMemPoolFree(mpool_h handle, char *pMem) { - int index; + int32_t index; pool_t *pool_p = (pool_t *)handle; if (pMem == NULL) return; - index = (int)(pMem - pool_p->pool) % pool_p->blockSize; + index = (int32_t)(pMem - pool_p->pool) % pool_p->blockSize; if (index != 0) { uError("invalid free address:%p\n", pMem); return; } - index = (int)((pMem - pool_p->pool) / pool_p->blockSize); + index = (int32_t)((pMem - pool_p->pool) / pool_p->blockSize); if (index < 0 || index >= pool_p->numOfBlock) { - uError("mempool: error, invalid address:%p\n", pMem); + uError("mempool: error, invalid address:%p", pMem); return; }