comparison PrivateQueue.c @ 36:d6da470bbd38

fixed bug in pure c queue -- enlarges correctly now
author Some Random Person <seanhalle@yahoo.com>
date Thu, 24 May 2012 07:53:13 -0700
parents c5d2f2a94133
children
comparison
equal deleted inserted replaced
20:11e7c3bb6780 22:febb0f7fde7c
24 */ 24 */
25 25
26 PrivQueueStruc* makePrivQ() 26 PrivQueueStruc* makePrivQ()
27 { 27 {
28 PrivQueueStruc* retQ; 28 PrivQueueStruc* retQ;
29 //This malloc is not safe to use inside VMS-language! 29 //This malloc is not safe to use in wrapper lib nor app code!
30 retQ = (PrivQueueStruc *) malloc( sizeof( PrivQueueStruc ) ); 30 retQ = (PrivQueueStruc *) VMS_int__malloc( sizeof( PrivQueueStruc ) );
31 31
32 //This malloc is not safe to use inside VMS-language! 32 //This malloc is not safe to use in wrapper lib nor app code!
33 retQ->startOfData = malloc( 1024 * sizeof(void *) ); 33 retQ->startOfData = VMS_WL__malloc( 1024 * sizeof(void *) );
34 memset( retQ->startOfData, 0, 1024 * sizeof(void *) ); 34 memset( retQ->startOfData, 0, 1024 * sizeof(void *) );
35 retQ->extractPos = &(retQ->startOfData[0]); //side by side == empty 35 retQ->extractPos = &(retQ->startOfData[0]); //side by side == empty
36 retQ->insertPos = &(retQ->startOfData[1]); // so start pos's have to be 36 retQ->insertPos = &(retQ->startOfData[1]); // so start pos's have to be
37 retQ->endOfData = &(retQ->startOfData[1023]); 37 retQ->endOfData = &(retQ->startOfData[1023]);
38 38
40 } 40 }
41 41
42 42
43 void 43 void
44 enlargePrivQ( PrivQueueStruc *Q ) 44 enlargePrivQ( PrivQueueStruc *Q )
45 { int32 oldSize, newSize; 45 { int32 oldSize, newSize, topPartSize, bottPartSize;
46 int8 *insertPos, *extractPos; 46 int8 *insertPos, *extractPos;
47 int8 *oldStartOfData, *oldEndOfData, *newStartOfData, *newEndOfData; 47 int8 *oldStartOfData, *oldEndOfData, *newStartOfData, *newEndOfData;
48 int8 *insertOffsetBytes, *extractOffsetBytes; 48 int32 insertOffsetBytes, extractOffsetBytes;
49 int8 *copyStartAddr;
49 50
50 oldStartOfData = (int8 *)Q->startOfData; 51 oldStartOfData = (int8 *)Q->startOfData;
51 oldEndOfData = (int8 *)Q->endOfData; 52 oldEndOfData = (int8 *)Q->endOfData;
52 insertPos = (int8 *)Q->insertPos; 53 insertPos = (int8 *)Q->insertPos;
53 extractPos = (int8 *)Q->extractPos; 54 extractPos = (int8 *)Q->extractPos;
54 55
55 //TODO: verify these get number of bytes correct 56 //TODO: verify these get number of bytes correct
56 insertOffsetBytes = insertPos - oldStartOfData; 57 insertOffsetBytes = (int32)(insertPos - oldStartOfData);
57 extractOffsetBytes = extractPos - oldStartOfData); 58 extractOffsetBytes = (int32)(extractPos - oldStartOfData);
58 59
59 oldSize = endOfData - startOfData + 1; //in bytes 60 oldSize = oldEndOfData - oldStartOfData + 1; //in bytes
60 newSize = 2 * oldSize; 61 newSize = 2 * oldSize;
61 62
62 //This malloc is not safe to use inside VMS-language! 63 //This malloc is not safe to use in wrapper lib nor app code!
63 Q->startOfData = (void **)malloc( newSize ); 64 Q->startOfData = (void **)VMS_int__malloc( newSize );
64 newStartOfData = (int8 *)Q->startOfData; 65 newStartOfData = (int8 *)Q->startOfData;
65 newEndOfData = newStartOfData + newSize; //all calcs in Bytes 66 newEndOfData = newStartOfData + newSize; //all calcs in Bytes
66 Q->endOfData = (void **)newEndOfData; 67 Q->endOfData = (void **)newEndOfData;
67 68
68 //TODO: test all of this, for both cases 69 //TODO: test all of this, for both cases
73 // bottom part of old to bottom part of new, then the new insert 74 // 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 // and extract positions calculated by offset from top and bottom
75 //UNLESS the one case where old extract was at bottom and insert 76 //UNLESS the one case where old extract was at bottom and insert
76 // was at top. 77 // was at top.
77 //TODO: check that this is correct! 78 //TODO: check that this is correct!
78 if( extractPos == startOfData && insertPos == endOfData ) 79 if( extractPos == oldStartOfData && insertPos == oldEndOfData )
79 { 80 {
80 memcpy( newStartOfData, oldStartOfData, oldSize ); //oldSize is bytes 81 memcpy( newStartOfData, oldStartOfData, oldSize ); //oldSize is bytes
81 Q->extractPos = Q->startOfData; //start of valid data 82 Q->extractPos = Q->startOfData; //start of valid data
82 Q->insertPos = Q->startOfData + oldSize - 1; //end of valid data 83 Q->insertPos = Q->startOfData + oldSize - 1; //end of valid data
83 } 84 }
84 else //have to copy two parts separately, then calc positions 85 else //have to copy two parts separately, then calc positions
85 { //TODO: check end-addr, sizes, and new positions carefully 86 { //TODO: check end-addr, sizes, and new positions carefully
86 87
87 //copy top part, starting at extract up until end of data, 88 //copy top part, starting at extract up until end of data,
88 // into top of new array 89 // into top of new array
89 topPartSize = oldEndOfData - extractPos + 1; //+1 includes extractPos 90 topPartSize = oldEndOfData - extractPos + 1; //+1 includes extractPos
90 copyStartAddr = newEndOfData - topPartSize + 1;//+1 cancels other 91 copyStartAddr = newEndOfData - topPartSize + 1;//+1 cancels other
91 memcpy( copyStartAddr, Q->extractPos, topPartSize ); 92 memcpy( copyStartAddr, Q->extractPos, topPartSize );
92 Q->extractPos = (void **)copyStartAddr; //extract just-copied data 93 Q->extractPos = (void **)copyStartAddr; //extract just-copied data
93 94
94 //copy bottom part, from old start up to old insert, 95 //copy bottom part, from old start up to old insert,
95 // into bottom of new array 96 // into bottom of new array
96 bottPartSize = oldSize - topPartSize - 1; //-1 for empty insertPos 97 bottPartSize = oldSize - topPartSize - 1; //-1 for empty insertPos
97 memcpy( newStartOfData, oldStartOfData, bottPartSize ); 98 memcpy( newStartOfData, oldStartOfData, bottPartSize );
98 Q->insertPos = (void **)(newStartOfData + bottPartSize); 99 Q->insertPos = (void **)(newStartOfData + bottPartSize);
99 } 100 }
100 //This free is not safe to use inside VMS-language! 101 //This free is not safe to use in wrapper lib nor app code!
101 free(oldStartOfData); 102 VMS_int__free(oldStartOfData);
102 } 103 }
103 104
104 105
105 /*Returns NULL when queue is empty 106 /*Returns NULL when queue is empty
106 */ 107 */
161 162
162 163
163 /*Returns false when the queue was full. 164 /*Returns false when the queue was full.
164 * have option of calling make_larger_PrivQ to make more room, then try again 165 * have option of calling make_larger_PrivQ to make more room, then try again
165 */ 166 */
166 int writeIfSpacePrivQ( void * in, PrivQueueStruc* Q ) 167 bool32
168 writeIfSpacePrivQ( void * in, PrivQueueStruc* Q )
167 { 169 {
168 void **startOfData = Q->startOfData; 170 void **startOfData = Q->startOfData;
169 void **endOfData = Q->endOfData; 171 void **endOfData = Q->endOfData;
170 172
171 void **insertPos = Q->insertPos; 173 void **insertPos = Q->insertPos;
240 242
241 243
242 void 244 void
243 freePrivQ( PrivQueueStruc *Q ) 245 freePrivQ( PrivQueueStruc *Q )
244 { 246 {
245 //This free is not safe to use inside VMS-language! 247 //This free is not safe to use in wrapper lib nor app code!
246 free( Q->startOfData ); 248 VMS_int__free( Q->startOfData );
247 free( Q ); 249 VMS_int__free( Q );
248 } 250 }