From: Jérémie Galarneau Date: Wed, 2 Nov 2016 20:42:15 +0000 (-0400) Subject: Allow user data in heap compare function X-Git-Url: http://drtracing.org/?a=commitdiff_plain;h=352fe16f2e854d594af296955c6c26c9a8ba767d;p=deliverable%2Fbabeltrace.git Allow user data in heap compare function Signed-off-by: Jérémie Galarneau --- diff --git a/include/babeltrace/plugin/notification/heap-internal.h b/include/babeltrace/plugin/notification/heap-internal.h index 63a82e174..e21accbb3 100644 --- a/include/babeltrace/plugin/notification/heap-internal.h +++ b/include/babeltrace/plugin/notification/heap-internal.h @@ -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 */ diff --git a/include/babeltrace/plugin/notification/heap.h b/include/babeltrace/plugin/notification/heap.h index f56822c9c..7fde9eddb 100644 --- a/include/babeltrace/plugin/notification/heap.h +++ b/include/babeltrace/plugin/notification/heap.h @@ -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 diff --git a/lib/plugin-system/notification/heap.c b/lib/plugin-system/notification/heap.c index 1f9197211..c1226c9dd 100644 --- a/lib/plugin-system/notification/heap.c +++ b/lib/plugin-system/notification/heap.c @@ -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); diff --git a/tests/lib/test_bt_notification_heap.c b/tests/lib/test_bt_notification_heap.c index c200c6357..7fe538ade 100644 --- a/tests/lib/test_bt_notification_heap.c +++ b/tests/lib/test_bt_notification_heap.c @@ -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. */