lib: rename include dir to babeltrace2
[babeltrace.git] / lib / prio_heap / prio_heap.c
index a37e64c89542d10f017f8da835e01272dab2e2ef..97a69ac4b86293ee4b1d989948be23e5a45fd66c 100644 (file)
@@ -1,6 +1,4 @@
 /*
- * prio_heap.c
- *
  * Static-sized priority heap containing pointers. Based on CLRS,
  * chapter 6.
  *
  *
  * The above copyright notice and this permission notice shall be included in
  * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
  */
 
-#include <babeltrace/prio_heap.h>
-#include <babeltrace/babeltrace-internal.h>
+#include <babeltrace2/prio-heap-internal.h>
+#include <babeltrace2/babeltrace-internal.h>
+#include <babeltrace2/assert-internal.h>
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
-#include <assert.h>
-
-#ifndef max_t
-#define max_t(type, a, b)      \
-       ((type) (a) > (type) (b) ? (type) (a) : (type) (b))
-#endif
 
 #ifdef DEBUG_HEAP
 void check_heap(const struct ptr_heap *heap)
@@ -38,7 +39,7 @@ void check_heap(const struct ptr_heap *heap)
                return;
 
        for (i = 1; i < heap->len; i++)
-               assert(!heap->gt(heap->ptrs[i], heap->ptrs[0]));
+               BT_ASSERT(!heap->gt(heap->ptrs[i], heap->ptrs[0]));
 }
 #endif
 
@@ -94,7 +95,7 @@ int heap_set_len(struct ptr_heap *heap, size_t new_len)
        return 0;
 }
 
-int heap_init(struct ptr_heap *heap, size_t alloc_len,
+int bt_heap_init(struct ptr_heap *heap, size_t alloc_len,
              int gt(void *a, void *b))
 {
        heap->ptrs = NULL;
@@ -103,12 +104,12 @@ int heap_init(struct ptr_heap *heap, size_t alloc_len,
        heap->gt = gt;
        /*
         * Minimum size allocated is 1 entry to ensure memory allocation
-        * never fails within heap_replace_max.
+        * never fails within bt_heap_replace_max.
         */
        return heap_grow(heap, max_t(size_t, 1, alloc_len));
 }
 
-void heap_free(struct ptr_heap *heap)
+void bt_heap_free(struct ptr_heap *heap)
 {
        free(heap->ptrs);
 }
@@ -139,7 +140,7 @@ static void heapify(struct ptr_heap *heap, size_t i)
        check_heap(heap);
 }
 
-void *heap_replace_max(struct ptr_heap *heap, void *p)
+void *bt_heap_replace_max(struct ptr_heap *heap, void *p)
 {
        void *res;
 
@@ -157,7 +158,7 @@ void *heap_replace_max(struct ptr_heap *heap, void *p)
        return res;
 }
 
-int heap_insert(struct ptr_heap *heap, void *p)
+int bt_heap_insert(struct ptr_heap *heap, void *p)
 {
        void **ptrs;
        size_t pos;
@@ -178,7 +179,7 @@ int heap_insert(struct ptr_heap *heap, void *p)
        return 0;
 }
 
-void *heap_remove(struct ptr_heap *heap)
+void *bt_heap_remove(struct ptr_heap *heap)
 {
        switch (heap->len) {
        case 0:
@@ -190,10 +191,10 @@ void *heap_remove(struct ptr_heap *heap)
        /* Shrink, replace the current max by previous last entry and heapify */
        heap_set_len(heap, heap->len - 1);
        /* len changed. previous last entry is at heap->len */
-       return heap_replace_max(heap, heap->ptrs[heap->len]);
+       return bt_heap_replace_max(heap, heap->ptrs[heap->len]);
 }
 
-void *heap_cherrypick(struct ptr_heap *heap, void *p)
+void *bt_heap_cherrypick(struct ptr_heap *heap, void *p)
 {
        size_t pos, len = heap->len;
 
@@ -214,3 +215,21 @@ found:
        heapify(heap, pos);
        return p;
 }
+
+int bt_heap_copy(struct ptr_heap *dst, struct ptr_heap *src)
+{
+       int ret;
+
+       ret = bt_heap_init(dst, src->alloc_len, src->gt);
+       if (ret < 0)
+               goto end;
+
+       ret = heap_set_len(dst, src->len);
+       if (ret < 0)
+               goto end;
+
+       memcpy(dst->ptrs, src->ptrs, src->len * sizeof(void *));
+
+end:
+       return ret;
+}
This page took 0.025573 seconds and 4 git commands to generate.