void process_data_samples(uint64_t time) { if(!data_events) return; struct data_ev *event = pqueue_peek(data_events); while(event && event->rdt <= time) { /*printf("%s:%lu:%p:%s\n", event->type==MALLOC?"malloc":"free", event->rdt, (void*)event->free.begin, event->type==MALLOC?event->malloc.info:"");*/ if(event->type==MALLOC) { void * data = rbtree_lookup(active_data, (void*)event->free.begin, pointer_cmp_reverse); if(data) { //printf("#Variable inserted twice ?!\n"); ((struct data_ev *)data)->malloc.end = event->malloc.end; data_fail++; } else { rbtree_insert(active_data, (void*)event->malloc.begin, event, pointer_cmp_reverse); data_success++; } } else if(event->type==FREE) { void * data = rbtree_lookup(active_data, (void*)event->free.begin, pointer_cmp_reverse); if(!data) { //printf("#Free of unknown pointer!\n"); data_fail++; } else { rbtree_delete(active_data, (void*)event->free.begin, pointer_cmp_reverse); data_success++; } } processed_data_samples++; pqueue_pop(data_events); event = pqueue_peek(data_events); } }
int dtls1_process_buffered_records(SSL *s) { pitem *item; item = pqueue_peek(s->rlayer.d->unprocessed_rcds.q); if (item) { /* Check if epoch is current. */ if (s->rlayer.d->unprocessed_rcds.epoch != s->rlayer.d->r_epoch) return (1); /* Nothing to do. */ /* Process all the records. */ while (pqueue_peek(s->rlayer.d->unprocessed_rcds.q)) { dtls1_get_unprocessed_record(s); if (!dtls1_process_record(s)) return (0); if (dtls1_buffer_record(s, &(s->rlayer.d->processed_rcds), SSL3_RECORD_get_seq_num(&s->rlayer.rrec)) < 0) return -1; } } /* * sync epoch numbers once all the unprocessed records have been * processed */ s->rlayer.d->processed_rcds.epoch = s->rlayer.d->r_epoch; s->rlayer.d->unprocessed_rcds.epoch = s->rlayer.d->r_epoch + 1; return (1); }
void *squeue_peek(squeue_t *q) { squeue_event *evt = pqueue_peek(q); if (evt) return evt->data; return NULL; }
int dtls1_read_failed(SSL *s, int code) { DTLS1_STATE *state; BIO *bio; int send_alert = 0; if ( code > 0) { fprintf( stderr, "invalid state reached %s:%d", __FILE__, __LINE__); return 1; } bio = SSL_get_rbio(s); if ( ! BIO_dgram_recv_timedout(bio)) { /* not a timeout, none of our business, let higher layers handle this. in fact it's probably an error */ return code; } if ( ! SSL_in_init(s)) /* done, no need to send a retransmit */ { BIO_set_flags(SSL_get_rbio(s), BIO_FLAGS_READ); return code; } state = s->d1; state->timeout.num_alerts++; if ( state->timeout.num_alerts > DTLS1_TMO_ALERT_COUNT) { /* fail the connection, enough alerts have been sent */ SSLerr(SSL_F_DTLS1_READ_FAILED,SSL_R_READ_TIMEOUT_EXPIRED); return 0; } state->timeout.read_timeouts++; if ( state->timeout.read_timeouts > DTLS1_TMO_READ_COUNT) { send_alert = 1; state->timeout.read_timeouts = 1; } #if 0 /* for now, each alert contains only one record number */ item = pqueue_peek(state->rcvd_records); if ( item ) { /* send an alert immediately for all the missing records */ } else #endif #if 0 /* no more alert sending, just retransmit the last set of messages */ if ( send_alert) ssl3_send_alert(s,SSL3_AL_WARNING, DTLS1_AD_MISSING_HANDSHAKE_MESSAGE); #endif return dtls1_retransmit_buffered_messages(s) ; }
} T_END_TEST T_TEST(t_pqueue_alt_insert) { struct pqueue* q = pqueue_create(comp); int i; int num = 10; T_ASSERT(q); for (i = 0; i < num; i++) { T_ASSERT(0 == pqueue_insert(q, (void*)((i % 2) ? i : num - i - (num % 2 ? 1 : 2)))); } T_ASSERT(!pqueue_is_empty(q)); i = 0; while (!pqueue_is_empty(q)) { T_ASSERT(i == (int)pqueue_peek(q)); T_ASSERT(i == (int)pqueue_pop(q)); i++; } T_ASSERT(i == num); pqueue_destroy(q); } T_END_TEST
static int dtls1_retrieve_buffered_fragment(SSL *s, long max, int *ok) { /* (0) check whether the desired fragment is available * if so: * (1) copy over the fragment to s->init_buf->data[] * (2) update s->init_num */ pitem *item; hm_fragment *frag; int al; *ok = 0; item = pqueue_peek(s->d1->buffered_messages); if ( item == NULL) return 0; frag = (hm_fragment *)item->data; /* Don't return if reassembly still in progress */ if (frag->reassembly != NULL) return 0; if ( s->d1->handshake_read_seq == frag->msg_header.seq) { unsigned long frag_len = frag->msg_header.frag_len; pqueue_pop(s->d1->buffered_messages); al=dtls1_preprocess_fragment(s,&frag->msg_header,max); if (al==0) /* no alert */ { unsigned char *p = (unsigned char *)s->init_buf->data+DTLS1_HM_HEADER_LENGTH; memcpy(&p[frag->msg_header.frag_off], frag->fragment,frag->msg_header.frag_len); } dtls1_hm_fragment_free(frag); pitem_free(item); if (al==0) { *ok = 1; return frag_len; } ssl3_send_alert(s,SSL3_AL_FATAL,al); s->init_num = 0; *ok = 0; return -1; } else return 0; }
} T_END_TEST T_TEST(t_pqueue_duplicate) { struct pqueue* q = pqueue_create(comp); void* v = (void*)0xdeadbeef; T_ASSERT(q); T_ASSERT(0 == pqueue_insert(q, v)); T_ASSERT(0 == pqueue_insert(q, v)); T_ASSERT(!pqueue_is_empty(q)); T_ASSERT(v == pqueue_peek(q)); T_ASSERT(v == pqueue_pop(q)); T_ASSERT(!pqueue_is_empty(q)); T_ASSERT(v == pqueue_peek(q)); T_ASSERT(v == pqueue_pop(q)); T_ASSERT(pqueue_is_empty(q)); pqueue_destroy(q); } T_END_TEST
/* * Checks all available alarms and runs their handlers */ void check_alarms() { alarm_id a; alarm_t alarm; // Keep checking queue until we find an alarm that should not execute while (pqueue_peek(alarm_pqueue, &a) == 0) { alarm = (alarm_t) a; if (time_ticks * PERIOD >= alarm->time) { // Executes the alarm pqueue_dequeue(alarm_pqueue, &a); alarm = (alarm_t) a; alarm->func(alarm->arg); free(alarm); // Frees alarm after execution } else { break; } } }
static int dtls1_retrieve_buffered_fragment(SSL *s, unsigned long *copied) { /* (0) check whether the desired fragment is available * if so: * (1) copy over the fragment to s->init_buf->data[] * (2) update s->init_num */ pitem *item; hm_fragment *frag; unsigned long overlap; unsigned char *p; item = pqueue_peek(s->d1->buffered_messages); if ( item == NULL) return 0; frag = (hm_fragment *)item->data; if ( s->d1->handshake_read_seq == frag->msg_header.seq && frag->msg_header.frag_off <= (unsigned int)s->init_num - DTLS1_HM_HEADER_LENGTH) { pqueue_pop(s->d1->buffered_messages); overlap = s->init_num - DTLS1_HM_HEADER_LENGTH - frag->msg_header.frag_off; p = frag->fragment; memcpy(&s->init_buf->data[s->init_num], p + DTLS1_HM_HEADER_LENGTH + overlap, frag->msg_header.frag_len - overlap); OPENSSL_free(frag->fragment); OPENSSL_free(frag); pitem_free(item); *copied = frag->msg_header.frag_len - overlap; return *copied; } else return 0; }
static int trivial() { pqueue q = pqueue_new(); if (q == NULL) { return 0; } int32_t data = 0xdeadbeef; uint8_t priority[8] = {0}; pitem *item = pitem_new(priority, &data); if (item == NULL || pqueue_insert(q, item) != item || pqueue_size(q) != 1 || pqueue_peek(q) != item || pqueue_pop(q) != item || pqueue_size(q) != 0 || pqueue_pop(q) != NULL) { return 0; } pitem_free(item); pqueue_free(q); return 1; }
int dtls1_read_failed(SSL *s, int code) { if ( code > 0) { fprintf( stderr, "invalid state reached %s:%d", __FILE__, __LINE__); return 1; } if (!dtls1_is_timer_expired(s)) { /* not a timeout, none of our business, let higher layers handle this. in fact it's probably an error */ return code; } if ( ! SSL_in_init(s)) /* done, no need to send a retransmit */ { BIO_set_flags(SSL_get_rbio(s), BIO_FLAGS_READ); return code; } #if 0 /* for now, each alert contains only one record number */ item = pqueue_peek(state->rcvd_records); if ( item ) { /* send an alert immediately for all the missing records */ } else #endif #if 0 /* no more alert sending, just retransmit the last set of messages */ if ( state->timeout.read_timeouts >= DTLS1_TMO_READ_COUNT) ssl3_send_alert(s,SSL3_AL_WARNING, DTLS1_AD_MISSING_HANDSHAKE_MESSAGE); #endif return dtls1_handle_timeout(s); }
pitem * pqueue_iterator(pqueue_s *pq) { return pqueue_peek(pq); }
int dtls1_process_buffered_records(SSL *s) { pitem *item; SSL3_BUFFER *rb; SSL3_RECORD *rr; DTLS1_BITMAP *bitmap; unsigned int is_next_epoch; int replayok = 1; item = pqueue_peek(s->rlayer.d->unprocessed_rcds.q); if (item) { /* Check if epoch is current. */ if (s->rlayer.d->unprocessed_rcds.epoch != s->rlayer.d->r_epoch) return 1; /* Nothing to do. */ rr = RECORD_LAYER_get_rrec(&s->rlayer); rb = RECORD_LAYER_get_rbuf(&s->rlayer); if (SSL3_BUFFER_get_left(rb) > 0) { /* * We've still got data from the current packet to read. There could * be a record from the new epoch in it - so don't overwrite it * with the unprocessed records yet (we'll do it when we've * finished reading the current packet). */ return 1; } /* Process all the records. */ while (pqueue_peek(s->rlayer.d->unprocessed_rcds.q)) { dtls1_get_unprocessed_record(s); bitmap = dtls1_get_bitmap(s, rr, &is_next_epoch); if (bitmap == NULL) { /* * Should not happen. This will only ever be NULL when the * current record is from a different epoch. But that cannot * be the case because we already checked the epoch above */ SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DTLS1_PROCESS_BUFFERED_RECORDS, ERR_R_INTERNAL_ERROR); return 0; } #ifndef OPENSSL_NO_SCTP /* Only do replay check if no SCTP bio */ if (!BIO_dgram_is_sctp(SSL_get_rbio(s))) #endif { /* * Check whether this is a repeat, or aged record. We did this * check once already when we first received the record - but * we might have updated the window since then due to * records we subsequently processed. */ replayok = dtls1_record_replay_check(s, bitmap); } if (!replayok || !dtls1_process_record(s, bitmap)) { if (ossl_statem_in_error(s)) { /* dtls1_process_record called SSLfatal() */ return -1; } /* dump this record */ rr->length = 0; RECORD_LAYER_reset_packet_length(&s->rlayer); continue; } if (dtls1_buffer_record(s, &(s->rlayer.d->processed_rcds), SSL3_RECORD_get_seq_num(s->rlayer.rrec)) < 0) { /* SSLfatal() already called */ return 0; } } } /* * sync epoch numbers once all the unprocessed records have been * processed */ s->rlayer.d->processed_rcds.epoch = s->rlayer.d->r_epoch; s->rlayer.d->unprocessed_rcds.epoch = s->rlayer.d->r_epoch + 1; return 1; }
int main(int argc, char **argv) { PQueue pqueue; void *data; int intval[30], i; /***************************************************************************** * * * Initialize the priority queue. * * * *****************************************************************************/ pqueue_init(&pqueue, compare_int, NULL); /***************************************************************************** * * * Perform some priority queue operations. * * * *****************************************************************************/ i = 0; intval[i] = 5; fprintf(stdout, "Inserting %03d\n", intval[i]); if (pqueue_insert(&pqueue, &intval[i]) != 0) return 1; print_pqueue(&pqueue); i++; intval[i] = 10; fprintf(stdout, "Inserting %03d\n", intval[i]); if (pqueue_insert(&pqueue, &intval[i]) != 0) return 1; print_pqueue(&pqueue); i++; intval[i] = 20; fprintf(stdout, "Inserting %03d\n", intval[i]); if (pqueue_insert(&pqueue, &intval[i]) != 0) return 1; print_pqueue(&pqueue); i++; intval[i] = 1; fprintf(stdout, "Inserting %03d\n", intval[i]); if (pqueue_insert(&pqueue, &intval[i]) != 0) return 1; print_pqueue(&pqueue); i++; intval[i] = 25; fprintf(stdout, "Inserting %03d\n", intval[i]); if (pqueue_insert(&pqueue, &intval[i]) != 0) return 1; print_pqueue(&pqueue); i++; intval[i] = 22; fprintf(stdout, "Inserting %03d\n", intval[i]); if (pqueue_insert(&pqueue, &intval[i]) != 0) return 1; print_pqueue(&pqueue); i++; intval[i] = 12; fprintf(stdout, "Inserting %03d\n", intval[i]); if (pqueue_insert(&pqueue, &intval[i]) != 0) return 1; print_pqueue(&pqueue); i++; while (pqueue_size(&pqueue) > 0) { fprintf(stdout, "Peeking at the highest priority element..Value=%03d\n", *(int *)pqueue_peek(&pqueue)); if (pqueue_extract(&pqueue, (void **)&data) != 0) return 1; fprintf(stdout, "Extracting %03d\n", *(int *)data); print_pqueue(&pqueue); } /***************************************************************************** * * * Destroy the priority queue. * * * *****************************************************************************/ fprintf(stdout, "Destroying the pqueue\n"); pqueue_destroy(&pqueue); return 0; }
int a_star(int x0, int y0, int x1, int y1, int *map, int w, int h, int *path, int sz) { static int dirs[8][3] = { {-1, 0, 10}, { 0, -1, 10}, { 0, 1, 10}, { 1, 0, 10}, {-1, -1, 14}, {-1, 1, 14}, { 1, -1, 14}, { 1, 1, 14}, }; int ret = 1, i, nx, ny; struct a_star_node *nodes = NULL; struct a_star_node *ntmp = NULL, *head = NULL, *reversed = NULL; struct a_star_node *lowest = NULL, *neighbor = NULL; struct pqueue open; nodes = malloc(sizeof *nodes * w * h); if (!nodes) { ret = -1; goto failure; } memset(nodes, 0, sizeof *nodes * w * h); nodes[y0 * w + x0].x = x0; nodes[y0 * w + x0].y = y0; pqueue_init(&open, 256); pqueue_push_min(&open, &nodes[y0 * w + x0], a_star_node_cmp); while (open.length) { pqueue_peek(&open, (void **)&lowest); pqueue_pop_min(&open, NULL, a_star_node_cmp); lowest->closed = 1; if (lowest->x == x1 && lowest->y == y1) break; for (i = 0; i < 8; ++i) { nx = lowest->x + dirs[i][0]; ny = lowest->y + dirs[i][1]; neighbor = &nodes[ny * w + nx]; if (nx >= 0 && nx < w && ny >= 0 && ny < h && !map[ny * w + nx] && !neighbor->closed) { if (!pqueue_has(&open, neighbor)) { neighbor->x = nx; neighbor->y = ny; neighbor->g = lowest->g + dirs[i][2]; neighbor->h = heuristic(nx, ny, x1, y1); pqueue_push_min(&open, neighbor, a_star_node_cmp); neighbor->parent = lowest; } else if (lowest->g + dirs[i][2] < neighbor->g) { neighbor->g = lowest->g + dirs[i][2]; neighbor->parent = lowest; } } } } if (!nodes[y1 * w + x1].parent) { path[0] = -1; path[1] = -1; ret = 0; goto failure; } for (head = &nodes[y1 * w + x1]; head; ) { ntmp = head; head = head->parent; ntmp->parent = reversed; reversed = ntmp; } for (i = 0; reversed; reversed = reversed->parent) { path[i * 2 + 0] = reversed->x; path[i * 2 + 1] = reversed->y; if (++i >= sz - 1) break; } path[i * 2 + 0] = -1; path[i * 2 + 1] = -1; failure: free(nodes); pqueue_uninit(&open, NULL); return ret; }