diff --git a/source/dnode/vnode/src/inc/vnodeInt.h b/source/dnode/vnode/src/inc/vnodeInt.h index e56f130c2c4af7a70957e29c34962f0b06331c9e..052ab084536accd74084706cef6fd146efedbdfb 100644 --- a/source/dnode/vnode/src/inc/vnodeInt.h +++ b/source/dnode/vnode/src/inc/vnodeInt.h @@ -91,6 +91,7 @@ typedef struct SCommitInfo SCommitInfo; // vnd.h void* vnodeBufPoolMalloc(SVBufPool* pPool, int size); +void* vnodeBufPoolMallocAligned(SVBufPool* pPool, int size); void vnodeBufPoolFree(SVBufPool* pPool, void* p); void vnodeBufPoolRef(SVBufPool* pPool); void vnodeBufPoolUnRef(SVBufPool* pPool); diff --git a/source/dnode/vnode/src/meta/metaCommit.c b/source/dnode/vnode/src/meta/metaCommit.c index ac8d99ccf0afbbf1074732ae75fc54e515122f80..f61930b84cf54b189af0bc36cb66ff69402b27fc 100644 --- a/source/dnode/vnode/src/meta/metaCommit.c +++ b/source/dnode/vnode/src/meta/metaCommit.c @@ -15,8 +15,10 @@ #include "meta.h" -static FORCE_INLINE void *metaMalloc(void *pPool, size_t size) { return vnodeBufPoolMalloc((SVBufPool *)pPool, size); } -static FORCE_INLINE void metaFree(void *pPool, void *p) { vnodeBufPoolFree((SVBufPool *)pPool, p); } +static FORCE_INLINE void *metaMalloc(void *pPool, size_t size) { + return vnodeBufPoolMallocAligned((SVBufPool *)pPool, size); +} +static FORCE_INLINE void metaFree(void *pPool, void *p) { vnodeBufPoolFree((SVBufPool *)pPool, p); } // begin a meta txn int metaBegin(SMeta *pMeta, int8_t heap) { diff --git a/source/dnode/vnode/src/vnd/vnodeBufPool.c b/source/dnode/vnode/src/vnd/vnodeBufPool.c index dcc323f778dcb58eda9e9bd9cc160d1e7b522619..83a414dae08bd48e41e12682922daf7055e1f5a0 100644 --- a/source/dnode/vnode/src/vnd/vnodeBufPool.c +++ b/source/dnode/vnode/src/vnd/vnodeBufPool.c @@ -35,7 +35,7 @@ static int vnodeBufPoolCreate(SVnode *pVnode, int64_t size, SVBufPool **ppPool) return -1; } if (taosThreadSpinInit(pPool->lock, 0) != 0) { - taosMemoryFree((void*)pPool->lock); + taosMemoryFree((void *)pPool->lock); taosMemoryFree(pPool); terrno = TAOS_SYSTEM_ERROR(errno); return -1; @@ -62,7 +62,7 @@ static int vnodeBufPoolDestroy(SVBufPool *pPool) { vnodeBufPoolReset(pPool); if (pPool->lock) { taosThreadSpinDestroy(pPool->lock); - taosMemoryFree((void*)pPool->lock); + taosMemoryFree((void *)pPool->lock); } taosMemoryFree(pPool); return 0; @@ -123,6 +123,46 @@ void vnodeBufPoolReset(SVBufPool *pPool) { pPool->ptr = pPool->node.data; } +void *vnodeBufPoolMallocAligned(SVBufPool *pPool, int size) { + SVBufPoolNode *pNode; + void *p = NULL; + uint8_t *ptr = NULL; + int paddingLen = 0; + ASSERT(pPool != NULL); + + if (pPool->lock) taosThreadSpinLock(pPool->lock); + + ptr = pPool->ptr; + paddingLen = (((long)ptr + 7) & ~7) - (long)ptr; + + if (pPool->node.size >= pPool->ptr - pPool->node.data + size + paddingLen) { + // allocate from the anchor node + p = pPool->ptr + paddingLen; + size += paddingLen; + pPool->ptr = pPool->ptr + size; + pPool->size += size; + } else { + // allocate a new node + pNode = taosMemoryMalloc(sizeof(*pNode) + size); + if (pNode == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + if (pPool->lock) taosThreadSpinUnlock(pPool->lock); + return NULL; + } + + p = pNode->data; + pNode->size = size; + pNode->prev = pPool->pTail; + pNode->pnext = &pPool->pTail; + pPool->pTail->pnext = &pNode->prev; + pPool->pTail = pNode; + + pPool->size = pPool->size + sizeof(*pNode) + size; + } + if (pPool->lock) taosThreadSpinUnlock(pPool->lock); + return p; +} + void *vnodeBufPoolMalloc(SVBufPool *pPool, int size) { SVBufPoolNode *pNode; void *p = NULL;