sw/inc/unocrsrhelper.hxx                                |    3 +-
 sw/source/core/doc/DocumentContentOperationsManager.cxx |   11 +++++-----
 sw/source/core/doc/docsort.cxx                          |   10 +++------
 sw/source/core/unocore/unocrsrhelper.cxx                |   17 +++++++---------
 sw/source/uibase/uno/unotxvw.cxx                        |    6 ++---
 5 files changed, 23 insertions(+), 24 deletions(-)

New commits:
commit 50a27ef3fb3d7ceab2acacdccd4048cbedb866f1
Author:     Noel Grandin <noelgran...@gmail.com>
AuthorDate: Wed May 29 18:32:52 2024 +0200
Commit:     Noel Grandin <noel.gran...@collabora.co.uk>
CommitDate: Thu May 30 09:17:37 2024 +0200

    reduce allocation of SwPaM
    
    Change-Id: I32536dbdb178a5f234cf64a8e7627a569986d942
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/168228
    Tested-by: Jenkins
    Reviewed-by: Noel Grandin <noel.gran...@collabora.co.uk>

diff --git a/sw/source/core/doc/DocumentContentOperationsManager.cxx 
b/sw/source/core/doc/DocumentContentOperationsManager.cxx
index 0e5e0db06be1..366cd276440b 100644
--- a/sw/source/core/doc/DocumentContentOperationsManager.cxx
+++ b/sw/source/core/doc/DocumentContentOperationsManager.cxx
@@ -95,6 +95,7 @@
 
 #include <tuple>
 #include <memory>
+#include <optional>
 
 using namespace ::com::sun::star::i18n;
 
@@ -2051,10 +2052,10 @@ bool DocumentContentOperationsManager::CopyRange(SwPaM& 
rPam, SwPosition& rPos,
         }
     }
 
-    SwPaM* pRedlineRange = nullptr;
+    std::optional<SwPaM> pRedlineRange;
     if( rDoc.getIDocumentRedlineAccess().IsRedlineOn() ||
         (!rDoc.getIDocumentRedlineAccess().IsIgnoreRedline() && 
!rDoc.getIDocumentRedlineAccess().GetRedlineTable().empty() ) )
-        pRedlineRange = new SwPaM( rPos );
+        pRedlineRange.emplace( rPos );
 
     RedlineFlags eOld = rDoc.getIDocumentRedlineAccess().GetRedlineFlags();
 
@@ -2062,7 +2063,7 @@ bool DocumentContentOperationsManager::CopyRange(SwPaM& 
rPam, SwPosition& rPos,
 
     if( &rDoc != &m_rDoc )
     {   // ordinary copy
-        bRet = CopyImpl(rPam, rPos, flags & ~SwCopyFlags::CheckPosInFly, 
pRedlineRange);
+        bRet = CopyImpl(rPam, rPos, flags & ~SwCopyFlags::CheckPosInFly, 
pRedlineRange ? &*pRedlineRange : nullptr);
     }
     else if( ! ( *pStt <= rPos && rPos < *pEnd &&
             ( pStt->GetNode() != pEnd->GetNode() ||
@@ -2070,7 +2071,7 @@ bool DocumentContentOperationsManager::CopyRange(SwPaM& 
rPam, SwPosition& rPos,
     {
         // Copy to a position outside of the area, or copy a single TextNode
         // Do an ordinary copy
-        bRet = CopyImpl(rPam, rPos, flags & ~SwCopyFlags::CheckPosInFly, 
pRedlineRange);
+        bRet = CopyImpl(rPam, rPos, flags & ~SwCopyFlags::CheckPosInFly, 
pRedlineRange ? &*pRedlineRange : nullptr);
     }
     else
     {
@@ -2086,7 +2087,7 @@ bool DocumentContentOperationsManager::CopyRange(SwPaM& 
rPam, SwPosition& rPos,
                 new SwRangeRedline(RedlineType::Insert, *pRedlineRange, 
nMovedID), true);
         else
             rDoc.getIDocumentRedlineAccess().SplitRedline( *pRedlineRange );
-        delete pRedlineRange;
+        pRedlineRange.reset();
     }
 
     return bRet;
diff --git a/sw/source/core/doc/docsort.cxx b/sw/source/core/doc/docsort.cxx
index 5be48cdedd30..df5aaf6e5c89 100644
--- a/sw/source/core/doc/docsort.cxx
+++ b/sw/source/core/doc/docsort.cxx
@@ -312,14 +312,14 @@ bool SwDoc::SortText(const SwPaM& rPaM, const 
SwSortOptions& rOpt)
         GetIDocumentUndoRedo().StartUndo( SwUndoId::START, nullptr );
     }
 
-    SwPaM* pRedlPam = nullptr;
+    std::optional<SwPaM> pRedlPam;
     SwUndoRedlineSort* pRedlUndo = nullptr;
     SwUndoSort* pUndoSort = nullptr;
 
     // To-Do - add 'SwExtraRedlineTable' also ?
     if( getIDocumentRedlineAccess().IsRedlineOn() || 
(!getIDocumentRedlineAccess().IsIgnoreRedline() && 
!getIDocumentRedlineAccess().GetRedlineTable().empty() ))
     {
-        pRedlPam = new SwPaM( pStart->GetNode(), pEnd->GetNode(), 
SwNodeOffset(-1), SwNodeOffset(1) );
+        pRedlPam.emplace( pStart->GetNode(), pEnd->GetNode(), 
SwNodeOffset(-1), SwNodeOffset(1) );
         SwContentNode* pCNd = pRedlPam->GetMarkContentNode();
         if( pCNd )
             pRedlPam->GetMark()->SetContent( pCNd->Len() );
@@ -362,8 +362,7 @@ bool SwDoc::SortText(const SwPaM& rPaM, const 
SwSortOptions& rOpt)
         else
         {
             getIDocumentRedlineAccess().DeleteRedline( *pRedlPam, true, 
RedlineType::Any );
-            delete pRedlPam;
-            pRedlPam = nullptr;
+            pRedlPam.reset();
         }
     }
 
@@ -447,8 +446,7 @@ bool SwDoc::SortText(const SwPaM& rPaM, const 
SwSortOptions& rOpt)
             pRedlUndo->SetValues( *pRedlPam );
         }
 
-        delete pRedlPam;
-        pRedlPam = nullptr;
+        pRedlPam.reset();
     }
     GetIDocumentUndoRedo().DoUndo( bUndo );
     if( bUndo )
commit ee4d61b08dabe1c184a49c7301a301ed0dcfc710
Author:     Noel Grandin <noelgran...@gmail.com>
AuthorDate: Wed May 29 18:46:00 2024 +0200
Commit:     Noel Grandin <noel.gran...@collabora.co.uk>
CommitDate: Thu May 30 09:17:27 2024 +0200

    reduce heap allocation
    
    Change-Id: I9e99244cb6a5e79e8856eefceac16cacbca49a96
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/168230
    Tested-by: Jenkins
    Reviewed-by: Noel Grandin <noel.gran...@collabora.co.uk>

diff --git a/sw/inc/unocrsrhelper.hxx b/sw/inc/unocrsrhelper.hxx
index 17a86716c318..d7f8dc3f22ef 100644
--- a/sw/inc/unocrsrhelper.hxx
+++ b/sw/inc/unocrsrhelper.hxx
@@ -23,6 +23,7 @@
 #include "flyenum.hxx"
 #include "pam.hxx"
 
+#include <optional>
 #include <map>
 #include <string_view>
 
@@ -232,7 +233,7 @@ namespace SwUnoCursorHelper
     SW_DLLPUBLIC void GetSelectableFromAny(
         css::uno::Reference<css::uno::XInterface> const& xIfc,
         SwDoc & rTargetDoc,
-        SwPaM *& o_rpPaM, std::pair<OUString, FlyCntType> & o_rFrame,
+        std::optional<SwPaM>& o_rpPaM, std::pair<OUString, FlyCntType> & 
o_rFrame,
         OUString & o_rTableName, SwUnoTableCursor const*& o_rpTableCursor,
         ::sw::mark::IMark const*& o_rpMark,
         std::vector<SdrObject *> & o_rSdrObjects);
diff --git a/sw/source/core/unocore/unocrsrhelper.cxx 
b/sw/source/core/unocore/unocrsrhelper.cxx
index d8868d4f1b02..88c2baafd61d 100644
--- a/sw/source/core/unocore/unocrsrhelper.cxx
+++ b/sw/source/core/unocore/unocrsrhelper.cxx
@@ -105,16 +105,15 @@ using namespace ::com::sun::star::lang;
 namespace SwUnoCursorHelper
 {
 
-static SwPaM* lcl_createPamCopy(const SwPaM& rPam)
+static void lcl_createPamCopy(std::optional<SwPaM>& o_rpPam, const SwPaM& rPam)
 {
-    SwPaM *const pRet = new SwPaM(*rPam.GetPoint());
-    ::sw::DeepCopyPaM(rPam, *pRet);
-    return pRet;
+    o_rpPam.emplace(*rPam.GetPoint());
+    ::sw::DeepCopyPaM(rPam, *o_rpPam);
 }
 
 void GetSelectableFromAny(uno::Reference<uno::XInterface> const& xIfc,
         SwDoc & rTargetDoc,
-        SwPaM *& o_rpPaM, std::pair<OUString, FlyCntType> & o_rFrame,
+        std::optional<SwPaM>& o_rpPaM, std::pair<OUString, FlyCntType> & 
o_rFrame,
         OUString & o_rTableName, SwUnoTableCursor const*& o_rpTableCursor,
         ::sw::mark::IMark const*& o_rpMark,
         std::vector<SdrObject *> & o_rSdrObjects)
@@ -166,7 +165,7 @@ void GetSelectableFromAny(uno::Reference<uno::XInterface> 
const& xIfc,
     {
         if (pCursor->GetDoc() == &rTargetDoc)
         {
-            o_rpPaM = lcl_createPamCopy(*pCursor->GetPaM());
+            lcl_createPamCopy(o_rpPaM, *pCursor->GetPaM());
         }
         return;
     }
@@ -177,7 +176,7 @@ void GetSelectableFromAny(uno::Reference<uno::XInterface> 
const& xIfc,
         SwUnoCursor const* pUnoCursor = pRanges->GetCursor();
         if (pUnoCursor && &pUnoCursor->GetDoc() == &rTargetDoc)
         {
-            o_rpPaM = lcl_createPamCopy(*pUnoCursor);
+            lcl_createPamCopy(o_rpPaM, *pUnoCursor);
         }
         return;
     }
@@ -220,7 +219,7 @@ void GetSelectableFromAny(uno::Reference<uno::XInterface> 
const& xIfc,
             {
                 SwPaM aPam(*pBox->GetSttNd());
                 aPam.Move(fnMoveForward, GoInNode);
-                o_rpPaM = lcl_createPamCopy(aPam);
+                lcl_createPamCopy(o_rpPaM, aPam);
             }
         }
         return;
@@ -232,7 +231,7 @@ void GetSelectableFromAny(uno::Reference<uno::XInterface> 
const& xIfc,
         SwUnoInternalPaM aPam(rTargetDoc);
         if (::sw::XTextRangeToSwPaM(aPam, xTextRange))
         {
-            o_rpPaM = lcl_createPamCopy(aPam);
+            lcl_createPamCopy(o_rpPaM, aPam);
         }
         return;
     }
diff --git a/sw/source/uibase/uno/unotxvw.cxx b/sw/source/uibase/uno/unotxvw.cxx
index fd6e6fdf8bb6..b0ea0d2318ac 100644
--- a/sw/source/uibase/uno/unotxvw.cxx
+++ b/sw/source/uibase/uno/unotxvw.cxx
@@ -151,7 +151,7 @@ sal_Bool SwXTextView::select(const uno::Any& aInterface)
     }
     else
     {
-        SwPaM * pPaM(nullptr);
+        std::optional<SwPaM> pPaM;
         std::pair<OUString, FlyCntType> frame;
         OUString tableName;
         SwUnoTableCursor const* pTableCursor(nullptr);
@@ -163,12 +163,12 @@ sal_Bool SwXTextView::select(const uno::Any& aInterface)
             rSh.EnterStdMode();
             rSh.SetSelection(*pPaM);
             // the pPaM has been copied - delete it
-            while (pPaM->GetNext() != pPaM)
+            while (pPaM->GetNext() != &*pPaM)
             {
                 // coverity[deref_arg] - the SwPaM delete moves a new entry 
into GetNext()
                 delete pPaM->GetNext();
             }
-            delete pPaM;
+            pPaM.reset();
             return true;
         }
         else if (!frame.first.isEmpty())

Reply via email to