Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6)

Unified Diff: src/IceDefs.h

Issue 1181013016: Subzero. Fixes memory leaks. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-subzero.git@master
Patch Set: Addresses codereview comments. Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/IceConverter.cpp ('k') | src/IceELFObjectWriter.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/IceDefs.h
diff --git a/src/IceDefs.h b/src/IceDefs.h
index 0a1fdd11326693dcd41e5b5be2aabddcaa6008b6..71dfb63dfa2b8d38fa784b2b295afab2e5e19377 100644
--- a/src/IceDefs.h
+++ b/src/IceDefs.h
@@ -93,6 +93,46 @@ inline bool operator!=(const CfgLocalAllocator<T> &,
return false;
}
+// makeUnique should be used when memory is expected to be allocated from the
+// heap (as opposed to allocated from some Allocator.) It is intended to be used
+// instead of new.
+//
+// The expected usage is as follows
+//
+// class MyClass {
+// public:
+// static std::unique_ptr<MyClass> create(<ctor_args>) {
+// return makeUnique<MyClass>(<ctor_args>);
+// }
+//
+// private:
+// ENABLE_MAKE_UNIQUE;
+//
+// MyClass(<ctor_args>) ...
+// }
+//
+// ENABLE_MAKE_UNIQUE is a trick that is necessary if MyClass' ctor is private.
+// Private ctors are highly encouraged when you're writing a class that you'd
+// like to have allocated with makeUnique as it would prevent users from
+// declaring stack allocated variables.
+namespace Internal {
+struct MakeUniqueEnabler {
+ template <class T, class... Args>
+ static std::unique_ptr<T> create(Args &&... TheArgs) {
+ std::unique_ptr<T> Unique(new T(std::forward<Args>(TheArgs)...));
+ return Unique;
+ }
+};
+} // end of namespace Internal
+
+template <class T, class... Args>
+static std::unique_ptr<T> makeUnique(Args &&... TheArgs) {
+ return ::Ice::Internal::MakeUniqueEnabler::create<T>(
+ std::forward<Args>(TheArgs)...);
+}
+
+#define ENABLE_MAKE_UNIQUE friend struct ::Ice::Internal::MakeUniqueEnabler
+
typedef std::string IceString;
typedef llvm::ilist<Inst> InstList;
// Ideally PhiList would be llvm::ilist<InstPhi>, and similar for
« no previous file with comments | « src/IceConverter.cpp ('k') | src/IceELFObjectWriter.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698