From 7b5d7f76ccd2b17bb721a57ba4762dd3aa17db0f Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Tue, 25 Apr 2017 21:24:43 -0400 Subject: [PATCH] Add bt_clock_class_priority_map_copy() MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Because bt_notification_event_create() and bt_notification_inactivity_create() freeze their clock class priority map on success, this utility should prove useful for components which need to dynamically add clock classes. Signed-off-by: Philippe Proulx Signed-off-by: Jérémie Galarneau --- .../graph/clock-class-priority-map-internal.h | 2 +- .../graph/clock-class-priority-map.h | 21 ++++++++++++ lib/graph/clock-class-priority-map.c | 34 +++++++++++++++++++ tests/lib/test_cc_prio_map.c | 23 +++++++++++-- 4 files changed, 77 insertions(+), 3 deletions(-) diff --git a/include/babeltrace/graph/clock-class-priority-map-internal.h b/include/babeltrace/graph/clock-class-priority-map-internal.h index 3cba2bb9..04942145 100644 --- a/include/babeltrace/graph/clock-class-priority-map-internal.h +++ b/include/babeltrace/graph/clock-class-priority-map-internal.h @@ -40,7 +40,7 @@ struct bt_clock_class_priority_map { /* Array of struct bt_ctf_clock_class *, owned by this */ GPtrArray *entries; - /* struct bt_ctf_clock_class * (weak) to priority (uint64_t) */ + /* struct bt_ctf_clock_class * (weak) to priority (uint64_t *) */ GHashTable *prios; /* Clock class (weak) with the currently highest priority */ diff --git a/include/babeltrace/graph/clock-class-priority-map.h b/include/babeltrace/graph/clock-class-priority-map.h index 575e31c1..95d3168b 100644 --- a/include/babeltrace/graph/clock-class-priority-map.h +++ b/include/babeltrace/graph/clock-class-priority-map.h @@ -280,6 +280,27 @@ extern int bt_clock_class_priority_map_add_clock_class( struct bt_clock_class_priority_map *clock_class_priority_map, struct bt_ctf_clock_class *clock_class, uint64_t priority); +/** +@brief Creates a copy of the clock class priority map + \p clock_class_priority_map. + +You can copy a frozen clock class priority map: the resulting copy is +not frozen. + +@param[in] clock_class_priority_map Clock class priority map to copy. +@returns Copy of \p clock_class_priority_map + on success, or a negative value + on error. + +@prenotnull{clock_class_priority_map} +@postrefcountsame{clock_class_priority_map} +@postsuccessrefcountret1 +@post On success, the returned clock class priority map + is not frozen. +*/ +extern struct bt_clock_class_priority_map *bt_clock_class_priority_map_copy( + struct bt_clock_class_priority_map *clock_class_priority_map); + /** @} */ #ifdef __cplusplus diff --git a/lib/graph/clock-class-priority-map.c b/lib/graph/clock-class-priority-map.c index 136ed276..2d4fac11 100644 --- a/lib/graph/clock-class-priority-map.c +++ b/lib/graph/clock-class-priority-map.c @@ -265,3 +265,37 @@ end: return ret; } + +struct bt_clock_class_priority_map *bt_clock_class_priority_map_copy( + struct bt_clock_class_priority_map *orig_cc_prio_map) +{ + struct bt_clock_class_priority_map *cc_prio_map; + size_t i; + + cc_prio_map = bt_clock_class_priority_map_create(); + if (!cc_prio_map) { + goto error; + } + + for (i = 0; i < orig_cc_prio_map->entries->len; i++) { + struct bt_ctf_clock_class *clock_class = + g_ptr_array_index(orig_cc_prio_map->entries, i); + uint64_t *prio = g_hash_table_lookup(orig_cc_prio_map->prios, + clock_class); + int ret = bt_clock_class_priority_map_add_clock_class( + cc_prio_map, clock_class, *prio); + + if (ret) { + goto error; + } + } + + cc_prio_map->highest_prio_cc = orig_cc_prio_map->highest_prio_cc; + goto end; + +error: + BT_PUT(cc_prio_map); + +end: + return cc_prio_map; +} diff --git a/tests/lib/test_cc_prio_map.c b/tests/lib/test_cc_prio_map.c index c883815c..23305c2a 100644 --- a/tests/lib/test_cc_prio_map.c +++ b/tests/lib/test_cc_prio_map.c @@ -23,11 +23,12 @@ #include "tap/tap.h" -#define NR_TESTS 13 +#define NR_TESTS 18 static void test_clock_class_priority_map(void) { struct bt_clock_class_priority_map *cc_prio_map; + struct bt_clock_class_priority_map *cc_prio_map_copy; struct bt_ctf_clock_class *cc1; struct bt_ctf_clock_class *cc2; struct bt_ctf_clock_class *cc3; @@ -83,12 +84,30 @@ static void test_clock_class_priority_map(void) assert(ret == 0); ok(prio == 11, "bt_clock_class_priority_map_get_clock_class_priority() returns the expected priority (3)"); - + cc_prio_map_copy = bt_clock_class_priority_map_copy(cc_prio_map); + ok(cc_prio_map_copy, "bt_clock_class_priority_map_copy() succeeds"); + ret = bt_clock_class_priority_map_get_clock_class_priority(cc_prio_map_copy, cc1, &prio); + assert(ret == 0); + ok(prio == 1001, + "bt_clock_class_priority_map_get_clock_class_priority() returns the expected priority (1, copy)"); + ret = bt_clock_class_priority_map_get_clock_class_priority(cc_prio_map_copy, cc2, &prio); + assert(ret == 0); + ok(prio == 75, + "bt_clock_class_priority_map_get_clock_class_priority() returns the expected priority (2, copy)"); + ret = bt_clock_class_priority_map_get_clock_class_priority(cc_prio_map_copy, cc3, &prio); + assert(ret == 0); + ok(prio == 11, + "bt_clock_class_priority_map_get_clock_class_priority() returns the expected priority (3, copy)"); + cc = bt_clock_class_priority_map_get_highest_priority_clock_class(cc_prio_map_copy); + ok(cc == cc3, + "bt_clock_class_priority_map_get_highest_priority_clock_class() returns the expected clock class (3, copy)"); BT_PUT(cc); + BT_PUT(cc3); BT_PUT(cc2); BT_PUT(cc1); BT_PUT(cc_prio_map); + BT_PUT(cc_prio_map_copy); } int main(int argc, char **argv) -- 2.34.1