未验证 提交 8914c0cc 编写于 作者: S slguan 提交者: GitHub

Merge pull request #345 from localvar/improve-memory-pool

improve memory pool
...@@ -64,6 +64,7 @@ mpool_h taosMemPoolInit(int numOfBlock, int blockSize) { ...@@ -64,6 +64,7 @@ mpool_h taosMemPoolInit(int numOfBlock, int blockSize) {
pthread_mutex_init(&(pool_p->mutex), NULL); pthread_mutex_init(&(pool_p->mutex), NULL);
memset(pool_p->pool, 0, (size_t)(blockSize * numOfBlock));
for (i = 0; i < pool_p->numOfBlock; ++i) pool_p->freeList[i] = i; for (i = 0; i < pool_p->numOfBlock; ++i) pool_p->freeList[i] = i;
pool_p->first = 0; pool_p->first = 0;
...@@ -78,9 +79,7 @@ char *taosMemPoolMalloc(mpool_h handle) { ...@@ -78,9 +79,7 @@ char *taosMemPoolMalloc(mpool_h handle) {
pthread_mutex_lock(&(pool_p->mutex)); pthread_mutex_lock(&(pool_p->mutex));
if (pool_p->numOfFree <= 0) { if (pool_p->numOfFree > 0) {
pTrace("mempool: out of memory");
} else {
pos = pool_p->pool + pool_p->blockSize * (pool_p->freeList[pool_p->first]); pos = pool_p->pool + pool_p->blockSize * (pool_p->freeList[pool_p->first]);
pool_p->first++; pool_p->first++;
pool_p->first = pool_p->first % pool_p->numOfBlock; pool_p->first = pool_p->first % pool_p->numOfBlock;
...@@ -88,7 +87,8 @@ char *taosMemPoolMalloc(mpool_h handle) { ...@@ -88,7 +87,8 @@ char *taosMemPoolMalloc(mpool_h handle) {
} }
pthread_mutex_unlock(&(pool_p->mutex)); pthread_mutex_unlock(&(pool_p->mutex));
if (pos != NULL) memset(pos, 0, (size_t)pool_p->blockSize);
if (pos == NULL) pTrace("mempool: out of memory");
return pos; return pos;
} }
...@@ -98,24 +98,25 @@ void taosMemPoolFree(mpool_h handle, char *pMem) { ...@@ -98,24 +98,25 @@ void taosMemPoolFree(mpool_h handle, char *pMem) {
if (pMem == NULL) return; if (pMem == NULL) return;
pthread_mutex_lock(&pool_p->mutex);
index = (int)(pMem - pool_p->pool) % pool_p->blockSize; index = (int)(pMem - pool_p->pool) % pool_p->blockSize;
if (index != 0) { if (index != 0) {
pError("invalid free address:%p\n", pMem); pError("invalid free address:%p\n", pMem);
} else { return;
index = (int)((pMem - pool_p->pool) / pool_p->blockSize);
if (index < 0 || index >= pool_p->numOfBlock) {
pError("mempool: error, invalid address:%p\n", pMem);
} else {
pool_p->freeList[(pool_p->first + pool_p->numOfFree) % pool_p->numOfBlock] = index;
pool_p->numOfFree++;
memset(pMem, 0, (size_t)pool_p->blockSize);
}
} }
index = (int)((pMem - pool_p->pool) / pool_p->blockSize);
if (index < 0 || index >= pool_p->numOfBlock) {
pError("mempool: error, invalid address:%p\n", pMem);
return;
}
memset(pMem, 0, (size_t)pool_p->blockSize);
pthread_mutex_lock(&pool_p->mutex);
pool_p->freeList[(pool_p->first + pool_p->numOfFree) % pool_p->numOfBlock] = index;
pool_p->numOfFree++;
pthread_mutex_unlock(&pool_p->mutex); pthread_mutex_unlock(&pool_p->mutex);
} }
...@@ -125,6 +126,6 @@ void taosMemPoolCleanUp(mpool_h handle) { ...@@ -125,6 +126,6 @@ void taosMemPoolCleanUp(mpool_h handle) {
pthread_mutex_destroy(&pool_p->mutex); pthread_mutex_destroy(&pool_p->mutex);
if (pool_p->pool) free(pool_p->pool); if (pool_p->pool) free(pool_p->pool);
if (pool_p->freeList) free(pool_p->freeList); if (pool_p->freeList) free(pool_p->freeList);
memset(&pool_p, 0, sizeof(pool_p)); memset(pool_p, 0, sizeof(*pool_p));
free(pool_p); free(pool_p);
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册