Fix: strndup does not exist on Solaris 10
[lttng-tools.git] / src / common / optional.h
CommitLineData
1545fd17
JG
1/*
2 * Copyright (C) 2019 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18#ifndef LTTNG_OPTIONAL_H
19#define LTTNG_OPTIONAL_H
20
21#include <stdint.h>
22#include <assert.h>
23
24/*
25 * Define wrapper structure representing an optional value.
26 *
27 * This macro defines an "is_set" boolean field that must be checked
28 * when accessing the optional field. This "is_set" field provides
29 * the semantics that would be expected of a typical "raw pointer" field
30 * which would be checked for NULL.
31 *
32 * Prefer using this macro where "special" values would be used, e.g.
33 * -1ULL for uint64_t types.
34 *
1545fd17
JG
35 * Declaration example:
36 * struct my_struct {
37 * int a;
38 * LTTNG_OPTIONAL(int, b);
39 * };
40 *
41 * Usage example:
42 * struct my_struct foo = LTTNG_OPTIONAL_INIT;
43 *
44 * LTTNG_OPTIONAL_SET(&foo.b, 42);
45 * if (foo.b.is_set) {
46 * printf("%d", foo.b.value);
47 * }
48 *
49 * LTTNG_OPTIONAL_UNSET(&foo.b);
50 */
51#define LTTNG_OPTIONAL(type) \
52 struct { \
53 uint8_t is_set; \
54 type value; \
55 }
56
b9bd106f
JG
57/*
58 * Alias used for communication structures. If the layout of an LTTNG_OPTIONAL
59 * is changed, the original layout should still be used for communication
60 * purposes.
61 *
62 * LTTNG_OPTIONAL_COMM should be combined with the LTTNG_PACKED macro when
63 * used for IPC / network communication.
64 */
65#define LTTNG_OPTIONAL_COMM LTTNG_OPTIONAL
66
1545fd17
JG
67/*
68 * This macro is available as a 'convenience' to allow sites that assume
69 * an optional value is set to assert() that it is set when accessing it.
70 *
71 * Since this returns the 'optional' by value, it is not suitable for all
72 * wrapped optional types. It is meant to be used with PODs.
73 */
74#define LTTNG_OPTIONAL_GET(optional) \
75 ({ \
76 assert(optional.is_set); \
77 optional.value; \
78 })
79
80/*
81 * Initialize an optional field.
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 { .is_set = 0 }
87
88/* Set the value of an optional field. */
89#define LTTNG_OPTIONAL_SET(field_ptr, val) \
90 (field_ptr)->value = val; \
91 (field_ptr)->is_set = 1;
92
93/* Put an optional field in the "unset" (NULL-ed) state. */
94#define LTTNG_OPTIONAL_UNSET(field_ptr) \
95 (field_ptr)->is_set = 0;
96
97#endif /* LTTNG_OPTIONAL_H */
This page took 0.044863 seconds and 4 git commands to generate.