#include "LockList.h"
#include "extnDefn.h"

LockList*   localLockList = NULL;  /* Local list of locks */
int         reduceNotification = 0;  /* locks that must be deleted 
                                            when free */


/*

    deposit_locks_6()
    -------------------

               Arguments:
                     num, number of locks to be deposited;

     This function deposits locks to the local LockList.

*/

#ifdef NEW_VER_RPC

void*
deposit_locks_6_svc( int* num, struct svc_req *junk)

#else
    
void*
deposit_locks_6( int* num)

#endif    

{
    int         i;
    int         last;
    LockList*   current;
    LockInf     data;
    

    if( localLockList != NULL)
    {
        current =  GetLockInfLink( localLockList, 
                    LockListLength( localLockList));  
    }
    else
    {
        current = localLockList = (LockList* )malloc(sizeof(LockList));
        SetLockInfNull(localLockList);
    }

    last = current->Data.LockId;
    
    for( i = 0; i < *num; i++)
    {
        last++;
        data.LockId = last;
        data.Status = FREE;
        PutInLockList(localLockList, data, last + 1);
    }

		printf("\nNo of locks added = %d \n",*num);
    PrintLockList(localLockList);
}

/*

    remove_locks_6()
    -------------------

               Arguments:
                     num, number of locks to be removed;

 This function removes locks from the local LockList, if not allocated.
 else set reduceNotiifcation.

*/

#ifdef NEW_VER_RPC

void*
remove_locks_6_svc( int* num, struct svc_req *junk)

#else
    
void*
remove_locks_6( int* num)

#endif

{
    int i, pos;

    if( (localLockList == NULL) || ( localLockList->Data.LockId == -1))
    {
        error("list empty: no locks to be removed");
        return NULL; 
    }

    for( i = 0; i < *num; i++)
    {
        pos = GetFreeLock(localLockList);

        if( pos <  LockListLength(localLockList) + 1 )
        {
            DeleteLockInfLink(localLockList, pos);
        }
        else
        {
            break;
        }
    }

    if ( i != *num)
    {
        reduceNotification = reduceNotification + *num - i;
    }
    PrintLockList(localLockList);
    PrintInLogFile(" reduce = %d \n", reduceNotification);
}

/*

        set_lock_allocated_6()
        --------------------------

                        Arguments: 
                            Lock which is to be marked allocated;

This function set the lock allocated in the local LockList.

*/

#ifdef NEW_VER_RPC

void*
set_lock_allocated_6_svc( Lock* lock, struct svc_req *junk)

#else

void*
set_lock_allocated_6( Lock* lock)

#endif
    
{
    int         pos;
    LockList*   temp;
    
    if ((temp = GetLockLink( localLockList, &pos, lock->LockId)) == NULL)
    {
        error(" set_lock_allocated: lock non exixting");
    }
    else
    {
        (temp->Data).Status = ALLOCATED;
    }
}
    
/*

        set_lock_free_6()
        --------------------------

                        Arguments: 
                            Lock which is to be marked free;

This function set the lock free in the local LockList.

*/

#ifdef NEW_VER_RPC

void*
set_lock_free_6_svc( Lock* lock, struct svc_req *junk)

#else
    
void*
set_lock_free_6( Lock* lock)

#endif

{
    int         pos;
    LockList*   temp;
    
    if ((temp = GetLockLink( localLockList, &pos, lock->LockId)) == NULL)
    {
        error(" set_lock_free: lock non exixting");
    }
    else
    {
        (temp->Data).Status = FREE;
    }
}

