int dtls1_new(SSL *s) { DTLS1_STATE *d1; if (!ssl3_new(s)) { return 0; } d1 = OPENSSL_malloc(sizeof *d1); if (d1 == NULL) { ssl3_free(s); return 0; } memset(d1, 0, sizeof *d1); d1->buffered_messages = pqueue_new(); d1->sent_messages = pqueue_new(); if (!d1->buffered_messages || !d1->sent_messages) { pqueue_free(d1->buffered_messages); pqueue_free(d1->sent_messages); OPENSSL_free(d1); ssl3_free(s); return 0; } s->d1 = d1; /* Set the version to the highest version for DTLS. This controls the initial * state of |s->enc_method| and what the API reports as the version prior to * negotiation. * * TODO(davidben): This is fragile and confusing. */ s->version = DTLS1_2_VERSION; return 1; }
int dtls1_new(SSL *ssl) { DTLS1_STATE *d1; if (!ssl3_new(ssl)) { return 0; } d1 = OPENSSL_malloc(sizeof *d1); if (d1 == NULL) { ssl3_free(ssl); return 0; } memset(d1, 0, sizeof *d1); d1->buffered_messages = pqueue_new(); d1->sent_messages = pqueue_new(); if (!d1->buffered_messages || !d1->sent_messages) { pqueue_free(d1->buffered_messages); pqueue_free(d1->sent_messages); OPENSSL_free(d1); ssl3_free(ssl); return 0; } ssl->d1 = d1; /* Set the version to the highest supported version. * * TODO(davidben): Move this field into |s3|, have it store the normalized * protocol version, and implement this pre-negotiation quirk in |SSL_version| * at the API boundary rather than in internal state. */ ssl->version = DTLS1_2_VERSION; return 1; }
int DTLS_RECORD_LAYER_new(RECORD_LAYER *rl) { DTLS_RECORD_LAYER *d; if ((d = OPENSSL_malloc(sizeof(*d))) == NULL) { SSLerr(SSL_F_DTLS_RECORD_LAYER_NEW, ERR_R_MALLOC_FAILURE); return 0; } rl->d = d; d->unprocessed_rcds.q = pqueue_new(); d->processed_rcds.q = pqueue_new(); d->buffered_app_data.q = pqueue_new(); if (d->unprocessed_rcds.q == NULL || d->processed_rcds.q == NULL || d->buffered_app_data.q == NULL) { pqueue_free(d->unprocessed_rcds.q); pqueue_free(d->processed_rcds.q); pqueue_free(d->buffered_app_data.q); OPENSSL_free(d); rl->d = NULL; return 0; } return 1; }
int DTLS_RECORD_LAYER_new(RECORD_LAYER *rl) { DTLS_RECORD_LAYER *d; if ((d = OPENSSL_malloc(sizeof(*d))) == NULL) return (0); rl->d = d; d->unprocessed_rcds.q = pqueue_new(); d->processed_rcds.q = pqueue_new(); d->buffered_app_data.q = pqueue_new(); if (d->unprocessed_rcds.q == NULL || d->processed_rcds.q == NULL || d->buffered_app_data.q == NULL) { pqueue_free(d->unprocessed_rcds.q); pqueue_free(d->processed_rcds.q); pqueue_free(d->buffered_app_data.q); OPENSSL_free(d); rl->d = NULL; return (0); } return 1; }
int dtls1_new(SSL *s) { DTLS1_STATE *d1; if (!ssl3_new(s)) return (0); if ((d1 = OPENSSL_malloc(sizeof *d1)) == NULL) return (0); memset(d1, 0, sizeof *d1); /* d1->handshake_epoch=0; */ #if defined(OPENSSL_SYS_VMS) || defined(VMS_TEST) d1->bitmap.length = 64; #else d1->bitmap.length = sizeof(d1->bitmap.map) * 8; #endif pq_64bit_init(&(d1->bitmap.map)); pq_64bit_init(&(d1->bitmap.max_seq_num)); d1->next_bitmap.length = d1->bitmap.length; pq_64bit_init(&(d1->next_bitmap.map)); pq_64bit_init(&(d1->next_bitmap.max_seq_num)); d1->unprocessed_rcds.q = pqueue_new(); d1->processed_rcds.q = pqueue_new(); d1->buffered_messages = pqueue_new(); d1->sent_messages = pqueue_new(); d1->buffered_app_data.q = pqueue_new(); if (s->server) { d1->cookie_len = sizeof(s->d1->cookie); } if (!d1->unprocessed_rcds.q || !d1->processed_rcds.q || !d1->buffered_messages || !d1->sent_messages || !d1->buffered_app_data.q) { if (d1->unprocessed_rcds.q) pqueue_free(d1->unprocessed_rcds.q); if (d1->processed_rcds.q) pqueue_free(d1->processed_rcds.q); if (d1->buffered_messages) pqueue_free(d1->buffered_messages); if (d1->sent_messages) pqueue_free(d1->sent_messages); if (d1->buffered_app_data.q) pqueue_free(d1->buffered_app_data.q); OPENSSL_free(d1); return (0); } s->d1 = d1; s->method->ssl_clear(s); return (1); }
int main(int argc, char *argv[]) { PQueue *q = NULL; Test *t = NULL; int i; srand(time(NULL)); /* A priority Queue containing a maximum of 10 elements */ q = pqueue_new(test_compare, 10); for(i = 0; i < 10; ++i) { /* Adding elements to priority Queue */ t = test_new(rand()); pqueue_minenqueue(q, t); } for(i = 0; i < 10; ++i) { Test * buffer = (Test*)pqueue_mindequeue(q); printf("%d - %s\n", buffer->priority, buffer->log); /* Free memory - me lazy */ } /* Free memory - me lazy */ return (0); }
void merge(int num_threads, int length, pivot_data *before_merge, int* merged){ //printf("In merge num_threads %d\n", num_threads); int i; for(i=0; i<num_threads; i++){ before_merge[i].ctr=0; //printf("i %d length %d\n", i, before_merge[i].length); } //printf("length %d\n", length); int ctr = 0; pivot_data* dq; PQueue *pq = pqueue_new(heap_compare_pivots, num_threads); for(i=0; i<num_threads; i++){ pqueue_enqueue(pq, &before_merge[i]); //printf("Enqueue %d\n", before_merge[i].pivots[0]); } while(pq->size != 0){ dq = (pivot_data*)pqueue_dequeue(pq); //printf("Dequeue %d\n", dq->pivots[dq->ctr]); merged[ctr++] = dq->pivots[dq->ctr++]; if(dq->ctr != dq->length){ pqueue_enqueue(pq, dq); //printf("Enqueue %d\n", dq->pivots[dq->ctr]); } } pqueue_delete(pq); aligned_free(before_merge); }
int main(void) { pitem *item; pqueue pq; pq = pqueue_new(); item = pitem_new(3, NULL); pqueue_insert(pq, item); item = pitem_new(1, NULL); pqueue_insert(pq, item); item = pitem_new(2, NULL); pqueue_insert(pq, item); item = pqueue_find(pq, 1); fprintf(stderr, "found %ld\n", item->priority); item = pqueue_find(pq, 2); fprintf(stderr, "found %ld\n", item->priority); item = pqueue_find(pq, 3); fprintf(stderr, "found %ld\n", item ? item->priority: 0); pqueue_print(pq); for(item = pqueue_pop(pq); item != NULL; item = pqueue_pop(pq)) pitem_free(item); pqueue_free(pq); return 0; }
int dtls1_new (SSL * s) { DTLS1_STATE *d1; if (!ssl3_new (s)) return (0); if ((d1 = OPENSSL_malloc (sizeof *d1)) == NULL) return (0); memset (d1, 0, sizeof *d1); /* d1->handshake_epoch=0; */ d1->unprocessed_rcds.q = pqueue_new (); d1->processed_rcds.q = pqueue_new (); d1->buffered_messages = pqueue_new (); d1->sent_messages = pqueue_new (); d1->buffered_app_data.q = pqueue_new (); if (s->server) { d1->cookie_len = sizeof (s->d1->cookie); } d1->link_mtu = 0; d1->mtu = 0; if (!d1->unprocessed_rcds.q || !d1->processed_rcds.q || !d1->buffered_messages || !d1->sent_messages || !d1->buffered_app_data.q) { if (d1->unprocessed_rcds.q) pqueue_free (d1->unprocessed_rcds.q); if (d1->processed_rcds.q) pqueue_free (d1->processed_rcds.q); if (d1->buffered_messages) pqueue_free (d1->buffered_messages); if (d1->sent_messages) pqueue_free (d1->sent_messages); if (d1->buffered_app_data.q) pqueue_free (d1->buffered_app_data.q); OPENSSL_free (d1); return (0); } s->d1 = d1; s->method->ssl_clear (s); return (1); }
int dtls1_new(SSL *s) { DTLS1_STATE *d1; if (!ssl3_new(s)) return (0); if ((d1 = calloc(1, sizeof *d1)) == NULL) { ssl3_free(s); return (0); } /* d1->handshake_epoch=0; */ d1->unprocessed_rcds.q = pqueue_new(); d1->processed_rcds.q = pqueue_new(); d1->buffered_messages = pqueue_new(); d1->sent_messages = pqueue_new(); d1->buffered_app_data.q = pqueue_new(); if (s->server) { d1->cookie_len = sizeof(s->d1->cookie); } if (!d1->unprocessed_rcds.q || !d1->processed_rcds.q || !d1->buffered_messages || !d1->sent_messages || !d1->buffered_app_data.q) { if (d1->unprocessed_rcds.q) pqueue_free(d1->unprocessed_rcds.q); if (d1->processed_rcds.q) pqueue_free(d1->processed_rcds.q); if (d1->buffered_messages) pqueue_free(d1->buffered_messages); if (d1->sent_messages) pqueue_free(d1->sent_messages); if (d1->buffered_app_data.q) pqueue_free(d1->buffered_app_data.q); free(d1); ssl3_free(s); return (0); } s->d1 = d1; s->method->ssl_clear(s); return (1); }
void mylib_barrier_calculate_pivots (struct barrier_node*b, int num_threads) { pthread_mutex_lock(&(b -> count_lock)); b -> count ++; if (b -> count == num_threads) { b -> count = 0; //This code should be executed by only one thread. int *merged_samples = (int*)malloc(sizeof(int)*num_threads*num_threads); int i = 0 , k= 0; PQueue*heap = pqueue_new(compare_heap_key,num_threads); for(;i<num_threads;++i) { if(samples_arr[i].count > 0 ) { pqueue_enqueue(heap,samples_arr + i); } } struct sample_size*ptr; while((ptr = pqueue_dequeue(heap)) != NULL) { merged_samples[k++] = ptr->addr[ptr->current_index++]; if(ptr->current_index < ptr->count) { pqueue_enqueue(heap,ptr); } } if(DEBUG_1) { puts("In merged samples"); for(i=0;i<k;++i) { printf("%d ",merged_samples[i]); } } //Find the p-1 pivots pivots = (int*)malloc(sizeof(int) * (num_threads - 1)); int pby2 = num_threads/2 - 1; for(i = 1;i < num_threads;++i) { int index = i*num_threads + pby2; if(index >= k) { break; } //Store unique pivots only. if(!total_pivots ||( pivots[total_pivots - 1] != merged_samples[index])) { pivots[total_pivots++] = merged_samples[index]; } } //free(merged_samples); pthread_cond_broadcast(&(b -> ok_to_proceed)); } else while (pthread_cond_wait(&(b -> ok_to_proceed), &(b -> count_lock)) != 0); pthread_mutex_unlock(&(b -> count_lock)); }
END_TEST START_TEST(test_pqueue_free) { PriorityQueue *pq = pqueue_new(); ck_assert_msg(pq != NULL, "Priority queue should not be NULL."); pqueue_free(pq); }
END_TEST ////////////////////////////////////////////////////////////////////// ///////////// pqueue unit tests ////////////////////////////////////////////////////////////////////// START_TEST(test_pqueue_new) { PriorityQueue *pq = pqueue_new(); ck_assert_msg(pq != NULL, "Priority queue should not be NULL."); }
int dtls1_new(SSL *s) { DTLS1_STATE *d1; if (!DTLS_RECORD_LAYER_new(&s->rlayer)) { return 0; } if (!ssl3_new(s)) return 0; if ((d1 = OPENSSL_zalloc(sizeof(*d1))) == NULL) { ssl3_free(s); return 0; } d1->buffered_messages = pqueue_new(); d1->sent_messages = pqueue_new(); if (s->server) { d1->cookie_len = sizeof(s->d1->cookie); } d1->link_mtu = 0; d1->mtu = 0; if (d1->buffered_messages == NULL || d1->sent_messages == NULL) { pqueue_free(d1->buffered_messages); pqueue_free(d1->sent_messages); OPENSSL_free(d1); ssl3_free(s); return 0; } s->d1 = d1; if (!s->method->ssl_clear(s)) return 0; return 1; }
END_TEST START_TEST(test_pqueue_enqueue) { PriorityQueue *pq = pqueue_new(); ck_assert_msg(pq != NULL, "Priority queue should not be NULL."); pqueue_enqueue(pq, tree_new()); ck_assert_int_eq(pqueue_size(pq), 1); pqueue_enqueue(pq, tree_new()); ck_assert_int_eq(pqueue_size(pq), 2); pqueue_enqueue(pq, tree_new()); ck_assert_int_eq(pqueue_size(pq), 3); pqueue_enqueue(pq, tree_new()); ck_assert_int_eq(pqueue_size(pq), 4); pqueue_free(pq); }
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 main(void) { pitem *item; pqueue pq; int one = 1; int two = 2; int three = 3; pq = pqueue_new(); item = pitem_new((unsigned char*)&three, NULL); pqueue_insert(pq, item); item = pitem_new((unsigned char*)&one, NULL); pqueue_insert(pq, item); item = pitem_new((unsigned char*)&two, NULL); pqueue_insert(pq, item); item = pqueue_find(pq, (unsigned char*)&one); TINYCLR_SSL_FPRINTF(OPENSSL_TYPE__FILE_STDERR, "found %ld\n", item->priority); item = pqueue_find(pq, (unsigned char*)&two); TINYCLR_SSL_FPRINTF(OPENSSL_TYPE__FILE_STDERR, "found %ld\n", item->priority); item = pqueue_find(pq, (unsigned char*)&three); TINYCLR_SSL_FPRINTF(OPENSSL_TYPE__FILE_STDERR, "found %ld\n", item ? item->priority: 0); pqueue_print(pq); for(item = pqueue_pop(pq); item != NULL; item = pqueue_pop(pq)) pitem_free(item); pqueue_free(pq); return 0; }
int main(void) { pitem *item; pqueue pq; pq = pqueue_new(); item = pitem_new(prio3, NULL); pqueue_insert(pq, item); item = pitem_new(prio1, NULL); pqueue_insert(pq, item); item = pitem_new(prio2, NULL); pqueue_insert(pq, item); item = pqueue_find(pq, prio1); fprintf(stderr, "found %p\n", item->priority); item = pqueue_find(pq, prio2); fprintf(stderr, "found %p\n", item->priority); item = pqueue_find(pq, prio3); fprintf(stderr, "found %p\n", item ? item->priority : 0); pqueue_print(pq); if (!pqueue_test(pq)) return 1; for (item = pqueue_pop(pq); item != NULL; item = pqueue_pop(pq)) pitem_free(item); pqueue_free(pq); return 0; }
END_TEST START_TEST(test_pqueue_dequeue) { PriorityQueue *pq = pqueue_new(); ck_assert_msg(pq != NULL, "Priority queue should not be NULL."); TreeNode *t = tree_new(); t->type = LEAF; t->freq.v = 10; t->freq.c = 'c'; t->left = NULL; t->right = NULL; pqueue_enqueue(pq, t); ck_assert_int_eq(pqueue_size(pq), 1); t = tree_new(); t->type = LEAF; t->freq.v = 15; t->freq.c = 'x'; t->left = NULL; t->right = NULL; pqueue_enqueue(pq, t); ck_assert_int_eq(pqueue_size(pq), 2); t = tree_new(); t->type = LEAF; t->freq.v = 9; t->freq.c = 'q'; t->left = NULL; t->right = NULL; pqueue_enqueue(pq, t); ck_assert_int_eq(pqueue_size(pq), 3); t = tree_new(); t->type = LEAF; t->freq.v = 999; t->freq.c = 'a'; t->left = NULL; t->right = NULL; pqueue_enqueue(pq, t); ck_assert_int_eq(pqueue_size(pq), 4); t = pqueue_dequeue(pq); ck_assert_int_eq(t->freq.c, 'q'); ck_assert_int_eq(pqueue_size(pq), 3); free(t); t = pqueue_dequeue(pq); ck_assert_int_eq(t->freq.c, 'c'); ck_assert_int_eq(pqueue_size(pq), 2); free(t); t = pqueue_dequeue(pq); ck_assert_int_eq(t->freq.c, 'x'); ck_assert_int_eq(pqueue_size(pq), 1); free(t); t = pqueue_dequeue(pq); ck_assert_int_eq(t->freq.c, 'a'); ck_assert_int_eq(pqueue_size(pq), 0); free(t); pqueue_free(pq); }
/* * Initialize alarm priority queue */ void initialize_alarms() { alarm_pqueue = pqueue_new(); }
static int fixed_random() { /* Random order of 10 elements, chosen by * random.choice(list(itertools.permutations(range(10)))) */ int ordering[NUM_ITEMS] = {9, 6, 3, 4, 0, 2, 7, 1, 8, 5}; int i; pqueue q = pqueue_new(); uint8_t priority[8] = {0}; piterator iter; pitem *curr, *item; if (q == NULL) { return 0; } /* Insert the elements */ for (i = 0; i < NUM_ITEMS; i++) { priority[7] = ordering[i]; item = pitem_new(priority, &ordering[i]); if (pqueue_insert(q, item) != item) { return 0; } } /* Insert the elements again. This inserts duplicates and should * fail. */ for (i = 0; i < NUM_ITEMS; i++) { priority[7] = ordering[i]; item = pitem_new(priority, &ordering[i]); if (pqueue_insert(q, item) != NULL) { return 0; } pitem_free(item); } if (pqueue_size(q) != NUM_ITEMS) { return 0; } /* Iterate over the elements. */ iter = pqueue_iterator(q); curr = pqueue_next(&iter); if (curr == NULL) { return 0; } while (1) { pitem *next = pqueue_next(&iter); int *curr_data, *next_data; if (next == NULL) { break; } curr_data = (int*)curr->data; next_data = (int*)next->data; if (*curr_data >= *next_data) { return 0; } curr = next; } pqueue_free(q); return 1; }
/* * Solves the puzzle and prints results. * Returns 1 on success and 0 on nonexistence of a solution. */ static int solve(int *start, int *end) { Grid *root, *goal, *cur, *child, *iter, **result; Pqueue *pq; Set *visited; int goal_grid_code, child_code; int i, ch; int path_length; root = (Grid *) malloc(sizeof(Grid)); memcpy(root->g, start, sizeof(int) * 9); root->parent = NULL; root->hole = 1; root->depth = 0; goal = (Grid *) malloc(sizeof(Grid)); memcpy(goal->g, end, sizeof(int) * 9); goal_grid_code = grid_code(goal); get_correct_positions(goal); path_length = 0; i = 0; pq = pqueue_new(); visited = set_new(4); pqueue_insert(pq, root); set_insert(visited, grid_code(root)); while (!empty(pq)) { cur = pqueue_extract_min(pq); if (verbose) { fprintf(output, "%d.\n", ++i); fprintf(output, "Depth: %d\n", cur->depth); fprintf(output, "Grid:\n"); grid_print(output, cur); fprintf(output, "f: %2d\n", weight(cur)); fprintf(output, "\n"); } if (grid_code(cur) == goal_grid_code) break; ch = 0; #define ADD_CHILD() { \ child_code = grid_code(child); \ if (!set_contains(visited, child_code)) { \ set_insert(visited, child_code); \ pqueue_insert(pq, child); \ cur->child[ch++] = child; \ } else \ free(child); \ } /* Hole not on the left wall. */ if (cur->hole % 3 > 0) { child = make_child(cur); grid_move_hole(child, child->hole - 1); ADD_CHILD(); } /* Hole not on the right wall. */ if (cur->hole % 3 < 2) { child = make_child(cur); grid_move_hole(child, child->hole + 1); ADD_CHILD(); } /* Hole not on the top wall. */ if (cur->hole / 3 > 0) { child = make_child(cur); grid_move_hole(child, child->hole - 3); ADD_CHILD(); } /* Hole not on the bottom wall. */ if (cur->hole / 3 < 2) { child = make_child(cur); grid_move_hole(child, child->hole + 3); ADD_CHILD(); } #undef ADD_CHILD /* End of children character. */ cur->child[ch] = NULL; if (verbose) { fprintf(output, "Children:\n"); grid_children(output, cur); fprintf(output, "------------------------\n"); fprintf(output, "\n"); STEP(); } } if (grid_code(cur) != goal_grid_code) return 0; /* Collect result path. */ for (iter = cur; iter != NULL; iter = iter->parent) path_length ++; result = (Grid**) malloc(sizeof(Grid*) * path_length); i = path_length - 1; for (iter = cur; iter != NULL; iter = iter->parent) result[i--] = iter; if (verbose) fprintf(output, "Solution sequence:\n"); for (i = 0; i < path_length; i++) { grid_print(output, result[i]); STEP(); fprintf(output, "\n"); } /* Clean up. */ grid_dispose(root); set_dispose(visited); pqueue_dispose(pq); free(result); free(goal); return 1; }