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
parent 13fbb06532
commit 6d4ad6c4c6
3 changed files with 53 additions and 81 deletions

View File

@ -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.
*/
using namespace mozilla;
nsGrid::nsGrid():mBox(nullptr),
mRows(nullptr),
mColumns(nullptr),
mRowsBox(nullptr),
mColumnsBox(nullptr),
mNeedsRebuild(true),
@ -102,7 +102,6 @@ nsGrid::nsGrid():mBox(nullptr),
mColumnCount(0),
mExtraRowCount(0),
mExtraColumnCount(0),
mCellMap(nullptr),
mMarkingDirty(false)
{
MOZ_COUNT_CTOR(nsGrid);
@ -205,8 +204,8 @@ nsGrid::RebuildIfNeeded()
}
// build and poplulate row and columns arrays
BuildRows(mRowsBox, rowCount, &mRows, true);
BuildRows(mColumnsBox, columnCount, &mColumns, false);
mRows = BuildRows(mRowsBox, rowCount, true);
mColumns = BuildRows(mColumnsBox, columnCount, false);
// build and populate the cell map
mCellMap = BuildCellMap(rowCount, columnCount);
@ -215,22 +214,13 @@ nsGrid::RebuildIfNeeded()
mColumnCount = columnCount;
// populate the cell map from column and row children
PopulateCellMap(mRows, mColumns, mRowCount, mColumnCount, true);
PopulateCellMap(mColumns, mRows, mColumnCount, mRowCount, false);
PopulateCellMap(mRows.get(), mColumns.get(), mRowCount, mColumnCount, true);
PopulateCellMap(mColumns.get(), mRows.get(), mColumnCount, mRowCount, false);
}
void
nsGrid::FreeMap()
{
if (mRows)
delete[] mRows;
if (mColumns)
delete[] mColumns;
if (mCellMap)
delete[] mCellMap;
mRows = nullptr;
mColumns = 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.
*/
void
nsGrid::BuildRows(nsIFrame* aBox, int32_t aRowCount, nsGridRow** aRows, bool aIsHorizontal)
UniquePtr<nsGridRow[]>
nsGrid::BuildRows(nsIFrame* aBox, int32_t aRowCount, bool aIsHorizontal)
{
// if no rows then return null
if (aRowCount == 0) {
// make sure we free up the memory.
if (*aRows)
delete[] (*aRows);
*aRows = nullptr;
return;
return nullptr;
}
// create the array
nsGridRow* row;
UniquePtr<nsGridRow[]> row;
// only create new rows if we have to. Reuse old rows.
if (aIsHorizontal)
{
if (aRowCount > mRowCount) {
delete[] mRows;
row = new nsGridRow[aRowCount];
row = MakeUnique<nsGridRow[]>(aRowCount);
} else {
for (int32_t i=0; i < mRowCount; i++)
mRows[i].Init(nullptr, false);
row = mRows;
row = Move(mRows);
}
} else {
if (aRowCount > mColumnCount) {
delete[] mColumns;
row = new nsGridRow[aRowCount];
row = MakeUnique<nsGridRow[]>(aRowCount);
} else {
for (int32_t i=0; i < mColumnCount; i++)
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);
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
*/
nsGridCell*
UniquePtr<nsGridCell[]>
nsGrid::BuildCellMap(int32_t aRows, int32_t aColumns)
{
int32_t size = aRows*aColumns;
int32_t oldsize = mRowCount*mColumnCount;
if (size == 0) {
delete[] mCellMap;
return nullptr;
}
else {
if (size > oldsize) {
delete[] mCellMap;
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;
}
if (size > oldsize) {
return MakeUnique<nsGridCell[]>(size);
}
return nullptr;
// clear out cellmap
for (int32_t i=0; i < oldsize; i++) {
mCellMap[i].SetBoxInRow(nullptr);
mCellMap[i].SetBoxInColumn(nullptr);
}
return Move(mCellMap);
}
/**

View File

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

View File

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