/*  ============================================================================
 *  Copyright (c) Texas Instruments Inc 2002, 2003, 2004, 2005
 *
 *  Use of this software is controlled by the terms and conditions found in the
 *  license agreement under which this software has been supplied.
 *  ===========================================================================
 */

/** @file csl_dmaxGetNextFreeParamEntry.c
 *
 *  @brief  File for functional layer of CSL 
 *					API @a CSL_dmaxGetNextFreeParamEntry()
 *
 *  Description
 *    - The @a CSL_dmaxGetNextFreeParamEntry() function definition & it's 
 *		associated functions
 *
 *  Modification 1
 *    - Modified on: 11/Mar/2005
 *    - Reason: created the sources
 * 
 *  @author asr.
 */

#include <csl_dmax.h>
extern CSL_DmaxResourceAlloc CslDmaxAlloc;
/** ===========================================================================
* @func CSL_dmaxGetNextFreeParamEntry
 *
 *   @b Description
 *   @n Searchs for next free ParamEntry for given resource type
 *
 * 
 *       
 *   @b Arguments
 *   @verbatim
 *    
 *          uid  		Pointer to Parameter Table Entry where data is stored
 *          st  		Status
 *           
 *   @endverbatim
 *
 *   <b> Return Value </b> CSL_Status
 *   @li                   CSL_SOK - Found next free Parameter Entry.     
 *
 *   @li                   CSL_ESYS_OVFL - Next free Parameter Entry not found
 *
 *
 *   <b> Pre Condition </b>
 *   @n  Both CSL_dmaxInit() and a CSL_dmaxOpen() must be called
 *
 *   <b> Post Condition </b>
 *   @n  None 
 *
 *   @b  Modifies
 *   @n  None
 *
 *   @b Example
 *   @verbatim  
    
      CSL_dmaxGetNextFreeParameterEntry(hDmax->paramUid, &status);
     @endverbatim
 * 
 * ============================================================================
 */
Uint32 CSL_dmaxGetNextFreeParamEntry (
    Uint32		uid,
    CSL_Status	*st
)
{
    Uint32			ndx = ((uid) >> 28 & 0xFFFFu) - 1;
    CSL_DmaxAlloc	*allocPtr = (CSL_DmaxAlloc *) (&CslDmaxAlloc);
    Uint32			found = 0, nextFreeBit = 0;
    Uint32			mask = 1;

    while ((found != 1) && (nextFreeBit <= allocPtr[ndx].maxCount)) {
        if ((allocPtr[ndx].allocMap & mask) != 0) {
            nextFreeBit++;
            mask = mask << 1;
        }
        else {
            found = 1;
        }
    }
    if (nextFreeBit >= allocPtr[ndx].maxCount) {
        *st = CSL_ESYS_OVFL;
        nextFreeBit = (Uint32) CSL_ESYS_OVFL;
    }
    else {
        *st = CSL_SOK;
    }
    return nextFreeBit;
}
