void el_loop_free(eventloop_t *el) { el_context_free(el); minheap_free(el->timers); rr_free(el->events); rr_free(el->fired); rr_free(el); }
eventloop_t *el_loop_create(int size) { eventloop_t *el; int i; if ((el = rr_malloc(sizeof(*el))) == NULL) goto err; el->events = rr_malloc(sizeof(event_t)*size); el->fired = rr_malloc(sizeof(fired_event_t)*size); el->timers = minheap_create(1, sizeof(ev_timer_t), timer_cmp, timer_cpy, timer_swp); if (el->events == NULL || el->fired == NULL || el->timers == NULL) goto err; el->size = size; el->stop = 0; el->maxfd = -1; el->before_polling = NULL; if (el_context_create(el) == -1) goto err; for (i = 0; i < size; i++) el->events[i].mask = RR_EV_NONE; return el; err: if (el) { rr_free(el->events); rr_free(el->fired); minheap_free(el->timers); rr_free(el); } return NULL; }
int main() { int32_t i; u_char* ptr=NULL; const char *a="a", *b="b", *c="c", *d="d", *e="e", *f="f", *g="g"; minheap_t* heap = minheap_create(256); if (heap == NULL) { printf("malloc minheap memory error!\n"); } minheap_insert(heap, 34, (u_char *)a); printf("index:%d\n", (int)minheap_insert(heap, 25, (u_char*)b)); minheap_insert(heap, 89, (u_char *)c); minheap_insert(heap, 4, (u_char *)d); minheap_insert(heap, 12, (u_char *)e); minheap_insert(heap, 25, (u_char *)b); minheap_insert(heap, 10, (u_char *)g); for(i=0; i<minheap_count(heap); i++) { printf("index:%d, weight:%d, ptr:%s\n", i, heap->mh_nodes[i].mn_weight, (char *)heap->mh_nodes[i].mn_ptr); } ptr = minheap_pop(heap); printf("minheap pop ptr:%p\n", ptr); printf("minheap pop ptr:%s\n", ptr); for(i=0; i<minheap_count(heap); i++) { printf("index:%d, weight:%d, ptr:%s\n", i, heap->mh_nodes[i].mn_weight, (char *)heap->mh_nodes[i].mn_ptr); } minheap_free(heap); }
void exampleMinHeap() { // Use pooling for efficiency, if you don't want to use pooling // then comment out this line. pool_minheap(32); MinHeap* H = newMinHeap(31); minheap_add(H, 99, "99"); minheap_add(H, 45, "45"); minheap_add(H, 57, "57"); minheap_add(H, 12, "12"); minheap_add(H, 87, "87"); minheap_add(H, 42, "42"); minheap_add(H, 67, "67"); minheap_display(H, 2, &toString); printf("Pop: '%s'\n", (char*)minheap_popMin(H)); minheap_display(H, 2, &toString); printf("Pop: '%s'\n", (char*)minheap_popMin(H)); minheap_display(H, 2, &toString); printf("Update 45 to 91\n"); minheap_update(H, 45, 91); minheap_set(H, 91, "91"); minheap_display(H, 2, &toString); printf("Update 91 to 1\n"); minheap_update(H, 91, 1); minheap_set(H, 1, "1"); minheap_display(H, 2, &toString); printf("Add 50\n"); minheap_add(H, 50, "50"); minheap_display(H, 2, &toString); printf("Pop: '%s'\n", (char*)minheap_popMin(H)); minheap_display(H, 2, &toString); printf("Pop: '%s'\n", (char*)minheap_popMin(H)); printf("Pop: '%s'\n", (char*)minheap_popMin(H)); printf("Pop: '%s'\n", (char*)minheap_popMin(H)); printf("Pop: '%s'\n", (char*)minheap_popMin(H)); minheap_display(H, 2, &toString); minheap_clear(H); printf("\n"); // Build a much bigger heap int total = 21; int keys[] = {0, 23, 3, 6, 41, 17, 21, 8, 9, 68, 2, 1, 34, 29, 38, 11, 15, 16, 45, 65, 39}; char* items[] = {"0", "23", "3", "6", "41", "17", "21", "8", "9", "68", "2", "1", "34", "29", "38", "11", "15", "16", "45", "65", "39"}; while (--total >= 0) minheap_add(H, keys[total], items[total]); minheap_display(H, 2, &toString); printf("Popping.. "); while (!minheap_isEmpty(H)) printf("%s ", (char*)minheap_popMin(H)); printf("\n"); minheap_free(H); // If you're not using pooling this can be commented out. This will // free all pooled nodes from memory. Always call this at the end // of using any List. unpool_minheap(); }
void fts_iter_free(struct fts_iterator_t *it) { minheap_free(it->docs); rr_free(it); }
void freeHeapqObject(robj *o) { minheap_free((minheap_t *) o->ptr); }