view PrivateQueue.c @ 34:c5d2f2a94133

updated to fixed versions from MC_shared brch, then removed VMS_ from malloc
author Some Random Person <seanhalle@yahoo.com>
date Wed, 14 Mar 2012 23:02:28 -0700
parents 1ed562d601d9
children d6da470bbd38
line source
1 /*
2 * Copyright 2009 OpenSourceStewardshipFoundation.org
3 * Licensed under GNU General Public License version 2
4 *
5 * NOTE: this version of SRSW correct as of April 25, 2010
6 *
7 * Author: seanhalle@yahoo.com
8 */
11 #include <stdio.h>
12 #include <string.h>
13 #include <errno.h>
14 #include <stdlib.h>
16 #include "PrivateQueue.h"
20 //===========================================================================
22 /*This kind of queue is private to a single core at a time -- has no
23 * synchronizations
24 */
26 PrivQueueStruc* makePrivQ()
27 {
28 PrivQueueStruc* retQ;
29 //This malloc is not safe to use inside VMS-language!
30 retQ = (PrivQueueStruc *) malloc( sizeof( PrivQueueStruc ) );
32 //This malloc is not safe to use inside VMS-language!
33 retQ->startOfData = malloc( 1024 * sizeof(void *) );
34 memset( retQ->startOfData, 0, 1024 * sizeof(void *) );
35 retQ->extractPos = &(retQ->startOfData[0]); //side by side == empty
36 retQ->insertPos = &(retQ->startOfData[1]); // so start pos's have to be
37 retQ->endOfData = &(retQ->startOfData[1023]);
39 return retQ;
40 }
43 void
44 enlargePrivQ( PrivQueueStruc *Q )
45 { int32 oldSize, newSize;
46 int8 *insertPos, *extractPos;
47 int8 *oldStartOfData, *oldEndOfData, *newStartOfData, *newEndOfData;
48 int8 *insertOffsetBytes, *extractOffsetBytes;
50 oldStartOfData = (int8 *)Q->startOfData;
51 oldEndOfData = (int8 *)Q->endOfData;
52 insertPos = (int8 *)Q->insertPos;
53 extractPos = (int8 *)Q->extractPos;
55 //TODO: verify these get number of bytes correct
56 insertOffsetBytes = insertPos - oldStartOfData;
57 extractOffsetBytes = extractPos - oldStartOfData);
59 oldSize = endOfData - startOfData + 1; //in bytes
60 newSize = 2 * oldSize;
62 //This malloc is not safe to use inside VMS-language!
63 Q->startOfData = (void **)malloc( newSize );
64 newStartOfData = (int8 *)Q->startOfData;
65 newEndOfData = newStartOfData + newSize; //all calcs in Bytes
66 Q->endOfData = (void **)newEndOfData;
68 //TODO: test all of this, for both cases
70 //Moving the data and pointers to the new array is
71 //a little trickier than at first it seems.. the top part
72 // of old queue must be moved to the top part of new queue, while
73 // bottom part of old to bottom part of new, then the new insert
74 // and extract positions calculated by offset from top and bottom
75 //UNLESS the one case where old extract was at bottom and insert
76 // was at top.
77 //TODO: check that this is correct!
78 if( extractPos == startOfData && insertPos == endOfData )
79 {
80 memcpy( newStartOfData, oldStartOfData, oldSize ); //oldSize is bytes
81 Q->extractPos = Q->startOfData; //start of valid data
82 Q->insertPos = Q->startOfData + oldSize - 1; //end of valid data
83 }
84 else //have to copy two parts separately, then calc positions
85 { //TODO: check end-addr, sizes, and new positions carefully
87 //copy top part, starting at extract up until end of data,
88 // into top of new array
89 topPartSize = oldEndOfData - extractPos + 1; //+1 includes extractPos
90 copyStartAddr = newEndOfData - topPartSize + 1;//+1 cancels other
91 memcpy( copyStartAddr, Q->extractPos, topPartSize );
92 Q->extractPos = (void **)copyStartAddr; //extract just-copied data
94 //copy bottom part, from old start up to old insert,
95 // into bottom of new array
96 bottPartSize = oldSize - topPartSize - 1; //-1 for empty insertPos
97 memcpy( newStartOfData, oldStartOfData, bottPartSize );
98 Q->insertPos = (void **)(newStartOfData + bottPartSize);
99 }
100 //This free is not safe to use inside VMS-language!
101 free(oldStartOfData);
102 }
105 /*Returns NULL when queue is empty
106 */
107 void* readPrivQ( PrivQueueStruc* Q )
108 { void *out = 0;
109 void **startOfData = Q->startOfData;
110 void **endOfData = Q->endOfData;
112 void **insertPos = Q->insertPos;
113 void **extractPos = Q->extractPos;
115 //if not empty -- (extract is just below insert when empty)
116 if( insertPos - extractPos != 1 &&
117 !(extractPos == endOfData && insertPos == startOfData))
118 { //move before read
119 if( extractPos == endOfData ) //write new pos exactly once, correctly
120 { Q->extractPos = startOfData; //can't overrun then fix it 'cause
121 } // other thread might read bad pos
122 else
123 { Q->extractPos++;
124 }
125 out = *(Q->extractPos);
126 return out;
127 }
128 //Q is empty
129 return NULL;
130 }
133 /*Expands the queue size automatically when it's full
134 */
135 void
136 writePrivQ( void * in, PrivQueueStruc* Q )
137 {
138 void **startOfData = Q->startOfData;
139 void **endOfData = Q->endOfData;
141 void **insertPos = Q->insertPos;
142 void **extractPos = Q->extractPos;
144 tryAgain:
145 //Full? (insert is just below extract when full)
146 if( extractPos - insertPos != 1 &&
147 !(insertPos == endOfData && extractPos == startOfData))
148 { *(Q->insertPos) = in; //insert before move
149 if( insertPos == endOfData ) //write new pos exactly once, correctly
150 { Q->insertPos = startOfData;
151 }
152 else
153 { Q->insertPos++;
154 }
155 return;
156 }
157 //Q is full
158 enlargePrivQ( Q );
159 goto tryAgain;
160 }
163 /*Returns false when the queue was full.
164 * have option of calling make_larger_PrivQ to make more room, then try again
165 */
166 int writeIfSpacePrivQ( void * in, PrivQueueStruc* Q )
167 {
168 void **startOfData = Q->startOfData;
169 void **endOfData = Q->endOfData;
171 void **insertPos = Q->insertPos;
172 void **extractPos = Q->extractPos;
174 if( extractPos - insertPos != 1 &&
175 !(insertPos == endOfData && extractPos == startOfData))
176 { *(Q->insertPos) = in; //insert before move
177 if( insertPos == endOfData ) //write new pos exactly once, correctly
178 { Q->insertPos = startOfData;
179 }
180 else
181 { Q->insertPos++;
182 }
183 return TRUE;
184 }
185 //Q is full
186 return FALSE;
187 }
189 int32
190 numInPrivQ( PrivQueueStruc *Q )
191 { int32 size, numIn;
193 if( Q->insertPos < Q->extractPos )
194 { //insert has wrapped around so numIn is:
195 // insertPos + size - extractPos -- Consider, is empty when
196 // extractPos = endOfData and insert = start -- correctly get zero
197 size = Q->endOfData - Q->startOfData + 1; //sz of 10 is 0..9
198 numIn = Q->insertPos - Q->extractPos + size - 1; //-1 bec insrt empty
199 }
200 else
201 {
202 numIn = Q->insertPos - Q->extractPos -1;//-1 bec insertPos empty
203 }
204 return numIn;
205 }
208 /*Treats queue as a stack -- no matter contents, if read done right after
209 * a push, then the pushed item is what comes out.
210 * Expands the queue size automatically when it's full.
211 */
212 void
213 pushPrivQ( void * in, PrivQueueStruc* Q )
214 {
215 while(1){
216 void **startOfData = Q->startOfData;
217 void **endOfData = Q->endOfData;
219 void **insertPos = Q->insertPos;
220 void **extractPos = Q->extractPos;
222 //Full? (insert is just below extract when full)
223 if( extractPos - insertPos != 1 &&
224 !(insertPos == endOfData && extractPos == startOfData))
225 { //insert -- but go backwards, inserting at read position then
226 // move read pos backwards
227 *(Q->extractPos) = in;
228 if( extractPos == startOfData ) //write new pos exactly once, correctly
229 { Q->extractPos = endOfData; //can't overrun then fix it 'cause
230 } // other thread might read bad pos
231 else
232 { Q->extractPos--;
233 }
234 return;
235 }
236 //Q is full
237 enlargePrivQ( Q );
238 }
239 }
242 void
243 freePrivQ( PrivQueueStruc *Q )
244 {
245 //This free is not safe to use inside VMS-language!
246 free( Q->startOfData );
247 free( Q );