Fix: syscall event rule: emission sites not compared in is_equal
[lttng-tools.git] / src / common / optional.hpp
CommitLineData
2c5ff4e4 1/*
ab5be9fa 2 * Copyright (C) 2019 Jérémie Galarneau <jeremie.galarneau@efficios.com>
2c5ff4e4 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
2c5ff4e4 5 *
2c5ff4e4
JG
6 */
7
8#ifndef LTTNG_OPTIONAL_H
9#define LTTNG_OPTIONAL_H
10
11#include <stdint.h>
2c5ff4e4
JG
12
13/*
14 * Define wrapper structure representing an optional value.
15 *
16 * This macro defines an "is_set" boolean field that must be checked
17 * when accessing the optional field. This "is_set" field provides
18 * the semantics that would be expected of a typical "raw pointer" field
19 * which would be checked for NULL.
20 *
21 * Prefer using this macro where "special" values would be used, e.g.
22 * -1ULL for uint64_t types.
23 *
2c5ff4e4
JG
24 * Declaration example:
25 * struct my_struct {
26 * int a;
8250d597 27 * LTTNG_OPTIONAL(int) b;
2c5ff4e4
JG
28 * };
29 *
30 * Usage example:
31 * struct my_struct foo = LTTNG_OPTIONAL_INIT;
32 *
33 * LTTNG_OPTIONAL_SET(&foo.b, 42);
34 * if (foo.b.is_set) {
35 * printf("%d", foo.b.value);
36 * }
37 *
38 * LTTNG_OPTIONAL_UNSET(&foo.b);
39 */
28f23191
JG
40#define LTTNG_OPTIONAL(type) \
41 struct { \
2c5ff4e4 42 uint8_t is_set; \
28f23191 43 type value; \
2c5ff4e4
JG
44 }
45
0a30bf9b
JG
46/*
47 * Alias used for communication structures. If the layout of an LTTNG_OPTIONAL
48 * is changed, the original layout should still be used for communication
49 * purposes.
50 *
51 * LTTNG_OPTIONAL_COMM should be combined with the LTTNG_PACKED macro when
52 * used for IPC / network communication.
53 */
54#define LTTNG_OPTIONAL_COMM LTTNG_OPTIONAL
55
2c5ff4e4
JG
56/*
57 * This macro is available as a 'convenience' to allow sites that assume
a0377dfe 58 * an optional value is set to LTTNG_ASSERT() that it is set when accessing it.
2c5ff4e4
JG
59 *
60 * Since this returns the 'optional' by value, it is not suitable for all
61 * wrapped optional types. It is meant to be used with PODs.
62 */
28f23191
JG
63#define LTTNG_OPTIONAL_GET(optional) \
64 ({ \
65 LTTNG_ASSERT((optional).is_set); \
66 (optional).value; \
2c5ff4e4
JG
67 })
68
3da864a9
JR
69/*
70 * This macro is available as a 'convenience' to allow sites that assume
a0377dfe 71 * an optional value is set to LTTNG_ASSERT() that it is set when fecthing the
3da864a9
JR
72 * underlying value's address.
73 */
28f23191
JG
74#define LTTNG_OPTIONAL_GET_PTR(optional) \
75 ({ \
76 LTTNG_ASSERT((optional).is_set); \
77 &(optional).value; \
3da864a9
JR
78 })
79
2c5ff4e4 80/*
5747e45a 81 * Initialize an optional field as unset.
2c5ff4e4
JG
82 *
83 * The wrapped field is set to the value it would gave if it had static storage
84 * duration.
85 */
28f23191
JG
86#define LTTNG_OPTIONAL_INIT_UNSET \
87 { \
88 }
5747e45a
JG
89
90/*
91 * Initialize an optional field as 'set' with a given value.
92 */
5c7248cd
JG
93#define LTTNG_OPTIONAL_INIT_VALUE(val) \
94 { \
95 .is_set = 1, .value = (val) \
96 }
2c5ff4e4
JG
97
98/* Set the value of an optional field. */
28f23191
JG
99#define LTTNG_OPTIONAL_SET(field_ptr, val) \
100 do { \
101 (field_ptr)->is_set = 1; \
102 (field_ptr)->value = (val); \
bc2dc8d4 103 } while (0)
2c5ff4e4
JG
104
105/* Put an optional field in the "unset" (NULL-ed) state. */
28f23191
JG
106#define LTTNG_OPTIONAL_UNSET(field_ptr) \
107 do { \
108 (field_ptr)->is_set = 0; \
bc2dc8d4 109 } while (0)
2c5ff4e4
JG
110
111#endif /* LTTNG_OPTIONAL_H */
This page took 0.063782 seconds and 4 git commands to generate.