Bug 1220190 - use UniquePtr<T[]> instead of delete[] calls in layout/xul/; r=dholbert

This commit is contained in:
Nathan Froyd 2015-10-30 11:45:39 -04:00
Родитель 103dd0c968
Коммит ffdbeed5f1
3 изменённых файлов: 53 добавлений и 81 удалений

Просмотреть файл

@ -92,9 +92,9 @@ In this case 5 extra columns will be added to the column list to handle the situ
These are called extraColumns/Rows. These are called extraColumns/Rows.
*/ */
using namespace mozilla;
nsGrid::nsGrid():mBox(nullptr), nsGrid::nsGrid():mBox(nullptr),
mRows(nullptr),
mColumns(nullptr),
mRowsBox(nullptr), mRowsBox(nullptr),
mColumnsBox(nullptr), mColumnsBox(nullptr),
mNeedsRebuild(true), mNeedsRebuild(true),
@ -102,7 +102,6 @@ nsGrid::nsGrid():mBox(nullptr),
mColumnCount(0), mColumnCount(0),
mExtraRowCount(0), mExtraRowCount(0),
mExtraColumnCount(0), mExtraColumnCount(0),
mCellMap(nullptr),
mMarkingDirty(false) mMarkingDirty(false)
{ {
MOZ_COUNT_CTOR(nsGrid); MOZ_COUNT_CTOR(nsGrid);
@ -205,8 +204,8 @@ nsGrid::RebuildIfNeeded()
} }
// build and poplulate row and columns arrays // build and poplulate row and columns arrays
BuildRows(mRowsBox, rowCount, &mRows, true); mRows = BuildRows(mRowsBox, rowCount, true);
BuildRows(mColumnsBox, columnCount, &mColumns, false); mColumns = BuildRows(mColumnsBox, columnCount, false);
// build and populate the cell map // build and populate the cell map
mCellMap = BuildCellMap(rowCount, columnCount); mCellMap = BuildCellMap(rowCount, columnCount);
@ -215,22 +214,13 @@ nsGrid::RebuildIfNeeded()
mColumnCount = columnCount; mColumnCount = columnCount;
// populate the cell map from column and row children // populate the cell map from column and row children
PopulateCellMap(mRows, mColumns, mRowCount, mColumnCount, true); PopulateCellMap(mRows.get(), mColumns.get(), mRowCount, mColumnCount, true);
PopulateCellMap(mColumns, mRows, mColumnCount, mRowCount, false); PopulateCellMap(mColumns.get(), mRows.get(), mColumnCount, mRowCount, false);
} }
void void
nsGrid::FreeMap() nsGrid::FreeMap()
{ {
if (mRows)
delete[] mRows;
if (mColumns)
delete[] mColumns;
if (mCellMap)
delete[] mCellMap;
mRows = nullptr; mRows = nullptr;
mColumns = nullptr; mColumns = nullptr;
mCellMap = nullptr; mCellMap = nullptr;
@ -313,44 +303,36 @@ nsGrid::CountRowsColumns(nsIFrame* aRowBox, int32_t& aRowCount, int32_t& aComput
/** /**
* Given the number of rows create nsGridRow objects for them and full them out. * Given the number of rows create nsGridRow objects for them and full them out.
*/ */
void UniquePtr<nsGridRow[]>
nsGrid::BuildRows(nsIFrame* aBox, int32_t aRowCount, nsGridRow** aRows, bool aIsHorizontal) nsGrid::BuildRows(nsIFrame* aBox, int32_t aRowCount, bool aIsHorizontal)
{ {
// if no rows then return null // if no rows then return null
if (aRowCount == 0) { if (aRowCount == 0) {
return nullptr;
// make sure we free up the memory.
if (*aRows)
delete[] (*aRows);
*aRows = nullptr;
return;
} }
// create the array // create the array
nsGridRow* row; UniquePtr<nsGridRow[]> row;
// only create new rows if we have to. Reuse old rows. // only create new rows if we have to. Reuse old rows.
if (aIsHorizontal) if (aIsHorizontal)
{ {
if (aRowCount > mRowCount) { if (aRowCount > mRowCount) {
delete[] mRows; row = MakeUnique<nsGridRow[]>(aRowCount);
row = new nsGridRow[aRowCount];
} else { } else {
for (int32_t i=0; i < mRowCount; i++) for (int32_t i=0; i < mRowCount; i++)
mRows[i].Init(nullptr, false); mRows[i].Init(nullptr, false);
row = mRows; row = Move(mRows);
} }
} else { } else {
if (aRowCount > mColumnCount) { if (aRowCount > mColumnCount) {
delete[] mColumns; row = MakeUnique<nsGridRow[]>(aRowCount);
row = new nsGridRow[aRowCount];
} else { } else {
for (int32_t i=0; i < mColumnCount; i++) for (int32_t i=0; i < mColumnCount; i++)
mColumns[i].Init(nullptr, false); mColumns[i].Init(nullptr, false);
row = mColumns; row = Move(mColumns);
} }
} }
@ -359,40 +341,36 @@ nsGrid::BuildRows(nsIFrame* aBox, int32_t aRowCount, nsGridRow** aRows, bool aIs
{ {
nsCOMPtr<nsIGridPart> monument = GetPartFromBox(aBox); nsCOMPtr<nsIGridPart> monument = GetPartFromBox(aBox);
if (monument) { if (monument) {
monument->BuildRows(aBox, row); monument->BuildRows(aBox, row.get());
} }
} }
*aRows = row; return row;
} }
/** /**
* Given the number of rows and columns. Build a cellmap * Given the number of rows and columns. Build a cellmap
*/ */
nsGridCell* UniquePtr<nsGridCell[]>
nsGrid::BuildCellMap(int32_t aRows, int32_t aColumns) nsGrid::BuildCellMap(int32_t aRows, int32_t aColumns)
{ {
int32_t size = aRows*aColumns; int32_t size = aRows*aColumns;
int32_t oldsize = mRowCount*mColumnCount; int32_t oldsize = mRowCount*mColumnCount;
if (size == 0) { if (size == 0) {
delete[] mCellMap; return nullptr;
} }
else {
if (size > oldsize) { if (size > oldsize) {
delete[] mCellMap; return MakeUnique<nsGridCell[]>(size);
return new nsGridCell[size];
} else {
// clear out cellmap
for (int32_t i=0; i < oldsize; i++)
{
mCellMap[i].SetBoxInRow(nullptr);
mCellMap[i].SetBoxInColumn(nullptr);
}
return mCellMap;
}
} }
return nullptr;
// clear out cellmap
for (int32_t i=0; i < oldsize; i++) {
mCellMap[i].SetBoxInRow(nullptr);
mCellMap[i].SetBoxInColumn(nullptr);
}
return Move(mCellMap);
} }
/** /**

Просмотреть файл

@ -10,6 +10,7 @@
#include "nsStackLayout.h" #include "nsStackLayout.h"
#include "nsIGridPart.h" #include "nsIGridPart.h"
#include "nsCOMPtr.h" #include "nsCOMPtr.h"
#include "mozilla/UniquePtr.h"
class nsBoxLayoutState; class nsBoxLayoutState;
class nsGridCell; class nsGridCell;
@ -84,8 +85,9 @@ private:
void FreeMap(); void FreeMap();
void FindRowsAndColumns(nsIFrame** aRows, nsIFrame** aColumns); void FindRowsAndColumns(nsIFrame** aRows, nsIFrame** aColumns);
void BuildRows(nsIFrame* aBox, int32_t aSize, nsGridRow** aColumnsRows, bool aIsHorizontal = true); mozilla::UniquePtr<nsGridRow[]> BuildRows(nsIFrame* aBox, int32_t aSize,
nsGridCell* BuildCellMap(int32_t aRows, int32_t aColumns); bool aIsHorizontal = true);
mozilla::UniquePtr<nsGridCell[]> BuildCellMap(int32_t aRows, int32_t aColumns);
void PopulateCellMap(nsGridRow* aRows, nsGridRow* aColumns, int32_t aRowCount, int32_t aColumnCount, bool aIsHorizontal = true); void PopulateCellMap(nsGridRow* aRows, nsGridRow* aColumns, int32_t aRowCount, int32_t aColumnCount, bool aIsHorizontal = true);
void CountRowsColumns(nsIFrame* aBox, int32_t& aRowCount, int32_t& aComputedColumnCount); void CountRowsColumns(nsIFrame* aBox, int32_t& aRowCount, int32_t& aComputedColumnCount);
void SetLargestSize(nsSize& aSize, nscoord aHeight, bool aIsHorizontal = true); void SetLargestSize(nsSize& aSize, nscoord aHeight, bool aIsHorizontal = true);
@ -96,10 +98,10 @@ private:
nsIFrame* mBox; nsIFrame* mBox;
// an array of row object // an array of row object
nsGridRow* mRows; mozilla::UniquePtr<nsGridRow[]> mRows;
// an array of columns objects. // an array of columns objects.
nsGridRow* mColumns; mozilla::UniquePtr<nsGridRow[]> mColumns;
// the first in the <grid> that implements the <rows> tag. // the first in the <grid> that implements the <rows> tag.
nsIFrame* mRowsBox; nsIFrame* mRowsBox;
@ -120,7 +122,7 @@ private:
int32_t mExtraColumnCount; int32_t mExtraColumnCount;
// x,y array of cells in the rows and columns // x,y array of cells in the rows and columns
nsGridCell* mCellMap; mozilla::UniquePtr<nsGridCell[]> mCellMap;
// a flag that when true suppresses all other MarkDirties. This // a flag that when true suppresses all other MarkDirties. This
// prevents lots of extra work being done. // prevents lots of extra work being done.

Просмотреть файл

@ -36,6 +36,7 @@
#include "nsContentUtils.h" #include "nsContentUtils.h"
#include "mozilla/dom/Element.h" #include "mozilla/dom/Element.h"
#include "mozilla/MouseEvents.h" #include "mozilla/MouseEvents.h"
#include "mozilla/UniquePtr.h"
using namespace mozilla; using namespace mozilla;
@ -104,7 +105,7 @@ public:
ResizeType GetResizeAfter(); ResizeType GetResizeAfter();
State GetState(); State GetState();
void Reverse(nsSplitterInfo*& aIndexes, int32_t aCount); void Reverse(UniquePtr<nsSplitterInfo[]>& aIndexes, int32_t aCount);
bool SupportsCollapseDirection(CollapseDirection aDirection); bool SupportsCollapseDirection(CollapseDirection aDirection);
void EnsureOrient(); void EnsureOrient();
@ -116,8 +117,8 @@ public:
nscoord mCurrentPos; nscoord mCurrentPos;
nsIFrame* mParentBox; nsIFrame* mParentBox;
bool mPressed; bool mPressed;
nsSplitterInfo* mChildInfosBefore; UniquePtr<nsSplitterInfo[]> mChildInfosBefore;
nsSplitterInfo* mChildInfosAfter; UniquePtr<nsSplitterInfo[]> mChildInfosAfter;
int32_t mChildInfosBeforeCount; int32_t mChildInfosBeforeCount;
int32_t mChildInfosAfterCount; int32_t mChildInfosAfterCount;
State mState; State mState;
@ -144,8 +145,6 @@ nsSplitterFrameInner::GetResizeBefore()
nsSplitterFrameInner::~nsSplitterFrameInner() nsSplitterFrameInner::~nsSplitterFrameInner()
{ {
delete[] mChildInfosBefore;
delete[] mChildInfosAfter;
} }
nsSplitterFrameInner::ResizeType nsSplitterFrameInner::ResizeType
@ -272,8 +271,6 @@ nsSplitterFrame::Init(nsIContent* aContent,
mInner = new nsSplitterFrameInner(this); mInner = new nsSplitterFrameInner(this);
mInner->AddRef(); mInner->AddRef();
mInner->mChildInfosAfter = nullptr;
mInner->mChildInfosBefore = nullptr;
mInner->mState = nsSplitterFrameInner::Open; mInner->mState = nsSplitterFrameInner::Open;
mInner->mDragging = false; mInner->mDragging = false;
@ -432,8 +429,6 @@ nsSplitterFrameInner::MouseUp(nsPresContext* aPresContext,
//printf("MouseUp\n"); //printf("MouseUp\n");
} }
delete[] mChildInfosBefore;
delete[] mChildInfosAfter;
mChildInfosBefore = nullptr; mChildInfosBefore = nullptr;
mChildInfosAfter = nullptr; mChildInfosAfter = nullptr;
mChildInfosBeforeCount = 0; mChildInfosBeforeCount = 0;
@ -480,7 +475,9 @@ nsSplitterFrameInner::MouseDrag(nsPresContext* aPresContext,
nscoord oldPos = pos; nscoord oldPos = pos;
ResizeChildTo(aPresContext, pos, mChildInfosBefore, mChildInfosAfter, mChildInfosBeforeCount, mChildInfosAfterCount, bounded); ResizeChildTo(aPresContext, pos,
mChildInfosBefore.get(), mChildInfosAfter.get(),
mChildInfosBeforeCount, mChildInfosAfterCount, bounded);
State currentState = GetState(); State currentState = GetState();
bool supportsBefore = SupportsCollapseDirection(Before); bool supportsBefore = SupportsCollapseDirection(Before);
@ -651,10 +648,8 @@ nsSplitterFrameInner::MouseDown(nsIDOMEvent* aMouseEvent)
ResizeType resizeBefore = GetResizeBefore(); ResizeType resizeBefore = GetResizeBefore();
ResizeType resizeAfter = GetResizeAfter(); ResizeType resizeAfter = GetResizeAfter();
delete[] mChildInfosBefore; mChildInfosBefore = MakeUnique<nsSplitterInfo[]>(childCount);
delete[] mChildInfosAfter; mChildInfosAfter = MakeUnique<nsSplitterInfo[]>(childCount);
mChildInfosBefore = new nsSplitterInfo[childCount];
mChildInfosAfter = new nsSplitterInfo[childCount];
// create info 2 lists. One of the children before us and one after. // create info 2 lists. One of the children before us and one after.
int32_t count = 0; int32_t count = 0;
@ -727,12 +722,8 @@ nsSplitterFrameInner::MouseDown(nsIDOMEvent* aMouseEvent)
Reverse(mChildInfosAfter, mChildInfosAfterCount); Reverse(mChildInfosAfter, mChildInfosAfterCount);
// Now swap the two arrays. // Now swap the two arrays.
nscoord newAfterCount = mChildInfosBeforeCount; Swap(mChildInfosBeforeCount, mChildInfosAfterCount);
mChildInfosBeforeCount = mChildInfosAfterCount; Swap(mChildInfosBefore, mChildInfosAfter);
mChildInfosAfterCount = newAfterCount;
nsSplitterInfo* temp = mChildInfosAfter;
mChildInfosAfter = mChildInfosBefore;
mChildInfosBefore = temp;
} }
// if resizebefore is not Farthest, reverse the list because the first child // if resizebefore is not Farthest, reverse the list because the first child
@ -791,15 +782,14 @@ nsSplitterFrameInner::MouseMove(nsIDOMEvent* aMouseEvent)
} }
void void
nsSplitterFrameInner::Reverse(nsSplitterInfo*& aChildInfos, int32_t aCount) nsSplitterFrameInner::Reverse(UniquePtr<nsSplitterInfo[]>& aChildInfos, int32_t aCount)
{ {
nsSplitterInfo* infos = new nsSplitterInfo[aCount]; UniquePtr<nsSplitterInfo[]> infos(new nsSplitterInfo[aCount]);
for (int i=0; i < aCount; i++) for (int i=0; i < aCount; i++)
infos[i] = aChildInfos[aCount - 1 - i]; infos[i] = aChildInfos[aCount - 1 - i];
delete[] aChildInfos; aChildInfos = Move(infos);
aChildInfos = infos;
} }
bool bool
@ -898,8 +888,10 @@ nsSplitterFrameInner::AdjustChildren(nsPresContext* aPresContext)
EnsureOrient(); EnsureOrient();
bool isHorizontal = !mOuter->IsHorizontal(); bool isHorizontal = !mOuter->IsHorizontal();
AdjustChildren(aPresContext, mChildInfosBefore, mChildInfosBeforeCount, isHorizontal); AdjustChildren(aPresContext, mChildInfosBefore.get(),
AdjustChildren(aPresContext, mChildInfosAfter, mChildInfosAfterCount, isHorizontal); mChildInfosBeforeCount, isHorizontal);
AdjustChildren(aPresContext, mChildInfosAfter.get(),
mChildInfosAfterCount, isHorizontal);
} }
static nsIFrame* GetChildBoxForContent(nsIFrame* aParentBox, nsIContent* aContent) static nsIFrame* GetChildBoxForContent(nsIFrame* aParentBox, nsIContent* aContent)