2 * Copyright (c) 2017, The OpenThread Authors.
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.
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.
31 * This file includes utility macros for coding.
33 #ifndef OTBR_COMMON_CODE_UTILS_HPP_
34 #define OTBR_COMMON_CODE_UTILS_HPP_
36 #include "common/logging.hpp"
39 * This aligns the pointer to @p aAlignType.
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.
44 * @returns A @aAlignType pointer to aligned memory.
47 #define OTBR_ALIGNED(aMem, aAlignType) \
48 reinterpret_cast<aAlignType>( \
49 ((reinterpret_cast<unsigned long>(aMem) + sizeof(aAlignType) - 1) / sizeof(aAlignType)) * sizeof(aAlignType))
51 #ifndef CONTAINING_RECORD
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 */
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.
62 * @param[in] aStatus A scalar status to be evaluated against zero (0).
65 #define SuccessOrExit(aStatus) \
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.
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.
82 #define SuccessOrDie(aStatus, aMessage) \
87 otbrLog(OTBR_LOG_EMERG, "FAILED %s:%d - %s", __FUNCTION__, __LINE__, aMessage); \
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.
97 * @param[in] aCondition A Boolean expression to be evaluated.
98 * @param[in] ... An expression or block to execute when the
102 #define VerifyOrExit(aCondition, ...) \
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.
116 * @param[in] aCondition The condition to verify
117 * @param[in] aMessage A message (text string) to print on failure.
120 #define VerifyOrDie(aCondition, aMessage) \
125 otbrLog(OTBR_LOG_EMERG, "FAILED %s:%d - %s", __FUNCTION__, __LINE__, aMessage); \
131 * This unconditionally executes @a ... and branches to the local
134 * @note The use of this interface implies neither success nor
135 * failure for the overall exit status of the enclosing
138 * @param[in] ... An optional expression or block to execute
139 * when the assertion fails.
142 #define ExitNow(...) \
150 #define OTBR_UNUSED_VARIABLE(variable) ((void)(variable))
152 #endif // OTBR_COMMON_CODE_UTILS_HPP_