2013-07-24 11:41:39 +04: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-04-06 13:16:33 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2013-07-24 11:41:39 +04:00
|
|
|
* 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/. */
|
2012-04-06 13:16:33 +04:00
|
|
|
|
2014-07-11 04:45:45 +04:00
|
|
|
/* DEPRECATED: Use UniquePtr.h instead. */
|
2012-04-06 13:16:33 +04:00
|
|
|
|
2013-07-24 11:41:39 +04:00
|
|
|
#ifndef mozilla_Scoped_h
|
|
|
|
#define mozilla_Scoped_h
|
2012-04-06 13:16:33 +04:00
|
|
|
|
|
|
|
/*
|
2014-07-11 04:45:45 +04:00
|
|
|
* DEPRECATED: Use UniquePtr.h instead.
|
|
|
|
*
|
2012-04-06 13:16:33 +04:00
|
|
|
* Resource Acquisition Is Initialization is a programming idiom used
|
|
|
|
* to write robust code that is able to deallocate resources properly,
|
|
|
|
* even in presence of execution errors or exceptions that need to be
|
2016-03-02 19:44:27 +03:00
|
|
|
* propagated. The Scoped* classes defined via the |SCOPED_TEMPLATE|
|
|
|
|
* and |MOZ_TYPE_SPECIFIC_SCOPED_POINTER_TEMPLTE| macros perform the
|
2012-04-06 13:16:33 +04:00
|
|
|
* deallocation of the resource they hold once program execution
|
|
|
|
* reaches the end of the scope for which they have been defined.
|
2016-03-02 19:44:27 +03:00
|
|
|
* These macros have been used to automatically close file
|
|
|
|
* descriptors/file handles when reaching the end of the scope,
|
|
|
|
* graphics contexts, etc.
|
2012-04-06 13:16:33 +04:00
|
|
|
*
|
2016-03-02 19:44:27 +03:00
|
|
|
* The general scenario for RAII classes created by the above macros
|
|
|
|
* is the following:
|
2012-04-06 13:16:33 +04:00
|
|
|
*
|
|
|
|
* ScopedClass foo(create_value());
|
|
|
|
* // ... In this scope, |foo| is defined. Use |foo.get()| or |foo.rwget()|
|
|
|
|
* to access the value.
|
|
|
|
* // ... In case of |return| or |throw|, |foo| is deallocated automatically.
|
|
|
|
* // ... If |foo| needs to be returned or stored, use |foo.forget()|
|
|
|
|
*
|
|
|
|
* Note that the RAII classes defined in this header do _not_ perform any form
|
|
|
|
* of reference-counting or garbage-collection. These classes have exactly two
|
|
|
|
* behaviors:
|
2012-06-12 03:16:47 +04:00
|
|
|
*
|
2012-04-06 13:16:33 +04:00
|
|
|
* - if |forget()| has not been called, the resource is always deallocated at
|
|
|
|
* the end of the scope;
|
|
|
|
* - if |forget()| has been called, any control on the resource is unbound
|
|
|
|
* and the resource is not deallocated by the class.
|
|
|
|
*/
|
|
|
|
|
Bug 1609996 - Reorder some includes affected by the previous patches. r=froydnj
This was done by:
This was done by applying:
```
diff --git a/python/mozbuild/mozbuild/code-analysis/mach_commands.py b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
index 789affde7bbf..fe33c4c7d4d1 100644
--- a/python/mozbuild/mozbuild/code-analysis/mach_commands.py
+++ b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
@@ -2007,7 +2007,7 @@ class StaticAnalysis(MachCommandBase):
from subprocess import Popen, PIPE, check_output, CalledProcessError
diff_process = Popen(self._get_clang_format_diff_command(commit), stdout=PIPE)
- args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format]
+ args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format, '-sort-includes']
if not output_file:
args.append("-i")
```
Then running `./mach clang-format -c <commit-hash>`
Then undoing that patch.
Then running check_spidermonkey_style.py --fixup
Then running `./mach clang-format`
I had to fix four things:
* I needed to move <utility> back down in GuardObjects.h because I was hitting
obscure problems with our system include wrappers like this:
0:03.94 /usr/include/stdlib.h:550:14: error: exception specification in declaration does not match previous declaration
0:03.94 extern void *realloc (void *__ptr, size_t __size)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/malloc_decls.h:53:1: note: previous declaration is here
0:03.94 MALLOC_DECL(realloc, void*, void*, size_t)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozilla/mozalloc.h:22:32: note: expanded from macro 'MALLOC_DECL'
0:03.94 MOZ_MEMORY_API return_type name##_impl(__VA_ARGS__);
0:03.94 ^
0:03.94 <scratch space>:178:1: note: expanded from here
0:03.94 realloc_impl
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozmemory_wrap.h:142:41: note: expanded from macro 'realloc_impl'
0:03.94 #define realloc_impl mozmem_malloc_impl(realloc)
Which I really didn't feel like digging into.
* I had to restore the order of TrustOverrideUtils.h and related files in nss
because the .inc files depend on TrustOverrideUtils.h being included earlier.
* I had to add a missing include to RollingNumber.h
* Also had to partially restore include order in JsepSessionImpl.cpp to avoid
some -WError issues due to some static inline functions being defined in a
header but not used in the rest of the compilation unit.
Differential Revision: https://phabricator.services.mozilla.com/D60327
--HG--
extra : moz-landing-system : lando
2020-01-20 19:19:48 +03:00
|
|
|
#include <utility>
|
|
|
|
|
2014-03-27 20:52:48 +04:00
|
|
|
#include "mozilla/Assertions.h"
|
2012-04-06 13:16:33 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
#include "mozilla/GuardObjects.h"
|
|
|
|
|
2012-06-12 03:16:47 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
2012-04-06 13:16:33 +04:00
|
|
|
/*
|
|
|
|
* Scoped is a helper to create RAII wrappers
|
|
|
|
* Type argument |Traits| is expected to have the following structure:
|
|
|
|
*
|
2014-07-11 06:10:17 +04:00
|
|
|
* struct Traits
|
|
|
|
* {
|
2012-04-06 13:16:33 +04:00
|
|
|
* // Define the type of the value stored in the wrapper
|
|
|
|
* typedef value_type type;
|
|
|
|
* // Returns the value corresponding to the uninitialized or freed state
|
|
|
|
* const static type empty();
|
|
|
|
* // Release resources corresponding to the wrapped value
|
|
|
|
* // This function is responsible for not releasing an |empty| value
|
|
|
|
* const static void release(type);
|
|
|
|
* }
|
|
|
|
*/
|
2012-06-04 07:36:43 +04:00
|
|
|
template <typename Traits>
|
2015-09-03 19:15:23 +03:00
|
|
|
class MOZ_NON_TEMPORARY_CLASS Scoped {
|
2014-07-11 06:10:17 +04:00
|
|
|
public:
|
|
|
|
typedef typename Traits::type Resource;
|
2012-06-04 07:36:43 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
explicit Scoped(MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM)
|
|
|
|
: mValue(Traits::empty()) {
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
|
|
|
|
}
|
2014-03-27 20:52:48 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
explicit Scoped(const Resource& aValue MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
|
|
|
|
: mValue(aValue) {
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
|
|
|
|
}
|
2014-03-27 20:52:48 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/* Move constructor. */
|
2015-09-03 17:51:42 +03:00
|
|
|
Scoped(Scoped&& aOther MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
|
2018-05-30 22:15:35 +03:00
|
|
|
: mValue(std::move(aOther.mValue)) {
|
2014-07-11 06:10:17 +04:00
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
|
|
|
|
aOther.mValue = Traits::empty();
|
|
|
|
}
|
2014-03-27 20:52:48 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
~Scoped() { Traits::release(mValue); }
|
2012-06-04 07:36:43 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
// Constant getter
|
|
|
|
operator const Resource&() const { return mValue; }
|
|
|
|
const Resource& operator->() const { return mValue; }
|
|
|
|
const Resource& get() const { return mValue; }
|
|
|
|
// Non-constant getter.
|
|
|
|
Resource& rwget() { return mValue; }
|
2012-06-04 07:36:43 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/*
|
|
|
|
* Forget the resource.
|
|
|
|
*
|
|
|
|
* Once |forget| has been called, the |Scoped| is neutralized, i.e. it will
|
|
|
|
* have no effect at destruction (unless it is reset to another resource by
|
|
|
|
* |operator=|).
|
|
|
|
*
|
|
|
|
* @return The original resource.
|
|
|
|
*/
|
|
|
|
Resource forget() {
|
|
|
|
Resource tmp = mValue;
|
|
|
|
mValue = Traits::empty();
|
|
|
|
return tmp;
|
|
|
|
}
|
2012-06-04 07:36:43 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/*
|
|
|
|
* Perform immediate clean-up of this |Scoped|.
|
|
|
|
*
|
|
|
|
* If this |Scoped| is currently empty, this method has no effect.
|
|
|
|
*/
|
|
|
|
void dispose() {
|
|
|
|
Traits::release(mValue);
|
|
|
|
mValue = Traits::empty();
|
|
|
|
}
|
2012-06-04 07:36:43 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
bool operator==(const Resource& aOther) const { return mValue == aOther; }
|
2012-06-04 07:36:43 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/*
|
|
|
|
* Replace the resource with another resource.
|
|
|
|
*
|
|
|
|
* Calling |operator=| has the side-effect of triggering clean-up. If you do
|
|
|
|
* not want to trigger clean-up, you should first invoke |forget|.
|
|
|
|
*
|
|
|
|
* @return this
|
|
|
|
*/
|
|
|
|
Scoped& operator=(const Resource& aOther) { return reset(aOther); }
|
2012-06-04 07:36:43 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
Scoped& reset(const Resource& aOther) {
|
|
|
|
Traits::release(mValue);
|
|
|
|
mValue = aOther;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Move assignment operator. */
|
|
|
|
Scoped& operator=(Scoped&& aRhs) {
|
|
|
|
MOZ_ASSERT(&aRhs != this, "self-move-assignment not allowed");
|
|
|
|
this->~Scoped();
|
2018-05-30 22:15:35 +03:00
|
|
|
new (this) Scoped(std::move(aRhs));
|
2014-07-11 06:10:17 +04:00
|
|
|
return *this;
|
|
|
|
}
|
2014-03-27 20:52:48 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
private:
|
2015-01-07 02:35:02 +03:00
|
|
|
explicit Scoped(const Scoped& aValue) = delete;
|
|
|
|
Scoped& operator=(const Scoped& aValue) = delete;
|
2012-06-04 07:36:43 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
private:
|
|
|
|
Resource mValue;
|
|
|
|
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
|
2012-04-06 13:16:33 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SCOPED_TEMPLATE defines a templated class derived from Scoped
|
|
|
|
* This allows to implement templates such as ScopedFreePtr.
|
|
|
|
*
|
|
|
|
* @param name The name of the class to define.
|
|
|
|
* @param Traits A struct implementing clean-up. See the implementations
|
|
|
|
* for more details.
|
|
|
|
*/
|
2014-07-11 06:10:17 +04:00
|
|
|
#define SCOPED_TEMPLATE(name, Traits) \
|
|
|
|
template <typename Type> \
|
2015-09-03 19:15:23 +03:00
|
|
|
struct MOZ_NON_TEMPORARY_CLASS name \
|
|
|
|
: public mozilla::Scoped<Traits<Type> > { \
|
2014-07-11 06:10:17 +04:00
|
|
|
typedef mozilla::Scoped<Traits<Type> > Super; \
|
|
|
|
typedef typename Super::Resource Resource; \
|
|
|
|
name& operator=(Resource aRhs) { \
|
|
|
|
Super::operator=(aRhs); \
|
|
|
|
return *this; \
|
|
|
|
} \
|
2020-02-13 14:20:27 +03:00
|
|
|
name& operator=(name&& aRhs) = default; \
|
|
|
|
\
|
2014-07-11 06:10:17 +04:00
|
|
|
explicit name(MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM) \
|
|
|
|
: Super(MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM_TO_PARENT) {} \
|
|
|
|
explicit name(Resource aRhs MOZ_GUARD_OBJECT_NOTIFIER_PARAM) \
|
|
|
|
: Super(aRhs MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT) {} \
|
2015-09-03 17:51:42 +03:00
|
|
|
name(name&& aRhs MOZ_GUARD_OBJECT_NOTIFIER_PARAM) \
|
2014-07-11 06:10:17 +04:00
|
|
|
: Super(std::move(aRhs) MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT) {} \
|
|
|
|
\
|
|
|
|
private: \
|
2020-02-13 14:20:27 +03:00
|
|
|
explicit name(const name&) = delete; \
|
|
|
|
name& operator=(const name&) = delete; \
|
2012-04-06 13:16:33 +04:00
|
|
|
};
|
|
|
|
|
2012-07-25 02:36:06 +04:00
|
|
|
/*
|
|
|
|
* MOZ_TYPE_SPECIFIC_SCOPED_POINTER_TEMPLATE makes it easy to create scoped
|
|
|
|
* pointers for types with custom deleters; just overload
|
|
|
|
* TypeSpecificDelete(T*) in the same namespace as T to call the deleter for
|
|
|
|
* type T.
|
|
|
|
*
|
|
|
|
* @param name The name of the class to define.
|
|
|
|
* @param Type A struct implementing clean-up. See the implementations
|
|
|
|
* for more details.
|
|
|
|
* *param Deleter The function that is used to delete/destroy/free a
|
|
|
|
* non-null value of Type*.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* MOZ_TYPE_SPECIFIC_SCOPED_POINTER_TEMPLATE(ScopedPRFileDesc, PRFileDesc, \
|
|
|
|
* PR_Close)
|
|
|
|
* ...
|
|
|
|
* {
|
|
|
|
* ScopedPRFileDesc file(PR_OpenFile(...));
|
|
|
|
* ...
|
|
|
|
* } // file is closed with PR_Close here
|
|
|
|
*/
|
|
|
|
#define MOZ_TYPE_SPECIFIC_SCOPED_POINTER_TEMPLATE(name, Type, Deleter) \
|
2014-07-11 06:10:17 +04:00
|
|
|
template <> \
|
|
|
|
inline void TypeSpecificDelete(Type* aValue) { \
|
|
|
|
Deleter(aValue); \
|
|
|
|
} \
|
2012-07-25 02:36:06 +04:00
|
|
|
typedef ::mozilla::TypeSpecificScopedPointer<Type> name;
|
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
template <typename T>
|
|
|
|
void TypeSpecificDelete(T* aValue);
|
2012-11-16 05:02:11 +04:00
|
|
|
|
2012-07-25 02:36:06 +04:00
|
|
|
template <typename T>
|
|
|
|
struct TypeSpecificScopedPointerTraits {
|
2014-07-11 06:10:17 +04:00
|
|
|
typedef T* type;
|
|
|
|
static type empty() { return nullptr; }
|
|
|
|
static void release(type aValue) {
|
|
|
|
if (aValue) {
|
|
|
|
TypeSpecificDelete(aValue);
|
2012-07-25 02:36:06 +04:00
|
|
|
}
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2012-07-25 02:36:06 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
SCOPED_TEMPLATE(TypeSpecificScopedPointer, TypeSpecificScopedPointerTraits)
|
|
|
|
|
2012-06-12 03:16:47 +04:00
|
|
|
} /* namespace mozilla */
|
|
|
|
|
2013-07-24 11:41:39 +04:00
|
|
|
#endif /* mozilla_Scoped_h */
|