00001 #include "queueing.h"
00002 #include <converse.h>
00003 #include <string.h>
00004
00033 #if 1
00034
00035
00036 #define CmiAlloc malloc
00037 #define CmiFree free
00038 #endif
00039
00041 int schedAdaptMemThresholdMB;
00042
00043
00045 static void CqsDeqInit(_deq d)
00046 {
00047 d->bgn = d->space;
00048 d->end = d->space+4;
00049 d->head = d->space;
00050 d->tail = d->space;
00051 }
00052
00054 static void CqsDeqExpand(_deq d)
00055 {
00056 int rsize = (d->end - d->head);
00057 int lsize = (d->head - d->bgn);
00058 int oldsize = (d->end - d->bgn);
00059 int newsize = (oldsize << 1);
00060 void **ovec = d->bgn;
00061 void **nvec = (void **)CmiAlloc(newsize * sizeof(void *));
00062 memcpy(nvec, d->head, rsize * sizeof(void *));
00063 memcpy(nvec+rsize, d->bgn, lsize * sizeof(void *));
00064 d->bgn = nvec;
00065 d->end = nvec + newsize;
00066 d->head = nvec;
00067 d->tail = nvec + oldsize;
00068 if (ovec != d->space) CmiFree(ovec);
00069 }
00070
00072 void CqsDeqEnqueueFifo(_deq d, void *data)
00073 {
00074 void **tail = d->tail;
00075 *tail = data;
00076 tail++;
00077 if (tail == d->end) tail = d->bgn;
00078 d->tail = tail;
00079 if (tail == d->head) CqsDeqExpand(d);
00080 }
00081
00083 void CqsDeqEnqueueLifo(_deq d, void *data)
00084 {
00085 void **head = d->head;
00086 if (head == d->bgn) head = d->end;
00087 head--;
00088 *head = data;
00089 d->head = head;
00090 if (head == d->tail) CqsDeqExpand(d);
00091 }
00092
00094 void *CqsDeqDequeue(_deq d)
00095 {
00096 void **head;
00097 void **tail;
00098 void *data;
00099 head = d->head;
00100 tail = d->tail;
00101 if (head == tail) return 0;
00102 data = *head;
00103 head++;
00104 if (head == d->end) head = d->bgn;
00105 d->head = head;
00106 return data;
00107 }
00108
00110 static void CqsPrioqInit(_prioq pq)
00111 {
00112 int i;
00113 pq->heapsize = 100;
00114 pq->heapnext = 1;
00115 pq->hash_key_size = PRIOQ_TABSIZE;
00116 pq->hash_entry_size = 0;
00117 pq->heap = (_prioqelt *)CmiAlloc(100 * sizeof(_prioqelt));
00118 pq->hashtab = (_prioqelt *)CmiAlloc(pq->hash_key_size * sizeof(_prioqelt));
00119 for (i=0; i<pq->hash_key_size; i++) pq->hashtab[i]=0;
00120 }
00121
00122 #if CMK_C_INLINE
00123 inline
00124 #endif
00125
00126 static void CqsPrioqExpand(_prioq pq)
00127 {
00128 int oldsize = pq->heapsize;
00129 int newsize = oldsize * 2;
00130 _prioqelt *oheap = pq->heap;
00131 _prioqelt *nheap = (_prioqelt *)CmiAlloc(newsize*sizeof(_prioqelt));
00132 memcpy(nheap, oheap, oldsize * sizeof(_prioqelt));
00133 pq->heap = nheap;
00134 pq->heapsize = newsize;
00135 CmiFree(oheap);
00136 }
00137 #ifndef FASTQ
00138
00139 void CqsPrioqRehash(_prioq pq)
00140 {
00141 int oldHsize = pq->hash_key_size;
00142 int newHsize = oldHsize * 2;
00143 unsigned int hashval;
00144 _prioqelt pe, pe1, pe2;
00145 int i,j;
00146
00147 _prioqelt *ohashtab = pq->hashtab;
00148 _prioqelt *nhashtab = (_prioqelt *)CmiAlloc(newHsize*sizeof(_prioqelt));
00149
00150 pq->hash_key_size = newHsize;
00151
00152 for(i=0; i<newHsize; i++)
00153 nhashtab[i] = 0;
00154
00155 for(i=0; i<oldHsize; i++) {
00156 for(pe=ohashtab[i]; pe; ) {
00157 pe2 = pe->ht_next;
00158 hashval = pe->pri.bits;
00159 for (j=0; j<pe->pri.ints; j++) hashval ^= pe->pri.data[j];
00160 hashval = (hashval&0x7FFFFFFF)%newHsize;
00161
00162 pe1=nhashtab[hashval];
00163 pe->ht_next = pe1;
00164 pe->ht_handle = (nhashtab+hashval);
00165 if (pe1) pe1->ht_handle = &(pe->ht_next);
00166 nhashtab[hashval]=pe;
00167 pe = pe2;
00168 }
00169 }
00170 pq->hashtab = nhashtab;
00171 pq->hash_key_size = newHsize;
00172 CmiFree(ohashtab);
00173 }
00174 #endif
00175
00176 int CqsPrioGT_(unsigned int ints1, unsigned int *data1, unsigned int ints2, unsigned int *data2)
00177 {
00178 unsigned int val1;
00179 unsigned int val2;
00180 while (1) {
00181 if (ints1==0) return 0;
00182 if (ints2==0) return 1;
00183 val1 = *data1++;
00184 val2 = *data2++;
00185 if (val1 < val2) return 0;
00186 if (val1 > val2) return 1;
00187 ints1--;
00188 ints2--;
00189 }
00190 }
00191
00198 int CqsPrioGT(_prio prio1, _prio prio2)
00199 {
00200 #ifndef FASTQ
00201 unsigned int ints1 = prio1->ints;
00202 unsigned int ints2 = prio2->ints;
00203 #endif
00204 unsigned int *data1 = prio1->data;
00205 unsigned int *data2 = prio2->data;
00206 #ifndef FASTQ
00207 unsigned int val1;
00208 unsigned int val2;
00209 #endif
00210 while (1) {
00211 #ifndef FASTQ
00212 if (ints1==0) return 0;
00213 if (ints2==0) return 1;
00214 #else
00215 if (prio1->ints==0) return 0;
00216 if (prio2->ints==0) return 1;
00217 #endif
00218 #ifndef FASTQ
00219 val1 = *data1++;
00220 val2 = *data2++;
00221 if (val1 < val2) return 0;
00222 if (val1 > val2) return 1;
00223 ints1--;
00224 ints2--;
00225 #else
00226 if(*data1++ < *data2++) return 0;
00227 if(*data1++ > *data2++) return 1;
00228 (prio1->ints)--;
00229 (prio2->ints)--;
00230 #endif
00231 }
00232 }
00233
00235 _deq CqsPrioqGetDeq(_prioq pq, unsigned int priobits, unsigned int *priodata)
00236 {
00237 unsigned int prioints = (priobits+CINTBITS-1)/CINTBITS;
00238 unsigned int hashval, i;
00239 int heappos;
00240 _prioqelt *heap, pe, next, parent;
00241 _prio pri;
00242 int mem_cmp_res;
00243 unsigned int pri_bits_cmp;
00244 static int cnt_nilesh=0;
00245
00246 #ifdef FASTQ
00247
00248 #endif
00249
00250 hashval = priobits;
00251 for (i=0; i<prioints; i++) hashval ^= priodata[i];
00252 hashval = (hashval&0x7FFFFFFF)%PRIOQ_TABSIZE;
00253 #ifndef FASTQ
00254 for (pe=pq->hashtab[hashval]; pe; pe=pe->ht_next)
00255 if (priobits == pe->pri.bits)
00256 if (memcmp(priodata, pe->pri.data, sizeof(int)*prioints)==0)
00257 return &(pe->data);
00258 #else
00259 parent=NULL;
00260 for(pe=pq->hashtab[hashval]; pe; )
00261 {
00262 parent=pe;
00263 pri_bits_cmp=pe->pri.bits;
00264 mem_cmp_res=memcmp(priodata,pe->pri.data,sizeof(int)*prioints);
00265 if(priobits == pri_bits_cmp && mem_cmp_res==0)
00266 return &(pe->data);
00267 else if(priobits > pri_bits_cmp || (priobits == pri_bits_cmp && mem_cmp_res>0))
00268 {
00269 pe=pe->ht_right;
00270 }
00271 else
00272 {
00273 pe=pe->ht_left;
00274 }
00275 }
00276 #endif
00277
00278
00279 pe = (_prioqelt)CmiAlloc(sizeof(struct prioqelt_struct)+((prioints-1)*sizeof(int)));
00280 pe->pri.bits = priobits;
00281 pe->pri.ints = prioints;
00282 memcpy(pe->pri.data, priodata, (prioints*sizeof(int)));
00283 CqsDeqInit(&(pe->data));
00284 pri=&(pe->pri);
00285
00286
00287 next = pq->hashtab[hashval];
00288 #ifndef FASTQ
00289 pe->ht_next = next;
00290 pe->ht_handle = (pq->hashtab+hashval);
00291 if (next) next->ht_handle = &(pe->ht_next);
00292 pq->hashtab[hashval] = pe;
00293 #else
00294 pe->ht_parent = parent;
00295 pe->ht_left = NULL;
00296 pe->ht_right = NULL;
00297 if(priobits > pri_bits_cmp || (priobits == pri_bits_cmp && mem_cmp_res>0))
00298 {
00299 if(parent) {
00300 parent->ht_right = pe;
00301 pe->ht_handle = &(parent->ht_right);
00302 }
00303 else {
00304 pe->ht_handle = (pq->hashtab+hashval);
00305 pq->hashtab[hashval] = pe;
00306 }
00307
00308 }
00309 else
00310 {
00311 if(parent) {
00312 parent->ht_left = pe;
00313 pe->ht_handle = &(parent->ht_left);
00314 }
00315 else {
00316 pe->ht_handle = (pq->hashtab+hashval);
00317 pq->hashtab[hashval] = pe;
00318 }
00319
00320 }
00321 if(!next)
00322 pq->hashtab[hashval] = pe;
00323 #endif
00324 pq->hash_entry_size++;
00325 #ifndef FASTQ
00326 if(pq->hash_entry_size > 2*pq->hash_key_size)
00327 CqsPrioqRehash(pq);
00328 #endif
00329
00330 heappos = pq->heapnext++;
00331 if (heappos == pq->heapsize) CqsPrioqExpand(pq);
00332 heap = pq->heap;
00333 while (heappos > 1) {
00334 int parentpos = (heappos >> 1);
00335 _prioqelt parent = heap[parentpos];
00336 if (CqsPrioGT(pri, &(parent->pri))) break;
00337 heap[heappos] = parent; heappos=parentpos;
00338 }
00339 heap[heappos] = pe;
00340
00341 #ifdef FASTQ
00342
00343 #endif
00344
00345 return &(pe->data);
00346 }
00347
00349 void *CqsPrioqDequeue(_prioq pq)
00350 {
00351 _prio pri;
00352 _prioqelt pe, old; void *data;
00353 int heappos, heapnext;
00354 _prioqelt *heap = pq->heap;
00355 int left_child;
00356 _prioqelt temp1_ht_right, temp1_ht_left, temp1_ht_parent;
00357 _prioqelt *temp1_ht_handle;
00358 static int cnt_nilesh1=0;
00359
00360 #ifdef FASTQ
00361
00362 #endif
00363 if (pq->heapnext==1) return 0;
00364 pe = heap[1];
00365 data = CqsDeqDequeue(&(pe->data));
00366 if (pe->data.head == pe->data.tail) {
00367
00368 #ifndef FASTQ
00369 _prioqelt next = pe->ht_next;
00370 _prioqelt *handle = pe->ht_handle;
00371 if (next) next->ht_handle = handle;
00372 *handle = next;
00373 old=pe;
00374 #else
00375 old=pe;
00376 prioqelt *handle;
00377 if(pe->ht_parent)
00378 {
00379 if(pe->ht_parent->ht_left==pe) left_child=1;
00380 else left_child=0;
00381 }
00382 else
00383 {
00384 handle = pe->ht_handle;
00385 }
00386
00387 if(!pe->ht_left && !pe->ht_right)
00388 {
00389 if(pe->ht_parent) {
00390 if(left_child) pe->ht_parent->ht_left=NULL;
00391 else pe->ht_parent->ht_right=NULL;
00392 }
00393 else {
00394 *handle = NULL;
00395 }
00396 }
00397 else if(!pe->ht_right)
00398 {
00399
00400 pe->ht_left->ht_parent=pe->ht_parent;
00401 if(pe->ht_parent)
00402 {
00403 if(left_child) {
00404 pe->ht_parent->ht_left = pe->ht_left;
00405 pe->ht_left->ht_handle = &(pe->ht_parent->ht_left);
00406 }
00407 else {
00408 pe->ht_parent->ht_right = pe->ht_left;
00409 pe->ht_left->ht_handle = &(pe->ht_parent->ht_right);
00410 }
00411 }
00412 else {
00413 pe->ht_left->ht_handle = handle;
00414 *handle = pe->ht_left;
00415 }
00416 }
00417 else if(!pe->ht_left)
00418 {
00419
00420 pe->ht_right->ht_parent=pe->ht_parent;
00421
00422 if(pe->ht_parent)
00423 {
00424 if(left_child) {
00425 pe->ht_parent->ht_left = pe->ht_right;
00426 pe->ht_right->ht_handle = &(pe->ht_parent->ht_left);
00427 }
00428 else {
00429 pe->ht_parent->ht_right = pe->ht_right;
00430 pe->ht_right->ht_handle = &(pe->ht_parent->ht_right);
00431 }
00432 }
00433 else {
00434 pe->ht_right->ht_handle = handle;
00435 *handle = pe->ht_right;
00436 }
00437 }
00438 else if(!pe->ht_right->ht_left)
00439 {
00440 pe->ht_right->ht_parent=pe->ht_parent;
00441 if(pe->ht_parent)
00442 {
00443 if(left_child) {
00444 pe->ht_parent->ht_left = pe->ht_right;
00445 pe->ht_right->ht_handle = &(pe->ht_parent->ht_left);
00446 }
00447 else {
00448 pe->ht_parent->ht_right = pe->ht_right;
00449 pe->ht_right->ht_handle = &(pe->ht_parent->ht_right);
00450 }
00451 }
00452 else {
00453 pe->ht_right->ht_handle = handle;
00454 *handle = pe->ht_right;
00455 }
00456 if(pe->ht_left) {
00457 pe->ht_right->ht_left = pe->ht_left;
00458 pe->ht_left->ht_parent = pe->ht_right;
00459 pe->ht_left->ht_handle = &(pe->ht_right->ht_left);
00460 }
00461 }
00462 else
00463 {
00464
00465 for(pe=pe->ht_right; pe; )
00466 {
00467 if(pe->ht_left) pe=pe->ht_left;
00468 else
00469 {
00470 if(old->ht_parent)
00471 {
00472 if(left_child) {
00473 old->ht_parent->ht_left = pe;
00474 pe->ht_handle = &(old->ht_parent->ht_left);
00475 }
00476 else {
00477 old->ht_parent->ht_right = pe;
00478 pe->ht_handle = &(old->ht_parent->ht_right);
00479 }
00480 }
00481 else {
00482 pe->ht_handle = handle;
00483 *handle = pe;
00484 }
00485 temp1_ht_right = pe->ht_right;
00486 temp1_ht_left = pe->ht_left;
00487 temp1_ht_parent = pe->ht_parent;
00488 temp1_ht_handle = pe->ht_handle;
00489
00490 pe->ht_parent = old->ht_parent;
00491 pe->ht_left = old->ht_left;
00492 pe->ht_right = old->ht_right;
00493 if(pe->ht_left) {
00494 pe->ht_left->ht_parent = pe;
00495 pe->ht_right->ht_handle = &(pe->ht_right);
00496 }
00497 if(pe->ht_right) {
00498 pe->ht_right->ht_parent = pe;
00499 pe->ht_right->ht_handle = &(pe->ht_right);
00500 }
00501 temp1_ht_parent->ht_left = temp1_ht_right;
00502 if(temp1_ht_right) {
00503 temp1_ht_right->ht_handle = &(temp1_ht_parent->ht_left);
00504 temp1_ht_right->ht_parent = temp1_ht_parent;
00505 }
00506 break;
00507 }
00508 }
00509 }
00510 #endif
00511 pq->hash_entry_size--;
00512
00513
00514 heapnext = (--pq->heapnext);
00515 pe = heap[heapnext];
00516 pri = &(pe->pri);
00517 heappos = 1;
00518 while (1) {
00519 int childpos1, childpos2, childpos;
00520 _prioqelt ch1, ch2, child;
00521 childpos1 = heappos<<1;
00522 if (childpos1>=heapnext) break;
00523 childpos2 = childpos1+1;
00524 if (childpos2>=heapnext)
00525 { childpos=childpos1; child=heap[childpos1]; }
00526 else {
00527 ch1 = heap[childpos1];
00528 ch2 = heap[childpos2];
00529 if (CqsPrioGT(&(ch1->pri), &(ch2->pri)))
00530 {childpos=childpos2; child=ch2;}
00531 else {childpos=childpos1; child=ch1;}
00532 }
00533 if (CqsPrioGT(&(child->pri), pri)) break;
00534 heap[heappos]=child; heappos=childpos;
00535 }
00536 heap[heappos]=pe;
00537
00538
00539 if (old->data.bgn != old->data.space) CmiFree(old->data.bgn);
00540 CmiFree(old);
00541 }
00542 return data;
00543 }
00544
00545 Queue CqsCreate(void)
00546 {
00547 Queue q = (Queue)CmiAlloc(sizeof(struct Queue_struct));
00548 q->length = 0;
00549 q->maxlen = 0;
00550 #ifdef FASTQ
00551
00552 #endif
00553 CqsDeqInit(&(q->zeroprio));
00554 CqsPrioqInit(&(q->negprioq));
00555 CqsPrioqInit(&(q->posprioq));
00556 return q;
00557 }
00558
00559 void CqsDelete(Queue q)
00560 {
00561 CmiFree(q->negprioq.heap);
00562 CmiFree(q->posprioq.heap);
00563 CmiFree(q);
00564 }
00565
00566 unsigned int CqsLength(Queue q)
00567 {
00568 return q->length;
00569 }
00570
00571 unsigned int CqsMaxLength(Queue q)
00572 {
00573 return q->maxlen;
00574 }
00575
00576 int CqsEmpty(Queue q)
00577 {
00578 return (q->length == 0);
00579 }
00580
00581 void CqsEnqueueGeneral(Queue q, void *data, int strategy,
00582 int priobits,unsigned int *prioptr)
00583 {
00584 _deq d; int iprio;
00585 CmiInt8 lprio0, lprio;
00586 switch (strategy) {
00587 case CQS_QUEUEING_FIFO:
00588 CqsDeqEnqueueFifo(&(q->zeroprio), data);
00589 break;
00590 case CQS_QUEUEING_LIFO:
00591 CqsDeqEnqueueLifo(&(q->zeroprio), data);
00592 break;
00593 case CQS_QUEUEING_IFIFO:
00594 iprio=prioptr[0]+(1U<<(CINTBITS-1));
00595 if ((int)iprio<0)
00596 d=CqsPrioqGetDeq(&(q->posprioq), CINTBITS, &iprio);
00597 else d=CqsPrioqGetDeq(&(q->negprioq), CINTBITS, &iprio);
00598 CqsDeqEnqueueFifo(d, data);
00599 break;
00600 case CQS_QUEUEING_ILIFO:
00601 iprio=prioptr[0]+(1U<<(CINTBITS-1));
00602 if ((int)iprio<0)
00603 d=CqsPrioqGetDeq(&(q->posprioq), CINTBITS, &iprio);
00604 else d=CqsPrioqGetDeq(&(q->negprioq), CINTBITS, &iprio);
00605 CqsDeqEnqueueLifo(d, data);
00606 break;
00607 case CQS_QUEUEING_BFIFO:
00608 if (priobits&&(((int)(prioptr[0]))<0))
00609 d=CqsPrioqGetDeq(&(q->posprioq), priobits, prioptr);
00610 else d=CqsPrioqGetDeq(&(q->negprioq), priobits, prioptr);
00611 CqsDeqEnqueueFifo(d, data);
00612 break;
00613 case CQS_QUEUEING_BLIFO:
00614 if (priobits&&(((int)(prioptr[0]))<0))
00615 d=CqsPrioqGetDeq(&(q->posprioq), priobits, prioptr);
00616 else d=CqsPrioqGetDeq(&(q->negprioq), priobits, prioptr);
00617 CqsDeqEnqueueLifo(d, data);
00618 break;
00619
00620
00621
00622
00623
00624 case CQS_QUEUEING_LFIFO:
00625 CmiAssert(priobits == CLONGBITS);
00626 lprio0 =((CmiInt8 *)prioptr)[0];
00627 lprio0 += (1ULL<<(CLONGBITS-1));
00628 if (CmiEndianness() == 0) {
00629 lprio =(((CmiUInt4 *)&lprio0)[0]*1LL)<<CINTBITS | ((CmiUInt4 *)&lprio0)[1];
00630 }
00631 else {
00632 lprio = lprio0;
00633 }
00634 if (lprio0<0)
00635 d=CqsPrioqGetDeq(&(q->posprioq), priobits, (unsigned int *)&lprio);
00636 else
00637 d=CqsPrioqGetDeq(&(q->negprioq), priobits, (unsigned int *)&lprio);
00638 CqsDeqEnqueueFifo(d, data);
00639 break;
00640 case CQS_QUEUEING_LLIFO:
00641 lprio0 =((CmiInt8 *)prioptr)[0];
00642 lprio0 += (1ULL<<(CLONGBITS-1));
00643 if (CmiEndianness() == 0) {
00644 lprio =(((CmiUInt4 *)&lprio0)[0]*1LL)<<CINTBITS | ((CmiUInt4 *)&lprio0)[1];
00645 }
00646 else {
00647 lprio = lprio0;
00648 }
00649 if (lprio0<0)
00650 d=CqsPrioqGetDeq(&(q->posprioq), priobits, (unsigned int *)&lprio);
00651 else
00652 d=CqsPrioqGetDeq(&(q->negprioq), priobits, (unsigned int *)&lprio);
00653 CqsDeqEnqueueLifo(d, data);
00654 break;
00655 default:
00656 CmiAbort("CqsEnqueueGeneral: invalid queueing strategy.\n");
00657 }
00658 q->length++; if (q->length>q->maxlen) q->maxlen=q->length;
00659 }
00660
00661 void CqsEnqueueFifo(Queue q, void *data)
00662 {
00663 CqsDeqEnqueueFifo(&(q->zeroprio), data);
00664 q->length++; if (q->length>q->maxlen) q->maxlen=q->length;
00665 }
00666
00667 void CqsEnqueueLifo(Queue q, void *data)
00668 {
00669 CqsDeqEnqueueLifo(&(q->zeroprio), data);
00670 q->length++; if (q->length>q->maxlen) q->maxlen=q->length;
00671 }
00672
00673 void CqsEnqueue(Queue q, void *data)
00674 {
00675 CqsDeqEnqueueFifo(&(q->zeroprio), data);
00676 q->length++; if (q->length>q->maxlen) q->maxlen=q->length;
00677 }
00678
00679 void CqsDequeue(Queue q, void **resp)
00680 {
00681 #ifdef ADAPT_SCHED_MEM
00682
00683 if((q->length > 1) && (CmiMemoryUsage() > schedAdaptMemThresholdMB*1024*1024) ){
00684
00685 CqsIncreasePriorityForMemCriticalEntries(q);
00686 }
00687 #endif
00688
00689 if (q->length==0)
00690 { *resp = 0; return; }
00691 if (q->negprioq.heapnext>1)
00692 { *resp = CqsPrioqDequeue(&(q->negprioq)); q->length--; return; }
00693 if (q->zeroprio.head != q->zeroprio.tail)
00694 { *resp = CqsDeqDequeue(&(q->zeroprio)); q->length--; return; }
00695 if (q->posprioq.heapnext>1)
00696 { *resp = CqsPrioqDequeue(&(q->posprioq)); q->length--; return; }
00697 *resp = 0; return;
00698 }
00699
00700 static struct prio_struct kprio_zero = { 0, 0, {0} };
00701 static struct prio_struct kprio_max = { 32, 1, {((unsigned int)(-1))} };
00702
00703 _prio CqsGetPriority(Queue q)
00704 {
00705 if (q->negprioq.heapnext>1) return &(q->negprioq.heap[1]->pri);
00706 if (q->zeroprio.head != q->zeroprio.tail) { return &kprio_zero; }
00707 if (q->posprioq.heapnext>1) return &(q->posprioq.heap[1]->pri);
00708 return &kprio_max;
00709 }
00710
00711
00712
00713
00714
00715
00716
00717
00718
00724 void** CqsEnumerateDeq(_deq q, int *num){
00725 void **head, **tail;
00726 void **result;
00727 int count = 0;
00728 int i;
00729
00730 head = q->head;
00731 tail = q->tail;
00732
00733 while(head != tail){
00734 count++;
00735 head++;
00736 if(head == q->end)
00737 head = q->bgn;
00738 }
00739
00740 result = (void **)CmiAlloc(count * sizeof(void *));
00741 i = 0;
00742 head = q->head;
00743 tail = q->tail;
00744 while(head != tail){
00745 result[i] = *head;
00746 i++;
00747 head++;
00748 if(head == q->end)
00749 head = q->bgn;
00750 }
00751 *num = count;
00752 return(result);
00753 }
00754
00760 void** CqsEnumeratePrioq(_prioq q, int *num){
00761 void **head, **tail;
00762 void **result;
00763 int i,j;
00764 int count = 0;
00765 _prioqelt pe;
00766
00767 for(i = 1; i < q->heapnext; i++){
00768 pe = (q->heap)[i];
00769 head = pe->data.head;
00770 tail = pe->data.tail;
00771 while(head != tail){
00772 count++;
00773 head++;
00774 if(head == (pe->data).end)
00775 head = (pe->data).bgn;
00776 }
00777 }
00778
00779 result = (void **)CmiAlloc((count) * sizeof(void *));
00780 *num = count;
00781
00782 j = 0;
00783 for(i = 1; i < q->heapnext; i++){
00784 pe = (q->heap)[i];
00785 head = pe->data.head;
00786 tail = pe->data.tail;
00787 while(head != tail){
00788 result[j] = *head;
00789 j++;
00790 head++;
00791 if(head ==(pe->data).end)
00792 head = (pe->data).bgn;
00793 }
00794 }
00795
00796 return result;
00797 }
00798
00799 void CqsEnumerateQueue(Queue q, void ***resp){
00800 void **result;
00801 int num;
00802 int i,j;
00803
00804 *resp = (void **)CmiAlloc(q->length * sizeof(void *));
00805 j = 0;
00806
00807 result = CqsEnumeratePrioq(&(q->negprioq), &num);
00808 for(i = 0; i < num; i++){
00809 (*resp)[j] = result[i];
00810 j++;
00811 }
00812 CmiFree(result);
00813
00814 result = CqsEnumerateDeq(&(q->zeroprio), &num);
00815 for(i = 0; i < num; i++){
00816 (*resp)[j] = result[i];
00817 j++;
00818 }
00819 CmiFree(result);
00820
00821 result = CqsEnumeratePrioq(&(q->posprioq), &num);
00822 for(i = 0; i < num; i++){
00823 (*resp)[j] = result[i];
00824 j++;
00825 }
00826 CmiFree(result);
00827 }
00828
00838 int CqsRemoveSpecificDeq(_deq q, const void *msgPtr){
00839 void **head, **tail;
00840
00841 head = q->head;
00842 tail = q->tail;
00843
00844 while(head != tail){
00845 if(*head == msgPtr){
00846
00847
00848 return 1;
00849 }
00850 head++;
00851 if(head == q->end)
00852 head = q->bgn;
00853 }
00854 return 0;
00855 }
00856
00866 int CqsRemoveSpecificPrioq(_prioq q, const void *msgPtr){
00867 void **head, **tail;
00868 void **result;
00869 int i;
00870 _prioqelt pe;
00871
00872 for(i = 1; i < q->heapnext; i++){
00873 pe = (q->heap)[i];
00874 head = pe->data.head;
00875 tail = pe->data.tail;
00876 while(head != tail){
00877 if(*head == msgPtr){
00878
00879 *head = NULL;
00880 return 1;
00881 }
00882 head++;
00883 if(head == (pe->data).end)
00884 head = (pe->data).bgn;
00885 }
00886 }
00887 return 0;
00888 }
00889
00890 void CqsRemoveSpecific(Queue q, const void *msgPtr){
00891 if( CqsRemoveSpecificPrioq(&(q->negprioq), msgPtr) == 0 )
00892 if( CqsRemoveSpecificDeq(&(q->zeroprio), msgPtr) == 0 )
00893 if(CqsRemoveSpecificPrioq(&(q->posprioq), msgPtr) == 0){
00894 CmiPrintf("Didn't remove the specified entry because it was not found\n");
00895 }
00896 }
00897
00898 #ifdef ADAPT_SCHED_MEM
00899 int numMemCriticalEntries=0;
00900 int *memCriticalEntries=NULL;
00901 #endif
00902