src/msphash.c

filter: Statementstmt 61.9% 91 / 147 Branchbran 40.3% 25 / 62 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 55.5% 116 / 209 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/msputils.c ↑ Summary src/ppersist.c »
1 /****************************************************************************/
2 /* */
3 /* Program name: MQIsdp protocol C Language implementation */
4 /* */
5 /* Description: This source file contains functions for managing hash */
6 /* tables and linked lists. */
7 /* */
8 /* Statement: Licensed Materials - Property of IBM */
9 /* */
10 /* MQSeries SupportPac IA93 */
11 /* (C) Copyright IBM Corp. 2002 */
12 /* */
13 /****************************************************************************/
14 /* Version @(#) IA93/ship/msphash.c, SupportPacs, S000 1.3 03/11/28 16:43:45 */
15 /* */
16 /* Function: */
17 /* */
18 /* Both queues for sending and receiving data to/from the broker are */
19 /* implemented as linked lists and are hashed. */
20 /* */
21 /****************************************************************************/
22 /* */
23 /* Change history: */
24 /* */
25 /* V1.0 19-02-2003 IRH Initial release */
26 /* */
27 /*==========================================================================*/
28 /* Module Name: msphash.c */
29 #include <mspdmn.h>
30
31 int mspCalcHashCode( MHASHT* pHash, short msgId );
32
33 /* Create a hash table with the specified number of keys */
34 14 MHASHT *mspInitHash( HCONNCB *pHconn, int nKeys ) {
35 14 MHASHT *pHash = NULL;
36 14 int hashSz = 0;
37
38 14 ▶ if ( nKeys > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( nKeys > 0 ) { 14 0 14
39 14 hashSz = sizeof(MHASHT) + ((nKeys - 1 )*sizeof(MHASHENTRY*));
40
41 14 pHash = (MHASHT*)mspMalloc( &(pHconn->comParms), hashSz );
42 14 ▶ if ( pHash != NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( pHash != NULL ) { 14 0 14
43 14 memset( pHash, 0, hashSz );
44 14 pHash->nKeys = nKeys;
45 }
46 }
47
48 14 return pHash;
49 }
50
51 /* Delete the identified hash table */
52 8 void mspTermHash( HCONNCB *pHconn, MHASHT *pHash ) {
53 MHASHENTRY *pCurEntry, *pNextEntry;
54 int k;
55 8 int hashSz = 0;
56
57 136 ▶ for ( k=0; k < pHash->nKeys; k++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( k=0; k < pHash->nKeys; k++ ) { 128 8 136
58 128 pCurEntry = pHash->pKeys[k];
59 128 ▶ while ( pCurEntry != NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( pCurEntry != NULL ) { 0 128 128
60 0 pNextEntry = pCurEntry->Next;
61 0 mspFree( &(pHconn->comParms), pCurEntry, sizeof(MHASHENTRY) );
62 0 pCurEntry = pNextEntry;
63 }
64 }
65
66 8 hashSz = sizeof(MHASHT) + ((pHash->nKeys - 1 )*sizeof(MHASHENTRY*));
67 8 mspFree( &(pHconn->comParms), pHash, hashSz );
68 8 }
69
70 /* Add an entry to a hash table, where msgId is the hash key */
71 7 int mspAddToHash( HCONNCB *pHconn, MHASHT* pHash, short msgId, void *dataPtr ) {
72 int hCode;
73 MHASHENTRY *pCurEntry;
74
75 /* Avoid duplicate entries */
76 7 mspDelFromHash( pHconn, pHash, msgId );
77
78 /* Calculate the hash code */
79 7 hCode = mspCalcHashCode( pHash, msgId );
80
81 7 pCurEntry = (MHASHENTRY*)mspMalloc( &(pHconn->comParms), sizeof(MHASHENTRY) );
82 7 ▶ if ( pCurEntry == NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( pCurEntry == NULL ) { 0 7 7
83 0 return 1;
84 }
85
86 /* Add the current entry to the beginning of the list */
87 /* Set next of the new entry to be current beginning of the list */
88 /* Set the prev of current beginning of the list to be the new entry */
89 7 ▶ if ( pHash->pKeys[hCode] != NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( pHash->pKeys[hCode] != NULL ) { 0 7 7
90 0 pCurEntry->Next = pHash->pKeys[hCode];
91 0 pHash->pKeys[hCode]->Prev = pCurEntry;
92 } else {
93 7 pCurEntry->Next = NULL;
94 }
95
96 /* Set the new entry to be the beginning of the list */
97 7 pHash->pKeys[hCode] = pCurEntry;
98
99 7 pCurEntry->msgId = msgId;
100 7 pCurEntry->dataPtr = dataPtr;
101 7 pCurEntry->Prev = NULL;
102
103 7 return 0;
104 }
105
106 /* Read an entry from a hash table, as identified by hash key msgId */
107 7 void* mspReadFromHash( MHASHT* pHash, short msgId ) {
108 MHASHENTRY* pCurEntry;
109 int hCode;
110
111 7 hCode = mspCalcHashCode( pHash, msgId );
112
113 7 ▶ for( pCurEntry = pHash->pKeys[hCode]; pCurEntry != NULL; pCurEntry = pCurEntry->Next )
Branch: 1 branch
BranchTrueFalseTotal
for( pCurEntry = pHash->pKeys[hCode]; pCurEntry != NULL; pCurEntry = pCurEntry->Next ) 0 7 7
114 {
115 0 ▶ if( pCurEntry->msgId == msgId ) {
Branch: 1 branch
BranchTrueFalseTotal
if( pCurEntry->msgId == msgId ) { 0 0 0
116 0 return( pCurEntry->dataPtr );
117 }
118 }
119
120 7 return(NULL);
121 }
122
123 #if 0
124 /* This is a useful debug function if the contents of a hash table need to be dumped */
125 int mspDumpHash( MHASHT* pHash ) {
126 MHASHENTRY* pCurEntry;
127 int hCode;
128
129
130 for ( hCode = 0; hCode < pHash->nKeys; hCode++ ) {
131 for( pCurEntry = pHash->pKeys[hCode]; pCurEntry != NULL; pCurEntry = pCurEntry->Next )
132 {
133 printf( "MSGID :%d\n", pCurEntry->msgId );
134 printf( "DATAPTR:%p\n", pCurEntry->dataPtr );
135 }
136 }
137
138 return 0;
139 }
140 #endif
141
142 /* Get a complete entry from a hash table, as identified by hash key msgId */
143 0 MHASHENTRY* mspGetHashEntry( MHASHT* pHash, short msgId ) {
144 MHASHENTRY* pCurEntry;
145 int hCode;
146
147 0 hCode = mspCalcHashCode( pHash, msgId );
148
149 0 ▶ for( pCurEntry = pHash->pKeys[hCode]; pCurEntry != NULL; pCurEntry = pCurEntry->Next )
Branch: 1 branch
BranchTrueFalseTotal
for( pCurEntry = pHash->pKeys[hCode]; pCurEntry != NULL; pCurEntry = pCurEntry->Next ) 0 0 0
150 {
151 0 ▶ if( pCurEntry->msgId == msgId ) {
Branch: 1 branch
BranchTrueFalseTotal
if( pCurEntry->msgId == msgId ) { 0 0 0
152 0 return( pCurEntry );
153 }
154 }
155
156 0 return(NULL);
157 }
158
159 /* Delete an entry from a hash table, as identified by hash key msgId */
160 9 void mspDelFromHash( HCONNCB *pHconn, MHASHT* pHash, short msgId ) {
161 MHASHENTRY *pCurEntry, *next, *prev;
162 int hCode;
163
164 9 hCode = mspCalcHashCode( pHash, msgId );
165
166 9 ▶ for( pCurEntry = pHash->pKeys[hCode]; pCurEntry != NULL; pCurEntry = pCurEntry->Next )
Branch: 1 branch
BranchTrueFalseTotal
for( pCurEntry = pHash->pKeys[hCode]; pCurEntry != NULL; pCurEntry = pCurEntry->Next ) 2 7 9
167 {
168 2 ▶ if( pCurEntry->msgId == msgId ) {
Branch: 1 branch
BranchTrueFalseTotal
if( pCurEntry->msgId == msgId ) { 2 0 2
169
170 2 prev = pCurEntry->Prev;
171 2 next = pCurEntry->Next;
172
173 2 ▶ if ( prev != NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( prev != NULL ) { 0 2 2
174 0 prev->Next = next;
175 } else {
176 /* Deleting the first in the chain */
177 2 pHash->pKeys[hCode] = next;
178 }
179
180 2 ▶ if ( next != NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( next != NULL ) { 0 2 2
181 0 next->Prev = prev;
182 }
183
184 2 mspFree( &(pHconn->comParms), pCurEntry, sizeof(MHASHENTRY) );
185 2 return;
186 }
187 }
188 }
189
190 /* Calculate the hash value from the key */
191 23 int mspCalcHashCode( MHASHT* pHash, short msgId ) {
192 int hc, j;
193 char* pcMsgId;
194
195 23 pcMsgId = (char*)&msgId;
196
197 /* For each byte of memory in data to be hashed */
198 69 ▶ for( hc=0, j=0; j < sizeof(short); pcMsgId++, j++ )
Branch: 1 branch
BranchTrueFalseTotal
for( hc=0, j=0; j < sizeof(short); pcMsgId++, j++ ) 46 23 69
199 {
200 /* The hash code is the current value of the hash code multiplied */
201 /* by 131 plus the current value of the byte we are looking at */
202 46 hc = 131*hc + *pcMsgId;
203 }
204
205 /* hc is between 0 and the number of hashkeys - 1 */
206 23 hc = abs(hc)%pHash->nKeys;
207 23 return( hc );
208 }
209
210 /* Functions for Adding and Deleting an 'in progress message' to/from the InProgressQ linked list */
211 7 IPQ* mspAddIPMToList( HCONNCB *pHconn, long dataLen, void *msgData, short msgId ){
212 7 IPQ *ipqEntry = NULL;
213
214 7 ipqEntry = (IPQ*)mspMalloc( &(pHconn->comParms), sizeof(IPQ) );
215 7 ▶ if ( ipqEntry != NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( ipqEntry != NULL ) { 7 0 7
216 7 ipqEntry->msgData = msgData;
217 7 ipqEntry->msgLength = dataLen;
218 7 ipqEntry->msgId = msgId;
219 7 ipqEntry->flags = 0;
220 7 ipqEntry->msgStatus = MQISDP_IN_PROGRESS;
221 7 ipqEntry->Next = NULL;
222
223 7 switch ( (*(char*)msgData) & MSP_FH_GET_QOS ) {
224 0 case 0:
225 0 ipqEntry->flags |= MSP_IPQ_QOS_0 ;
226 0 break;
227 0 case 1:
228 0 ipqEntry->flags |= MSP_IPQ_QOS_1 ;
229 0 break;
230 1 case 2:
231 1 ipqEntry->flags |= MSP_IPQ_QOS_2 ;
232 1 break;
233 }
234
235 /* Add entries to the end of the list so that they are in the order sent */
236 /* incase any retries are required. */
237 7 ▶ if ( pHconn->outQ.inProgressQ == NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( pHconn->outQ.inProgressQ == NULL ) { 3 4 7
238 3 pHconn->outQ.inProgressQ = ipqEntry;
239 3 ipqEntry->Prev = NULL;
240 } else {
241 4 ipqEntry->Prev = pHconn->outQ.pLastEntry;
242 4 pHconn->outQ.pLastEntry->Next = ipqEntry;
243 }
244 7 pHconn->outQ.pLastEntry = ipqEntry;
245
246 7 pHconn->outQ.ipEntries++;
247 7 pHconn->outQ.numBytesQueued += sizeof(IPQ) + ipqEntry->msgLength;
248 }
249
250 7 return ipqEntry;
251 }
252
253 2 int mspDeleteIPMFromList( HCONNCB *pHconn, IPQ* delEntry ){
254 IPQ *prev, *next;
255
256 2 ▶ if ( delEntry != NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( delEntry != NULL ) { 2 0 2
257 2 prev = delEntry->Prev;
258 2 next = delEntry->Next;
259
260 2 ▶ if ( prev != NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( prev != NULL ) { 0 2 2
261 0 prev->Next = next;
262 } else {
263 /* Deleting the first in the chain */
264 2 pHconn->outQ.inProgressQ = next;
265 }
266
267 2 ▶ if ( next != NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( next != NULL ) { 1 1 2
268 1 next->Prev = prev;
269 } else {
270 /* Deleting the last entry, so move the last entry */
271 /* pointer to the previous entry */
272 1 pHconn->outQ.pLastEntry = prev;
273 }
274
275 2 pHconn->outQ.ipEntries--;
276 2 pHconn->outQ.numBytesQueued -= (delEntry->msgLength + sizeof(IPQ));
277 2 ▶ if ( pHconn->outQ.ipEntries == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( pHconn->outQ.ipEntries == 0 ) { 1 1 2
278 1 pHconn->outQ.pLastEntry = NULL;
279 }
280
281 2 ▶ if ( delEntry->msgData != NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( delEntry->msgData != NULL ) { 2 0 2
282 2 mspFree( &(pHconn->comParms), delEntry->msgData, delEntry->msgLength );
283 }
284 2 mspFree( &(pHconn->comParms), delEntry, sizeof(IPQ) );
285 }
286
287 2 return 0;
288 }
289
290 /* Functions for Adding and Deleting a publication to/from the RcvdPubsQ linked list */
291 0 int mspAddRPMToList( HCONNCB *pHconn, RPQ *newEntry ){
292 0 int rc = 0;
293
294 0 ▶ if ( newEntry != NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( newEntry != NULL ) { 0 0 0
295 0 newEntry->Next = NULL;
296
297 /* Add entries to the end of the list so that they are in the order received */
298 0 ▶ if ( pHconn->inQ.rcvdPubsQ == NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( pHconn->inQ.rcvdPubsQ == NULL ) { 0 0 0
299 0 pHconn->inQ.rcvdPubsQ = newEntry;
300 0 newEntry->Prev = NULL;
301 } else {
302 0 newEntry->Prev = pHconn->inQ.pLastEntry;
303 0 pHconn->inQ.pLastEntry->Next = newEntry;
304 }
305 0 pHconn->inQ.pLastEntry = newEntry;
306
307 0 ▶ if ( newEntry->readyToPublish == MQISDP_RELEASED ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( newEntry->readyToPublish == MQISDP_RELEASED ) { 0 0 0
308 0 ▶ if ( pHconn->inQ.rtpEntries == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( pHconn->inQ.rtpEntries == 0 ) { 0 0 0
309 /* If the publications available to receive changes */
310 /* from 0 to 1 signal the receive semaphore. */
311 0 mspSignalSemaphore( pHconn->ipcCb.receiveSemaphore );
312 }
313 0 pHconn->inQ.rtpEntries++;
314 }
315 0 pHconn->inQ.numBytesQueued += sizeof(RPQ) + newEntry->bufferLength;
316 }
317
318 0 return rc;
319 }
320
321 0 int mspDeleteRPMFromList( HCONNCB *pHconn, RPQ* delEntry ){
322 RPQ *prev, *next;
323
324 0 ▶ if ( delEntry != NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( delEntry != NULL ) { 0 0 0
325 0 prev = delEntry->Prev;
326 0 next = delEntry->Next;
327
328 0 ▶ if ( prev != NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( prev != NULL ) { 0 0 0
329 0 prev->Next = next;
330 } else {
331 /* Deleting the first in the chain */
332 0 pHconn->inQ.rcvdPubsQ = next;
333 }
334
335 0 ▶ if ( next != NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( next != NULL ) { 0 0 0
336 0 next->Prev = prev;
337 } else {
338 /* Deleting the last entry, so move the last entry */
339 /* pointer to the previous entry */
340 0 pHconn->inQ.pLastEntry = prev;
341 }
342
343 0 ▶ if ( delEntry->readyToPublish == MQISDP_RELEASED ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( delEntry->readyToPublish == MQISDP_RELEASED ) { 0 0 0
344 0 pHconn->inQ.rtpEntries--;
345 }
346
347 0 pHconn->inQ.numBytesQueued -= (delEntry->bufferLength + sizeof(RPQ));
348
349 0 ▶ if ( delEntry->buffer != NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( delEntry->buffer != NULL ) { 0 0 0
350 0 mspFree( &(pHconn->comParms), delEntry->buffer, delEntry->bufferLength );
351 }
352 0 mspFree( &(pHconn->comParms), delEntry, sizeof(RPQ) );
353 }
354
355 0 return 0;
356 }
357
358
« src/msputils.c ↑ Summary src/ppersist.c »