mempool
This commit is contained in:
parent
a282997149
commit
6834200db3
|
@ -12,25 +12,24 @@
|
||||||
* You should have received a copy of the GNU Affero General Public License
|
* 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/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
#ifndef _TD_UTIL_MEMPOOL_H
|
#ifndef _TD_UTIL_MEMPOOL_H_
|
||||||
#define _TD_UTIL_MEMPOOL_H
|
#define _TD_UTIL_MEMPOOL_H_
|
||||||
|
|
||||||
|
#include "os.h"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define mpool_h void *
|
typedef void *mpool_h;
|
||||||
|
|
||||||
mpool_h taosMemPoolInit(int maxNum, int blockSize);
|
|
||||||
|
|
||||||
|
mpool_h taosMemPoolInit(int32_t maxNum, int32_t blockSize);
|
||||||
char *taosMemPoolMalloc(mpool_h handle);
|
char *taosMemPoolMalloc(mpool_h handle);
|
||||||
|
|
||||||
void taosMemPoolFree(mpool_h handle, char *p);
|
void taosMemPoolFree(mpool_h handle, char *p);
|
||||||
|
|
||||||
void taosMemPoolCleanUp(mpool_h handle);
|
void taosMemPoolCleanUp(mpool_h handle);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#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/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "tlog.h"
|
#define _DEFAULT_SOURCE
|
||||||
#include "tmempool.h"
|
#include "tmempool.h"
|
||||||
|
#include "tlog.h"
|
||||||
#include "tutil.h"
|
#include "tutil.h"
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int numOfFree; /* number of free slots */
|
int32_t numOfFree; /* number of free slots */
|
||||||
int first; /* the first free slot */
|
int32_t first; /* the first free slot */
|
||||||
int numOfBlock; /* the number of blocks */
|
int32_t numOfBlock; /* the number of blocks */
|
||||||
int blockSize; /* block size in bytes */
|
int32_t blockSize; /* block size in bytes */
|
||||||
int * freeList; /* the index list */
|
int32_t *freeList; /* the index list */
|
||||||
char *pool; /* the actual mem block */
|
char *pool; /* the actual mem block */
|
||||||
pthread_mutex_t mutex;
|
pthread_mutex_t mutex;
|
||||||
} pool_t;
|
} pool_t;
|
||||||
|
|
||||||
mpool_h taosMemPoolInit(int numOfBlock, int blockSize) {
|
mpool_h taosMemPoolInit(int32_t numOfBlock, int32_t blockSize) {
|
||||||
int i;
|
int32_t i;
|
||||||
pool_t *pool_p;
|
pool_t *pool_p;
|
||||||
|
|
||||||
if (numOfBlock <= 1 || blockSize <= 1) {
|
if (numOfBlock <= 1 || blockSize <= 1) {
|
||||||
|
@ -47,7 +48,7 @@ mpool_h taosMemPoolInit(int numOfBlock, int blockSize) {
|
||||||
pool_p->blockSize = blockSize;
|
pool_p->blockSize = blockSize;
|
||||||
pool_p->numOfBlock = numOfBlock;
|
pool_p->numOfBlock = numOfBlock;
|
||||||
pool_p->pool = (char *)malloc((size_t)(blockSize * 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) {
|
if (pool_p->pool == NULL || pool_p->freeList == NULL) {
|
||||||
uError("failed to allocate memory\n");
|
uError("failed to allocate memory\n");
|
||||||
|
@ -88,20 +89,20 @@ char *taosMemPoolMalloc(mpool_h handle) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void taosMemPoolFree(mpool_h handle, char *pMem) {
|
void taosMemPoolFree(mpool_h handle, char *pMem) {
|
||||||
int index;
|
int32_t index;
|
||||||
pool_t *pool_p = (pool_t *)handle;
|
pool_t *pool_p = (pool_t *)handle;
|
||||||
|
|
||||||
if (pMem == NULL) return;
|
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) {
|
if (index != 0) {
|
||||||
uError("invalid free address:%p\n", pMem);
|
uError("invalid free address:%p\n", pMem);
|
||||||
return;
|
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) {
|
if (index < 0 || index >= pool_p->numOfBlock) {
|
||||||
uError("mempool: error, invalid address:%p\n", pMem);
|
uError("mempool: error, invalid address:%p", pMem);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue