/* Deallocates source or destination memory, allocated. 
This function can be called to deallocate data memory that was DMAed successfully */
int dmacHw_freeMem(dmacHw_CONFIG_t *pConfig, void *pDescriptor, void(*fpFree)(void *))
/* Deallocates source or destination memory, allocated. 
This function can be called to deallocate data memory that was DMAed successfully */
int dmacHw_freeMem(dmacHw_CONFIG_t *pConfig, void *pDescriptor, void(*fpFree)(void *))
{
	dmacHw_DESC_RING_t *pRing = dmacHw_GET_DESC_RING(pDescriptor);
	uint32_t count = 0;
	if (fpFree == NULL) {
		return -1;
	}
	while ((pRing->pFree != pRing->pTail)
	       && (pRing->pFree->ctl.lo & dmacHw_DESC_FREE)) {
		if (pRing->pFree->devCtl == dmacHw_FREE_USER_MEMORY) {
			if (dmacHw_DST_IS_MEMORY(pConfig->transferType)) {
				(*fpFree) ((void *)pRing->pFree->dar);
			} else {
				(*fpFree) ((void *)pRing->pFree->sar);
			}
			pRing->pFree->devCtl = ~dmacHw_FREE_USER_MEMORY;
		}
		dmacHw_NEXT_DESC(pRing, pFree);
		count++;
	}
	return count;
}