From 30d4df57785de9208898c5011637d9c13402d6b7 Mon Sep 17 00:00:00 2001 From: Ehsan Akhgari Date: Sat, 29 Jul 2017 02:43:39 -0400 Subject: [PATCH] Bug 1385538 - Avoid dynamic memory allocation for EditorBase::mSelState; r=masayuki --- editor/libeditor/EditorBase.cpp | 5 ++--- editor/libeditor/EditorBase.h | 4 ++-- editor/libeditor/PlaceholderTransaction.cpp | 20 +++++++------------- editor/libeditor/PlaceholderTransaction.h | 7 +++---- 4 files changed, 14 insertions(+), 22 deletions(-) diff --git a/editor/libeditor/EditorBase.cpp b/editor/libeditor/EditorBase.cpp index 235d03abc505..b5b22631455b 100644 --- a/editor/libeditor/EditorBase.cpp +++ b/editor/libeditor/EditorBase.cpp @@ -128,7 +128,6 @@ using namespace widget; EditorBase::EditorBase() : mPlaceholderName(nullptr) - , mSelState(nullptr) , mModCount(0) , mFlags(0) , mUpdateCount(0) @@ -974,7 +973,7 @@ EditorBase::BeginPlaceHolderTransaction(nsIAtom* aName) mPlaceholderName = aName; RefPtr selection = GetSelection(); if (selection) { - mSelState = MakeUnique(); + mSelState.emplace(); mSelState->SaveSelection(selection); // Composition transaction can modify multiple nodes and it merges text // node for ime into single text node. @@ -1039,7 +1038,7 @@ EditorBase::EndPlaceHolderTransaction() if (mPlaceholderName == nsGkAtoms::IMETxnName) { mRangeUpdater.DropSelectionState(*mSelState); } - mSelState = nullptr; + mSelState.reset(); } // We might have never made a placeholder if no action took place. if (mPlaceholderTransaction) { diff --git a/editor/libeditor/EditorBase.h b/editor/libeditor/EditorBase.h index 339c48021bf7..8bd52151ab7f 100644 --- a/editor/libeditor/EditorBase.h +++ b/editor/libeditor/EditorBase.h @@ -7,10 +7,10 @@ #define mozilla_EditorBase_h #include "mozilla/Assertions.h" // for MOZ_ASSERT, etc. +#include "mozilla/Maybe.h" // for Maybe #include "mozilla/OwningNonNull.h" // for OwningNonNull #include "mozilla/SelectionState.h" // for RangeUpdater, etc. #include "mozilla/StyleSheet.h" // for StyleSheet -#include "mozilla/UniquePtr.h" #include "mozilla/WeakPtr.h" // for WeakPtr #include "mozilla/dom/Text.h" #include "nsCOMPtr.h" // for already_AddRefed, nsCOMPtr @@ -1133,7 +1133,7 @@ protected: // Name of placeholder transaction. nsIAtom* mPlaceholderName; // Saved selection state for placeholder transaction batching. - mozilla::UniquePtr mSelState; + mozilla::Maybe mSelState; // IME composition this is not null between compositionstart and // compositionend. RefPtr mComposition; diff --git a/editor/libeditor/PlaceholderTransaction.cpp b/editor/libeditor/PlaceholderTransaction.cpp index 142a85075ff2..8f1fb812d30a 100644 --- a/editor/libeditor/PlaceholderTransaction.cpp +++ b/editor/libeditor/PlaceholderTransaction.cpp @@ -19,12 +19,12 @@ using namespace dom; PlaceholderTransaction::PlaceholderTransaction( EditorBase& aEditorBase, nsIAtom* aName, - UniquePtr aSelState) + Maybe&& aSelState) : mAbsorb(true) , mForwarding(nullptr) , mCompositionTransaction(nullptr) , mCommitted(false) - , mStartSel(Move(aSelState)) + , mStartSel(Move(*aSelState)) , mEditorBase(&aEditorBase) { mName = aName; @@ -38,19 +38,15 @@ NS_IMPL_CYCLE_COLLECTION_CLASS(PlaceholderTransaction) NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(PlaceholderTransaction, EditAggregateTransaction) - if (tmp->mStartSel) { - ImplCycleCollectionUnlink(*tmp->mStartSel); - } NS_IMPL_CYCLE_COLLECTION_UNLINK(mEditorBase); + NS_IMPL_CYCLE_COLLECTION_UNLINK(mStartSel); NS_IMPL_CYCLE_COLLECTION_UNLINK(mEndSel); NS_IMPL_CYCLE_COLLECTION_UNLINK_END NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(PlaceholderTransaction, EditAggregateTransaction) - if (tmp->mStartSel) { - ImplCycleCollectionTraverse(cb, *tmp->mStartSel, "mStartSel", 0); - } NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mEditorBase); + NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mStartSel); NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mEndSel); NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END @@ -78,12 +74,10 @@ PlaceholderTransaction::UndoTransaction() nsresult rv = EditAggregateTransaction::UndoTransaction(); NS_ENSURE_SUCCESS(rv, rv); - NS_ENSURE_TRUE(mStartSel, NS_ERROR_NULL_POINTER); - // now restore selection RefPtr selection = mEditorBase->GetSelection(); NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER); - return mStartSel->RestoreSelection(selection); + return mStartSel.RestoreSelection(selection); } NS_IMETHODIMP @@ -222,11 +216,11 @@ PlaceholderTransaction::StartSelectionEquals(SelectionState* aSelState, // determine if starting selection matches the given selection state. // note that we only care about collapsed selections. NS_ENSURE_TRUE(aResult && aSelState, NS_ERROR_NULL_POINTER); - if (!mStartSel->IsCollapsed() || !aSelState->IsCollapsed()) { + if (!mStartSel.IsCollapsed() || !aSelState->IsCollapsed()) { *aResult = false; return NS_OK; } - *aResult = mStartSel->IsEqual(aSelState); + *aResult = mStartSel.IsEqual(aSelState); return NS_OK; } diff --git a/editor/libeditor/PlaceholderTransaction.h b/editor/libeditor/PlaceholderTransaction.h index d97c06814339..5b726b994505 100644 --- a/editor/libeditor/PlaceholderTransaction.h +++ b/editor/libeditor/PlaceholderTransaction.h @@ -8,7 +8,7 @@ #include "EditAggregateTransaction.h" #include "mozilla/EditorUtils.h" -#include "mozilla/UniquePtr.h" +#include "mozilla/Maybe.h" #include "nsIAbsorbingTransaction.h" #include "nsIDOMNode.h" #include "nsCOMPtr.h" @@ -33,7 +33,7 @@ public: NS_DECL_ISUPPORTS_INHERITED PlaceholderTransaction(EditorBase& aEditorBase, nsIAtom* aName, - UniquePtr aSelState); + Maybe&& aSelState); NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(PlaceholderTransaction, EditAggregateTransaction) @@ -81,8 +81,7 @@ protected: // at the end. This is so that UndoTransaction() and RedoTransaction() can // restore the selection properly. - // Use a pointer because this is constructed before we exist. - UniquePtr mStartSel; + SelectionState mStartSel; SelectionState mEndSel; // The editor for this transaction.