Skip to content

Commit

Permalink
Bug 1219246 - use UniquePtr instead of nsAuto*Ptr in xpcom/; r=erahm
Browse files Browse the repository at this point in the history
  • Loading branch information
froydnj committed Oct 31, 2015
1 parent 6362a3e commit 737d993
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 37 deletions.
17 changes: 9 additions & 8 deletions xpcom/components/nsComponentManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
#include "nsArrayEnumerator.h"
#include "nsStringEnumerator.h"
#include "mozilla/FileUtils.h"
#include "mozilla/UniquePtr.h"
#include "nsDataHashtable.h"

#include <new> // for placement new
Expand Down Expand Up @@ -620,18 +621,18 @@ DoRegisterManifest(NSLocationType aType,
MOZ_ASSERT(!aXPTOnly || !nsComponentManagerImpl::gComponentManager);
uint32_t len;
FileLocation::Data data;
nsAutoArrayPtr<char> buf;
UniquePtr<char[]> buf;
nsresult rv = aFile.GetData(data);
if (NS_SUCCEEDED(rv)) {
rv = data.GetSize(&len);
}
if (NS_SUCCEEDED(rv)) {
buf = new char[len + 1];
rv = data.Copy(buf, len);
buf = MakeUnique<char[]>(len + 1);
rv = data.Copy(buf.get(), len);
}
if (NS_SUCCEEDED(rv)) {
buf[len] = '\0';
ParseManifest(aType, aFile, buf, aChromeOnly, aXPTOnly);
ParseManifest(aType, aFile, buf.get(), aChromeOnly, aXPTOnly);
} else if (NS_BOOTSTRAPPED_LOCATION != aType) {
nsCString uri;
aFile.GetURIString(uri);
Expand Down Expand Up @@ -699,17 +700,17 @@ DoRegisterXPT(FileLocation& aFile)

uint32_t len;
FileLocation::Data data;
nsAutoArrayPtr<char> buf;
UniquePtr<char[]> buf;
nsresult rv = aFile.GetData(data);
if (NS_SUCCEEDED(rv)) {
rv = data.GetSize(&len);
}
if (NS_SUCCEEDED(rv)) {
buf = new char[len];
rv = data.Copy(buf, len);
buf = MakeUnique<char[]>(len);
rv = data.Copy(buf.get(), len);
}
if (NS_SUCCEEDED(rv)) {
XPTInterfaceInfoManager::GetSingleton()->RegisterBuffer(buf, len);
XPTInterfaceInfoManager::GetSingleton()->RegisterBuffer(buf.get(), len);
#ifdef MOZ_B2G_LOADER
MarkRegisteredXPTIInfo(aFile);
#endif
Expand Down
24 changes: 13 additions & 11 deletions xpcom/glue/nsINIParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#define READ_BINARYMODE "r"
#endif

using namespace mozilla;

#ifdef XP_WIN
inline FILE*
TS_tfopen(const char* aPath, const wchar_t* aMode)
Expand Down Expand Up @@ -123,7 +125,7 @@ nsINIParser::InitFromFILE(FILE* aFd)
}

/* malloc an internal buf the size of the file */
mFileContents = new char[flen + 2];
mFileContents = MakeUnique<char[]>(flen + 2);
if (!mFileContents) {
return NS_ERROR_OUT_OF_MEMORY;
}
Expand All @@ -133,7 +135,7 @@ nsINIParser::InitFromFILE(FILE* aFd)
return NS_BASE_STREAM_OSERROR;
}

int rd = fread(mFileContents, sizeof(char), flen, aFd);
int rd = fread(mFileContents.get(), sizeof(char), flen, aFd);
if (rd != flen) {
return NS_BASE_STREAM_OSERROR;
}
Expand Down Expand Up @@ -172,13 +174,13 @@ nsINIParser::InitFromFILE(FILE* aFd)
return NS_ERROR_FAILURE;
}

nsAutoArrayPtr<char> utf8Buffer(new char[flen]);
UniquePtr<char[]> utf8Buffer(new char[flen]);
if (WideCharToMultiByte(CP_UTF8, 0, reinterpret_cast<LPWSTR>(buffer), -1,
utf8Buffer, flen, nullptr, nullptr) == 0) {
utf8Buffer.get(), flen, nullptr, nullptr) == 0) {
return NS_ERROR_FAILURE;
}
mFileContents = utf8Buffer.forget();
buffer = mFileContents;
mFileContents = Move(utf8Buffer);
buffer = mFileContents.get();
}
#endif

Expand Down Expand Up @@ -242,13 +244,13 @@ nsINIParser::InitFromFILE(FILE* aFd)
break;
}
if (!v->next) {
v->next = new INIValue(key, token);
v->next = MakeUnique<INIValue>(key, token);
if (!v->next) {
return NS_ERROR_OUT_OF_MEMORY;
}
break;
}
v = v->next;
v = v->next.get();
}
NS_ASSERTION(v, "v should never be null coming out of this loop");
}
Expand All @@ -269,7 +271,7 @@ nsINIParser::GetString(const char* aSection, const char* aKey,
return NS_OK;
}

val = val->next;
val = val->next.get();
}

return NS_ERROR_FAILURE;
Expand All @@ -293,7 +295,7 @@ nsINIParser::GetString(const char* aSection, const char* aKey,
return NS_OK;
}

val = val->next;
val = val->next.get();
}

return NS_ERROR_FAILURE;
Expand All @@ -318,7 +320,7 @@ nsINIParser::GetStrings(const char* aSection,

for (mSections.Get(aSection, &val);
val;
val = val->next) {
val = val->next.get()) {

if (!aCB(val->key, val->value, aClosure)) {
return NS_OK;
Expand Down
6 changes: 3 additions & 3 deletions xpcom/glue/nsINIParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

#include "nscore.h"
#include "nsClassHashtable.h"
#include "nsAutoPtr.h"
#include "mozilla/UniquePtr.h"

#include <stdio.h>

Expand Down Expand Up @@ -106,11 +106,11 @@ class nsINIParser

const char* key;
const char* value;
nsAutoPtr<INIValue> next;
mozilla::UniquePtr<INIValue> next;
};

nsClassHashtable<nsDepCharHashKey, INIValue> mSections;
nsAutoArrayPtr<char> mFileContents;
mozilla::UniquePtr<char[]> mFileContents;

nsresult InitFromFILE(FILE* aFd);
};
Expand Down
31 changes: 16 additions & 15 deletions xpcom/tests/gtest/TestExpirationTracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "nsIFile.h"
#include "prinrval.h"
#include "nsThreadUtils.h"
#include "mozilla/UniquePtr.h"
#include "gtest/gtest.h"

namespace TestExpirationTracker {
Expand Down Expand Up @@ -49,7 +50,7 @@ template <uint32_t K> class Tracker : public nsExpirationTracker<Object,K> {
LogAction(obj, "Created");
}

nsTArray<nsAutoArrayPtr<Object> > mUniverse;
nsTArray<mozilla::UniquePtr<Object>> mUniverse;

void LogAction(Object* aObj, const char* aAction) {
if (logging) {
Expand Down Expand Up @@ -79,28 +80,28 @@ template <uint32_t K> class Tracker : public nsExpirationTracker<Object,K> {
break;
}
case 1: {
obj = mUniverse[uint32_t(rand())%mUniverse.Length()];
if (obj->mExpiration.IsTracked()) {
nsExpirationTracker<Object,K>::RemoveObject(obj);
LogAction(obj, "Removed");
UniquePtr<Object>& objref = mUniverse[uint32_t(rand())%mUniverse.Length()];
if (objref->mExpiration.IsTracked()) {
nsExpirationTracker<Object,K>::RemoveObject(objref.get());
LogAction(objref.get(), "Removed");
}
break;
}
case 2: {
obj = mUniverse[uint32_t(rand())%mUniverse.Length()];
if (!obj->mExpiration.IsTracked()) {
obj->Touch();
nsExpirationTracker<Object,K>::AddObject(obj);
LogAction(obj, "Added");
UniquePtr<Object>& objref = mUniverse[uint32_t(rand())%mUniverse.Length()];
if (!objref->mExpiration.IsTracked()) {
objref->Touch();
nsExpirationTracker<Object,K>::AddObject(objref.get());
LogAction(objref.get(), "Added");
}
break;
}
case 3: {
obj = mUniverse[uint32_t(rand())%mUniverse.Length()];
if (obj->mExpiration.IsTracked()) {
obj->Touch();
nsExpirationTracker<Object,K>::MarkUsed(obj);
LogAction(obj, "Marked used");
UniquePtr<Object>& objref = mUniverse[uint32_t(rand())%mUniverse.Length()];
if (objref->mExpiration.IsTracked()) {
objref->Touch();
nsExpirationTracker<Object,K>::MarkUsed(objref.get());
LogAction(objref.get(), "Marked used");
}
break;
}
Expand Down

0 comments on commit 737d993

Please sign in to comment.