Allow user data in heap compare function
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Wed, 2 Nov 2016 20:42:15 +0000 (16:42 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Sat, 27 May 2017 18:09:06 +0000 (14:09 -0400)
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
include/babeltrace/plugin/notification/heap-internal.h
include/babeltrace/plugin/notification/heap.h
lib/plugin-system/notification/heap.c
tests/lib/test_bt_notification_heap.c

index 63a82e174917134711c025c83131ae6e2f40628f..e21accbb3f08af9bdddd3a3ca4068e98c49ffbea 100644 (file)
@@ -35,6 +35,7 @@ struct bt_notification_heap {
        GPtrArray *ptrs;
        size_t count;
        bt_notification_time_compare_func compare;
+       void *compare_data;
 };
 
 #endif /* CTF_NOTIF_HEAP_INTERNAL_H */
index f56822c9cd25b806bf76970b8cce26bd0348889d..7fde9eddb8d3fd910d986aaae0d95bf72db5b724 100644 (file)
@@ -41,7 +41,8 @@
  * ordering over multiple runs.
  */
 typedef bool (*bt_notification_time_compare_func)(
-               struct bt_notification *a, struct bt_notification *b);
+               struct bt_notification *a, struct bt_notification *b,
+               void *user_data);
 
 /**
  * bt_notification_heap_create - create a new bt_notification heap.
@@ -51,7 +52,7 @@ typedef bool (*bt_notification_time_compare_func)(
  * Returns a new notification heap, NULL on error.
  */
 extern struct bt_notification_heap *bt_notification_heap_create(
-               bt_notification_time_compare_func comparator);
+               bt_notification_time_compare_func comparator, void *user_data);
 
 /**
  * bt_notification_heap_insert - insert an element into the heap
index 1f91972119dfc74f6f06d28f35af447b32feff9f..c1226c9dd5399b9dcecac5e097d593aa84f0cc7b 100644 (file)
@@ -43,7 +43,8 @@ void check_heap(struct bt_notification_heap *heap)
 
        for (i = 1; i < heap->count; i++) {
                assert(!heap->compare(g_ptr_array_index(heap->ptrs, i),
-                               g_ptr_array_index(heap->ptrs, 0)));
+                               g_ptr_array_index(heap->ptrs, 0),
+                               heap->compare_data));
        }
 }
 #else
@@ -114,12 +115,14 @@ void heapify(struct bt_notification_heap *heap, size_t i)
 
                l = left(i);
                r = right(i);
-               if (l < heap->count && heap->compare(ptrs[l], ptrs[i])) {
+               if (l < heap->count && heap->compare(ptrs[l], ptrs[i],
+                               heap->compare_data)) {
                        largest = l;
                } else {
                        largest = i;
                }
-               if (r < heap->count && heap->compare(ptrs[r], ptrs[largest])) {
+               if (r < heap->count && heap->compare(ptrs[r], ptrs[largest],
+                               heap->compare_data)) {
                        largest = r;
                }
                if (unlikely(largest == i)) {
@@ -172,7 +175,7 @@ void bt_notification_heap_destroy(struct bt_object *obj)
 }
 
 struct bt_notification_heap *bt_notification_heap_create(
-               bt_notification_time_compare_func comparator)
+               bt_notification_time_compare_func comparator, void *user_data)
 {
        struct bt_notification_heap *heap = NULL;
 
@@ -193,6 +196,7 @@ struct bt_notification_heap *bt_notification_heap_create(
        }
 
        heap->compare = comparator;
+       heap->compare_data = user_data;
 end:
        return heap;
 }
@@ -219,7 +223,8 @@ int bt_notification_heap_insert(struct bt_notification_heap *heap,
 
        ptrs = (struct bt_notification **) heap->ptrs->pdata;
        pos = heap->count - 1;
-       while (pos > 0 && heap->compare(notification, ptrs[parent(pos)])) {
+       while (pos > 0 && heap->compare(notification, ptrs[parent(pos)],
+                       heap->compare_data)) {
                /* Move parent down until we find the right spot. */
                ptrs[pos] = ptrs[parent(pos)];
                pos = parent(pos);
index c200c6357fd8003a654b9ed06f9723e8bfed2576..7fe538ade71bffe44980af35955c1b47334a7222 100644 (file)
@@ -71,7 +71,8 @@ error:
 }
 
 static
-bool compare_notifications(struct bt_notification *a, struct bt_notification *b)
+bool compare_notifications(struct bt_notification *a, struct bt_notification *b,
+               void *unused)
 {
        uint64_t val_a = ((struct dummy_notification *) a)->value;
        uint64_t val_b = ((struct dummy_notification *) b)->value;
@@ -91,7 +92,7 @@ int main(int argc, char **argv)
 
        /* Initialize tap harness before any tests */
        plan_tests(NR_TESTS);
-       heap = bt_notification_heap_create(compare_notifications);
+       heap = bt_notification_heap_create(compare_notifications, NULL);
        ok(heap, "Created a notification heap");
 
        /* Insert 10 000 notifications with random values. */
This page took 0.02681 seconds and 4 git commands to generate.