ClRcT
clLogCompEntryUnpackNAdd(ClBufferHandleT        msg,
                         ClLogStreamOwnerDataT  *pStreamOwnerData)
{
    ClRcT             rc        = CL_OK;
    ClLogCompKeyT     compKey   = {0};
    ClLogCompKeyT     *pCompKey = NULL;
    ClLogSOCompDataT  compData  = {0};
    ClLogSOCompDataT  *pData    = NULL;

    CL_LOG_DEBUG_TRACE(("Enter"));

    rc = VDECL_VER(clXdrUnmarshallClLogCompKeyT, 4, 0, 0)(msg, &compKey);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("VDECL_VER(clXdrUnmarshallClLogCompKeyT, 4, 0, 0)(): rc[0x %x]", rc));
        return rc;
    }
    rc = VDECL_VER(clXdrUnmarshallClLogSOCompDataT, 4, 0, 0)(msg, &compData);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("VDECL_VER(clXdrUnmarshallClLogSOCompDataT, 4, 0, 0)(): rc[0x %x]", rc));    
        return rc;
    }

    pCompKey = (ClLogCompKeyT*)clHeapCalloc(1, sizeof(ClLogCompKeyT));
    if( NULL == pCompKey )
    {
        CL_LOG_DEBUG_ERROR(( "clHeapCalloc()"));
        return CL_LOG_RC(CL_ERR_NO_MEMORY);
    }    
    *pCompKey = compKey;
    CL_LOG_DEBUG_VERBOSE(("compKey.nodeAddress: %u", compKey.nodeAddr));
    CL_LOG_DEBUG_VERBOSE(("compKey.compId     : %u", compKey.compId));

    pData = (ClLogSOCompDataT*)clHeapCalloc(1, sizeof(ClLogSOCompDataT));
    if( NULL == pData )
    {
        CL_LOG_DEBUG_ERROR(( "clHeapCalloc()"));
        clHeapFree(pCompKey);
        return CL_LOG_RC(CL_ERR_NO_MEMORY);
    }    
    *pData = compData;
    CL_LOG_DEBUG_VERBOSE(("compData.refCount    : %u", pData->refCount));
    CL_LOG_DEBUG_VERBOSE(("compData.ackerCnt    : %u", pData->ackerCnt));
    CL_LOG_DEBUG_VERBOSE(("compData.nonAckerCnt : %u", pData->nonAckerCnt));

    rc = clCntNodeAdd(pStreamOwnerData->hCompTable, 
                      (ClCntKeyHandleT) pCompKey,
                      (ClCntDataHandleT) pData, NULL);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(( "clCntNodeAdd(): rc[0x %x]", rc));
        clHeapFree(pData);
        clHeapFree(pCompKey);
    }

    CL_LOG_DEBUG_TRACE(("Exit"));
    return CL_OK;
}
ClRcT
clLogBitmapPack(ClBitmapHandleT  hBitmap,
                ClBufferHandleT  msg)
{
    ClRcT                   rc        = CL_OK;
    ClUint32T  nBytes   = 0;
    ClUint8T   *pBitMap = NULL;

    CL_LOG_DEBUG_TRACE(("Enter"));

    rc = clBitmap2BufferGet(hBitmap, &nBytes, &pBitMap);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clBitmap2PositionListGet(): rc[0x %x]", rc));
        return rc;
    }    
    rc = clXdrMarshallClUint32T(&nBytes, msg, 0);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clXdrMarshallClUint32T(): rc[0x %x]", rc));
        clHeapFree(pBitMap);
        return rc;
    }
    rc = clXdrMarshallArrayClUint8T(pBitMap, nBytes, msg, 0);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clXdrMarshallArrayClUint8T(): rc[0x %x]", rc));
    }
    clHeapFree(pBitMap);
    
    CL_LOG_DEBUG_TRACE(("Exit"));
        return rc;
    }    
ClRcT clBackingStorageDelete(ClBackingStorageHandleT *pHandle)
{
    ClRcT rc = CL_BACKING_STORAGE_RC(CL_ERR_INVALID_PARAMETER);
    ClBackingStorageT *pStorage = NULL;

    if(!pHandle)
    {
        clLogError(CL_LOG_AREA_BACKING_STORAGE, CL_LOG_CONTEXT_EXIT, "NULL handle");
        goto out;
    }

    if(*pHandle)
    {
        pStorage = (ClBackingStorageT*)*pHandle;
        if(pStorage->pPrivateData)
        {
            clHeapFree(pStorage->pPrivateData);
        }
        clHeapFree(pStorage);
        *pHandle = 0;
    }
    rc = CL_OK;

    out:
    return rc;
}
ClRcT
clBitmapDestroy(ClBitmapHandleT  hBitmap)
{
    ClRcT          rc           = CL_OK;
    ClBitmapInfoT  *pBitmapInfo = hBitmap;

    CL_DEBUG_PRINT(CL_DEBUG_TRACE, ("Enter"));

    if( CL_BM_INVALID_BITMAP_HANDLE == hBitmap )
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Invalid Handle"));
        return CL_BITMAP_RC(CL_ERR_INVALID_HANDLE);
    }

    do
    {
        rc = clOsalMutexDelete(pBitmapInfo->bitmapLock);
    }while( CL_OSAL_ERR_MUTEX_EBUSY == rc );

    clHeapFree(pBitmapInfo->pBitmap);
    clHeapFree(pBitmapInfo);

    CL_DEBUG_PRINT(CL_DEBUG_TRACE, ("Exit"));
    return rc;
}
static ClRcT fileEntryRecoverBaseVersion(ClLogMasterEoDataT *pMasterEoEntry,
        ClBufferHandleT hFileEntryBuf)
{
    ClRcT rc = CL_OK;
    ClLogFileKeyT        fileKey       = {{0}};
    ClLogFileKeyT        *pFileKey     = NULL;
    ClLogFileDataT       *pFileData    = NULL;

    rc = clLogMasterFileKeyUnpack(&fileKey, hFileEntryBuf);
    if( CL_OK != rc )
    {
        return rc;
    }
    clLogInfo(CL_LOG_AREA_MASTER, CL_LOG_CTX_CKPT_READ,
              "Recreating files fileName: %.*s fileLocation: %.*s",
              fileKey.fileName.length, fileKey.fileName.pValue,
              fileKey.fileLocation.length, fileKey.fileLocation.pValue);

    rc = clLogFileKeyCreate(&fileKey.fileName, &fileKey.fileLocation,
                            pMasterEoEntry->maxFiles, &pFileKey);
    if( CL_OK != rc )
    {
        clHeapFree(fileKey.fileName.pValue);
        clHeapFree(fileKey.fileLocation.pValue);
        return rc;
    }
    clHeapFree(fileKey.fileName.pValue);
    clHeapFree(fileKey.fileLocation.pValue);

    /* find out the filelocation is available */
    pFileData = (ClLogFileDataT*) clHeapCalloc(1, sizeof(ClLogFileDataT));
    if( NULL == pFileData )
    {
        CL_LOG_DEBUG_ERROR(("clHeapCalloc()"));
        clLogFileKeyDestroy(pFileKey);
        return rc;
    }

    pFileData->nActiveStreams = 0;
    rc = clLogMasterFileDataRecreate(pFileData, hFileEntryBuf);
    if( CL_OK != rc )
    {
        clLogFileKeyDestroy(pFileKey);
        return rc;
    }

    rc = clCntNodeAdd(pMasterEoEntry->hMasterFileTable,
                      (ClCntKeyHandleT) pFileKey,
                      (ClCntDataHandleT) pFileData, NULL);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clCntNodeAdd()"));
        CL_LOG_CLEANUP(clCntDelete(pFileData->hStreamTable), CL_OK); //FIXME
        clHeapFree(pFileData);
        clLogFileKeyDestroy(pFileKey);
        return rc;
    }

    return rc;
}
ClRcT clEoClientCallbackDbFinalize(void)
{
    ClRcT rc;
    ClUint32T i;
    
    rc = clOsalMutexLock(&gpCallbackDb.lock);
    if(rc != CL_OK)
    {
        /* Print a message here */
        return rc;
    }

    for(i = 0 ; i < gpCallbackDb.numRecs; i++)
    {
        if(gpCallbackDb.pDb[i] != NULL)
            clHeapFree(gpCallbackDb.pDb[i]);
    }

    clHeapFree(gpCallbackDb.pDb);

    gpCallbackDb.numRecs = 0;

    clOsalMutexUnlock(&gpCallbackDb.lock);

    return CL_OK;
}
void timeStamp(ClCharT timeStr [], ClUint32T    len)
{
    time_t  timer ;
    struct  tm *t = NULL;
    ClCharT * pBuff =NULL;

   struct tm *localtimebuf = clHeapAllocate(sizeof(struct tm));;
   if(localtimebuf == NULL)
   {
      CL_DEBUG_PRINT(CL_DEBUG_ERROR,( "\nMemory Allocation failed\n"));
      return ;
   }
   ClCharT *asctimebuf   = clHeapAllocate(BUFFER_LENGTH*sizeof(ClCharT));
   if(asctimebuf == NULL)
   {
      CL_DEBUG_PRINT(CL_DEBUG_ERROR,( "\nMemory Allocation failed\n"));
      return ;
   }

   memset(&timer,0,sizeof(time_t));
    time(&timer);
    t = localtime_r(&timer,localtimebuf);
    pBuff=asctime_r(t,asctimebuf);
    strncpy(timeStr,pBuff,len-1);
    timeStr[strlen(timeStr) - 1] ='\0';
	clHeapFree(localtimebuf);
	clHeapFree(asctimebuf);
}
static void clCorNodeNameTableMoIdDestroyCallBack(ClCntKeyHandleT userKey, 
				ClCntDataHandleT userData)
{
	/* The key is MOId and data is node name. Just deallocate it. */
	clHeapFree((void *) userKey);
	clHeapFree((void *) userData);
}
void
clLogSvrSOFGResponse(ClIdlHandleT  hLogIdl,
                     SaNameT       *pStreamName,
                     SaNameT       *pStreamScopeNode,
                     ClLogFilterT  *pStreamFilter,
                     ClRcT         retCode,
                     ClPtrT        pCookie)
{
    ClRcT  rc = CL_OK;

    CL_LOG_DEBUG_TRACE(("Enter"));

    CL_ASSERT( NULL != pCookie );
    if( CL_OK != retCode )
    {
        CL_LOG_DEBUG_ERROR(("clLogSvrSOFGResponse(): rc[0x %x]", retCode));
        return ;
    }
    rc = clLogSvrSOFGResponseProcess(pStreamFilter, pCookie);

    clHeapFree(pStreamFilter->pMsgIdSet);
    clHeapFree(pStreamFilter->pCompIdSet);

    CL_LOG_DEBUG_TRACE(("Exit"));
    (void)rc;
    return ;
}
static void  dispatchQDestroyCallback (ClUint32T callbackType,
                                       void*     callbackData)
{
	if (callbackData == NULL)
	{
		return;
	}

    switch (callbackType)
    {
        case CL_GMS_CLIENT_CLUSTER_TRACK_CALLBACK:
            if (((SaClmClusterTrackDataT*)callbackData)->notificationBuffer != NULL)
            {
                if (((SaClmClusterTrackDataT*)callbackData)->notificationBuffer->notification != 
                        NULL)
                {
                    clHeapFree(((SaClmClusterTrackDataT*)callbackData)->notificationBuffer->notification);
                }
                clHeapFree(((SaClmClusterTrackDataT*)callbackData)->notificationBuffer);
            }
            break;
        case CL_GMS_CLIENT_CLUSTER_MEMBER_GET_CALLBACK:
            if (((SaClmClusterNodeGetDataT*)callbackData)->clusterNode != NULL)
            {
                clHeapFree(((SaClmClusterNodeGetDataT*)callbackData)->clusterNode);
            }
            break;
    }
    clHeapFree(callbackData);
}
ClRcT
clTestSectionCreate(ClCkptHdlT  ckptHdl, 
                    ClUint32T   sectionIdx)
{
    ClRcT                             rc            = CL_OK;
    ClCkptSectionCreationAttributesT  secCreateAttr = {0};

    secCreateAttr.sectionId = clHeapCalloc(1, sizeof(ClCkptSectionIdT));
    if( NULL == secCreateAttr.sectionId ) 
    {
        return CL_OK;
    }
    secCreateAttr.expirationTime = CL_TIME_END;
    secCreateAttr.sectionId->id = clHeapCalloc(1, 15);
    if( NULL == secCreateAttr.sectionId->id ) 
    {
        clHeapFree(secCreateAttr.sectionId);
        return CL_OK;
    }
    snprintf((ClCharT *) secCreateAttr.sectionId->id,15, "section%d", sectionIdx);
    secCreateAttr.sectionId->idLen = strlen((ClCharT *) secCreateAttr.sectionId->id) + 1; 
    rc = clCkptSectionCreate(ckptHdl, &secCreateAttr, NULL, 0);
    if( CL_OK != rc )
    {
        return rc;
    }
    clHeapFree(secCreateAttr.sectionId->id);
    clHeapFree(secCreateAttr.sectionId);
    return CL_OK;
}
/**
 * Delete/free transaction-job definition
 */
ClRcT clTxnAppJobDelete(
        CL_IN   ClTxnAppJobDefnT *pTxnAppJob)
{
    CL_FUNC_ENTER();

    if (NULL == pTxnAppJob)
    {
        CL_FUNC_EXIT();
        clDbgCodeError(CL_ERR_NULL_POINTER, ("Null pointer on the job list"));
        return CL_ERR_NULL_POINTER;
    }

    CL_DEBUG_PRINT(CL_DEBUG_TRACE, ("Deleting txn-job %p", (ClPtrT) pTxnAppJob));

    if(pTxnAppJob->compList)
    {
        //clCntAllNodesDelete(pTxnAppJob->compList);
        clCntDelete(pTxnAppJob->compList);
    }

    if (pTxnAppJob->appJobDefn)
        clHeapFree( (ClPtrT) pTxnAppJob->appJobDefn);
    
    memset(pTxnAppJob, 0, sizeof(ClTxnAppJobDefnT));  /* to crash uses after delete */
    clHeapFree(pTxnAppJob);

    return (CL_OK);
}
static ClRcT clAmsEntityLocate(ClAmsEntityT *pEntity)
{
    ClRcT rc = CL_OK;
    register ClInt32T i;

    for(i = CL_AMS_ENTITY_TYPE_ENTITY + 1;  i < CL_AMS_ENTITY_TYPE_MAX + 1; ++i)
    {
        ClAmsEntityConfigT *pEntityConfig = NULL;
        pEntity->type = (ClAmsEntityTypeT) i;
        rc = clAmsMgmtEntityGetConfig(gClAmsEntityTriggerMgmtHandle,
                                      pEntity,
                                      &pEntityConfig);
        if(rc != CL_OK)
        {
            if(pEntityConfig)
                clHeapFree(pEntityConfig);

            continue;
        }
        memcpy(pEntity, pEntityConfig, sizeof(*pEntity));
        clHeapFree(pEntityConfig);
        return CL_OK;
    }
    return CL_AMS_RC(CL_ERR_NOT_EXIST);

}
/*
 * The cnt delete callback
 */
void clAlarmPayloadCntEntryDeleteCallback(ClCntKeyHandleT key, ClCntDataHandleT userData)
{
    CL_DEBUG_PRINT(CL_DEBUG_TRACE,("clCntNodeAdd deleting \n"));
    clHeapFree((ClAlarmPayloadCntT *)userData);
	clHeapFree((ClAlarmPayloadCntKeyT *)key);
	return;
}
static void clCorNodeNameTableNodeNameDestroyCallBack(ClCntKeyHandleT userKey, 
				ClCntDataHandleT userData)
{
	/* The data is moid and key is node Name. freeing it. */
    clHeapFree(((ClCorNodeDataPtrT)userData)->pMoId);
    clHeapFree((void *)userKey);
	clHeapFree((void *) userData);
}
/*
 * This Api is used for packaging the fault structure
 * on query from the debug prompt
 */
ClRcT 
clFaultRecordPack(ClFaultRecordPtr hRec,ClCharT** retStr){

	ClUint32T categoryStringIndex=0,severityStringIndex=0,probableCauseStringIndex=0;
	
    if(hRec->seqNum==0)
    {
        clFaultCliStrPrint(retStr, "no record found");
    }
    else
    {
        ClCharT *str1 = NULL;
        ClCharT *tmp = NULL;


        str1 = clHeapAllocate(2000);/* The allocation size is a close indication
                                       of the number of bytes used to store the
                                       fault record structure */	   
        if(!str1)
            return CL_FAULT_RC(CL_ERR_NO_MEMORY);

        tmp = clHeapAllocate(2000);/* The allocatino size is a close measure of
                                      the number of bytes used to store each 
                                      attribute of a fault record structure 
                                      attached with a meaningful message
                                      preceeding it */
        if(!tmp)
            return CL_FAULT_RC(CL_ERR_NO_MEMORY);

        sprintf (tmp,"existing record found\n");
        strcat(str1,tmp);

        categoryStringIndex = clFaultInternal2CategoryTranslate((hRec->event).category);
        sprintf (tmp," Category........... %s\n",clFaultCategoryString[categoryStringIndex]);
        strcat(str1,tmp);

        sprintf (tmp," SpecificProblem.... %d\n",(hRec->event).specificProblem);
        strcat(str1,tmp);

        severityStringIndex = clFaultInternal2SeverityTranslate((hRec->event).severity);
        sprintf (tmp," Severity........... %s\n",clFaultSeverityString[severityStringIndex]);
        strcat(str1,tmp);

        if((hRec->event).cause >= CL_ALARM_PROB_CAUSE_LOSS_OF_SIGNAL &&
               (hRec->event).cause <= CL_ALARM_PROB_CAUSE_ENCLOSURE_DOOR_OPEN)
            probableCauseStringIndex = (hRec->event).cause;
        sprintf (tmp," cause.............. %s\n",clFaultProbableCauseString[probableCauseStringIndex]);
        strcat(str1,tmp);

        sprintf (tmp," SequenceNumber..... %d\n",(hRec->seqNum)-1);
        strcat(str1,tmp);
        clFaultCliStrPrint(retStr, str1);
        clHeapFree(str1);
        clHeapFree(tmp);
    }
	return CL_OK;
}
static void differenceVectorDelete(ClDifferenceBlockT *block)
{
    hashDel(&block->hash);
    if(block->data) clHeapFree(block->data);
    clDifferenceVectorKeyFree(&block->key);
    if(block->md5List)
        clHeapFree(block->md5List);
    clHeapFree(block);
}
/**
 * Release resources for allocated class
 *  
 * This routine undoes the operation that was performed in the
 * clOmClassInitialize () Object Manager Library interface. This is called to
 * release the resources that was allocated to facilitate a clean
 * unload of the Object Manager Library.
 *                                                                       
 * @param pClassTab - Class control block.
 * @param classId - Class Id of the class
 * 
 * @returns CL_OK if all the clean up operations are successful
 */
ClRcT 
clOmClassFinalize(ClOmClassControlBlockT *pClassTab, ClOmClassTypeT classId)
{
	ClUint32T   maxEntries;
	ClUint32T   instCount;
	ClUint32T **tmpInstance;

	CL_ASSERT(pClassTab);
    CL_FUNC_ENTER();

	/* TODO: Delete this module from the debug agents module list */

	/* Validate the input arguments */
	if (omClassTypeValidate(classId) != CL_OK)
	{
		CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Invalid input arguments")); 
		return (CL_OM_SET_RC(CL_OM_ERR_INVALID_CLASS));
	}

	/*
	 * OPEN_ISSUE: Check if the instance table for the specified 
	 * class is already released. If released take either of two 
	 * actions.
	 * 1. Silently discard the request and return.
	 * 2. Return an ERROR indicating a duplicate free.
	 */
	if (!pClassTab->pInstTab)
	{
		CL_DEBUG_PRINT(CL_DEBUG_TRACE, 
                   ("Duplicate free of Instance Table for this class"));
		return (CL_OM_SET_RC(CL_OM_ERR_INSTANCE_TAB_DUPFREE));
	}

	/*
	 * Now, run through the instance table and free all the memory 
	 * allocated in OM context. Use the reserved bits encoded within
	 * the address to determine this.
	 */
        maxEntries = pClassTab->maxObjInst;	
	tmpInstance = pClassTab->pInstTab;	
	for (instCount = 0; instCount < maxEntries; instCount++)
	{
		if (tmpInstance[instCount] && mALLOC_BY_OM(tmpInstance[instCount]))
			clHeapFree((ClPtrT)mGET_REAL_ADDR(tmpInstance[instCount]));
	}

	/* Now, free the Instance table	for this class */
	clHeapFree((void *)tmpInstance);

	/* Now setup the Peer class table */
	pClassTab->pInstTab    = (ClUint32T **)NULL;
	pClassTab->curObjCount = 0;


   	CL_FUNC_EXIT();
	return (CL_OK);
}
ClUint32T clGetTable ( ClSnmpReqInfoT* reqInfo,
        void* data, 
        ClInt32T* pErrCode)
{
    ClMedOpT        opInfo;
    ClMedVarBindT   *tempVarInfo = NULL;
    ClInt32T        errorCode = CL_OK;
    ClInt32T        retVal = 0;
    ClCharT         oid[CL_SNMP_DISPLAY_STRING_SIZE];

    opInfo.varCount = 1;
    tempVarInfo =
        (ClMedVarBindT *) clHeapCalloc(1,opInfo.varCount * sizeof (ClMedVarBindT));   
    if (tempVarInfo == NULL)
    {
        clLogError("SNM","OPE", "Failed while allocating the varbind.");
        return (CL_RC(CL_CID_SNMP, CL_ERR_NO_MEMORY));
    }
    retVal = 0;
    strcpy(oid, reqInfo->oid);
    /*oid received till this point is that of the table. Add .1.1 to 
      get oid of the first entry in the table
     */
    strcat(oid,".1.1");

    opInfo.varInfo = tempVarInfo;
    opInfo.varInfo[0].pVal = data;
    opInfo.varInfo[0].errId = 0;
    opInfo.varInfo[0].pInst = reqInfo;
    opInfo.opCode = reqInfo->opCode;
    opInfo.varInfo[0].attrId.id = (ClUint8T*)oid;
    opInfo.varInfo[0].attrId.len = (ClUint32T)strlen(oid) + 1;
    opInfo.varInfo[0].len = reqInfo->dataLen;

    errorCode = clMedOperationExecute (gSubAgentInfo.medHdl, &opInfo);

    if ((opInfo.varInfo[0].errId == 0) && (errorCode == 0))
    {
        clHeapFree (tempVarInfo);
        return (CL_OK);
    }
    else
    {
        if (errorCode != CL_OK)
            *pErrCode = errorCode;
        else
            *pErrCode = opInfo.varInfo[0].errId;

        clLogError(CL_LOG_AREA_UNSPECIFIED,CL_LOG_CONTEXT_UNSPECIFIED,
                   "clMedOperationExecute returned error. error code = %x, error id = %d",
                   errorCode, opInfo.varInfo[0].errId);
    }
    clHeapFree (tempVarInfo);
    return (errorCode);
}
/*----------------------------------------------------------------------------
 *  Cluster Track Callback Handler
 *---------------------------------------------------------------------------*/
ClRcT clGmsClusterTrackCallbackHandler(
    CL_IN   ClGmsClusterTrackCallbackDataT* const res)
{
    ClRcT rc = CL_OK;
    struct gms_instance *gms_instance_ptr = NULL;
    ClGmsHandleT gmsHandle = CL_GMS_INVALID_HANDLE;

    CL_ASSERT(res != NULL);
    clLog(INFO,NA,NA,"received cluster track callback");

    gmsHandle = res->gmsHandle;
    rc = clHandleCheckout(gmsHandleDb, gmsHandle, (void**)&gms_instance_ptr);
    if (rc != CL_OK)
    {
        goto error_free_res;
    }

    if (gms_instance_ptr == NULL)
    {
        rc = CL_GMS_RC(CL_ERR_NULL_POINTER);
        goto error_free_res;
    }

    if (gms_instance_ptr->callbacks.clGmsClusterTrackCallback == NULL)
    {
        rc = CL_GMS_RC(CL_ERR_NO_CALLBACK);
        goto error_checkin_free_res;
    }

    /*
     * Calling the user's callback function with the data.  The user cannot
     * free the data we provide.  If it needs to reatin it, it has to copy
     * it out from what we provide here.
     */
            (*gms_instance_ptr->callbacks.clGmsClusterTrackCallback)
            (gmsHandle, &res->buffer, res->numberOfMembers, res->rc);
  

error_checkin_free_res:
    if (clHandleCheckin(gmsHandleDb, gmsHandle) != CL_OK)
    {
        clLogError(CLM,NA,
                   "\nclHandleCheckin failed");
    }

error_free_res:
    /* Need to free data (res) if are not able to call the actual callback */
    if (res->buffer.notification != NULL)
    {
        clHeapFree((void*)res->buffer.notification);
    }
    clHeapFree((void*)res);
    return rc;
}
void
clLogClntFileKeyDestroy(ClLogClntFileKeyT  *pFileKey)
{
    CL_LOG_DEBUG_TRACE(("Enter"));

    clHeapFree(pFileKey->fileLocation.pValue);
    clHeapFree(pFileKey->fileName.pValue);
    clHeapFree(pFileKey);

    CL_LOG_DEBUG_TRACE(("Exit"));
    return ;
}
ClRcT VDECL(_corObjectGetNextOp) (ClUint32T cData, ClBufferHandleT  inMsgHandle,
                                  ClBufferHandleT  outMsgHandle)
{
    ClRcT rc = CL_OK;
    corObjGetNextInfo_t* pData = NULL; 
    CL_FUNC_ENTER();

    if(gCorInitStage == CL_COR_INIT_INCOMPLETE)
    {
        clLogError("OBN", "EXP", "The COR server Initialization is in progress....");
        return CL_COR_SET_RC(CL_COR_ERR_TRY_AGAIN);
    }

    pData = clHeapAllocate(sizeof(corObjGetNextInfo_t));
    if(pData == NULL)
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ( "NULL input parameter") );
        return CL_COR_SET_RC(CL_COR_ERR_NULL_PTR);   
    }
    if((rc = clXdrUnmarshallcorObjGetNextInfo_t(inMsgHandle, (void *)pData)) != CL_OK)
	{
		clHeapFree(pData);
		CL_DEBUG_PRINT(CL_DEBUG_ERROR,("Failed to Unmarshall corObjGetNextInfo_t"));
		return rc;
	}

  	clCorClientToServerVersionValidate(version, rc);
  	if(rc != CL_OK)
	{
		clHeapFree(pData);
		return CL_COR_SET_RC(CL_COR_ERR_VERSION_UNSUPPORTED);
	}
	
    switch(pData->operation)
    {
        case COR_OBJ_OP_NEXTMO_GET:
		{
              ClCorObjectHandleT nxtOH;
              rc =  _clCorNextMOGet(pData->objHdl, pData->classId, pData->svcId, &nxtOH);
              /* Write to the message*/
              clBufferNBytesWrite (outMsgHandle, (ClUint8T *)&nxtOH, sizeof(ClCorObjectHandleT));
        } 
        break;
        default:
             CL_DEBUG_PRINT(CL_DEBUG_ERROR, ( "INVALID OPERATION, rc = %x", rc) );
             rc = CL_COR_SET_RC(CL_COR_ERR_INVALID_PARAM);
        break;
    }
   
    CL_FUNC_EXIT();
	clHeapFree(pData);
    return rc;
}
/*
 * The cnt add function
 * The information is read from COR and then populated in the container
 * This api would be called as and when the object is created and the corresponding entry 
 * within the container needs to be added.
 */
ClRcT clAlarmPayloadCntAdd(ClAlarmInfoT *pAlarmInfo)
{
	ClRcT rc = CL_OK;
	ClCntNodeHandleT nodeH;
    ClAlarmPayloadCntT *payloadInfo;
    
    ClAlarmPayloadCntKeyT *pCntKey = clHeapAllocate(sizeof(ClAlarmPayloadCntKeyT));
    if(NULL == pCntKey)
    {
          CL_DEBUG_PRINT (CL_DEBUG_CRITICAL,("Memory allocation failed with rc 0x%x ", rc));
          return CL_ALARM_RC(CL_ALARM_ERR_NO_MEMORY);
    }    

    payloadInfo = NULL;
    pCntKey->probCause = pAlarmInfo->probCause;
    pCntKey->specificProblem = pAlarmInfo->specificProblem;
    pCntKey->moId = pAlarmInfo->moId;

    payloadInfo = clHeapAllocate(sizeof(ClAlarmPayloadCntT)+pAlarmInfo->len);
    if(NULL == payloadInfo)
    {
          CL_DEBUG_PRINT (CL_DEBUG_CRITICAL,("Memory allocation failed with rc 0x%x ", rc));
          clHeapFree(pCntKey);
          return CL_ALARM_RC(CL_ALARM_ERR_NO_MEMORY);
    }    
    payloadInfo->len = pAlarmInfo->len;
    memcpy(payloadInfo->buff, pAlarmInfo->buff, payloadInfo->len);

	clOsalMutexLock(gClAlarmPayloadCntMutex);        
    rc = clCntNodeFind(gPayloadCntHandle,(ClCntKeyHandleT)pCntKey,&nodeH);
	if(rc != CL_OK)
	{
		rc = clCntNodeAdd((ClCntHandleT)gPayloadCntHandle,
							(ClCntKeyHandleT)pCntKey,
							(ClCntDataHandleT)payloadInfo,
							NULL);
		if (CL_OK != rc)
		{
			CL_DEBUG_PRINT(CL_DEBUG_ERROR,("clCntNodeAdd failed with rc = %x\n", rc));
            clHeapFree(payloadInfo);
            clHeapFree(pCntKey);
        }
        CL_DEBUG_PRINT(CL_DEBUG_TRACE,("clCntNodeAdd adding payloadInfo->len : %d\n",payloadInfo->len));
	}
	else
	{
		CL_DEBUG_PRINT(CL_DEBUG_INFO,("Node already exist\n"));
        clHeapFree(payloadInfo);
        clHeapFree(pCntKey);
	}
	clOsalMutexUnlock(gClAlarmPayloadCntMutex);
	return rc;
}
/**
 * Callback function to delete service-registration information at agent
 */
void _clTxnCompServiceDelete(CL_IN ClCntKeyHandleT userKey, 
                             CL_IN ClCntDataHandleT userData)
{
    ClTxnAgentCompServiceInfoT   *pCompService = NULL;
    CL_DEBUG_PRINT(CL_DEBUG_TRACE, ("Deleting an entry for txn-agent component-service\n"));
    pCompService = (ClTxnAgentCompServiceInfoT *) userData;

    if(pCompService)
    {
        clHeapFree(pCompService->pCompCallbacks);
        clHeapFree(pCompService);
    }
}
static ClBoolT clCkptEntryExist(ClCachedCkptSvcInfoT *serviceInfo, const SaNameT *sectionName)
{
    ClBoolT                    retVal         = CL_FALSE;
    ClRcT                      rc;
    ClHandleT                  hSecIter       = CL_HANDLE_INVALID_VALUE;
    ClCkptSectionDescriptorT   secDescriptor  = {{0}};

    rc = clCkptSectionIterationInitialize(serviceInfo->ckptHandle,
                                          CL_CKPT_SECTIONS_ANY,
                                          CL_TIME_END, &hSecIter);
    if( CL_OK != rc )
    {
        clLogError("CCK", "EXIST", "clCkptSectionIterationInitialize(): rc [0x%x].",rc);
        goto out;
    }

    do
    {
        rc = clCkptSectionIterationNext(hSecIter, &secDescriptor);
        if( CL_OK != rc)
        {
            break;
        }

        if ((sectionName->length == secDescriptor.sectionId.idLen)
                && (memcmp(sectionName->value, secDescriptor.sectionId.id, sectionName->length)==0) )
        {
            clHeapFree(secDescriptor.sectionId.id);
            secDescriptor.sectionId.idLen = 0;
            retVal = CL_TRUE;
            goto out_free;
        }
        clHeapFree(secDescriptor.sectionId.id);
        secDescriptor.sectionId.id = NULL;
        secDescriptor.sectionId.idLen = 0;
    } while( (rc == CL_OK) );

out_free:
    if(hSecIter)
    {
        rc = clCkptSectionIterationFinalize(hSecIter);
        if( CL_OK != rc )
        {
            clLogError("CCK", "EXIST", "clCkptSectionIterationFinalize(): rc [0x%x].",rc);
        }
    }

out:
    return retVal;
}
ClRcT
clTestCkptRead_withTime(ClCkptHdlT  ckptHdl,
               ClUint32T   numSections)
{
    ClRcT           rc = CL_OK;
    ClUint32T       i = 0, j = 0;
    ClCkptIOVectorElementT  iov[numSections];
    ClUint32T               readIdx = 0;
    ClUint32T         timeDelay[] = {1000000, 100000, 10000, 1000, 100, 10 , 0};

    memset(iov, 0, numSections * sizeof(ClCkptIOVectorElementT));
    for( i = 0; i < numSections; i++ )
    {
        if( numSections != 1 )
        {
            iov[i].sectionId.id = clHeapCalloc(1, 15);
            if( NULL == iov[i].sectionId.id )
            {   
                return CL_OK;
            }
            snprintf( (ClCharT *) iov[i].sectionId.id,15, "section%d", i);
            iov[i].sectionId.idLen = strlen((ClCharT *) iov[i].sectionId.id) + 1;
        }
        iov[i].dataSize = 0;
        iov[i].dataBuffer = NULL;
        iov[i].readSize = 0;
        iov[i].dataOffset = 0;
    }
    for( i = 0; i < (sizeof(timeDelay) / sizeof(sizeof(timeDelay[0]))); i++ )
    {
        for( j = 0; j < 1000; j++ )
        {
            rc = clCkptCheckpointRead(ckptHdl, iov, numSections, &readIdx); 
            for( i = 0; i < numSections; i++ )
            {
                clHeapFree(iov[i].dataBuffer);
                iov[i].dataBuffer = NULL; 
            }
            usleep(timeDelay[i]);
        }
    }
    for( i = 0; i < numSections; i++ )
    {
        if( NULL != iov[i].sectionId.id ) 
        {
            clHeapFree(iov[i].sectionId.id);
        }
    }
    return CL_OK;
}
ClRcT clSnmpnameTableIndexTlvGet(ClPtrT pIndexInfo, ClAlarmUtilTlvInfoPtrT pTlvList)
{
    ClAlarmUtilTlvT * pTlv = NULL;
    ClSnmpnameTableIndexInfoT * pnameTableIndex = (ClSnmpnameTableIndexInfoT *)pIndexInfo;

    if(!pIndexInfo || !pTlvList)
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("NULL arguments received!"));
        return CL_ERR_NULL_POINTER;
    }
    pTlvList->pTlv = (ClAlarmUtilTlvT *) clHeapAllocate (1 * sizeof (ClAlarmUtilTlvT));
    if (pTlvList->pTlv == NULL)
    {
        clLog(CL_LOG_ERROR, CL_SNMP_AREA, CL_SNMP_GEN_OP_COTX, "nameTableIndexTlvGet unable to allocate memory!");
        return (CL_ERR_NO_MEMORY);
    }
    pTlvList->numTlvs = 1;

    pTlv = pTlvList->pTlv + 0;
    pTlv->value = clHeapAllocate (sizeof (ClUint32T));
    if (pTlv->value == NULL)
    {
        clHeapFree(pTlvList->pTlv);
        return (CL_ERR_NO_MEMORY);
    }
    memcpy(pTlv->value, &(pnameTableIndex->nodeAdd), sizeof (ClUint32T));
    pTlv->length = sizeof (ClUint32T);
    pTlv->type = CL_COR_UINT32;
    return CL_OK;
}
static ClRcT clAmsMgmtOIMoIdGet(ClAmsMgmtHandleT handle,
                                ClAmsEntityT *pEntity,
                                ClCorMOIdT *pMoId)
{
    ClCharT *userData = NULL;
    ClUint32T dataSize = 0;
    ClRcT rc = CL_OK;

    if(!pEntity || !pMoId)
        return CL_AMS_RC(CL_ERR_INVALID_PARAMETER);

    rc = clAmsMgmtEntityUserDataGetKey(handle, pEntity, &pEntity->name,
                                       &userData, &dataSize);
    if(rc != CL_OK)
    {
        clLogError("AMF", "MGMT", "Entity user data get key for [%s] returned [%#x]",
                   pEntity->name.value, rc);
        return rc;
    }

    if(!userData || !dataSize)
        return CL_AMS_RC(CL_ERR_INVALID_STATE);

    rc = clCorMoIdUnpack(userData, dataSize, pMoId);
    clHeapFree(userData);
    return rc;
}
ClRcT clIdlFree(void *pData)
{
    /* This is expected behavior, marshalling allocates using clHeapAllocate, so no need for warning */
    /* clLogWarning(CL_LOG_AREA_UNSPECIFIED,CL_LOG_CONTEXT_UNSPECIFIED,"Warning! clIdlFree in file %s uses clHeapFree for freeing memory allocated by server function.\n", __FILE__); */
    clHeapFree(pData);
    return CL_OK;    
}
/*
 * This api would be called as and when the object is no more and the corresponding entry 
 * within the container needs to be deleted.
 */
ClRcT clAlarmPayloadCntDelete(ClCorMOIdPtrT pMoId, ClAlarmProbableCauseT probCause, ClAlarmSpecificProblemT specificProblem)
{
	ClRcT rc = CL_OK;
	ClCntNodeHandleT nodeH;
    ClAlarmPayloadCntKeyT *pCntKey = clHeapAllocate(sizeof(ClAlarmPayloadCntKeyT));
    if(NULL == pCntKey)
    {
          CL_DEBUG_PRINT (CL_DEBUG_CRITICAL,("Memory allocation failed with rc 0x%x ", rc));
          return (rc);
    }    
    pCntKey->probCause = probCause;
    pCntKey->specificProblem = specificProblem;
    pCntKey->moId = *pMoId;

    clOsalMutexLock(gClAlarmPayloadCntMutex);        
    rc = clCntNodeFind(gPayloadCntHandle,(ClCntKeyHandleT)pCntKey,&nodeH);
	if(CL_OK == rc)
	{
    	rc = clCntAllNodesForKeyDelete(gPayloadCntHandle,(ClCntKeyHandleT)pCntKey);
    	if (CL_OK != rc)
	    {
        	CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("clCntAllNodesForKeyDelete failed w rc:0x%x\n", rc));
	    }        
	}
	else
	{
		CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Node does not exist with rc:0x%x\n", rc));
	}
    clHeapFree(pCntKey);
	clOsalMutexUnlock(gClAlarmPayloadCntMutex);    
	return rc;
}