queue.c

00001 /***************************************************************************
00002                           queue.c  -  description
00003                              -------------------
00004     begin                : Wed Jan 14 2004
00005     copyright            : (C) 2004 by Dynacube Team
00006     email                : mdshah82@yahoo.com
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018  #include "common/ds/queue.h"
00019 
00020   QUEUE ready_q;
00021   QUEUE device_q[MAX_DEVICE];
00022   QUEUE message_q;
00023   QUEUE intr_q;
00024   QUEUE timer_q;
00025   QUEUE gui_q;
00026   QUEUE fs_q;
00027   
00028   void q_init(QUEUE *q_ptr)
00029   {
00030     q_ptr->front = 0;
00031     q_ptr->end   = 0;
00032     q_ptr->max   = MAX_PROC;
00033     q_ptr->n     = 0;
00034   }
00035   
00036   SDW enq(QUEUE *q_ptr,DW pid)
00037   {
00038     DB i, j;
00039     
00040     for(i = q_ptr->front, j = 0 ; j < q_ptr->n ; j++)
00041     {
00042       if(q_ptr->q[i] == pid)
00043           return E_EXISTS;
00044           
00045       i = (i+1) % q_ptr->max;
00046     }
00047     
00048     if(q_ptr->n < q_ptr->max)
00049     {      
00050      q_ptr->q[q_ptr->end] = pid;
00051      q_ptr->end++;
00052      q_ptr->end %= q_ptr->max;
00053      q_ptr->n++;
00054     }
00055     else
00056       return E_OVERFLOW;    
00057   }
00058   
00059   SDW deq(QUEUE *q_ptr)
00060   {
00061     DW temp;
00062     
00063     if(q_ptr->n != 0)
00064     {
00065      temp = q_ptr->q[q_ptr->front];
00066      q_ptr->front++;
00067      q_ptr->front %= q_ptr->max;
00068      q_ptr->n--;
00069      enq(q_ptr,temp);
00070      return temp;
00071     }
00072     else
00073       return 0;        
00074   }
00075 
00076   SDW get_element(QUEUE *q_ptr, DW index)
00077   {
00078     if(index >= q_ptr->n)
00079       return E_FAILURE;
00080     else
00081     {
00082       return q_ptr->q[(q_ptr->front + index)%q_ptr->max];
00083     }
00084   }
00085 
00086   SDW get_first(QUEUE *q_ptr)
00087   {
00088     DW temp;
00089 
00090     if(q_ptr->n != 0)
00091     {
00092      temp = q_ptr->q[q_ptr->front];
00093      return temp;
00094     }
00095     return -1;
00096   }
00097 
00098   SDW remove(QUEUE *q_ptr, DW pid)
00099   {
00100    DB i, j;
00101    
00102    if(q_ptr->q[q_ptr->front] == pid && q_ptr->n > 0)
00103    {
00104      q_ptr->front = (q_ptr->front+1) % q_ptr->max;
00105      q_ptr->n--;
00106      return 0;
00107    }
00108    else
00109    {
00110     for(i = q_ptr->front, j = 0 ; j < q_ptr->n ; j++)
00111     {
00112       if(q_ptr->q[i] == pid)
00113       {
00114         for(;j < q_ptr->n-1;j++)
00115         {
00116          q_ptr->q[i] = q_ptr->q[(i+1)%q_ptr->max];
00117          i = (i+1) % q_ptr->max;         
00118         }
00119         q_ptr->n--;
00120         if(q_ptr->end != 0)
00121            q_ptr->end--;
00122         else
00123            q_ptr->end = q_ptr->max-1; 
00124         return 0;
00125       }
00126       
00127       i = (i+1) % q_ptr->max;
00128     }
00129    }
00130    
00131    return E_FAILURE;
00132   }
00133   
00134   SDW findq(QUEUE *q_ptr, DW pid)
00135   {
00136    DB i, j;
00137 
00138    for(i = q_ptr->front, j = 0 ; j < q_ptr->n ; j++)
00139     {
00140       if(q_ptr->q[i] == pid)
00141       {
00142         return 0;
00143       }
00144       i = (i+1) % q_ptr->max;
00145     }
00146     
00147     return E_FAILURE;   
00148   }
00149 
00150   void printQ(QUEUE *q_ptr)
00151   {
00152     DW i,j;
00153 
00154     for(i = q_ptr->front, j = 0 ; j < q_ptr->n ; j++)
00155     {
00156       if(j%10 == 0)
00157          printf("\n");
00158       printf(" %d",q_ptr->q[i]);
00159       i = (i+1) % q_ptr->max;      
00160     }    
00161   }

Generated on Thu Jul 27 23:52:27 2006 for Dynacube by  doxygen 1.4.7