提交 6834200d 编写于 作者: S Shengliang Guan

mempool

上级 a2829971
......@@ -12,25 +12,24 @@
* 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/>.
*/
#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_*/
......@@ -13,22 +13,23 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#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;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册