Commit d4b40c09 authored by sashab's avatar sashab Committed by Commit bot

Added ASSERT_SIZE macro for checking size of size-sensitive classes

Added ASSERT_SIZE macro for checking size of size-sensitive classes,
which can be used with the SameSizeAs structs to give better compile
errors with both the expected and actual size values. Also added the
assert to the check for SameSizeAsCSSValue.

Patch written with help by timloh@.

BUG=486252

Review-Url: https://codereview.chromium.org/2231953003
Cr-Commit-Position: refs/heads/master@{#417540}
parent 7c098a49
...@@ -62,14 +62,14 @@ ...@@ -62,14 +62,14 @@
#include "core/css/CSSValueList.h" #include "core/css/CSSValueList.h"
#include "core/css/CSSValuePair.h" #include "core/css/CSSValuePair.h"
#include "core/css/CSSVariableReferenceValue.h" #include "core/css/CSSVariableReferenceValue.h"
#include "wtf/SizeAssertions.h"
namespace blink { namespace blink {
struct SameSizeAsCSSValue : public GarbageCollectedFinalized<SameSizeAsCSSValue> { struct SameSizeAsCSSValue : public GarbageCollectedFinalized<SameSizeAsCSSValue> {
uint32_t bitfields; uint32_t bitfields;
}; };
ASSERT_SIZE(CSSValue, SameSizeAsCSSValue);
static_assert(sizeof(CSSValue) <= sizeof(SameSizeAsCSSValue), "CSSValue should stay small");
bool CSSValue::isImplicitInitialValue() const bool CSSValue::isImplicitInitialValue() const
{ {
......
...@@ -110,6 +110,7 @@ component("wtf") { ...@@ -110,6 +110,7 @@ component("wtf") {
"RefPtr.h", "RefPtr.h",
"RetainPtr.h", "RetainPtr.h",
"SaturatedArithmetic.h", "SaturatedArithmetic.h",
"SizeAssertions.h",
"SizeLimits.cpp", "SizeLimits.cpp",
"SpinLock.cpp", "SpinLock.cpp",
"SpinLock.h", "SpinLock.h",
......
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef WTF_SizeAssertions_h
#define WTF_SizeAssertions_h
namespace WTF {
// The ASSERT_SIZE macro can be used to check that a given struct is the same
// size as a class. This is useful to visualize where the space is being used in
// a class, as well as give a useful compile error message when the size doesn't
// match the expected value.
template<class T, class U> struct assert_size {
template<int ActualSize, int ExpectedSize> struct assertSizeEqual {
static_assert(ActualSize == ExpectedSize, "Class should stay small");
static const bool innerValue = true;
};
static const bool value = assertSizeEqual<sizeof(T), sizeof(U)>::innerValue;
};
} // namespace WTF
#define ASSERT_SIZE(className, sameSizeAsClassName) \
static_assert(WTF::assert_size<className, sameSizeAsClassName>::value, "");
#endif // WTF_SizeAssertions_h
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment