| 1 | /* |
| 2 | * Copyright (C) 2019 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #ifndef LTTNG_OPTIONAL_H |
| 9 | #define LTTNG_OPTIONAL_H |
| 10 | |
| 11 | #include <stdint.h> |
| 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 | * |
| 24 | * Declaration example: |
| 25 | * struct my_struct { |
| 26 | * int a; |
| 27 | * LTTNG_OPTIONAL(int) b; |
| 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 | */ |
| 40 | #define LTTNG_OPTIONAL(type) \ |
| 41 | struct { \ |
| 42 | uint8_t is_set; \ |
| 43 | type value; \ |
| 44 | } |
| 45 | |
| 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 | |
| 56 | /* |
| 57 | * This macro is available as a 'convenience' to allow sites that assume |
| 58 | * an optional value is set to LTTNG_ASSERT() that it is set when accessing it. |
| 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 | */ |
| 63 | #define LTTNG_OPTIONAL_GET(optional) \ |
| 64 | ({ \ |
| 65 | LTTNG_ASSERT((optional).is_set); \ |
| 66 | (optional).value; \ |
| 67 | }) |
| 68 | |
| 69 | /* |
| 70 | * This macro is available as a 'convenience' to allow sites that assume |
| 71 | * an optional value is set to LTTNG_ASSERT() that it is set when fecthing the |
| 72 | * underlying value's address. |
| 73 | */ |
| 74 | #define LTTNG_OPTIONAL_GET_PTR(optional) \ |
| 75 | ({ \ |
| 76 | LTTNG_ASSERT((optional).is_set); \ |
| 77 | &(optional).value; \ |
| 78 | }) |
| 79 | |
| 80 | /* |
| 81 | * Initialize an optional field as unset. |
| 82 | * |
| 83 | * The wrapped field is set to the value it would gave if it had static storage |
| 84 | * duration. |
| 85 | */ |
| 86 | #define LTTNG_OPTIONAL_INIT_UNSET {} |
| 87 | |
| 88 | /* |
| 89 | * Initialize an optional field as 'set' with a given value. |
| 90 | */ |
| 91 | #define LTTNG_OPTIONAL_INIT_VALUE(val) \ |
| 92 | { \ |
| 93 | .is_set = 1, .value = (val) \ |
| 94 | } |
| 95 | |
| 96 | /* Set the value of an optional field. */ |
| 97 | #define LTTNG_OPTIONAL_SET(field_ptr, val) \ |
| 98 | do { \ |
| 99 | (field_ptr)->is_set = 1; \ |
| 100 | (field_ptr)->value = (val); \ |
| 101 | } while (0) |
| 102 | |
| 103 | /* Put an optional field in the "unset" (NULL-ed) state. */ |
| 104 | #define LTTNG_OPTIONAL_UNSET(field_ptr) \ |
| 105 | do { \ |
| 106 | (field_ptr)->is_set = 0; \ |
| 107 | } while (0) |
| 108 | |
| 109 | #endif /* LTTNG_OPTIONAL_H */ |