int enQueue(QUEUE_STRUCTURE * Q, void * item) { if (isQueueFull(Q)) { if (growQueue(Q) == FALSE) return FALSE; } QUEUE_ITEM * tempItem = malloc(sizeof(QUEUE_ITEM)); if (tempItem == NULL) { logError("Cannot allocate memory for inserting item in Queue!"); return FALSE; } tempItem->item = item; Q->queueArray[Q->tail] = tempItem; if (Q->tail == Q->length - 1) { Q->tail = 0; } else { Q->tail++; } Q->itemAdded++; return TRUE; }
//add node to the front void enque(Queue *q, Hash *h, unsigned int page_no) { if(q == NULL || h == NULL || page_no > h->capacity) return; //if queue is full, remove page from the rear //remive it from hash table if(isQueueFull(q)) { dequeue(q); h->array[q->rear->page_no] = NULL; } QNode *node = new_node(page_no); node->next = q->front; if(isQueueEmpty(q)) q->front = q->rear = node; else { q->front->prev = node; q->front = node; } //make an entry into hash table h->array[page_no] = node; q->count++; }
int enQueue( MyQueue *p, char *element ) { int typesize = 0; int maxsize = 0; int offset = 0; if( !isQueueInit(p) || element == NULL ) { printf( "[%s][%d]the queue donot init\n", __FILE__, __LINE__ ); return MY_ERR; } if( isQueueFull(p) ) { printf( "[%s][%d]queue is full\n", __FILE__, __LINE__ ); return MY_ERR_Q_FULL; } //pthread_mutex_lock( &(p->mutex) ); typesize = p->typesize; maxsize = p->maxsize * typesize; offset = typesize; //printf( "[%s][%d]elem=[%d]\n", __FILE__, __LINE__, *((int *)element) ); memcpy( p->queue+p->rear, element, typesize ); p->rear = (p->rear+offset)%(maxsize); p->length++; //pthread_mutex_unlock( &(p->mutex) ); return MY_SUCCESS; }
void enqueue(struct Queue *Q,int e) { if(!isQueueFull(*Q)) { (*Q).rear++; (*Q).rear=(*Q).rear%(*Q).size; (*Q).arr[(*Q).rear]=e; } }
void dataPush() { float cpuLoad=(1000-idle)*0.1; intToChar(cpuLoad,cpuLoadBuffer[myQueue.end]); if(isQueueFull(&myQueue)!=1) { enqQueue(&myQueue,(void *)cpuLoadBuffer[myQueue.end]); } else { deqQueue(&myQueue); enqQueue(&myQueue,(void *)cpuLoadBuffer[myQueue.end]); } }
void enqueue(struct Queue * Q, struct Data d) { struct queueNode * newNode; if (!isQueueFull(Q)) { newNode = (struct queueNode *)malloc(sizeof(struct queueNode)); newNode->data=(struct Data *)malloc(sizeof(struct Data)); *(newNode->data)=d; newNode->link=NULL; if (Q->rear==NULL) { Q->rear=newNode; Q->front=newNode; } else { Q->rear->link=newNode; Q->rear=newNode; } Q->currSize++; } }
/************************************************************************************ * * addElementToQueue * * Adds a new data element to the queue * * Arguments: * * char *data - localDataStructure of data * short size - Size of data array * int i - ? * short type - Type of data * uint threadNumber - Thread number to which this queue belongs * uint fd - Queue's file descriptor * Queue inputQueue - Input queue * * Return Value: * * void * *************************************************************************************/ void addElementToQueue(char *data,short size,int i, short type,uint threadNumber,uint fd, Queue inputQueue) { if (isQueueFull(inputQueue)) { if (inputQueue->growOnDemand) { // effectively double the size of the capacity increaseQueueCapacity(inputQueue,inputQueue->currentCapacity); } } // increase the current size of our queue inputQueue->currentSize++; incrementIndex(&inputQueue->currentRearIndex,&inputQueue->currentCapacity); //inputQueue->Rear = Succ(inputQueue->currentRearIndex, inputQueue); // free data from the previously set packet free(inputQueue->localDataStructure[inputQueue->currentRearIndex].data); // free data for the file descriptor in our packet free(inputQueue->localDataStructure[inputQueue->currentRearIndex].fd); //int i=0; // allocate memory for our data inputQueue->localDataStructure[inputQueue->currentRearIndex].data = (char*)malloc((size)*sizeof(char)); // set data to all nulls memset(inputQueue->localDataStructure[inputQueue->currentRearIndex].data,0x00,size); // allocate memory for our file descriptor inputQueue->localDataStructure[inputQueue->currentRearIndex].fd = (int*)malloc(sizeof(int)); inputQueue->localDataStructure[inputQueue->currentRearIndex].threadNumber=threadNumber; inputQueue->localDataStructure[inputQueue->currentRearIndex].connection=i; *inputQueue->localDataStructure[inputQueue->currentRearIndex].fd=fd; inputQueue->localDataStructure[inputQueue->currentRearIndex].type=type; // copy the data into our packet strncpy(inputQueue->localDataStructure[inputQueue->currentRearIndex].data,data,size); }
hwlmcb_rv_t roseHandleChainMatch(const struct RoseEngine *t, struct hs_scratch *scratch, u32 event, u64a top_squash_distance, u64a end, char in_catchup) { assert(event == MQE_TOP || event >= MQE_TOP_FIRST); struct core_info *ci = &scratch->core_info; u8 *aa = getActiveLeafArray(t, scratch->core_info.state); u32 aaCount = t->activeArrayCount; struct fatbit *activeQueues = scratch->aqa; u32 qCount = t->queueCount; const u32 qi = 0; /* MPV is always queue 0 if it exists */ struct mq *q = &scratch->queues[qi]; const struct NfaInfo *info = getNfaInfoByQueue(t, qi); s64a loc = (s64a)end - ci->buf_offset; assert(loc <= (s64a)ci->len && loc >= -(s64a)ci->hlen); if (!mmbit_set(aa, aaCount, qi)) { initQueue(q, qi, t, scratch); nfaQueueInitState(q->nfa, q); pushQueueAt(q, 0, MQE_START, loc); fatbit_set(activeQueues, qCount, qi); } else if (info->no_retrigger) { DEBUG_PRINTF("yawn\n"); /* nfa only needs one top; we can go home now */ return HWLM_CONTINUE_MATCHING; } else if (!fatbit_set(activeQueues, qCount, qi)) { initQueue(q, qi, t, scratch); loadStreamState(q->nfa, q, 0); pushQueueAt(q, 0, MQE_START, 0); } else if (isQueueFull(q)) { DEBUG_PRINTF("queue %u full -> catching up nfas\n", qi); /* we know it is a chained nfa and the suffixes/outfixes must already * be known to be consistent */ if (ensureMpvQueueFlushed(t, scratch, qi, loc, in_catchup) == HWLM_TERMINATE_MATCHING) { DEBUG_PRINTF("terminating...\n"); return HWLM_TERMINATE_MATCHING; } } if (top_squash_distance) { assert(q->cur != q->end); struct mq_item *last = &q->items[q->end - 1]; if (last->type == event && last->location >= loc - (s64a)top_squash_distance) { last->location = loc; goto event_enqueued; } } pushQueue(q, event, loc); event_enqueued: if (q_cur_loc(q) == (s64a)ci->len) { /* we may not run the nfa; need to ensure state is fine */ DEBUG_PRINTF("empty run\n"); pushQueueNoMerge(q, MQE_END, loc); char alive = nfaQueueExec(q->nfa, q, loc); if (alive) { scratch->tctxt.mpv_inactive = 0; q->cur = q->end = 0; pushQueueAt(q, 0, MQE_START, loc); } else { mmbit_unset(aa, aaCount, qi); fatbit_unset(scratch->aqa, qCount, qi); } } DEBUG_PRINTF("added mpv event at %lld\n", loc); scratch->tctxt.next_mpv_offset = 0; /* the top event may result in matches * earlier than expected */ return HWLM_CONTINUE_MATCHING; }
int main() { FILE *fp; Queue cola; tInfo info; char op; printf("\t\t\tEjercicio 9 tp3 cola estatica\n"); if(!openFile(&fp,"r+b",FILENAME,!CON_SIN_MSJ)) { createFile(); if(!openFile(&fp,"r+b",FILENAME,CON_SIN_MSJ)) return 0; } createQueue(&cola); fread(&info,1,sizeof(tInfo),fp); while(!feof(fp) && !isQueueFull(&cola)) { putInQueue(&cola,&info); fread(&info,1,sizeof(tInfo),fp); } fclose(fp); op = menuOption(MSJ,OPTION); while(op != 'D') { system("cls"); switch(op) { case 'A': { newInfo(&info); if(putInQueue(&cola, &info) == COLA_LLENA) puts("cola llena no se pudo cargar la nueva informacion"); break; } case 'B': { puts("Viendo primero de la cola: "); if(showFirstInQueue(&cola,&info) != COLA_VACIA) showInfo(&info); else puts("cola vacia, no se puede ver primero"); break; } case 'C': { puts("Sacando de cola..."); if(takeOfQueue(&cola,&info) != COLA_VACIA) showInfo(&info); else puts("No se puede sacar de cola, cola vacia"); break; } } op = menuOption(MSJ,OPTION); } if(!openFile(&fp,"r+b",FILENAME,CON_SIN_MSJ)) { puts("Vaciando cola..."); emptyQueue(&cola); return 0; } while(!isQueueEmpty(&cola)) { takeOfQueue(&cola,&info); putInTheEndFile(&fp,&info); } emptyQueue(&cola); fclose(fp); return 0; }