+void *heap_insert(struct ptr_heap *heap, void *p)
+{
+ void **ptrs = heap->ptrs;
+ void *tmp = NULL;
+
+ if (heap->size < heap->max) {
+ /* Add the element to the end */
+ heap->ptrs[heap->size++] = p;
+ /* rebalance */
+ heapify(heap, 0);
+ return NULL;
+ }
+
+ /*
+ * Full. We need to replace the largest (if we are
+ * smaller or equal to this element).
+ */
+ if (heap->gt(ptrs[0], p)) {
+ tmp = ptrs[0];
+ ptrs[0] = p;
+ /* rebalance */
+ heapify(heap, 0);
+ } else {
+ tmp = p;
+ }
+ return tmp;
+}
+