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 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 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) 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; }
void DTLS_RECORD_LAYER_free(RECORD_LAYER *rl) { DTLS_RECORD_LAYER_clear(rl); pqueue_free(rl->d->unprocessed_rcds.q); pqueue_free(rl->d->processed_rcds.q); pqueue_free(rl->d->buffered_app_data.q); OPENSSL_free(rl->d); rl->d = NULL; }
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); }
void tiz_pqueue_destroy (tiz_pqueue_t *p_q) { if (p_q) { assert (p_q->p_first == p_q->p_last); assert (p_q->p_first == NULL); assert (p_q->length == 0); pqueue_free (p_q->p_soa, p_q->pp_store); pqueue_free (p_q->p_soa, p_q); } }
void dtls1_free(SSL *s) { ssl3_free(s); dtls1_clear_queues(s); pqueue_free(s->d1->unprocessed_rcds.q); pqueue_free(s->d1->processed_rcds.q); pqueue_free(s->d1->buffered_messages); pqueue_free(s->d1->sent_messages); pqueue_free(s->d1->buffered_app_data.q); OPENSSL_free(s->d1); }
void dtls1_free(SSL *s) { DTLS_RECORD_LAYER_free(&s->rlayer); ssl3_free(s); dtls1_clear_queues(s); pqueue_free(s->d1->buffered_messages); pqueue_free(s->d1->sent_messages); OPENSSL_free(s->d1); s->d1 = NULL; }
void dtls1_free(SSL *s) { pitem *item = NULL; hm_fragment *frag = NULL; ssl3_free(s); while( (item = pqueue_pop(s->d1->unprocessed_rcds.q)) != NULL) { OPENSSL_free(item->data); pitem_free(item); } pqueue_free(s->d1->unprocessed_rcds.q); while( (item = pqueue_pop(s->d1->processed_rcds.q)) != NULL) { OPENSSL_free(item->data); pitem_free(item); } pqueue_free(s->d1->processed_rcds.q); while( (item = pqueue_pop(s->d1->buffered_messages)) != NULL) { frag = (hm_fragment *)item->data; OPENSSL_free(frag->fragment); OPENSSL_free(frag); pitem_free(item); } pqueue_free(s->d1->buffered_messages); while ( (item = pqueue_pop(s->d1->sent_messages)) != NULL) { frag = (hm_fragment *)item->data; OPENSSL_free(frag->fragment); OPENSSL_free(frag); pitem_free(item); } pqueue_free(s->d1->sent_messages); while ( (item = pqueue_pop(s->d1->buffered_app_data.q)) != NULL) { frag = (hm_fragment *)item->data; OPENSSL_free(frag->fragment); OPENSSL_free(frag); pitem_free(item); } pqueue_free(s->d1->buffered_app_data.q); OPENSSL_free(s->d1); }
void dtls1_free(SSL *s) { ssl3_free(s); if (s == NULL || s->d1 == NULL) { return; } dtls1_clear_queues(s); pqueue_free(s->d1->buffered_messages); pqueue_free(s->d1->sent_messages); OPENSSL_free(s->d1); s->d1 = NULL; }
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; }
void dtls1_free(SSL *s) { pitem *item = NULL; hm_fragment *frag = NULL; ssl3_free(s); while( (item = pqueue_pop(s->d1->unprocessed_rcds.q)) != NULL) { OPENSSL_free(item->data); pitem_free(item); } pqueue_free(s->d1->unprocessed_rcds.q); while( (item = pqueue_pop(s->d1->processed_rcds.q)) != NULL) { OPENSSL_free(item->data); pitem_free(item); } pqueue_free(s->d1->processed_rcds.q); while( (item = pqueue_pop(s->d1->buffered_messages)) != NULL) { frag = (hm_fragment *)item->data; OPENSSL_free(frag->fragment); OPENSSL_free(frag); pitem_free(item); } pqueue_free(s->d1->buffered_messages); while ( (item = pqueue_pop(s->d1->sent_messages)) != NULL) { frag = (hm_fragment *)item->data; OPENSSL_free(frag->fragment); OPENSSL_free(frag); pitem_free(item); } pqueue_free(s->d1->sent_messages); pq_64bit_free(&(s->d1->bitmap.map)); pq_64bit_free(&(s->d1->bitmap.max_seq_num)); pq_64bit_free(&(s->d1->next_bitmap.map)); pq_64bit_free(&(s->d1->next_bitmap.max_seq_num)); OPENSSL_free(s->d1); }
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); }
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); }
int main(void) { int i; int p; pqueue_t *pq; node_t *ns; node_t *n; /* We will need (N + 1) slots in "pris" vector. Extra one slot for spare * usages. */ pris = malloc(5 * sizeof(int *)); for (i = 0; i < 5; i++) pris[i] = malloc(2 * sizeof(int)); pris[0][0] = 4; pris[0][1] = 2; pris[1][0] = 3; pris[1][1] = 7; pris[2][0] = 3; pris[2][1] = 1; pris[3][0] = 5; pris[3][1] = 6; p = 4; /* Initialize spare slot. */ pq = pqueue_init(10, cmp_pri, get_pri, set_pri, get_pos, set_pos); ns = malloc(4 * sizeof(node_t)); ns[0].pri = 0; ns[0].val = 0; pqueue_insert(pq, &ns[0]); ns[1].pri = 1; ns[0].val = 1; pqueue_insert(pq, &ns[1]); ns[2].pri = 2; ns[0].val = 2; pqueue_insert(pq, &ns[2]); ns[3].pri = 3; ns[0].val = 3; pqueue_insert(pq, &ns[3]); printf("initial:\n"); pqueue_print(pq, stdout, pr_node); n = pqueue_pop(pq); printf("[pop] pri: %d, val: %d, real-pri: [%d %d]\n", n->pri, n->val, pris[n->pri][0], pris[n->pri][1]); printf("after first pop:\n"); pqueue_print(pq, stdout, pr_node); pris[p][0] = 3; pris[p][1] = 0; pqueue_change_priority(pq, p, &ns[3]); /* 3: (5,6) -> (3,0) */ p = 3; /* Move spare slot to 3. */ printf("after 3: (5,6) -> (3,0):\n"); pqueue_print(pq, stdout, pr_node); pris[p][0] = 3; pris[p][1] = -1; pqueue_change_priority(pq, p, &ns[0]); /* 0: (4,2) -> (3,-1) */ p = 0; /* Move spare slot to 0. */ printf("after 0: (4,2) -> (3,-1):\n"); pqueue_print(pq, stdout, pr_node); while ((n = pqueue_pop(pq))) printf("[pop] pri: %d, val: %d, real-pri: [%d %d]\n", n->pri, n->val, pris[n->pri][0], pris[n->pri][1]); pqueue_free(pq); free(ns); free(pris); return 0; }
void dtls1_free(SSL *s) { ssl3_free(s); dtls1_clear_queues(s); pqueue_free(s->d1->unprocessed_rcds.q); pqueue_free(s->d1->processed_rcds.q); pqueue_free(s->d1->buffered_messages); pqueue_free(s->d1->sent_messages); pqueue_free(s->d1->buffered_app_data.q); pq_64bit_free(&(s->d1->bitmap.map)); pq_64bit_free(&(s->d1->bitmap.max_seq_num)); pq_64bit_free(&(s->d1->next_bitmap.map)); pq_64bit_free(&(s->d1->next_bitmap.max_seq_num)); OPENSSL_free(s->d1); }
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; }
void dtls1_free(SSL *s) { if (s == NULL) return; ssl3_free(s); dtls1_clear_queues(s); pqueue_free(D1I(s)->unprocessed_rcds.q); pqueue_free(D1I(s)->processed_rcds.q); pqueue_free(D1I(s)->buffered_messages); pqueue_free(s->d1->sent_messages); pqueue_free(D1I(s)->buffered_app_data.q); explicit_bzero(s->d1->internal, sizeof(*s->d1->internal)); free(s->d1->internal); explicit_bzero(s->d1, sizeof(*s->d1)); free(s->d1); s->d1 = NULL; }
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); }
OMX_ERRORTYPE tiz_pqueue_init (tiz_pqueue_t ** pp_q, OMX_S32 a_max_prio, tiz_pq_cmp_f a_pf_cmp, tiz_soa_t * ap_soa, const char *ap_name) { tiz_pqueue_t *p_q = NULL; assert (pp_q != NULL); assert (a_max_prio >= 0); assert (a_pf_cmp != NULL); if (NULL == (p_q = (tiz_pqueue_t *) pqueue_calloc (ap_soa, sizeof (tiz_pqueue_t)))) { return OMX_ErrorInsufficientResources; } /* There is one pointer per priority category */ if (NULL == (p_q->pp_store = (tiz_pqueue_item_t **) pqueue_calloc (ap_soa, (size_t) (a_max_prio + 1) * sizeof (tiz_pqueue_item_t *)))) { pqueue_free (ap_soa, p_q); p_q = NULL; return OMX_ErrorInsufficientResources; } p_q->p_first = NULL; p_q->p_last = NULL; p_q->length = 0; p_q->max_prio = a_max_prio; p_q->pf_cmp = a_pf_cmp; p_q->p_soa = ap_soa; if (NULL != ap_name) { strncpy (p_q->name, ap_name, TIZ_PQUEUE_MAX_NAME_LEN); p_q->name[TIZ_PQUEUE_MAX_NAME_LEN - 1] = '\0'; } *pp_q = p_q; return OMX_ErrorNone; }
void pqueue_print(pqueue_t *q, FILE *out, pqueue_print_entry_f print) { pqueue_t *dup; void *e; dup = pqueue_init(q->size, q->cmppri, q->getpri, set_pri, q->getpos, set_pos); dup->size = q->size; dup->avail = q->avail; dup->step = q->step; memcpy(dup->d, q->d, (q->size * sizeof(void *))); while ((e = pqueue_pop(dup))) { print(out, e); } pqueue_free(dup); }
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() { pqueue_t* q = pqueue_create(); if (q == NULL) { fprintf(stderr, "Failed to create a pqueue.\n"); return EXIT_FAILURE; } size_t n = 100000; interval_bound_t* xs = malloc(n * sizeof(interval_bound_t)); memset(xs, 0, n * sizeof(interval_bound_t)); size_t i; for (i = 0; i < n; ++i) { xs[i].start_min = 100000.0 * ((double) rand() / (double) RAND_MAX); xs[i].end_max = xs[i].start_min + 1000.0 * ((double) rand() / (double) RAND_MAX); xs[i].x_max = (double) rand() / (double) RAND_MAX; pqueue_enqueue(q, &xs[i]); } sort_interval_bounds_asc(xs, n); interval_bound_t x; for (i = 0; i < n; ++i) { if (!pqueue_dequeue(q, &x)) { fprintf(stderr, "Pqueue was prematurely empty.\n"); return EXIT_FAILURE; } if (memcmp(&xs[i], &x, sizeof(interval_bound_t)) != 0) { fprintf(stderr, "Pop number %zu was incorrect.\n", i); return EXIT_FAILURE; } } pqueue_free(q); return EXIT_SUCCESS; }
void squeue_destroy(squeue_t *q, int flags) { unsigned int i; if (!q || pqueue_size(q) < 1) return; /* * Using two separate loops is a lot faster than * doing 1 cmp+branch for every queued item */ if (flags & SQUEUE_FREE_DATA) { for (i = 0; i < pqueue_size(q); i++) { free(((squeue_event *)q->d[i + 1])->data); free(q->d[i + 1]); } } else { for (i = 0; i < pqueue_size(q); i++) { free(q->d[i + 1]); } } pqueue_free(q); }
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; }
/** * Solve given instance of the knapsack problem using a branch and bound * approach. * TODO: the solution presented below represents a "rough" attempt and is not * in anyway optimized. * TODO: solution in terms of which items are picked along the optimal path * could easily be recovered by associating with each node a bitvector * encoding the decision made along the tree in order to arrive at that * particular node (e.g., for the node encoding that we did not take * the first item, but took the second and third items, the node's bit vector * would be equal to 011) */ static char * solve_knapsack_instance_bb(int n, int K, Item *items) { PQueue *pq; Node *u, *v; Item tmp; int maxvalue; char *sol = malloc(128 * sizeof(char)); pq = pqueue_init(n, node_get_bound); v = malloc(sizeof(Item)); if (!v) allocation_error(); v->level = -1; v->value = 0; v->weight = 0; pqueue_enqueue(pq, (void *) v); /* While priority queue is not empty ... */ while (pq->nElements > 1) { pqueue_dequeue(pq, (void **) &v, NULL); v->bound = bound(n, K, items, v); DEBUG_PRINT("maxvalue: %d\t v->bound: %f", maxvalue, v->bound); if (v->bound > maxvalue) { /* Set u to be child that includes next item. */ /* Better solution: preallocate and dynamically grow * array of Items to be used as nodes in search tree */ u = malloc(sizeof(Node)); if (!u) allocation_error(); u->level = v->level + 1; u->weight = v->weight + items[u->level].weight; u->value = v->value + items[u->level].value; if (u->weight <= K && u->value > maxvalue) maxvalue = u->value; u->bound = bound(n, K, items, u); if (u->bound > maxvalue) pqueue_enqueue(pq, (void *) u); else free(u); /* Set u to be child that does not include next item */ u = malloc(sizeof(Node)); if (!u) allocation_error(); u->level = v->level + 1; u->weight = v->weight; u->value = v->value; u->bound = bound(n, K, items, u); if (u->bound > maxvalue) pqueue_enqueue(pq, (void *) u); else free(u); free(v); } } pqueue_free(pq); sprintf(sol, "%d\n", maxvalue); return sol; }
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); }