QuakeGod
2024-08-08 1fb934cda3067a81f82a7add0fa5b39f5ebf3094
提交 | 用户 | age
483170 1 /**
Q 2   ******************************************************************************
3   * @file           : MyQueue.c
4   * @brief          : MyQueue program body
5   ******************************************************************************
6     */
7 #include "../inc/MyQueue.h"
8
9 #include "string.h"
10     int initQueue(stMyQueue * theQueue, void * pBuf, int nSize)
11     {
12         theQueue->buf1=pBuf;
13         theQueue->Caps=nSize;
14         theQueue->wp=0;
15         theQueue->rp=0;
842bb6 16 //        theQueue->RecvBytes=0;
Q 17         //theQueue->state=0;        
483170 18         theQueue->bFull=0;
Q 19         theQueue->bEmpty=1;
20             return 0;
21     }
22     int EmptyQueue(stMyQueue * theQueue)
23     {
24         theQueue->wp=0;
25         theQueue->rp=0;
842bb6 26         //theQueue->state=0;        
483170 27         theQueue->bFull=0;
Q 28         theQueue->bEmpty=1;
29         return 0;
30     }
31     int GetContinueEmptyRoom(stMyQueue * theQueue)
32     {
33         int rp=theQueue->rp;
34         int wp=theQueue->wp;
35         if (wp < rp)
36         {
37             return rp - wp;
38         }else if (wp > rp)
39         {
40             return theQueue->Caps - wp;
41         }else if (IsEmpty(theQueue))
42         {
43             return theQueue->Caps - wp;
44         }else if (IsFull(theQueue))
45         {
46             return 0;
47         }
48         return 0;
49     }
50     int GetContinueData(stMyQueue * theQueue)
51     {
52             int rp=theQueue->rp;
53           int wp=theQueue->wp;    
54         if (rp < wp)
55         {
56             return wp - rp;
57         }else if (rp > wp)
58         {
59             return theQueue->Caps - rp;
60         }else if (IsFull(theQueue))
61         {
62             return theQueue->Caps - rp;
63         }else if (IsEmpty(theQueue))
64         {
65             return 0;
66         }
67         return 0;
68     }
69     
70     int AddSpace(stMyQueue * theQueue, int nSize)
71     {
72         if (nSize==0) return nSize;
73         int wp=theQueue->wp+=nSize;
74         if (wp>=theQueue->Caps) wp-=theQueue->Caps;
75         theQueue->bEmpty=0;
76         if (wp == theQueue->rp) theQueue->bFull=1;
77         theQueue->wp = wp;
842bb6 78 //        theQueue->RecvBytes+=nSize;
483170 79         return nSize;
Q 80     }
81     
82     int DelData(stMyQueue * theQueue, int nSize)
83     {
84         if (nSize==0) return nSize;
85         int rp=theQueue->rp+nSize;
86         if (rp>=theQueue->Caps) rp-=theQueue->Caps;
87         theQueue->bFull=0;
88         theQueue->rp = rp;
89         if (rp == theQueue->wp)
90         {            
91             theQueue->wp=0;
92             theQueue->rp=0;
842bb6 93 //            theQueue->state=0;                    
Q 94             theQueue->bEmpty=1;
483170 95         }
Q 96         return nSize;
97     }
98     
99     int CopyData(stMyQueue * theQueue, void * pData, int nSize)
100     {
101         int len1=GetContinueData(theQueue);
102             if (nSize <= len1) 
103             {
104                 memcpy(pData,theQueue->buf1+theQueue->rp,nSize);
105                 return nSize;
106             }
107             else
108             {
109                 memcpy(pData,theQueue->buf1+theQueue->rp,len1);
110                 memcpy((char *)pData+len1,theQueue->buf1,nSize-len1);
111                 return nSize;
112             }    
113         
114     }
115     
116     
117     int PushOne(stMyQueue * theQueue, unsigned char ch)
118     {
119         theQueue->bEmpty=0;    
120         if (!IsFull(theQueue))
121         {
122             theQueue->buf1[theQueue->wp]=ch;
123             AddSpace(theQueue,1);
124             return 0;
125         }
126         return -1;
127     }
128     int PopOne(stMyQueue * theQueue)
129     {
130         theQueue->bFull=0;    
131         unsigned char ch=theQueue->buf1[theQueue->rp];
132         if (!IsEmpty(theQueue)) 
133         {
134             DelData(theQueue,1);
135         }
136         return ch;
137     }
138
139     int PushIn(stMyQueue * theQueue, void * pData, int nSize)
140     {
141         int len1=GetContinueEmptyRoom(theQueue);
142         if (nSize <= len1) 
143         {
144             memcpy(GetWriteBuffer(theQueue),pData,nSize);
145             AddSpace(theQueue, nSize);
146             return nSize;
147         }
148
149             memcpy(GetWriteBuffer(theQueue),pData,len1);
150             AddSpace(theQueue,len1);
151             int len2=GetContinueEmptyRoom(theQueue);
152             if (nSize-len1 <= len2) 
153             {
154                 memcpy(GetWriteBuffer(theQueue),(char *)pData+len1,nSize-len1);
155                 AddSpace(theQueue, nSize-len1);
156                 return nSize;
157             }
158             if (len2>0)
159             {
160                 memcpy(GetWriteBuffer(theQueue),(char *)pData+len1,len2);
161                 AddSpace(theQueue, len2);    
162                 len1+=len2;
163             }
164             return len1;
165     }
166     
167     int PopOut(stMyQueue * theQueue, void * pData, int nSize)
168     {
169 //        theQueue->rp+=nSize;
170 //        if (theQueue->rp >= theQueue ->Caps) theQueue->rp-=theQueue->Caps;
171 //        theQueue->DataSize-=nSize;
172         CopyData(theQueue,pData,nSize);
173         DelData(theQueue,nSize);
174         return nSize;
175     }
176