[code] add static assert (#3422)

This commit is contained in:
Yakun Xu
2019-01-23 21:50:19 -08:00
committed by Jonathan Hui
parent a00c538830
commit 9094c12652
6 changed files with 74 additions and 3 deletions
@@ -41,8 +41,6 @@
extern "C" {
#endif
#include "common/code_utils.hpp"
typedef struct
{
uint8_t hwKeyLen;
+1
View File
@@ -392,6 +392,7 @@ HEADERS_COMMON = \
utils/jam_detector.hpp \
utils/parse_cmdline.hpp \
utils/slaac_address.hpp \
utils/static_assert.hpp \
utils/wrap_stdbool.h \
utils/wrap_stdint.h \
utils/wrap_string.h \
+4
View File
@@ -43,6 +43,7 @@
#include "common/code_utils.hpp"
#include "common/encoding.hpp"
#include "common/message.hpp"
#include "utils/static_assert.hpp"
using ot::Encoding::BigEndian::HostSwap16;
@@ -572,6 +573,9 @@ private:
const HelpData &GetHelpData(void) const
{
OT_STATIC_ASSERT(sizeof(mBuffer.mHead.mInfo) + sizeof(HelpData) + kHelpDataAlignment <= sizeof(mBuffer),
"Insufficient buffer size for CoAP processing!");
return *static_cast<const HelpData *>(otALIGN(mBuffer.mHead.mData, kHelpDataAlignment));
}
+1
View File
@@ -36,6 +36,7 @@
#include "openthread-core-config.h"
#include "utils/static_assert.hpp"
#include "utils/wrap_stdbool.h"
/**
+4 -1
View File
@@ -39,6 +39,7 @@
#include <stddef.h>
#include "utils/static_assert.hpp"
#include "utils/wrap_stdint.h"
namespace ot {
@@ -233,7 +234,7 @@ private:
#else
kMemorySize = OPENTHREAD_CONFIG_HEAP_SIZE_NO_DTLS, ///< Size of memory buffer (bytes).
#endif
kAlignSize = sizeof(long), ///< The alignment size.
kAlignSize = sizeof(void *), ///< The alignment size.
kBlockRemainderSize = kAlignSize - sizeof(uint16_t) * 2, ///< Block unit remainder size.
kSuperBlockSize = kAlignSize - sizeof(Block), ///< Super block size.
kFirstBlockSize = kMemorySize - kAlignSize * 3 + kBlockRemainderSize, ///< First block size.
@@ -242,6 +243,8 @@ private:
kGuardBlockOffset = kMemorySize - sizeof(uint16_t), ///< Offset of the guard block.
};
OT_STATIC_ASSERT(kMemorySize % kAlignSize == 0, "The heap memory size is not aligned to kAlignSize!");
/**
* This method returns the block at offset @p aOffset.
*
+64
View File
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2019, The OpenThread Authors.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @file
* This file includes macros for static assert for C++03.
*/
#ifndef OPENTHREAD_STATIC_ASSERT_HPP_
#define OPENTHREAD_STATIC_ASSERT_HPP_
#ifdef __cplusplus
namespace ot {
template <int> struct StaticAssertError;
template <> struct StaticAssertError<true>
{
StaticAssertError(...);
};
} // namespace ot
/**
* This macro does static assert.
*
* @param[in] aExpression An expression to assert.
* @param[in] aMessage A message to describe what is wrong when @p aExpression is false.
*
*/
#define OT_STATIC_ASSERT(aExpression, aMessage) \
enum \
{ \
StaticAssertError##__LINE__ = sizeof(ot::StaticAssertError<(aExpression) != 0>(aMessage)), \
}
#endif // __cplusplus
#endif // OPENTHREAD_STATIC_ASSERT_HPP_