static ClRcT
clBitmapRealloc(ClBitmapInfoT  *pBitmapInfo, 
                ClUint32T      bitNum)
{
    ClRcT      rc     = CL_OK;
    ClUint32T  nBytes = 0;
    ClUint32T  nInit  = 0;

    CL_DEBUG_PRINT(CL_DEBUG_TRACE, ("Enter: %u", bitNum));

    nBytes = bitNum / CL_BM_BITS_IN_BYTE;
    ++nBytes; /* bitNum is 0-based */
    pBitmapInfo->pBitmap = clHeapRealloc(pBitmapInfo->pBitmap, nBytes); 
    if( NULL == pBitmapInfo->pBitmap )
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("clHeapRealloc() failed"));
        return CL_BITMAP_RC(CL_ERR_NO_MEMORY); 
    }
    CL_DEBUG_PRINT(CL_DEBUG_TRACE, ("Reallocated: %u from %u", nBytes, 
                   pBitmapInfo->nBytes));

    nInit = nBytes - pBitmapInfo->nBytes;
    memset(&(pBitmapInfo->pBitmap[pBitmapInfo->nBytes]), 0, nInit);
    pBitmapInfo->nBytes = nBytes;

    CL_DEBUG_PRINT(CL_DEBUG_TRACE, ("Exit"));
    return rc;
}
ClRcT
cosSysvShmSizeGet(ClOsalShmIdT shmId,ClUint32T* pSize)
{
    struct shmid_ds shmSize ;
    ClInt32T retCode = CL_OK;

    CL_FUNC_ENTER();
    if(NULL == pSize)
    {
        CL_DEBUG_PRINT (CL_DEBUG_TRACE,("\nShared Memory SecurityModeGet: FAILED"));
        retCode = CL_OSAL_RC(CL_ERR_NULL_POINTER);
        CL_FUNC_EXIT();
        return(retCode);
    }

    /* Get the current values set and modifiy it */
    retCode = shmctl ((int)shmId, IPC_STAT, &shmSize);

    if(0 != retCode)
    {
        CL_DEBUG_PRINT (CL_DEBUG_TRACE,("\nShared Memory SecurityModeGet: FAILED"));
        retCode = CL_OSAL_RC(CL_OSAL_ERR_SHM_SIZE);
        CL_FUNC_EXIT();
        return(retCode);
    }

    *pSize = (ClUint32T)shmSize.shm_segsz;

    CL_FUNC_EXIT();
    return (CL_OK);
}
ClPtrT clMemPartRealloc(ClMemPartHandleT handle, ClPtrT memBase, ClUint32T size)
{
    MEM_PART_STATS memPartStats;
    ClPtrT mem = NULL;
    ClMemPartT *pMemPart = NULL;
    ClRcT rc = CL_OK;

    if(!(pMemPart = (ClMemPartT*)handle) )
        return NULL;

    if(!size) size = 16;

    clOsalMutexLock(&pMemPart->mutex);
    if(memPartInfoGet(pMemPart->partId, &memPartStats) == ERROR)
    {
        clOsalMutexUnlock(&pMemPart->mutex);
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("memPartInfoGet for size [%d] failed with [%s]\n", size, strerror(errno)));
        return mem;
    }
    if(size >= memPartStats.numBytesFree)
    {
        if( (rc = clMemPartExpand(pMemPart, CL_MEM_PART_EXPANSION_SIZE) ) != CL_OK)
        {
            /* do nothing now and fall back to realloc.*/
        }
    }
    mem = memPartRealloc(pMemPart->partId, memBase, size);
    clOsalMutexUnlock(&pMemPart->mutex);
    if(!mem)
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Mem part realloc failure for size [%d]\n", size));
        CL_ASSERT(0);
    }
    return mem;
}
/**
 *NOT AN EXTERNAL API. PLEASE DO NOT DOCUMENT.
 *                                                                       
 * Validates the object handle that is provided as an input argument to
 * this routine.
 */
ClRcT
omValidateHandle(ClHandleT handle)
{
	ClOmClassControlBlockT * pTab;
	ClOmClassTypeT   classId = CL_OM_HANDLE_TO_CLASSID(handle);
	ClUint32T instId  = CL_OM_HANDLE_TO_INSTANCEID(handle);

    CL_FUNC_ENTER();

    /* validate the input argument class Id field */
	if (omClassTypeValidate(classId) != CL_OK)
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR,
                ("Invalid classID field within handle argument"));
	return (CL_OM_SET_RC(CL_OM_ERR_INVALID_CLASS));
    }
    pTab = clOmClassEntryGet(classId);
	CL_ASSERT(pTab);

    if (instId > pTab->maxObjInst)
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR,
                ("Invalid instance ID field within handle argument"));
	return (CL_OM_SET_RC(CL_OM_ERR_INVALID_OBJ_INSTANCE));
    }

    if (!mGET_REAL_ADDR(pTab->pInstTab[instId]))
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR,
                ("The Instance Table for this class is not initialized"));
	return (CL_OM_SET_RC(CL_OM_ERR_INSTANCE_TAB_NOINIT));
    }
    CL_FUNC_EXIT();
    return (CL_OK);
}
ClRcT
cosSysvShmSecurityModeGet(ClOsalShmIdT shmId,ClUint32T* pMode)
{
    struct shmid_ds shmPerm ;
    ClInt32T retCode = CL_OK;

    CL_FUNC_ENTER();
    if(NULL == pMode)
    {
        CL_DEBUG_PRINT (CL_DEBUG_TRACE,("\nShared Memory SecurityModeGet: FAILED"));
        retCode = CL_OSAL_RC(CL_ERR_NULL_POINTER);
        CL_FUNC_EXIT();
        return(retCode);
    }

    /* Get the current values set and modify it */
    retCode = shmctl ((int)shmId, IPC_STAT, &shmPerm);

    if(0 != retCode)
    {
        CL_DEBUG_PRINT (CL_DEBUG_TRACE,("\nShared Memory SecurityModeGet: FAILED"));
        retCode = CL_OSAL_RC(CL_OSAL_ERR_SHM_MODE_GET);
        CL_FUNC_EXIT();
        return(retCode);
    }

    *pMode = shmPerm.shm_perm.mode;

    CL_DEBUG_PRINT (CL_DEBUG_TRACE,("\nShared Memory SecurityModeGet: DONE"));
    CL_FUNC_EXIT();
    return (CL_OK);
}
/**
 * Get number of instances for given OM.
 *
 * This routine returns the total number of instances that are managed
 * by Object Manager for given class.
 *
 * @param handle - OM Handle
 * @param numInstances - [OUT]number of instances the class has
 *
 * @returns CL_OK if number of instances could be obtained successfully
 * @returns  CL_OM_ERR_INVALID_CLASS if the class passed by means of handle is
 *								is not valid
 */
ClRcT
clOmNumberOfOmClassInstancesGet(ClHandleT handle, ClUint32T *numInstances)
{
	ClOmClassTypeT classId = CL_OM_HANDLE_TO_CLASSID(handle);
	ClOmClassControlBlockT * pTab = NULL;

	CL_FUNC_ENTER();
	CL_ASSERT(numInstances);

        /* sanity check for input parameters */
        if(numInstances == NULL)
        {
		CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("NULL input argument"));
		return (CL_OM_SET_RC(CL_OM_ERR_NULL_PTR));
	}

	if (omClassTypeValidate(classId) != CL_OK)
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR,
                ("Invalid classID field within handle argument"));
	return (CL_OM_SET_RC(CL_OM_ERR_INVALID_CLASS));
    }
    pTab = clOmClassEntryGet(classId);
	CL_ASSERT(pTab);
	*numInstances = pTab->curObjCount;

	CL_FUNC_EXIT();
	return (CL_OK);
}
/**
 * Get OM object reference given handle
 *
 * This routine returns the reference to the OM object, from the input
 * OM object handle
 *
 * @param pThis - OM Object Handle
 * @param objRef - [OUT]Object reference
 *
 * @returns CL_OK if number of instances could be obtained successfully
 * @returns  CL_OM_ERR_INVALID_OBJ_HANDLE if the object handle passed
 *								is not valid
 */
ClRcT 
clOmObjectReferenceByOmHandleGet(ClHandleT pThis, void **objRef)
{
	ClOmClassControlBlockT * 	pTab = NULL;
	ClOmClassTypeT     classId;
	ClUint32T    	instId;

	CL_FUNC_ENTER();

	CL_ASSERT(pThis);
	CL_ASSERT(objRef);

	if (!objRef)
	{
		CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("NULL pointer passed"));
		return (CL_OM_SET_RC(CL_OM_ERR_INVALID_OBJ_REF));
	}

	if (omValidateHandle(pThis) != CL_OK)
	{
             CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Invalid input handle argument"));
	     return (CL_OM_SET_RC(CL_OM_ERR_INVALID_OBJ_HANDLE));
	}

	classId = CL_OM_HANDLE_TO_CLASSID(pThis);
	instId  = CL_OM_HANDLE_TO_INSTANCEID(pThis);
        pTab = clOmClassEntryGet(classId);
	CL_ASSERT(pTab);
	*objRef = (char *)mGET_REAL_ADDR(pTab->pInstTab[instId]);
	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);
}
/**
 * Delete OM object given reference.
 *
 * This routine deletes the object given an input reference to the object
 *
 * @param pThis - Object reference of the object to be deleted.
 *
 * @returns CL_OK if the delete operation is successful
 *
 */
ClRcT
clOmObjectByReferenceDelete(void *pThis)
{
    ClHandleT handle;
    int rc;

    CL_FUNC_ENTER();

    CL_ASSERT(pThis);

    if (!pThis)
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("NULL pointer passed"));
        return (CL_OM_SET_RC(CL_OM_ERR_INVALID_OBJ_REF));
    }
    handle = ((struct CL_OM_BASE_CLASS *)pThis)->__objType;
    CL_DEBUG_PRINT(CL_DEBUG_TRACE, ("The Handle is %#llX, Obj reference = %p",
                                    handle, pThis));

    if ((rc = omCommonDeleteObj(handle, 1, CL_OM_DELETE_FLAG, NULL, 0)) !=
            CL_OK)
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Failed to delete OM object"));
        return (CL_OM_SET_RC(CL_OM_ERR_INVALID_OBJ_REF));
    }

    return (CL_OK);
}
ClRcT dbalGetLibName(ClCharT  *pLibName)
{
    ClParserPtrT   parent     = NULL ;   /*parent*/
    ClParserPtrT   engineType = NULL ;   /*parser ptr to Component*/
    ClParserPtrT   libName    = NULL ;   /*parser ptr to ckptName*/
    ClParserPtrT   fileName    = NULL ;   /*parser ptr to ckptName*/
    ClRcT          rc         = CL_OK;
    ClCharT       *configPath = NULL;

    configPath = getenv("ASP_CONFIG");
    if (configPath != NULL)
    {
        parent    = clParserOpenFile(configPath, "clDbalConfig.xml");
        if (parent == NULL)
        {
          CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Xml file for config is not proper rc [0x %x]\n",rc));
          return CL_ERR_NULL_POINTER;
        }
    }
    else
    {
        rc = CL_ERR_NULL_POINTER;
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("ASP_CONFIG path is not set in the environment irc[0x %x] \n",rc));
    }
    if(parent != NULL)
    {
       engineType = clParserChild(parent,"Dbal");
       libName    = clParserChild(engineType,"Engine");
       fileName   = clParserChild(libName,"FileName");
    }
    if(fileName != NULL && pLibName != NULL)
      strcpy(pLibName, fileName->txt); 
    if (parent != NULL) clParserFree(parent);
    return rc;
}
/* The debug versions of the heap routines */
static
ClPtrT
heapAllocateDebug(
        ClUint32T size)
{
    ClPtrT          pAddress    = NULL;
    ClPoolConfigT*  pPoolConfig = NULL;
    ClPoolConfigT   poolKey     = { 0 };
    ClPoolT         poolHandle;
    ClPtrT           pCookie     = NULL;
    ClRcT           rc;
    ClUint32T       overhead    = 0;

    /* Add the overhead over here for debug allocation.  */
    CL_HEAP_OVERHEAD_DEBUG (overhead);

    size += overhead;

    poolKey.chunkSize = size;

    /*
     * Do a binary search to get an index into the chunk config and hence
     * pool handle to allocate from.
     */
    rc = clBinarySearch ((ClPtrT)&poolKey, gHeapList.heapConfig.pPoolConfig,
            gHeapList.heapConfig.numPools, sizeof (ClPoolConfigT),
            clPoolCmp, (ClPtrT*)&pPoolConfig);
    if (rc != CL_OK)
    {
        CL_HEAP_MEM_TRACKER_TRACE(NULL,size);
        CL_DEBUG_PRINT (CL_DEBUG_ERROR,
                ("Error in getting pool handle for size:%d\n", size));
        goto out;
    }

    poolHandle = gHeapList.pPoolHandles [
        pPoolConfig - gHeapList.heapConfig.pPoolConfig ];

    rc = clPoolAllocate (poolHandle, (ClUint8T**) &pAddress, &pCookie);
    if (rc != CL_OK)
    {
        CL_HEAP_MEM_TRACKER_TRACE(NULL,size);
        CL_DEBUG_PRINT (CL_DEBUG_ERROR,
                ("Error allocating memory from pool size:%d for input "
                 "size:%d\n", pPoolConfig->chunkSize, size));
        goto out;
    }

    CL_HEAP_MEM_TRACKER_ADD (pAddress, pPoolConfig->chunkSize, pCookie);

    size -= overhead;

    CL_HEAP_SET_DEBUG_COOKIE (pAddress, pPoolConfig->chunkSize, size, pCookie);

    /*Zero off the contents till ASP gets cured*/
    memset (pAddress, 0, size);

    out:
    return (ClPtrT)pAddress;
}
/*
 * clBitmapCreate()
 */
ClRcT
clBitmapCreate(ClBitmapHandleT  *phBitmap, 
               ClUint32T        bitNum)
{
    ClRcT          rc           = CL_OK;

    CL_DEBUG_PRINT(CL_DEBUG_TRACE, ("Enter"));

    if ( NULL == phBitmap)
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Null Pointer"));
        return CL_BITMAP_RC(CL_ERR_NULL_POINTER);
    }

    *phBitmap = clHeapRealloc(NULL, sizeof(ClBitmapInfoT)); 
    if( NULL == *phBitmap)
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("clHeapRealloc() failed"));
        return CL_BITMAP_RC(CL_ERR_NO_MEMORY); 
    }
    
    rc = clBitmapInfoInitialize(*phBitmap, bitNum);
    if (rc != CL_OK)
    {
        clHeapFree(phBitmap);
        return rc;
    }
    
    CL_DEBUG_PRINT(CL_DEBUG_TRACE, ("Exit"));
    return rc; 
}
static ClRcT 
clBitmapInfoInitialize(ClBitmapInfoT  *pBitmapInfo, 
                       ClUint32T      bitNum)
{
    ClRcT          rc           = CL_OK;    

    CL_DEBUG_PRINT(CL_DEBUG_TRACE, ("Enter"));

    rc = clOsalMutexCreate(&(pBitmapInfo->bitmapLock));
    if( CL_OK != rc )
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("clOsalMutexCreate() rc: %x", rc));
        return rc;
    }

    pBitmapInfo->pBitmap  = NULL;
    pBitmapInfo->nBytes   = 0;
    pBitmapInfo->nBits    = 0;
    pBitmapInfo->nBitsSet = 0;
    rc = clBitmapRealloc(pBitmapInfo, bitNum);    
    if( CL_OK != rc )
    {
        clOsalMutexDelete(pBitmapInfo->bitmapLock);
        return rc;
    }
    pBitmapInfo->nBits = bitNum + 1; /* bitNum is 0-based FIXME i felt this is
                                      wrong*/
    CL_DEBUG_PRINT(CL_DEBUG_TRACE, ("Exit"));
    return rc;
}
ClRcT
clBitmap2BufferGet(ClBitmapHandleT  hBitmap,
                   ClUint32T        *pListLen,
                   ClUint8T         **ppPositionList)
{
    ClBitmapInfoT  *pBitmapInfo = hBitmap;
    ClRcT          rc           = CL_OK;

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

    rc = clOsalMutexLock(pBitmapInfo->bitmapLock);
    if( CL_OK != rc )
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("clOsalMutexLock() rc: %x", rc)); 
        return rc;
    }
    *ppPositionList = clHeapCalloc(pBitmapInfo->nBytes, sizeof(ClUint8T));
    if( NULL == *ppPositionList )
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("clHeapCalloc()"));
        clOsalMutexUnlock(pBitmapInfo->bitmapLock);
        return rc;
    }
    memcpy(*ppPositionList, pBitmapInfo->pBitmap, pBitmapInfo->nBytes);
    *pListLen = pBitmapInfo->nBytes;

    clOsalMutexUnlock(pBitmapInfo->bitmapLock);
    CL_DEBUG_PRINT(CL_DEBUG_TRACE, ("Exit"));
    return rc;
}
/**
 * Get class name
 * 
 * Gives class name given the OM Handle.
 *
 * @param handle - OM Handle
 * @param nameBuf - [OUT]Buffer where class name is copied
 *
 * @returns CL_OK if the handle is valid and class name is obtained
 *					successfully
 *@returns CL_OM_ERR_INVALID_OBJ_HANDLE if the passed object handle 
 *					 is invalid
 */
ClRcT
clOmClassNameGet(ClHandleT handle, char *nameBuf)
{
	ClOmClassControlBlockT * pTab = NULL;

	CL_FUNC_ENTER();
	CL_ASSERT(nameBuf);

	if (omValidateHandle(handle) != CL_OK)
	{
		CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Invalid input handle argument"));
		return (CL_OM_SET_RC(CL_OM_ERR_INVALID_OBJ_HANDLE));
	}

	if (!nameBuf)
	{
		CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("NULL name buffer passed"));
		return (CL_ERR_NULL_POINTER);
	}
    pTab = clOmClassEntryGet(CL_OM_HANDLE_TO_CLASSID(handle));
	CL_ASSERT(pTab);
	strcpy(nameBuf, (const char*) pTab->className);
	CL_FUNC_EXIT();
	return (CL_OK);
}
ClRcT
cosSysvMutexValueSet(ClOsalMutexIdT mutexId, ClInt32T value)
{
    ClOsalMutexT *pMutex = (ClOsalMutexT *) mutexId;
    ClRcT rc = CL_OK;
    ClInt32T semId = pMutex->shared_lock.sem.semId;
    ClInt32T err = 0;
    CosSemCtl_t arg = {0};

    CL_FUNC_ENTER();

    if(value >= SEMVMX)
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Error in setting sem value to [%d]\n", value));
        rc = CL_OSAL_RC(CL_ERR_INVALID_PARAMETER);
        goto out;
    }
    arg.val = value;
    err = semctl(semId, 0, SETVAL, arg);
    if(err < 0 )
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Mutex value set- semctl error: [%s]\n", strerror(errno)));
        rc = CL_OSAL_RC(CL_ERR_LIBRARY);
        goto out;
    }

    CL_FUNC_EXIT();
out:
    return rc;
}
/**
 * This routine returns the class of the super class of the parent class
 */
ClRcT
clOmParentClassGet(ClHandleT handle, ClOmClassTypeT *peParentClass)
{
	ClOmClassTypeT classId = CL_OM_HANDLE_TO_CLASSID(handle); 
	ClOmClassControlBlockT * pTab = NULL;

	CL_FUNC_ENTER();

	/* sanity check for input parameters */
	if(NULL == peParentClass)
	{
		CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("NULL input argument"));
		return CL_OM_SET_RC(CL_OM_ERR_NULL_PTR);
	}

	if (omClassTypeValidate(classId) != CL_OK)
	{
		CL_DEBUG_PRINT(CL_DEBUG_ERROR,
					("Invalid classID field within handle argument"));
		return CL_OM_SET_RC(CL_OM_ERR_INVALID_CLASS);
	}
	pTab = clOmClassEntryGet(classId);
	if(NULL == pTab)
	{
		CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Class Entry not found for classId %d\n",classId));
		return CL_OM_SET_RC(CL_OM_ERR_INVALID_CLASS);
	}
	*peParentClass = pTab->eParentObj;

	CL_FUNC_EXIT();
	return CL_OK;
}
ClRcT
cosSysvShmAttach(ClOsalShmIdT shmId,void* pInMem, void** ppOutMem)
{
    ClRcT retCode = 0;
    void* pShared = NULL;

    CL_FUNC_ENTER();
    if(NULL == ppOutMem)
    {
        CL_DEBUG_PRINT (CL_DEBUG_INFO,("\nShared Memory Attach: FAILED"));
        retCode = CL_OSAL_RC(CL_ERR_NULL_POINTER);
        CL_FUNC_EXIT();
        return(retCode);
    }

    pShared = shmat ((int)shmId, pInMem, 0);

    if((void*)-1 == pShared)
    {
        CL_DEBUG_PRINT (CL_DEBUG_INFO,("\nShared Memory Attach: FAILED"));
        retCode = CL_OSAL_RC(CL_OSAL_ERR_SHM_ATTACH);
        CL_FUNC_EXIT();
        return(retCode);
    }
    else
    {
        *ppOutMem = pShared;
    }

    CL_DEBUG_PRINT (CL_DEBUG_TRACE,("\nShared Memory Attach: DONE"));
    CL_FUNC_EXIT();
    return (CL_OK);
}
/**
 * Get class version
 *
 * This routine returns the current version that is associated with the
 * class of objects.
 *
 * @param handle - OM handle
 * @param version - [OUT] Version of th class
 *
 * @returns CL_OK if number of instances could be obtained successfully
 * @returns  CL_OM_ERR_INVALID_CLASS if the class passed by means of handle is
 *								is not valid
 */
ClRcT
clOmClassVersionGet(ClHandleT handle, ClUint32T *version)
{
	ClOmClassTypeT classId = CL_OM_HANDLE_TO_CLASSID(handle);
	ClOmClassControlBlockT * pTab = NULL;

	CL_ASSERT(version);
	CL_FUNC_ENTER();

	if (omClassTypeValidate(classId) != CL_OK)
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR,
                ("Invalid classID field within handle argument"));
	return (CL_OM_SET_RC(CL_OM_ERR_INVALID_CLASS));
    }

	if (!version)
	{
            CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("NULL version argument passed"));
  	    return (CL_ERR_INVALID_PARAMETER);
	}

    pTab = clOmClassEntryGet(classId);
	CL_ASSERT(pTab);
	*version = pTab->vVersion;

	CL_FUNC_EXIT();
	return (CL_OK);
}
ClRcT
cosSysvShmDetach(void* pMem)
{
    ClInt32T retCode =  CL_OK;

    CL_FUNC_ENTER();
    if(NULL == pMem)
    {
        CL_DEBUG_PRINT (CL_DEBUG_TRACE,("\nShared Memory Detach: FAILED"));
        retCode = CL_OSAL_RC(CL_ERR_NULL_POINTER);
        CL_FUNC_EXIT();
        return(retCode);
    }

    retCode = shmdt (pMem);

    if(0 != retCode)
    {
        CL_DEBUG_PRINT (CL_DEBUG_TRACE,("\nShared Memory Detach: FAILED"));
        retCode = CL_OSAL_RC(CL_OSAL_ERR_SHM_DETACH);
        CL_FUNC_EXIT();
        return(retCode);
    }

    CL_DEBUG_PRINT (CL_DEBUG_TRACE,("\nShared Memory Detach: DONE"));
    CL_FUNC_EXIT();
    return (CL_OK);
}
/**
 * Get OM Handle given object reference.
 *
 * This routine returns the OM object handle, given the reference to the
 * om Object.
 *
 * @param pThis - Object reference.
 * @param pHandle - [OUT] Object handle for given object.
 *
 * @returns CL_OK if number of instances could be obtained successfully
 * @returns  CL_OM_ERR_INVALID_OBJ_HANDLE if the object handle passed
 *								is not valid
 */ 
ClRcT 
clOmOmHandleByObjectReferenceGet(void *pThis, ClHandleT *pHandle)
{
	ClRcT		rc = CL_OK;
#ifdef CL_DEBUG
	ClUint8T	aFuncName[] = "clOmOmHandleByObjectReferenceGet()";
#endif

	CL_FUNC_ENTER();

	if (!pThis)
	{
		CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("%s: NULL pointer passed\r\n", aFuncName));
		rc = CL_OM_SET_RC(CL_OM_ERR_INVALID_OBJ_REF);
		goto ERROR_CASE;
	}

	if (!pHandle)
	{
		CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("%s: NULL pointer passed\r\n", aFuncName));
		rc = CL_OM_SET_RC(CL_OM_ERR_INVALID_OBJ_HANDLE);
		goto ERROR_CASE;
	}

	*pHandle = ((struct CL_OM_BASE_CLASS *)pThis)->__objType;
	return (CL_OK);

	ERROR_CASE: 
		if(pHandle)
			*pHandle = 0;
        return (rc);
}
static ClRcT clMemPartExpand(ClMemPartT *pMemPart, ClUint32T size)
{
    STATUS rc = 0;
    if(pMemPart->index >= CL_MEM_PART_EXPANSION_SLOTS)
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Mem part out of expansion slots trying to expand for size [%d]\n", size));
        return CL_ERR_NO_SPACE;
    }
    if(pMemPart->partitions[pMemPart->index])
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Mem part already expanded\n"));
        return CL_ERR_UNSPECIFIED;
    }
    pMemPart->partitions[pMemPart->index] = malloc(size);
    if(!pMemPart->partitions[pMemPart->index])
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Mem part expand calloc failure for size [%d]\n", size));
        return CL_ERR_NO_MEMORY;
    }
    if( (rc = memPartAddToPool(pMemPart->partId, pMemPart->partitions[pMemPart->index], size) ) == ERROR )
    {
        free(pMemPart->partitions[pMemPart->index]);
        pMemPart->partitions[pMemPart->index] = NULL;
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Mem part add to pool for size [%d] failed with [%s]\n",
                                        size, strerror(errno)));
        return CL_ERR_NO_MEMORY;
    }
    ++pMemPart->index;
    return CL_OK;
}
ClRcT 
clCorDataSave()
{
    ClRcT  rc = CL_OK;
    corPersisInfo_t corData;

	memset(&corData, 0, sizeof(corData));
    CL_FUNC_ENTER();
    CL_DEBUG_PRINT(CL_DEBUG_TRACE,("\n Inside clCorDataSave \n"));

    /* corData.version   = CL_COR_VERSION_NO; */
	CL_COR_VERSION_SET(corData.version);
    corData.operation = COR_DATA_SAVE;
    
    COR_CALL_RMD(COR_EO_PERSISTENCY_OP,
                 VDECL_VER(clXdrMarshallcorPersisInfo_t, 4, 0, 0),
                 &corData,
                 sizeof(corData), 
                 VDECL_VER(clXdrUnmarshallcorPersisInfo_t, 4, 0, 0),
                 NULL,
                 NULL,
                 rc);
    if(rc != CL_OK)
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ( "clCorDataSave returns error,rc=%x",rc));
    }

    
    CL_FUNC_EXIT();
    return rc;
}
/*Currently we are having another API clCorClassDelete for same purpose. However,
  it takes class handle as input. If we plan to remove it. This will be useful.*/
ClRcT
dmClassDelete(ClCorClassTypeT id)
{
    ClRcT ret = CL_OK;
    CORClass_h tmp = 0;
    
    CL_FUNC_ENTER();

    if((!dmGlobal))
      {
        CL_FUNC_EXIT();  
        return(CL_COR_SET_RC(CL_COR_ERR_NULL_PTR));
      }
    
    CL_DEBUG_PRINT(CL_DEBUG_TRACE, ( "ClassDelete (Class:%04x)", id));
    
    /* check if class already present 
     */
    HASH_GET(dmGlobal->classTable, id, tmp);
    if(tmp) 
      {
        ret = dmClassByHandleDelete(tmp);
      } 
    else 
      {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ( "ClassDelete (Class:%04x) [Unknown class]", 
                              id));
        
        ret = CL_COR_SET_RC(CL_COR_ERR_CLASS_NOT_PRESENT);
      }
    
    CL_FUNC_EXIT();
    return (ret);
}
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;
}
/**
 * API to restore recovery logs of previously active transactions
 * from check-point
 */
ClRcT clTxnServiceCkptRecoveryLogRestore()
{
    ClRcT       rc = CL_OK;
    if(!clTxnServiceCfg->dsblCkpt)
    {
        SaNameT     txnCkptName;

        CL_FUNC_ENTER();

        txnCkptName.length = strlen(CL_TXN_CKPT_NAME) + 1;
        memset(&(txnCkptName.value[0]), '\0', txnCkptName.length);
        strncpy(&(txnCkptName.value[0]), CL_TXN_CKPT_NAME, txnCkptName.length);

        CL_DEBUG_PRINT(CL_DEBUG_TRACE, ("Restoring transaction-recovery logs"));

        /* AD-1: To use the checkpoint exists api to check whether its recovery or
         * normal startup */
        clLogDebug("SER", NULL,
                   "Checkpoint exists, reading data for recovery");
        rc = clCkptLibraryCkptDataSetRead(clTxnServiceCfg->txnCkptHandle, &txnCkptName,
                                          CL_TXN_RECOVERY_DATA_SET, 0x0);

        if (CL_OK != rc)
        {
            CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Failed to restore transaction-service state. rc:0x%x", rc));
            rc = CL_GET_ERROR_CODE(rc);
        }
    }

    CL_FUNC_EXIT();
    return (rc);
}
/*
 * 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;
}
ClRcT clComponentTimerInit(ClEoExecutionObjT* peoObj)
{
    ClRcT    rc = CL_OK;
    ClTimerTimeOutT     ComponentTimeOut = {0,0};
    

    ComponentTimeOut.tsSec = 0;
    ComponentTimeOut.tsMilliSec = COMPONENT_CALLBACK_TIME;
    
    rc= clTimerCreate(ComponentTimeOut,
                      CL_TIMER_ONE_SHOT,
                      CL_TIMER_SEPARATE_CONTEXT,
                      clComponentCallBack,
                      (void*)peoObj,
                      &gComponentTimerHandle);

    if (CL_OK != rc)
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, (" clTimerCreate failed with rc: 0x%x \n", rc));
        return rc;
    }

    rc = clTimerStart(gComponentTimerHandle);
    if (CL_OK != rc)
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, (" clTimerStart failed with rc: 0x%x \n", rc));
        return rc;
    }

    return rc;
}
ClRcT
clNameContextCkptNameUnpack(ClBufferHandleT  inMsg)
{
    ClRcT             rc      = CL_OK;
    ClUint32T         key     = 0;
    ClUint32T         size    = 0;
    ClUint32T         count   = 0;
    ClIocNodeAddressT localAddr = 0;
    ClIocNodeAddressT masterAddr = 0;

    CL_NAME_DEBUG_TRACE(("Enter"));
    rc = clXdrUnmarshallClUint32T(inMsg, &size);
    if( CL_OK != rc )
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR,
                ("clXdrUnmarshallClUint32T(): rc[0x %x]", rc));
        return rc;
    }    
    clLogDebug("SVR", "CKP", "Num of contexts: %d", size);
    for( count = 0; count < size; count++)
    {    
        rc = clXdrUnmarshallClUint32T(inMsg, &key);
        if( CL_OK != rc )
        {
            return rc;
        }    
        if( (key > CL_NS_DEFAULT_GLOBAL_CONTEXT) && 
            (key < CL_NS_BASE_NODELOCAL_CONTEXT) )
        {
            localAddr = clIocLocalAddressGet();
            rc = clCpmMasterAddressGet(&masterAddr);
            if (rc != CL_OK)
            {
                CL_DEBUG_PRINT(CL_DEBUG_ERROR,("clCpmMasterAddressGet failed with rc 0x%x",rc));
                return rc;
            }

            if( masterAddr == localAddr )
            {
                rc = clNameSvcCtxRecreate(key);
                if( CL_OK != rc )
                {
                    return rc;
                }    
            }    
        }    
        else
        {    
            rc = clNameSvcCtxRecreate(key);
            if( CL_OK != rc )
            {
                return rc;
            }    
        } 
    }
    
    CL_NAME_DEBUG_TRACE(("Exit"));
    return rc;
}    
/*
 * Insert the class tab entry into the class id table.
 */
static ClRcT 
omClassEntryReload (ClCharT *pClassName, ClOmClassTypeT classId)
{
	ClRcT		rc = CL_OK;
    ClOmClassControlBlockT *pClassTab = NULL;
    ClCntNodeHandleT nodeHandle = 0;

    CL_FUNC_ENTER();

    if(!pClassName)
    {
        rc = CL_OM_SET_RC(CL_OM_ERR_NULL_PTR);
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("NULL PTR PASSED"));
        CL_FUNC_EXIT();
        return rc;
    }

	CL_DEBUG_PRINT(CL_DEBUG_TRACE, ("OM entry reload for class = %s, class type %d", pClassName, classId));

    /*
     * First check if the class id entry is already present in the om class id table.
     */
    if(clCntNodeFind(ghOmClassHashTbl, (ClCntKeyHandleT)(ClWordT)classId, &nodeHandle) == CL_OK)
        return CL_OK;

	/* Add this class entry into the OM class lookup table */
	rc = clCntNodeFind(ghOmClassNameHashTbl, (ClCntKeyHandleT)pClassName, &nodeHandle);
	if (rc != CL_OK)
    {
		CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Unable to find OM Class name [%s] entry in om class table",
                                        pClassName));
		return (rc);
    }
    
    rc = clCntNodeUserDataGet(ghOmClassNameHashTbl, nodeHandle, (ClCntDataHandleT*)&pClassTab);
    if(rc != CL_OK)
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Unable to get data for OM Class [%s] from om class name table",
                                        pClassName));
        return rc;
    }

    /*
     * Insert this entry into the class id table.
     */
    pClassTab->eMyClassType = classId;

    rc = clCntNodeAdd(ghOmClassHashTbl, (ClCntKeyHandleT)(ClWordT)pClassTab->eMyClassType,
                      (ClCntDataHandleT)pClassTab, NULL);
    if(rc != CL_OK)
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Unable to add entry [%#x] for class [%s] to OM class id table",
                                        classId, pClassName));
        return rc;
    }

   	CL_FUNC_EXIT();
	return (rc);
}