From 5da147a6910e02d99ecb0d4494d19664fe4043e2 Mon Sep 17 00:00:00 2001 From: Jeff Tao Date: Sat, 22 Feb 2020 20:13:30 +0800 Subject: [PATCH] add API rpcReallocCont --- src/rpc/src/rpcMain.c | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/rpc/src/rpcMain.c b/src/rpc/src/rpcMain.c index e286af1598..57d332c55e 100755 --- a/src/rpc/src/rpcMain.c +++ b/src/rpc/src/rpcMain.c @@ -299,17 +299,16 @@ void rpcClose(void *param) { tfree(pRpc); } -void *rpcMallocCont(int size) { - char *pMsg = NULL; +void *rpcMallocCont(int contLen) { + int size = contLen + RPC_MSG_OVERHEAD; - size += RPC_MSG_OVERHEAD; - pMsg = (char *)calloc(1, (size_t)size); - if (pMsg == NULL) { + char *start = (char *)calloc(1, (size_t)size); + if (start == NULL) { tError("failed to malloc msg, size:%d", size); return NULL; } - return pMsg + sizeof(SRpcReqContext) + sizeof(SRpcHead); + return start + sizeof(SRpcReqContext) + sizeof(SRpcHead); } void rpcFreeCont(void *cont) { @@ -319,6 +318,24 @@ void rpcFreeCont(void *cont) { } } +void *rpcReallocCont(void *ptr, int contLen) { + if (ptr == NULL) return rpcMallocCont(contLen); + + char *start = ((char *)ptr) - sizeof(SRpcReqContext) - sizeof(SRpcHead); + if (contLen == 0 ) { + free(start); + } + + int size = contLen + RPC_MSG_OVERHEAD; + start = realloc(start, size); + if (start == NULL) { + tError("failed to realloc cont, size:%d", size); + return NULL; + } + + return start + sizeof(SRpcReqContext) + sizeof(SRpcHead); +} + void rpcSendRequest(void *shandle, SRpcIpSet *pIpSet, char type, void *pCont, int contLen, void *ahandle) { SRpcInfo *pRpc = (SRpcInfo *)shandle; SRpcReqContext *pContext; -- GitLab