2013-07-24 00:41:39 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-11-15 23:50:19 -08:00
|
|
|
|
2012-01-15 00:13:07 -08:00
|
|
|
/* Implementation of macros to ensure correct use of RAII Auto* objects. */
|
|
|
|
|
2011-11-15 23:50:19 -08:00
|
|
|
#ifndef mozilla_GuardObjects_h
|
|
|
|
#define mozilla_GuardObjects_h
|
|
|
|
|
2011-12-19 11:28:35 -08:00
|
|
|
#include "mozilla/Assertions.h"
|
2015-03-19 08:18:00 -07:00
|
|
|
#include "mozilla/Move.h"
|
2015-04-21 16:47:52 -07:00
|
|
|
#include "mozilla/Types.h"
|
2011-11-15 23:50:19 -08:00
|
|
|
|
2012-01-15 00:13:07 -08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
2011-11-15 23:50:19 -08:00
|
|
|
#ifdef DEBUG
|
2012-01-15 00:13:07 -08:00
|
|
|
|
2015-04-21 16:47:52 -07:00
|
|
|
/**
|
|
|
|
* A custom define is used rather than |mozPoisonValue()| due to cascading
|
|
|
|
* build failures relating to how mfbt is linked on different operating
|
|
|
|
* systems. See bug 1160253.
|
|
|
|
*/
|
|
|
|
#define MOZ_POISON uintptr_t(-1)
|
|
|
|
|
2012-01-15 00:13:07 -08:00
|
|
|
namespace mozilla {
|
|
|
|
namespace detail {
|
2012-06-03 20:36:43 -07:00
|
|
|
|
2012-01-15 00:13:07 -08:00
|
|
|
/*
|
|
|
|
* The following classes are designed to cause assertions to detect
|
|
|
|
* inadvertent use of guard objects as temporaries. In other words,
|
|
|
|
* when we have a guard object whose only purpose is its constructor and
|
|
|
|
* destructor (and is never otherwise referenced), the intended use
|
|
|
|
* might be:
|
|
|
|
*
|
2012-06-03 20:36:43 -07:00
|
|
|
* AutoRestore savePainting(mIsPainting);
|
2012-01-15 00:13:07 -08:00
|
|
|
*
|
|
|
|
* but is is easy to accidentally write:
|
|
|
|
*
|
2012-06-03 20:36:43 -07:00
|
|
|
* AutoRestore(mIsPainting);
|
2012-01-15 00:13:07 -08:00
|
|
|
*
|
|
|
|
* which compiles just fine, but runs the destructor well before the
|
|
|
|
* intended time.
|
|
|
|
*
|
|
|
|
* They work by adding (#ifdef DEBUG) an additional parameter to the
|
|
|
|
* guard object's constructor, with a default value, so that users of
|
|
|
|
* the guard object's API do not need to do anything. The default value
|
|
|
|
* of this parameter is a temporary object. C++ (ISO/IEC 14882:1998),
|
|
|
|
* section 12.2 [class.temporary], clauses 4 and 5 seem to assume a
|
|
|
|
* guarantee that temporaries are destroyed in the reverse of their
|
|
|
|
* construction order, but I actually can't find a statement that that
|
|
|
|
* is true in the general case (beyond the two specific cases mentioned
|
|
|
|
* there). However, it seems to be true.
|
|
|
|
*
|
|
|
|
* These classes are intended to be used only via the macros immediately
|
|
|
|
* below them:
|
|
|
|
*
|
|
|
|
* MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER declares (ifdef DEBUG) a member
|
|
|
|
* variable, and should be put where a declaration of a private
|
|
|
|
* member variable would be placed.
|
|
|
|
* MOZ_GUARD_OBJECT_NOTIFIER_PARAM should be placed at the end of the
|
|
|
|
* parameters to each constructor of the guard object; it declares
|
|
|
|
* (ifdef DEBUG) an additional parameter. (But use the *_ONLY_PARAM
|
|
|
|
* variant for constructors that take no other parameters.)
|
|
|
|
* MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL should likewise be used in
|
|
|
|
* the implementation of such constructors when they are not inline.
|
|
|
|
* MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT should be used in
|
|
|
|
* the implementation of such constructors to pass the parameter to
|
|
|
|
* a base class that also uses these macros
|
|
|
|
* MOZ_GUARD_OBJECT_NOTIFIER_INIT is a statement that belongs in each
|
|
|
|
* constructor. It uses the parameter declared by
|
|
|
|
* MOZ_GUARD_OBJECT_NOTIFIER_PARAM.
|
|
|
|
*
|
|
|
|
* For more details, and examples of using these macros, see
|
|
|
|
* https://developer.mozilla.org/en/Using_RAII_classes_in_Mozilla
|
|
|
|
*/
|
2014-03-05 04:50:55 -08:00
|
|
|
class GuardObjectNotifier
|
2012-01-15 00:13:07 -08:00
|
|
|
{
|
2014-06-12 23:34:08 -07:00
|
|
|
private:
|
|
|
|
bool* mStatementDone;
|
2012-01-15 00:13:07 -08:00
|
|
|
|
2014-06-12 23:34:08 -07:00
|
|
|
public:
|
2015-04-21 16:47:52 -07:00
|
|
|
GuardObjectNotifier()
|
|
|
|
: mStatementDone(reinterpret_cast<bool*>(MOZ_POISON))
|
|
|
|
{
|
|
|
|
}
|
2011-11-15 23:50:19 -08:00
|
|
|
|
2015-04-21 16:47:52 -07:00
|
|
|
~GuardObjectNotifier()
|
|
|
|
{
|
|
|
|
// Assert that the GuardObjectNotifier has been properly initialized by
|
|
|
|
// using the |MOZ_GUARD_OBJECT_NOTIFIER_INIT| macro. A poison value is
|
|
|
|
// used rather than a null check to appease static analyzers that were
|
|
|
|
// (incorrectly) detecting null pointer dereferences.
|
|
|
|
MOZ_ASSERT(mStatementDone != reinterpret_cast<bool*>(MOZ_POISON));
|
|
|
|
*mStatementDone = true;
|
|
|
|
}
|
2011-11-15 23:50:19 -08:00
|
|
|
|
2014-06-12 23:34:08 -07:00
|
|
|
void setStatementDone(bool* aStatementIsDone)
|
|
|
|
{
|
|
|
|
mStatementDone = aStatementIsDone;
|
|
|
|
}
|
2012-01-15 00:13:07 -08:00
|
|
|
};
|
2011-11-15 23:50:19 -08:00
|
|
|
|
2014-03-05 04:50:55 -08:00
|
|
|
class GuardObjectNotificationReceiver
|
2012-01-15 00:13:07 -08:00
|
|
|
{
|
2014-06-12 23:34:08 -07:00
|
|
|
private:
|
|
|
|
bool mStatementDone;
|
|
|
|
|
|
|
|
public:
|
|
|
|
GuardObjectNotificationReceiver() : mStatementDone(false) { }
|
|
|
|
|
|
|
|
~GuardObjectNotificationReceiver() {
|
|
|
|
/*
|
|
|
|
* Assert that the guard object was not used as a temporary. (Note that
|
|
|
|
* this assert might also fire if init is not called because the guard
|
|
|
|
* object's implementation is not using the above macros correctly.)
|
|
|
|
*/
|
|
|
|
MOZ_ASSERT(mStatementDone);
|
|
|
|
}
|
|
|
|
|
2015-03-19 08:18:00 -07:00
|
|
|
void init(GuardObjectNotifier& aNotifier)
|
2014-06-12 23:34:08 -07:00
|
|
|
{
|
2015-03-19 08:18:00 -07:00
|
|
|
aNotifier.setStatementDone(&mStatementDone);
|
2014-06-12 23:34:08 -07:00
|
|
|
}
|
2012-01-15 00:13:07 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
} /* namespace detail */
|
|
|
|
} /* namespace mozilla */
|
|
|
|
|
2015-04-21 16:47:52 -07:00
|
|
|
#undef MOZ_POISON
|
|
|
|
|
2012-01-15 00:13:07 -08:00
|
|
|
#endif /* DEBUG */
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
# define MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER \
|
|
|
|
mozilla::detail::GuardObjectNotificationReceiver _mCheckNotUsedAsTemporary;
|
|
|
|
# define MOZ_GUARD_OBJECT_NOTIFIER_PARAM \
|
2015-03-19 08:18:00 -07:00
|
|
|
, mozilla::detail::GuardObjectNotifier&& _notifier = \
|
2012-01-15 00:13:07 -08:00
|
|
|
mozilla::detail::GuardObjectNotifier()
|
|
|
|
# define MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM \
|
2015-03-19 08:18:00 -07:00
|
|
|
mozilla::detail::GuardObjectNotifier&& _notifier = \
|
2012-01-15 00:13:07 -08:00
|
|
|
mozilla::detail::GuardObjectNotifier()
|
|
|
|
# define MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL \
|
2015-03-19 08:18:00 -07:00
|
|
|
, mozilla::detail::GuardObjectNotifier&& _notifier
|
2013-01-21 04:38:54 -08:00
|
|
|
# define MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM_IN_IMPL \
|
2015-03-19 08:18:00 -07:00
|
|
|
mozilla::detail::GuardObjectNotifier&& _notifier
|
2012-01-15 00:13:07 -08:00
|
|
|
# define MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT \
|
2015-03-19 08:18:00 -07:00
|
|
|
, mozilla::Move(_notifier)
|
2012-02-15 16:17:02 -08:00
|
|
|
# define MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM_TO_PARENT \
|
2015-03-19 08:18:00 -07:00
|
|
|
mozilla::Move(_notifier)
|
2012-01-15 00:13:07 -08:00
|
|
|
# define MOZ_GUARD_OBJECT_NOTIFIER_INIT \
|
|
|
|
do { _mCheckNotUsedAsTemporary.init(_notifier); } while (0)
|
|
|
|
#else
|
|
|
|
# define MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
|
|
|
|
# define MOZ_GUARD_OBJECT_NOTIFIER_PARAM
|
|
|
|
# define MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM
|
|
|
|
# define MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL
|
2013-01-21 04:38:54 -08:00
|
|
|
# define MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM_IN_IMPL
|
2012-02-15 16:17:02 -08:00
|
|
|
# define MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM_TO_PARENT
|
2012-01-15 00:13:07 -08:00
|
|
|
# define MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT
|
|
|
|
# define MOZ_GUARD_OBJECT_NOTIFIER_INIT do { } while (0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* __cplusplus */
|
2011-11-15 23:50:19 -08:00
|
|
|
|
|
|
|
#endif /* mozilla_GuardObjects_h */
|