Bug 909514 - Include <new> before mozilla::Maybe (and move Maybe into mfbt/Maybe.h). r=waldo
authorJustin Lebar <justin.lebar@gmail.com>
Tue, 27 Aug 2013 15:10:28 -0700
changeset 144586 535e7c112fc63acb9d7ca92d71a1cad2fc7f03e6
parent 144585 f619e14327addef2cc1ad48e5e76aaaa3c71b926
child 144587 2308ce99493cd08869c214ac556ffdf595ade9b0
push id25168
push useremorley@mozilla.com
push dateWed, 28 Aug 2013 11:21:34 +0000
treeherdermozilla-central@6afb1f453688 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerswaldo
bugs909514
milestone26.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 909514 - Include <new> before mozilla::Maybe (and move Maybe into mfbt/Maybe.h). r=waldo
content/events/src/nsEventListenerService.cpp
content/events/src/nsEventListenerService.h
dom/bindings/BindingDeclarations.h
dom/src/json/nsJSON.cpp
gfx/layers/basic/BasicLayersImpl.h
image/src/ClippedImage.h
image/src/ImageMetadata.h
js/src/jsproxy.h
js/src/vm/Shape.h
js/xpconnect/src/nsCxPusher.h
layout/base/nsRefreshDriver.h
mfbt/Maybe.h
mfbt/Util.h
mfbt/exported_headers.mk
netwerk/base/src/nsMediaFragmentURIParser.h
startupcache/test/TestStartupCache.cpp
--- a/content/events/src/nsEventListenerService.cpp
+++ b/content/events/src/nsEventListenerService.cpp
@@ -15,16 +15,17 @@
 #include "nsJSUtils.h"
 #include "nsGUIEvent.h"
 #include "nsEventDispatcher.h"
 #include "nsIJSEventListener.h"
 #ifdef MOZ_JSDEBUGGER
 #include "jsdIDebuggerService.h"
 #endif
 #include "nsDOMClassInfoID.h"
+#include "mozilla/Maybe.h"
 
 using namespace mozilla::dom;
 using mozilla::AutoSafeJSContext;
 
 NS_IMPL_CYCLE_COLLECTION_1(nsEventListenerInfo, mListener)
 
 DOMCI_DATA(EventListenerInfo, nsEventListenerInfo)
 
--- a/content/events/src/nsEventListenerService.h
+++ b/content/events/src/nsEventListenerService.h
@@ -8,16 +8,20 @@
 #include "nsIEventListenerService.h"
 #include "nsAutoPtr.h"
 #include "nsIDOMEventListener.h"
 #include "nsString.h"
 #include "nsCycleCollectionParticipant.h"
 #include "jsapi.h"
 #include "mozilla/Attributes.h"
 
+namespace mozilla {
+template<typename T>
+class Maybe;
+} // namespace mozilla
 
 class nsEventListenerInfo : public nsIEventListenerInfo
 {
 public:
   nsEventListenerInfo(const nsAString& aType,
                       already_AddRefed<nsIDOMEventListener> aListener,
                       bool aCapturing, bool aAllowsUntrusted,
                       bool aInSystemEventGroup)
--- a/dom/bindings/BindingDeclarations.h
+++ b/dom/bindings/BindingDeclarations.h
@@ -11,17 +11,18 @@
  * BindingUtils.h
  */
 #ifndef mozilla_dom_BindingDeclarations_h__
 #define mozilla_dom_BindingDeclarations_h__
 
 #include "nsStringGlue.h"
 #include "js/Value.h"
 #include "js/RootingAPI.h"
-#include "mozilla/Util.h"
+#include "jsapi.h"
+#include "mozilla/Maybe.h"
 #include "nsCOMPtr.h"
 #include "nsDOMString.h"
 #include "nsStringBuffer.h"
 #include "nsTArray.h"
 #include "nsAutoPtr.h" // for nsRefPtr member variables
 
 struct JSContext;
 class JSObject;
--- a/dom/src/json/nsJSON.cpp
+++ b/dom/src/json/nsJSON.cpp
@@ -16,16 +16,17 @@
 #include "nsICharsetConverterManager.h"
 #include "nsXPCOMStrings.h"
 #include "nsNetUtil.h"
 #include "nsContentUtils.h"
 #include "nsIScriptError.h"
 #include "nsCRTGlue.h"
 #include "nsAutoPtr.h"
 #include "nsIScriptSecurityManager.h"
+#include "mozilla/Maybe.h"
 #include <algorithm>
 
 static const char kXPConnectServiceCID[] = "@mozilla.org/js/xpc/XPConnect;1";
 
 #define JSON_STREAM_BUFSIZE 4096
 
 NS_INTERFACE_MAP_BEGIN(nsJSON)
   NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIJSON)
--- a/gfx/layers/basic/BasicLayersImpl.h
+++ b/gfx/layers/basic/BasicLayersImpl.h
@@ -9,17 +9,17 @@
 #include "BasicImplData.h"              // for BasicImplData
 #include "BasicLayers.h"                // for BasicLayerManager
 #include "ReadbackLayer.h"              // for ReadbackLayer
 #include "gfxASurface.h"                // for gfxASurface
 #include "gfxContext.h"                 // for gfxContext, etc
 #include "gfxMatrix.h"                  // for gfxMatrix
 #include "ipc/AutoOpenSurface.h"        // for AutoOpenSurface
 #include "mozilla/Attributes.h"         // for MOZ_DELETE, MOZ_STACK_CLASS
-#include "mozilla/Util.h"               // for Maybe
+#include "mozilla/Maybe.h"              // for Maybe
 #include "mozilla/layers/LayersSurfaces.h"  // for SurfaceDescriptor
 #include "nsAutoPtr.h"                  // for nsRefPtr
 #include "nsDebug.h"                    // for NS_ASSERTION
 #include "nsISupportsImpl.h"            // for gfxContext::Release, etc
 #include "nsRegion.h"                   // for nsIntRegion
 #include "nsTraceRefcnt.h"              // for MOZ_COUNT_CTOR, etc
 
 namespace mozilla {
--- a/image/src/ClippedImage.h
+++ b/image/src/ClippedImage.h
@@ -2,17 +2,17 @@
 /* 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/. */
 
 #ifndef MOZILLA_IMAGELIB_CLIPPEDIMAGE_H_
 #define MOZILLA_IMAGELIB_CLIPPEDIMAGE_H_
 
 #include "ImageWrapper.h"
-#include "mozilla/Util.h" // for Maybe
+#include "mozilla/Maybe.h"
 
 namespace mozilla {
 namespace image {
 
 class ClippedImageCachedSurface;
 class DrawSingleTileCallback;
 
 /**
--- a/image/src/ImageMetadata.h
+++ b/image/src/ImageMetadata.h
@@ -1,16 +1,16 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  *
  * 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/. */
 
 #include <stdint.h>
-#include "mozilla/Util.h"
+#include "mozilla/Maybe.h"
 #include "nsSize.h"
 #include "Orientation.h"
 
 namespace mozilla {
 namespace image {
 
 class RasterImage;
 
--- a/js/src/jsproxy.h
+++ b/js/src/jsproxy.h
@@ -2,16 +2,18 @@
  * vim: set ts=8 sts=4 et sw=4 tw=99:
  * 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/. */
 
 #ifndef jsproxy_h
 #define jsproxy_h
 
+#include "mozilla/Maybe.h"
+
 #include "jsapi.h"
 #include "jsfriendapi.h"
 
 namespace js {
 
 class RegExpGuard;
 class JS_FRIEND_API(Wrapper);
 
--- a/js/src/vm/Shape.h
+++ b/js/src/vm/Shape.h
@@ -4,16 +4,17 @@
  * 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/. */
 
 #ifndef vm_Shape_h
 #define vm_Shape_h
 
 #include "mozilla/Attributes.h"
 #include "mozilla/GuardObjects.h"
+#include "mozilla/Maybe.h"
 #include "mozilla/MemoryReporting.h"
 #include "mozilla/TemplateLib.h"
 
 #include "jspropertytree.h"
 #include "jstypes.h"
 
 #include "gc/Heap.h"
 #include "js/HashTable.h"
--- a/js/xpconnect/src/nsCxPusher.h
+++ b/js/xpconnect/src/nsCxPusher.h
@@ -1,16 +1,16 @@
 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 
 #ifndef nsCxPusher_h
 #define nsCxPusher_h
 
 #include "jsapi.h"
-#include "mozilla/Util.h"
+#include "mozilla/Maybe.h"
 #include "nsCOMPtr.h"
 
 namespace mozilla {
 namespace dom {
 class EventTarget;
 }
 }
 
--- a/layout/base/nsRefreshDriver.h
+++ b/layout/base/nsRefreshDriver.h
@@ -15,17 +15,17 @@
 #include "mozilla/TimeStamp.h"
 #include "mozFlushType.h"
 #include "nsTObserverArray.h"
 #include "nsTArray.h"
 #include "nsTHashtable.h"
 #include "nsClassHashtable.h"
 #include "nsHashKeys.h"
 #include "mozilla/Attributes.h"
-#include "mozilla/Util.h"
+#include "mozilla/Maybe.h"
 
 class nsPresContext;
 class nsIPresShell;
 class nsIDocument;
 class imgIRequest;
 
 /**
  * An abstract base class to be implemented by callers wanting to be
copy from mfbt/Util.h
copy to mfbt/Maybe.h
--- a/mfbt/Util.h
+++ b/mfbt/Maybe.h
@@ -1,29 +1,24 @@
 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* 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/. */
 
-/*
- * Miscellaneous uncategorized functionality.  Please add new functionality to
- * new headers, or to other appropriate existing headers, not here.
- */
+/* A class for lazily constructing an object without sticking it on the heap. */
 
-#ifndef mozilla_Util_h
-#define mozilla_Util_h
-
-#include "mozilla/Assertions.h"
-#include "mozilla/Attributes.h"
-#include "mozilla/Types.h"
-
-#ifdef __cplusplus
+#ifndef mozilla_Maybe_h
+#define mozilla_Maybe_h
 
 #include "mozilla/Alignment.h"
+#include "mozilla/Assertions.h"
+
+// For placement new
+#include <new>
 
 namespace mozilla {
 
 /*
  * Small utility for lazily constructing objects without using dynamic storage.
  * When a Maybe<T> is constructed, it is |empty()|, i.e., no value of T has
  * been constructed and no T destructor will be called when the Maybe<T> is
  * destroyed. Upon calling |construct|, a T object will be constructed with the
@@ -158,63 +153,11 @@ class Maybe
         destroy();
     }
 
   private:
     Maybe(const Maybe& other) MOZ_DELETE;
     const Maybe& operator=(const Maybe& other) MOZ_DELETE;
 };
 
-/*
- * Safely subtract two pointers when it is known that end >= begin.  This avoids
- * the common compiler bug that if (size_t(end) - size_t(begin)) has the MSB
- * set, the unsigned subtraction followed by right shift will produce -1, or
- * size_t(-1), instead of the real difference.
- */
-template<class T>
-MOZ_ALWAYS_INLINE size_t
-PointerRangeSize(T* begin, T* end)
-{
-  MOZ_ASSERT(end >= begin);
-  return (size_t(end) - size_t(begin)) / sizeof(T);
-}
-
-/*
- * Compute the length of an array with constant length.  (Use of this method
- * with a non-array pointer will not compile.)
- *
- * Beware of the implicit trailing '\0' when using this with string constants.
- */
-template<typename T, size_t N>
-MOZ_CONSTEXPR size_t
-ArrayLength(T (&arr)[N])
-{
-  return N;
-}
+} // namespace mozilla
 
-/*
- * Compute the address one past the last element of a constant-length array.
- *
- * Beware of the implicit trailing '\0' when using this with string constants.
- */
-template<typename T, size_t N>
-MOZ_CONSTEXPR T*
-ArrayEnd(T (&arr)[N])
-{
-  return arr + ArrayLength(arr);
-}
-
-} /* namespace mozilla */
-
-#endif /* __cplusplus */
-
-/*
- * MOZ_ARRAY_LENGTH() is an alternative to mozilla::ArrayLength() for C files
- * that can't use C++ template functions and for static_assert() calls that
- * can't call ArrayLength() when it is not a C++11 constexpr function.
- */
-#ifdef MOZ_HAVE_CXX11_CONSTEXPR
-#  define MOZ_ARRAY_LENGTH(array)   mozilla::ArrayLength(array)
-#else
-#  define MOZ_ARRAY_LENGTH(array)   (sizeof(array)/sizeof((array)[0]))
-#endif
-
-#endif /* mozilla_Util_h */
+#endif /* mozilla_Maybe_h */
--- a/mfbt/Util.h
+++ b/mfbt/Util.h
@@ -18,157 +18,16 @@
 
 #ifdef __cplusplus
 
 #include "mozilla/Alignment.h"
 
 namespace mozilla {
 
 /*
- * Small utility for lazily constructing objects without using dynamic storage.
- * When a Maybe<T> is constructed, it is |empty()|, i.e., no value of T has
- * been constructed and no T destructor will be called when the Maybe<T> is
- * destroyed. Upon calling |construct|, a T object will be constructed with the
- * given arguments and that object will be destroyed when the owning Maybe<T>
- * is destroyed.
- *
- * N.B. GCC seems to miss some optimizations with Maybe and may generate extra
- * branches/loads/stores. Use with caution on hot paths.
- */
-template<class T>
-class Maybe
-{
-    AlignedStorage2<T> storage;
-    bool constructed;
-
-    T& asT() { return *storage.addr(); }
-
-  public:
-    Maybe() { constructed = false; }
-    ~Maybe() { if (constructed) asT().~T(); }
-
-    bool empty() const { return !constructed; }
-
-    void construct() {
-      MOZ_ASSERT(!constructed);
-      ::new (storage.addr()) T();
-      constructed = true;
-    }
-
-    template<class T1>
-    void construct(const T1& t1) {
-      MOZ_ASSERT(!constructed);
-      ::new (storage.addr()) T(t1);
-      constructed = true;
-    }
-
-    template<class T1, class T2>
-    void construct(const T1& t1, const T2& t2) {
-      MOZ_ASSERT(!constructed);
-      ::new (storage.addr()) T(t1, t2);
-      constructed = true;
-    }
-
-    template<class T1, class T2, class T3>
-    void construct(const T1& t1, const T2& t2, const T3& t3) {
-      MOZ_ASSERT(!constructed);
-      ::new (storage.addr()) T(t1, t2, t3);
-      constructed = true;
-    }
-
-    template<class T1, class T2, class T3, class T4>
-    void construct(const T1& t1, const T2& t2, const T3& t3, const T4& t4) {
-      MOZ_ASSERT(!constructed);
-      ::new (storage.addr()) T(t1, t2, t3, t4);
-      constructed = true;
-    }
-
-    template<class T1, class T2, class T3, class T4, class T5>
-    void construct(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5) {
-      MOZ_ASSERT(!constructed);
-      ::new (storage.addr()) T(t1, t2, t3, t4, t5);
-      constructed = true;
-    }
-
-    template<class T1, class T2, class T3, class T4, class T5,
-             class T6>
-    void construct(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5,
-                   const T6& t6) {
-      MOZ_ASSERT(!constructed);
-      ::new (storage.addr()) T(t1, t2, t3, t4, t5, t6);
-      constructed = true;
-    }
-
-    template<class T1, class T2, class T3, class T4, class T5,
-             class T6, class T7>
-    void construct(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5,
-                   const T6& t6, const T7& t7) {
-      MOZ_ASSERT(!constructed);
-      ::new (storage.addr()) T(t1, t2, t3, t4, t5, t6, t7);
-      constructed = true;
-    }
-
-    template<class T1, class T2, class T3, class T4, class T5,
-             class T6, class T7, class T8>
-    void construct(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5,
-                   const T6& t6, const T7& t7, const T8& t8) {
-      MOZ_ASSERT(!constructed);
-      ::new (storage.addr()) T(t1, t2, t3, t4, t5, t6, t7, t8);
-      constructed = true;
-    }
-
-    template<class T1, class T2, class T3, class T4, class T5,
-             class T6, class T7, class T8, class T9>
-    void construct(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5,
-                   const T6& t6, const T7& t7, const T8& t8, const T9& t9) {
-      MOZ_ASSERT(!constructed);
-      ::new (storage.addr()) T(t1, t2, t3, t4, t5, t6, t7, t8, t9);
-      constructed = true;
-    }
-
-    template<class T1, class T2, class T3, class T4, class T5,
-             class T6, class T7, class T8, class T9, class T10>
-    void construct(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5,
-                   const T6& t6, const T7& t7, const T8& t8, const T9& t9, const T10& t10) {
-      MOZ_ASSERT(!constructed);
-      ::new (storage.addr()) T(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10);
-      constructed = true;
-    }
-
-    T* addr() {
-      MOZ_ASSERT(constructed);
-      return &asT();
-    }
-
-    T& ref() {
-      MOZ_ASSERT(constructed);
-      return asT();
-    }
-
-    const T& ref() const {
-      MOZ_ASSERT(constructed);
-      return const_cast<Maybe*>(this)->asT();
-    }
-
-    void destroy() {
-      ref().~T();
-      constructed = false;
-    }
-
-    void destroyIfConstructed() {
-      if (!empty())
-        destroy();
-    }
-
-  private:
-    Maybe(const Maybe& other) MOZ_DELETE;
-    const Maybe& operator=(const Maybe& other) MOZ_DELETE;
-};
-
-/*
  * Safely subtract two pointers when it is known that end >= begin.  This avoids
  * the common compiler bug that if (size_t(end) - size_t(begin)) has the MSB
  * set, the unsigned subtraction followed by right shift will produce -1, or
  * size_t(-1), instead of the real difference.
  */
 template<class T>
 MOZ_ALWAYS_INLINE size_t
 PointerRangeSize(T* begin, T* end)
--- a/mfbt/exported_headers.mk
+++ b/mfbt/exported_headers.mk
@@ -26,16 +26,17 @@ EXPORTS_mozilla += \
   Endian.h \
   EnumSet.h \
   FloatingPoint.h \
   GuardObjects.h \
   HashFunctions.h \
   Likely.h \
   LinkedList.h \
   MathAlgorithms.h \
+  Maybe.h \
   MemoryChecking.h \
   MemoryReporting.h \
   Move.h \
   NullPtr.h \
   PodOperations.h \
   Poison.h \
   Range.h \
   RangedPtr.h \
--- a/netwerk/base/src/nsMediaFragmentURIParser.h
+++ b/netwerk/base/src/nsMediaFragmentURIParser.h
@@ -1,17 +1,17 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim:set ts=2 sw=2 sts=2 et cindent: */
 /* 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/. */
 #if !defined(nsMediaFragmentURIParser_h__)
 #define nsMediaFragmentURIParser_h__
 
-#include "mozilla/Util.h" // Maybe
+#include "mozilla/Maybe.h"
 #include "nsIURI.h"
 #include "nsString.h"
 #include "nsRect.h"
 
 // Class to handle parsing of a W3C media fragment URI as per
 // spec at: http://www.w3.org/TR/media-frags/
 // Only the temporaral URI portion of the spec is implemented.
 // To use:
--- a/startupcache/test/TestStartupCache.cpp
+++ b/startupcache/test/TestStartupCache.cpp
@@ -19,16 +19,17 @@
 #include "nsIURI.h"
 #include "nsStringAPI.h"
 #include "nsIPrefBranch.h"
 #include "nsIPrefService.h"
 #include "nsITelemetry.h"
 #include "nsIXPConnect.h"
 #include "jsapi.h"
 #include "prio.h"
+#include "mozilla/Maybe.h"
 
 using namespace JS;
 
 namespace mozilla {
 namespace scache {
 
 NS_IMPORT nsresult
 NewObjectInputStreamFromBuffer(char* buffer, uint32_t len,