SearchTree tree_empty(SearchTree tree) { if (tree != NULL) { tree->left = tree_empty(tree->left); tree->right = tree_empty(tree->right); free(tree); } return NULL; }
void tree_iterative_traverse(void (*func)(int), tree xs) { // {{{ if (!tree_empty(xs->left)) { tree_iterative_traverse(func, xs->left); } func(xs->value); if (!tree_empty(xs->right)) { tree_iterative_traverse(func, xs->right); } } // }}}
void tree_delete(tree xs) { // {{{ if (!tree_empty(xs->left)) { tree_delete(xs->left); } if (!tree_empty(xs->right)) { tree_delete(xs->right); } free(xs); } // }}}
bool tree_eq(tree a, tree b) { // {{{ // both empty if (tree_empty(a) && tree_empty(b)) return true; // one empty if (tree_empty(a) || tree_empty(b)) return false; // same value else if (a->value == b->value) { // check children as well return (tree_eq(a->left, b->left) && tree_eq(a->right, b->right)); } // different values return false; } // }}}
void tree_clear(bool (*func)(tree), tree xs) { // {{{ if (!tree_empty(xs->left)) { if (func(xs->left)) { tree_delete(xs->left); xs->left = NULL; } else { tree_clear(func, xs->left); } } if (!tree_empty(xs->right)) { if (func(xs->right)) { tree_delete(xs->right); xs->right = NULL; } else { tree_clear(func, xs->right); } } } // }}}
void tree_print(tree xs) { // {{{ if (tree_empty(xs)) { printf("null"); return; } printf("%i {", xs->value); tree_print(xs->left); printf(", "); tree_print(xs->right); printf("}"); } // }}}
void thread_free(thread_t * thread) { process_t * process = thread->process; assert(thread); assert(thread->references == 0); debug("Freed thread %s.", str_thread(thread)); free(thread); if (tree_empty(&process->threads)) process_del(process); process_put(process); /* the thread doesn't need the process pointer anymore */ }
static void loop() { alarm(10); while (likely(!signal_quit)) { #if 0 { /* sanity check */ void tc(thread_t * thread) { assert(thread->references == 2); } void pc(process_t * process) { assert(!tree_empty(&process->threads)); assert(process->references == 2 + tree_size(&process->threads)); thread_iter(process, tc); } process_iter(pc); }
static int queue_ram_envelope_delete(uint64_t evpid) { struct qr_envelope *evp; struct qr_message *msg; if ((msg = get_message(evpid_to_msgid(evpid))) == NULL) return (0); if ((evp = tree_pop(&msg->envelopes, evpid)) == NULL) { log_warnx("warn: queue-ram: not found"); return (0); } stat_decrement("queue.ram.envelope.size", evp->len); free(evp->buf); free(evp); if (tree_empty(&msg->envelopes)) { tree_xpop(&messages, evpid_to_msgid(evpid)); stat_decrement("queue.ram.message.size", msg->len); free(msg->buf); free(msg); } return (1); }
int tree_sum(tree xs) { // {{{ if (tree_empty(xs)) return 0; return xs->value + tree_sum(xs->left) + tree_sum(xs->right); } // }}}