lib: add unsigned and signed integer value API
[babeltrace.git] / include / babeltrace / common-internal.h
CommitLineData
1670bffd
PP
1#ifndef BABELTRACE_COMMON_INTERNAL_H
2#define BABELTRACE_COMMON_INTERNAL_H
3
d94d92ac
PP
4/*
5 * Copyright (c) 2018 EfficiOS Inc. and Linux Foundation
6 * Copyright (c) 2018 Philippe Proulx <pproulx@efficios.com>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26
c55a9f58 27#include <stdbool.h>
3dca2276 28#include <babeltrace/assert-internal.h>
1670bffd 29#include <babeltrace/babeltrace-internal.h>
c6bd8523 30#include <babeltrace/trace-ir/field-class-const.h>
40f4ba76
PP
31#include <babeltrace/trace-ir/field-path-const.h>
32#include <babeltrace/trace-ir/event-class-const.h>
bfa3b6d8 33#include <babeltrace/graph/self-message-iterator.h>
c6bd8523 34#include <babeltrace/value.h>
44c440bc 35#include <inttypes.h>
282b829b 36#include <stdarg.h>
44c440bc 37#include <stdint.h>
282b829b 38#include <unistd.h>
3dca2276 39#include <glib.h>
1670bffd 40
ad96d936
PP
41#define BT_COMMON_COLOR_RESET "\033[0m"
42#define BT_COMMON_COLOR_BOLD "\033[1m"
43#define BT_COMMON_COLOR_FG_DEFAULT "\033[39m"
44#define BT_COMMON_COLOR_FG_RED "\033[31m"
45#define BT_COMMON_COLOR_FG_GREEN "\033[32m"
46#define BT_COMMON_COLOR_FG_YELLOW "\033[33m"
47#define BT_COMMON_COLOR_FG_BLUE "\033[34m"
48#define BT_COMMON_COLOR_FG_MAGENTA "\033[35m"
49#define BT_COMMON_COLOR_FG_CYAN "\033[36m"
50#define BT_COMMON_COLOR_FG_LIGHT_GRAY "\033[37m"
51#define BT_COMMON_COLOR_BG_DEFAULT "\033[49m"
52#define BT_COMMON_COLOR_BG_RED "\033[41m"
53#define BT_COMMON_COLOR_BG_GREEN "\033[42m"
54#define BT_COMMON_COLOR_BG_YELLOW "\033[43m"
55#define BT_COMMON_COLOR_BG_BLUE "\033[44m"
56#define BT_COMMON_COLOR_BG_MAGENTA "\033[45m"
57#define BT_COMMON_COLOR_BG_CYAN "\033[46m"
58#define BT_COMMON_COLOR_BG_LIGHT_GRAY "\033[47m"
59
db0f160a
PP
60struct bt_common_lttng_live_url_parts {
61 GString *proto;
62 GString *hostname;
63 GString *target_hostname;
64 GString *session_name;
65
66 /* -1 means default port */
67 int port;
68};
69
6eecdc5f
PP
70/*
71 * Checks if the current process has setuid or setgid access rights.
72 * Returns `true` if so.
73 */
1670bffd
PP
74BT_HIDDEN
75bool bt_common_is_setuid_setgid(void);
76
6eecdc5f
PP
77/*
78 * Returns the system-wide plugin path, e.g.
79 * `/usr/lib/babeltrace/plugins`. Do not free the return value.
80 */
1670bffd
PP
81BT_HIDDEN
82const char *bt_common_get_system_plugin_path(void);
83
6eecdc5f
PP
84/*
85 * Returns the user plugin path, e.g.
86 * `/home/user/.local/lib/babeltrace/plugins`. You need to free the
87 * return value.
88 */
1670bffd
PP
89BT_HIDDEN
90char *bt_common_get_home_plugin_path(void);
91
6eecdc5f
PP
92/*
93 * Appends the list of directories in `paths` to the array `dirs`.
94 * `paths` is a list of directories separated by `:`. Returns 0 on
95 * success.
96 */
1670bffd
PP
97BT_HIDDEN
98int bt_common_append_plugin_path_dirs(const char *paths, GPtrArray *dirs);
99
6eecdc5f
PP
100/*
101 * Returns `true` if terminal color codes are supported for this
102 * process.
103 */
ad96d936
PP
104BT_HIDDEN
105bool bt_common_colors_supported(void);
106
290725f7
PP
107BT_HIDDEN
108const char *bt_common_color_reset(void);
109
110BT_HIDDEN
111const char *bt_common_color_bold(void);
112
113BT_HIDDEN
114const char *bt_common_color_fg_default(void);
115
116BT_HIDDEN
117const char *bt_common_color_fg_red(void);
118
119BT_HIDDEN
120const char *bt_common_color_fg_green(void);
121
122BT_HIDDEN
123const char *bt_common_color_fg_yellow(void);
124
125BT_HIDDEN
126const char *bt_common_color_fg_blue(void);
127
128BT_HIDDEN
129const char *bt_common_color_fg_magenta(void);
130
131BT_HIDDEN
132const char *bt_common_color_fg_cyan(void);
133
134BT_HIDDEN
135const char *bt_common_color_fg_light_gray(void);
136
137BT_HIDDEN
138const char *bt_common_color_bg_default(void);
139
140BT_HIDDEN
141const char *bt_common_color_bg_red(void);
142
143BT_HIDDEN
144const char *bt_common_color_bg_green(void);
145
146BT_HIDDEN
147const char *bt_common_color_bg_yellow(void);
148
149BT_HIDDEN
150const char *bt_common_color_bg_blue(void);
151
152BT_HIDDEN
153const char *bt_common_color_bg_magenta(void);
154
155BT_HIDDEN
156const char *bt_common_color_bg_cyan(void);
157
158BT_HIDDEN
159const char *bt_common_color_bg_light_gray(void);
160
6eecdc5f
PP
161/*
162 * Returns the substring from `input` to the first character found
163 * in the list of characters `end_chars`, unescaping any character
164 * found in `escapable_chars`, and sets `*end_pos` to the position of
165 * the end (from `input`). The caller owns the returned GString.
166 */
db0f160a
PP
167BT_HIDDEN
168GString *bt_common_string_until(const char *input, const char *escapable_chars,
169 const char *end_chars, size_t *end_pos);
170
6eecdc5f
PP
171/*
172 * Returns the quoted version of `input` for a shell. If
173 * `with_single_quotes` is `true`, prepends and appends the `'` prefix
174 * and suffix to the returned string; otherwise the caller should
175 * prepend and append them manually, although they are not always
176 * required. The caller owns the returned GString.
177 */
db0f160a 178BT_HIDDEN
36b405c6 179GString *bt_common_shell_quote(const char *input, bool with_single_quotes);
db0f160a 180
6eecdc5f
PP
181/*
182 * Returns `true` if `input` is a string made only of printable
183 * characters.
184 */
db0f160a
PP
185BT_HIDDEN
186bool bt_common_string_is_printable(const char *input);
187
6eecdc5f
PP
188/*
189 * Destroys the parts of an LTTng live URL as returned by
190 * bt_common_parse_lttng_live_url().
191 */
db0f160a
PP
192BT_HIDDEN
193void bt_common_destroy_lttng_live_url_parts(
194 struct bt_common_lttng_live_url_parts *parts);
195
6eecdc5f
PP
196/*
197 * Parses the LTTng live URL `url` and returns its different parts.
198 * If there's an error, writes the error message into `*error_buf`
199 * up to `error_buf_size` bytes. You must destroy the returned value
200 * with bt_common_destroy_lttng_live_url_parts().
201 */
db0f160a
PP
202BT_HIDDEN
203struct bt_common_lttng_live_url_parts bt_common_parse_lttng_live_url(
204 const char *url, char *error_buf, size_t error_buf_size);
205
6eecdc5f
PP
206/*
207 * Normalizes (in place) a star globbing pattern to be used with
208 * bt_common_star_glob_match(). This function always succeeds.
209 */
9009cc24
PP
210BT_HIDDEN
211void bt_common_normalize_star_glob_pattern(char *pattern);
212
6eecdc5f
PP
213/*
214 * Returns `true` if `candidate` (of size `candidate_len`) matches
215 * the star globbing pattern `pattern` (of size `pattern_len`).
216 */
9009cc24
PP
217BT_HIDDEN
218bool bt_common_star_glob_match(const char *pattern, size_t pattern_len,
219 const char *candidate, size_t candidate_len);
220
6eecdc5f
PP
221/*
222 * Normalizes the path `path`:
223 *
224 * * If it's a relative path, converts it to an absolute path using
225 * `wd` as the working directory (or the current working directory
226 * if `wd` is NULL).
227 * * Removes consecutive and trailing slashes.
228 * * Resolves `..` and `.` in the path (both in `path` and in `wd`).
229 * * Does NOT resolve symbolic links.
230 *
231 * The caller owns the returned GString.
232 */
e49a18d1
PP
233BT_HIDDEN
234GString *bt_common_normalize_path(const char *path, const char *wd);
235
85cd02cf
PP
236typedef void (* bt_common_handle_custom_specifier_func)(void *priv_data,
237 char **buf, size_t avail_size, const char **fmt, va_list *args);
238
239/*
240 * This is a custom vsnprintf() which handles the standard conversion
241 * specifier as well as custom ones.
242 *
243 * `fmt` is a typical printf()-style format string, with the following
244 * limitations:
245 *
246 * * The `*` width specifier is not accepted.
247 * * The `*` precision specifier is not accepted.
248 * * The `j` and `t` length modifiers are not accepted.
249 * * The `n` format specifier is not accepted.
250 * * The format specifiers defined in <inttypes.h> are not accepted
251 * except for `PRId64`, `PRIu64`, `PRIx64`, `PRIX64`, `PRIo64`, and
252 * `PRIi64`.
253 *
254 * `intro` specifies which special character immediately following an
255 * introductory `%` character in `fmt` is used to indicate a custom
256 * conversion specifier. For example, if `intro` is '@', then any `%@`
257 * sequence in `fmt` is the beginning of a custom conversion specifier.
258 *
259 * When a custom conversion specifier is encountered in `fmt`,
260 * the function calls `handle_specifier`. This callback receives:
261 *
262 * `priv_data`:
263 * Custom, private data.
264 *
265 * `buf`:
266 * Address of the current buffer pointer. `*buf` is the position to
267 * append new data. The callback must update `*buf` when appending
268 * new data. The callback must ensure not to write passed the whole
269 * buffer passed to bt_common_custom_vsnprintf().
270 *
271 * `avail_size`:
272 * Number of bytes left in whole buffer from the `*buf` point.
273 *
274 * `fmt`:
275 * Address of the current format string pointer. `*fmt` points to
276 * the introductory `%` character, which is followed by the
277 * character `intro`. The callback must update `*fmt` so that it
278 * points after the whole custom conversion specifier.
279 *
280 * `args`:
281 * Variable argument list. Use va_arg() to get new arguments from
282 * this list and update it at the same time.
283 *
284 * Because this is an internal utility, this function and its callback
285 * do not return error codes: they abort when there's any error (bad
286 * format string, for example).
287 */
288BT_HIDDEN
289void bt_common_custom_vsnprintf(char *buf, size_t buf_size,
290 char intro,
291 bt_common_handle_custom_specifier_func handle_specifier,
292 void *priv_data, const char *fmt, va_list *args);
293
294/*
295 * Variadic form of bt_common_custom_vsnprintf().
296 */
297BT_HIDDEN
298void bt_common_custom_snprintf(char *buf, size_t buf_size,
299 char intro,
300 bt_common_handle_custom_specifier_func handle_specifier,
301 void *priv_data, const char *fmt, ...);
302
108e5a1e 303/*
85cd02cf 304 * Returns the system page size.
108e5a1e
MJ
305 */
306BT_HIDDEN
307size_t bt_common_get_page_size(void);
308
282b829b
FD
309/*
310 * Wraps read() function to handle EINTR and partial reads.
311 * On success, it returns `count` received as parameter. On error, it returns a
312 * value smaller than the requested `count`.
313 */
314static inline
315ssize_t bt_common_read(int fd, void *buf, size_t count)
316{
317 size_t i = 0;
318 ssize_t ret;
319
320 BT_ASSERT(buf);
321
322 /* Never return an overflow value. */
323 BT_ASSERT(count <= SSIZE_MAX);
324
325 do {
326 ret = read(fd, buf + i, count - i);
327 if (ret < 0) {
328 if (errno == EINTR) {
329#ifdef BT_LOGD_STR
330 BT_LOGD_STR("read() call interrupted. Retrying...");
331#endif
332 /* retry operation */
333 continue;
334 } else {
335#ifdef BT_LOGE_ERRNO
336 BT_LOGE_ERRNO("Error while reading", ": fd=%d",
337 fd);
338#endif
339 goto end;
340 }
341 }
342 i += ret;
343 BT_ASSERT(i <= count);
344 } while (count - i > 0 && ret > 0);
345
346end:
347 if (ret >= 0) {
348 if (i == 0) {
349 ret = -1;
350 } else {
351 ret = i;
352 }
353 }
354
355 return ret;
356}
357
3dca2276 358static inline
864cad70 359const char *bt_common_field_class_type_string(enum bt_field_class_type class_type)
3dca2276 360{
864cad70
PP
361 switch (class_type) {
362 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER:
363 return "BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER";
364 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER:
365 return "BT_FIELD_CLASS_TYPE_SIGNED_INTEGER";
366 case BT_FIELD_CLASS_TYPE_REAL:
367 return "BT_FIELD_CLASS_TYPE_REAL";
368 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION:
369 return "BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION";
370 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION:
371 return "BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION";
372 case BT_FIELD_CLASS_TYPE_STRING:
373 return "BT_FIELD_CLASS_TYPE_STRING";
374 case BT_FIELD_CLASS_TYPE_STRUCTURE:
375 return "BT_FIELD_CLASS_TYPE_STRUCTURE";
376 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
377 return "BT_FIELD_CLASS_TYPE_STATIC_ARRAY";
378 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY:
379 return "BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY";
380 case BT_FIELD_CLASS_TYPE_VARIANT:
381 return "BT_FIELD_CLASS_TYPE_VARIANT";
3dca2276
PP
382 default:
383 return "(unknown)";
384 }
385};
386
387static inline
5cd6d0e5 388const char *bt_common_field_class_integer_preferred_display_base_string(enum bt_field_class_integer_preferred_display_base base)
3dca2276
PP
389{
390 switch (base) {
5cd6d0e5
PP
391 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY:
392 return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY";
393 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL:
394 return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL";
395 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL:
396 return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL";
397 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL:
398 return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL";
3dca2276
PP
399 default:
400 return "(unknown)";
401 }
402}
403
404static inline
405const char *bt_common_scope_string(enum bt_scope scope)
406{
407 switch (scope) {
44c440bc
PP
408 case BT_SCOPE_PACKET_CONTEXT:
409 return "BT_SCOPE_PACKET_CONTEXT";
44c440bc
PP
410 case BT_SCOPE_EVENT_COMMON_CONTEXT:
411 return "BT_SCOPE_EVENT_COMMON_CONTEXT";
412 case BT_SCOPE_EVENT_SPECIFIC_CONTEXT:
413 return "BT_SCOPE_EVENT_SPECIFIC_CONTEXT";
3dca2276
PP
414 case BT_SCOPE_EVENT_PAYLOAD:
415 return "BT_SCOPE_EVENT_PAYLOAD";
3dca2276
PP
416 default:
417 return "(unknown)";
418 }
419}
420
421static inline
422const char *bt_common_event_class_log_level_string(
423 enum bt_event_class_log_level level)
424{
425 switch (level) {
3dca2276
PP
426 case BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY:
427 return "BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY";
428 case BT_EVENT_CLASS_LOG_LEVEL_ALERT:
429 return "BT_EVENT_CLASS_LOG_LEVEL_ALERT";
430 case BT_EVENT_CLASS_LOG_LEVEL_CRITICAL:
431 return "BT_EVENT_CLASS_LOG_LEVEL_CRITICAL";
432 case BT_EVENT_CLASS_LOG_LEVEL_ERROR:
433 return "BT_EVENT_CLASS_LOG_LEVEL_ERROR";
434 case BT_EVENT_CLASS_LOG_LEVEL_WARNING:
435 return "BT_EVENT_CLASS_LOG_LEVEL_WARNING";
436 case BT_EVENT_CLASS_LOG_LEVEL_NOTICE:
437 return "BT_EVENT_CLASS_LOG_LEVEL_NOTICE";
438 case BT_EVENT_CLASS_LOG_LEVEL_INFO:
439 return "BT_EVENT_CLASS_LOG_LEVEL_INFO";
440 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM:
441 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM";
442 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM:
443 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM";
444 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS:
445 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS";
446 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE:
447 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE";
448 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT:
449 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT";
450 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION:
451 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION";
452 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE:
453 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE";
454 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG:
455 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG";
456 default:
457 return "(unknown)";
458 }
459};
460
da91b29a
PP
461static inline
462const char *bt_common_value_type_string(enum bt_value_type type)
463{
464 switch (type) {
465 case BT_VALUE_TYPE_NULL:
466 return "BT_VALUE_TYPE_NULL";
467 case BT_VALUE_TYPE_BOOL:
468 return "BT_VALUE_TYPE_BOOL";
fdd3a2da
PP
469 case BT_VALUE_TYPE_UNSIGNED_INTEGER:
470 return "BT_VALUE_TYPE_UNSIGNED_INTEGER";
471 case BT_VALUE_TYPE_SIGNED_INTEGER:
472 return "BT_VALUE_TYPE_SIGNED_INTEGER";
da91b29a
PP
473 case BT_VALUE_TYPE_REAL:
474 return "BT_VALUE_TYPE_REAL";
475 case BT_VALUE_TYPE_STRING:
476 return "BT_VALUE_TYPE_STRING";
477 case BT_VALUE_TYPE_ARRAY:
478 return "BT_VALUE_TYPE_ARRAY";
479 case BT_VALUE_TYPE_MAP:
480 return "BT_VALUE_TYPE_MAP";
481 default:
482 return "(unknown)";
483 }
484};
485
3dca2276 486static inline
bd1a54fe 487GString *bt_common_field_path_string(struct bt_field_path *path)
3dca2276
PP
488{
489 GString *str = g_string_new(NULL);
44c440bc 490 uint64_t i;
3dca2276
PP
491
492 BT_ASSERT(path);
493
494 if (!str) {
495 goto end;
496 }
497
498 g_string_append_printf(str, "[%s", bt_common_scope_string(
499 bt_field_path_get_root_scope(path)));
500
66ddcddf
PP
501 for (i = 0; i < bt_field_path_get_item_count(path); i++) {
502 const struct bt_field_path_item *fp_item =
503 bt_field_path_borrow_item_by_index_const(path, i);
504
505 switch (bt_field_path_item_get_type(fp_item)) {
506 case BT_FIELD_PATH_ITEM_TYPE_INDEX:
507 g_string_append_printf(str, ", %" PRIu64,
508 bt_field_path_item_index_get_index(fp_item));
509 break;
510 case BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT:
511 g_string_append(str, ", <CUR>");
512 break;
513 default:
514 abort();
515 }
3dca2276
PP
516 }
517
518 g_string_append(str, "]");
519
520end:
521 return str;
522}
523
bfa3b6d8 524static inline
bd1a54fe 525const char *bt_common_self_message_iterator_status_string(
bfa3b6d8
FD
526 enum bt_self_message_iterator_status status)
527{
528 switch (status) {
529 case BT_SELF_MESSAGE_ITERATOR_STATUS_AGAIN:
530 return "BT_SELF_MESSAGE_ITERATOR_STATUS_AGAIN";
531 case BT_SELF_MESSAGE_ITERATOR_STATUS_END:
532 return "BT_SELF_MESSAGE_ITERATOR_STATUS_END";
533 case BT_SELF_MESSAGE_ITERATOR_STATUS_OK:
534 return "BT_SELF_MESSAGE_ITERATOR_STATUS_OK";
535 case BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR:
536 return "BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR";
537 case BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM:
538 return "BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM";
539 default:
540 return "(unknown)";
541 }
542};
543
7de0e49a
PP
544#define NS_PER_S_I INT64_C(1000000000)
545#define NS_PER_S_U UINT64_C(1000000000)
546
547static inline
548int bt_common_clock_value_from_ns_from_origin(
549 int64_t cc_offset_seconds, uint64_t cc_offset_cycles,
550 uint64_t cc_freq, int64_t ns_from_origin,
551 uint64_t *raw_value)
552{
553 int ret = 0;
554 int64_t offset_in_ns;
555 uint64_t value_in_ns;
556 uint64_t rem_value_in_ns;
557 uint64_t value_periods;
558 uint64_t value_period_cycles;
559 int64_t ns_to_add;
560
561 BT_ASSERT(raw_value);
562
563 /* Compute offset part of requested value, in nanoseconds */
564 if (!bt_safe_to_mul_int64(cc_offset_seconds, NS_PER_S_I)) {
565 ret = -1;
566 goto end;
567 }
568
569 offset_in_ns = cc_offset_seconds * NS_PER_S_I;
570
571 if (cc_freq == NS_PER_S_U) {
572 ns_to_add = (int64_t) cc_offset_cycles;
573 } else {
574 if (!bt_safe_to_mul_int64((int64_t) cc_offset_cycles,
575 NS_PER_S_I)) {
576 ret = -1;
577 goto end;
578 }
579
580 ns_to_add = ((int64_t) cc_offset_cycles * NS_PER_S_I) /
581 (int64_t) cc_freq;
582 }
583
584 if (!bt_safe_to_add_int64(offset_in_ns, ns_to_add)) {
585 ret = -1;
586 goto end;
587 }
588
589 offset_in_ns += ns_to_add;
590
591 /* Value part in nanoseconds */
592 if (ns_from_origin < offset_in_ns) {
593 ret = -1;
594 goto end;
595 }
596
597 value_in_ns = (uint64_t) (ns_from_origin - offset_in_ns);
598
599 /* Number of whole clock periods in `value_in_ns` */
600 value_periods = value_in_ns / NS_PER_S_U;
601
602 /* Remaining nanoseconds in cycles + whole clock periods in cycles */
603 rem_value_in_ns = value_in_ns - value_periods * NS_PER_S_U;
604
605 if (value_periods > UINT64_MAX / cc_freq) {
606 ret = -1;
607 goto end;
608 }
609
610 if (!bt_safe_to_mul_uint64(value_periods, cc_freq)) {
611 ret = -1;
612 goto end;
613 }
614
615 value_period_cycles = value_periods * cc_freq;
616
617 if (!bt_safe_to_mul_uint64(cc_freq, rem_value_in_ns)) {
618 ret = -1;
619 goto end;
620 }
621
622 if (!bt_safe_to_add_uint64(cc_freq * rem_value_in_ns / NS_PER_S_U,
623 value_period_cycles)) {
624 ret = -1;
625 goto end;
626 }
627
628 *raw_value = cc_freq * rem_value_in_ns / NS_PER_S_U +
629 value_period_cycles;
630
631end:
632 return ret;
633}
634
c9766bce 635static inline
0af5c7b0
PP
636enum bt_self_message_iterator_status bt_common_message_iterator_status_to_self(
637 enum bt_message_iterator_status status)
c9766bce 638{
0af5c7b0 639 return (int) status;
c9766bce 640}
1670bffd 641#endif /* BABELTRACE_COMMON_INTERNAL_H */
This page took 0.062897 seconds and 4 git commands to generate.