lib: Add functions to borrow specialized component classes from specialized components
[babeltrace.git] / lib / util.c
1 /*
2 * Copyright (c) 2015-2018 Philippe Proulx <pproulx@efficios.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 #define BT_LOG_TAG "UTIL"
24 #include <babeltrace/lib-logging-internal.h>
25
26 #include <babeltrace/assert-pre-internal.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <string.h>
30 #include <inttypes.h>
31 #include <babeltrace/util.h>
32 #include <babeltrace/trace-ir/utils-internal.h>
33
34 bt_util_status bt_util_clock_cycles_to_ns_from_origin(uint64_t cycles,
35 uint64_t frequency, int64_t offset_seconds,
36 uint64_t offset_cycles, int64_t *ns)
37 {
38 bool overflows;
39 int64_t base_offset_ns;
40 bt_util_status status = BT_UTIL_STATUS_OK;
41 int ret;
42
43 BT_ASSERT_PRE_NON_NULL(ns, "Nanoseconds (output)");
44 BT_ASSERT_PRE(frequency != UINT64_C(-1) && frequency != 0,
45 "Invalid frequency: freq=%" PRIu64, frequency);
46 BT_ASSERT_PRE(offset_cycles < frequency,
47 "Offset (cycles) is greater than frequency: "
48 "offset-cycles=%" PRIu64 ", freq=%" PRIu64,
49 offset_cycles, frequency);
50
51 overflows = bt_util_get_base_offset_ns(offset_seconds, offset_cycles,
52 frequency, &base_offset_ns);
53 if (overflows) {
54 status = BT_UTIL_STATUS_OVERFLOW;
55 goto end;
56 }
57
58 ret = bt_util_ns_from_origin_inline(base_offset_ns,
59 offset_seconds, offset_cycles,
60 frequency, cycles, ns);
61 if (ret) {
62 status = BT_UTIL_STATUS_OVERFLOW;
63 }
64
65 end:
66 return status;
67 }
This page took 0.030113 seconds and 4 git commands to generate.