void cyclic_dump_node(cyc_cpu_t *cpu, cyc_index_t *heap, char **c, size_t w, int ndx, int l, int r, int depth) { int heap_left, heap_right; int me; int i, x = l + (r - l) / 2; size_t n = w - (x - 1); /* n bytes left for snprintf after c[][x - 1] */ heap_left = CYC_HEAP_LEFT(ndx); heap_right = CYC_HEAP_RIGHT(ndx); me = heap[ndx]; if (ndx >= cpu->cyp_nelems) return; if (me < 10) { (void) mdb_snprintf(&c[depth][x - 1], n, " %d", me); } else if (me >= 100) { (void) mdb_snprintf(&c[depth][x - 1], n, "%3d", me); } else { (void) mdb_snprintf(&c[depth][x - 1], n, "%s%2d%s", CYC_HEAP_LEFT(CYC_HEAP_PARENT(ndx)) == ndx ? " " : "", me, CYC_HEAP_LEFT(CYC_HEAP_PARENT(ndx)) == ndx ? "" : " "); } if (r - l > 5) { c[++depth][x] = '|'; depth++; for (i = l + (r - l) / 4; i < r - (r - l) / 4; i++) c[depth][i] = '-'; c[depth][l + (r - l) / 4] = '+'; c[depth][r - (r - l) / 4 - 1] = '+'; c[depth][x] = '+'; } else { if (heap_left >= cpu->cyp_nelems) return; (void) mdb_snprintf(&c[++depth][x - 1], n, "L%d", heap[heap_left]); if (heap_right >= cpu->cyp_nelems) return; (void) mdb_snprintf(&c[++depth][x - 1], n, "R%d", heap[heap_right]); return; } if (heap_left < cpu->cyp_nelems) cyclic_dump_node(cpu, heap, c, w, heap_left, l, x, depth + 1); if (heap_right < cpu->cyp_nelems) cyclic_dump_node(cpu, heap, c, w, heap_right, x, r, depth + 1); }
static void cyclic_downheap(cyc_cpu_t *cpu, cyc_index_t ndx) { cyclic_t *cyclics = cpu->cyp_cyclics; cyc_index_t *heap = cpu->cyp_heap; cyc_index_t heap_left, heap_right, heap_me = ndx; cyc_index_t left, right, me; cyc_index_t nelems = cpu->cyp_nelems; for (;;) { /* * If we don't have a left child (i.e., we're a leaf), we're * done. */ if ((heap_left = CYC_HEAP_LEFT(heap_me)) >= nelems) return; left = heap[heap_left]; me = heap[heap_me]; heap_right = CYC_HEAP_RIGHT(heap_me); /* * Even if we don't have a right child, we still need to compare * our expiration time against that of our left child. */ if (heap_right >= nelems) goto comp_left; right = heap[heap_right]; /* * We have both a left and a right child. We need to compare * the expiration times of the children to determine which * expires earlier. */ if (cyclics[right].cy_expire < cyclics[left].cy_expire) { /* * Our right child is the earlier of our children. * We'll now compare our expiration time to its; if * ours is the earlier, we're done. */ if (cyclics[me].cy_expire <= cyclics[right].cy_expire) return; /* * Our right child expires earlier than we do; swap * with our right child, and descend right. */ heap[heap_right] = me; heap[heap_me] = right; heap_me = heap_right; continue; } comp_left: /* * Our left child is the earlier of our children (or we have * no right child). We'll now compare our expiration time * to its; if ours is the earlier, we're done. */ if (cyclics[me].cy_expire <= cyclics[left].cy_expire) return; /* * Our left child expires earlier than we do; swap with our * left child, and descend left. */ heap[heap_left] = me; heap[heap_me] = left; heap_me = heap_left; } }