void isc_heap_destroy(isc_heap_t **heapp) { isc_heap_t *heap; REQUIRE(heapp != NULL); heap = *heapp; REQUIRE(VALID_HEAP(heap)); if (heap->array != NULL) isc_mem_put(heap->mctx, heap->array, heap->size * sizeof(void *)); heap->magic = 0; isc_mem_putanddetach(&heap->mctx, heap, sizeof(*heap)); *heapp = NULL; }
isc_result_t isc_heap_insert(isc_heap_t *heap, void *elt) { unsigned int new_last; REQUIRE(VALID_HEAP(heap)); new_last = heap->last + 1; RUNTIME_CHECK(new_last > 0); /* overflow check */ if (new_last >= heap->size && !resize(heap)) return (ISC_R_NOMEMORY); heap->last = new_last; float_up(heap, new_last, elt); return (ISC_R_SUCCESS); }
void isc_heap_delete(isc_heap_t *heap, unsigned int index) { void *elt; isc_boolean_t less; REQUIRE(VALID_HEAP(heap)); REQUIRE(index >= 1 && index <= heap->last); if (index == heap->last) { heap->last--; } else { elt = heap->array[heap->last--]; less = heap->compare(elt, heap->array[index]); heap->array[index] = elt; if (less) float_up(heap, index, heap->array[index]); else sink_down(heap, index, heap->array[index]); } }
static isc_boolean_t resize(isc_heap_t *heap) { void **new_array; size_t new_size; REQUIRE(VALID_HEAP(heap)); new_size = heap->size + heap->size_increment; new_array = isc_mem_get(heap->mctx, new_size * sizeof(void *)); if (new_array == NULL) return (ISC_FALSE); if (heap->array != NULL) { memcpy(new_array, heap->array, heap->size * sizeof(void *)); isc_mem_put(heap->mctx, heap->array, heap->size * sizeof(void *)); } heap->size = new_size; heap->array = new_array; return (ISC_TRUE); }