Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / ot-br-posix / repo / src / common / code_utils.hpp
1 /*
2  *    Copyright (c) 2017, The OpenThread Authors.
3  *    All rights reserved.
4  *
5  *    Redistribution and use in source and binary forms, with or without
6  *    modification, are permitted provided that the following conditions are met:
7  *    1. Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *    2. Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *    3. Neither the name of the copyright holder nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *    POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 /**
30  * @file
31  * This file includes utility macros for coding.
32  */
33 #ifndef OTBR_COMMON_CODE_UTILS_HPP_
34 #define OTBR_COMMON_CODE_UTILS_HPP_
35
36 #include "common/logging.hpp"
37
38 /**
39  *  This aligns the pointer to @p aAlignType.
40  *
41  *  @param[in]  aMem        A pointer to arbitrary memory.
42  *  @param[in]  aAlignType  The type to align with and convert the pointer to this type.
43  *
44  *  @returns A @aAlignType pointer to aligned memory.
45  *
46  */
47 #define OTBR_ALIGNED(aMem, aAlignType) \
48     reinterpret_cast<aAlignType>(      \
49         ((reinterpret_cast<unsigned long>(aMem) + sizeof(aAlignType) - 1) / sizeof(aAlignType)) * sizeof(aAlignType))
50
51 #ifndef CONTAINING_RECORD
52 #define BASE 0x1
53 #define myoffsetof(s, m) (((size_t) & (((s *)BASE)->m)) - BASE)
54 #define CONTAINING_RECORD(address, type, field) ((type *)((uint8_t *)(address)-myoffsetof(type, field)))
55 #endif /* CONTAINING_RECORD */
56
57 /**
58  *  This checks for the specified status, which is expected to
59  *  commonly be successful, and branches to the local label 'exit' if
60  *  the status is unsuccessful.
61  *
62  *  @param[in]  aStatus     A scalar status to be evaluated against zero (0).
63  *
64  */
65 #define SuccessOrExit(aStatus) \
66     do                         \
67     {                          \
68         if ((aStatus) != 0)    \
69         {                      \
70             goto exit;         \
71         }                      \
72     } while (false)
73
74 /**
75  * This macro verifies a given error status to be successful (compared against value zero (0)), otherwise, it emits a
76  * given error messages and exits the program.
77  *
78  * @param[in]  aStatus     A scalar error status to be evaluated against zero (0).
79  * @param[in]  aMessage    A message (text string) to print on failure.
80  *
81  */
82 #define SuccessOrDie(aStatus, aMessage)                                                     \
83     do                                                                                      \
84     {                                                                                       \
85         if ((aStatus) != 0)                                                                 \
86         {                                                                                   \
87             otbrLog(OTBR_LOG_EMERG, "FAILED %s:%d - %s", __FUNCTION__, __LINE__, aMessage); \
88             exit(-1);                                                                       \
89         }                                                                                   \
90     } while (false)
91
92 /**
93  *  This checks for the specified condition, which is expected to
94  *  commonly be true, and both executes @a ... and branches to the
95  *  local label 'exit' if the condition is false.
96  *
97  *  @param[in]  aCondition  A Boolean expression to be evaluated.
98  *  @param[in]  ...         An expression or block to execute when the
99  *                          assertion fails.
100  *
101  */
102 #define VerifyOrExit(aCondition, ...) \
103     do                                \
104     {                                 \
105         if (!(aCondition))            \
106         {                             \
107             __VA_ARGS__;              \
108             goto exit;                \
109         }                             \
110     } while (false)
111
112 /**
113  * This macro checks for the specified condition, which is expected to commonly be true,
114  * and both prints the message and terminates the program if the condition is false.
115  *
116  * @param[in]   aCondition  The condition to verify
117  * @param[in]   aMessage    A message (text string) to print on failure.
118  *
119  */
120 #define VerifyOrDie(aCondition, aMessage)                                                   \
121     do                                                                                      \
122     {                                                                                       \
123         if (!(aCondition))                                                                  \
124         {                                                                                   \
125             otbrLog(OTBR_LOG_EMERG, "FAILED %s:%d - %s", __FUNCTION__, __LINE__, aMessage); \
126             exit(-1);                                                                       \
127         }                                                                                   \
128     } while (false)
129
130 /**
131  *  This unconditionally executes @a ... and branches to the local
132  *  label 'exit'.
133  *
134  *  @note The use of this interface implies neither success nor
135  *        failure for the overall exit status of the enclosing
136  *        function body.
137  *
138  *  @param[in]  ...         An optional expression or block to execute
139  *                          when the assertion fails.
140  *
141  */
142 #define ExitNow(...) \
143     do               \
144     {                \
145         __VA_ARGS__; \
146         goto exit;   \
147     } while (false)
148
149 #define OTBR_NOOP
150 #define OTBR_UNUSED_VARIABLE(variable) ((void)(variable))
151
152 #endif // OTBR_COMMON_CODE_UTILS_HPP_