Bug 1249389 - part 4 - make StartupCache::GetBuffer take a UniquePtr outparam; r=erahm

This change eliminates a number of nsAutoArrayPtr usages, as well as
making the pattern GetBuffer() -> NewObjectInputStreamFromBuffer more
pleasant.
This commit is contained in:
Nathan Froyd 2016-02-18 12:26:28 -05:00
parent 460db498a3
commit 12d9670a60
7 changed files with 38 additions and 40 deletions

View File

@ -198,16 +198,15 @@ nsXBLDocumentInfo::ReadPrototypeBindings(nsIURI* aURI, nsXBLDocumentInfo** aDocI
StartupCache* startupCache = StartupCache::GetSingleton();
NS_ENSURE_TRUE(startupCache, NS_ERROR_FAILURE);
nsAutoArrayPtr<char> buf;
UniquePtr<char[]> buf;
uint32_t len;
rv = startupCache->GetBuffer(spec.get(), getter_Transfers(buf), &len);
rv = startupCache->GetBuffer(spec.get(), &buf, &len);
// GetBuffer will fail if the binding is not in the cache.
if (NS_FAILED(rv))
return rv;
nsCOMPtr<nsIObjectInputStream> stream;
rv = NewObjectInputStreamFromBuffer(UniquePtr<char[]>(buf.forget()),
len, getter_AddRefs(stream));
rv = NewObjectInputStreamFromBuffer(Move(buf), len, getter_AddRefs(stream));
NS_ENSURE_SUCCESS(rv, rv);
// The file compatibility.ini stores the build id. This is checked in

View File

@ -334,19 +334,18 @@ nsXULPrototypeCache::GetInputStream(nsIURI* uri, nsIObjectInputStream** stream)
if (NS_FAILED(rv))
return NS_ERROR_NOT_AVAILABLE;
nsAutoArrayPtr<char> buf;
UniquePtr<char[]> buf;
uint32_t len;
nsCOMPtr<nsIObjectInputStream> ois;
StartupCache* sc = StartupCache::GetSingleton();
if (!sc)
return NS_ERROR_NOT_AVAILABLE;
rv = sc->GetBuffer(spec.get(), getter_Transfers(buf), &len);
rv = sc->GetBuffer(spec.get(), &buf, &len);
if (NS_FAILED(rv))
return NS_ERROR_NOT_AVAILABLE;
rv = NewObjectInputStreamFromBuffer(UniquePtr<char[]>(buf.forget()),
len, getter_AddRefs(ois));
rv = NewObjectInputStreamFromBuffer(Move(buf), len, getter_AddRefs(ois));
NS_ENSURE_SUCCESS(rv, rv);
mInputStreamTable.Put(uri, ois);
@ -436,12 +435,12 @@ nsXULPrototypeCache::HasData(nsIURI* uri, bool* exists)
*exists = false;
return NS_OK;
}
nsAutoArrayPtr<char> buf;
UniquePtr<char[]> buf;
uint32_t len;
StartupCache* sc = StartupCache::GetSingleton();
if (sc)
rv = sc->GetBuffer(spec.get(), getter_Transfers(buf), &len);
else {
if (sc) {
rv = sc->GetBuffer(spec.get(), &buf, &len);
} else {
*exists = false;
return NS_OK;
}
@ -492,15 +491,14 @@ nsXULPrototypeCache::BeginCaching(nsIURI* aURI)
nsAutoCString fileChromePath, fileLocale;
nsAutoArrayPtr<char> buf;
UniquePtr<char[]> buf;
uint32_t len, amtRead;
nsCOMPtr<nsIObjectInputStream> objectInput;
rv = startupCache->GetBuffer(kXULCacheInfoKey, getter_Transfers(buf),
&len);
rv = startupCache->GetBuffer(kXULCacheInfoKey, &buf, &len);
if (NS_SUCCEEDED(rv))
rv = NewObjectInputStreamFromBuffer(UniquePtr<char[]>(buf.forget()),
len, getter_AddRefs(objectInput));
rv = NewObjectInputStreamFromBuffer(Move(buf), len,
getter_AddRefs(objectInput));
if (NS_SUCCEEDED(rv)) {
rv = objectInput->ReadCString(fileLocale);
@ -558,10 +556,10 @@ nsXULPrototypeCache::BeginCaching(nsIURI* aURI)
}
if (NS_SUCCEEDED(rv)) {
buf = new char[len];
rv = inputStream->Read(buf, len, &amtRead);
buf = MakeUnique<char[]>(len);
rv = inputStream->Read(buf.get(), len, &amtRead);
if (NS_SUCCEEDED(rv) && len == amtRead)
rv = startupCache->PutBuffer(kXULCacheInfoKey, buf, len);
rv = startupCache->PutBuffer(kXULCacheInfoKey, buf.get(), len);
else {
rv = NS_ERROR_UNEXPECTED;
}

View File

@ -667,7 +667,7 @@ public:
return;
}
uint32_t size;
char* buf;
UniquePtr<char[]> buf;
if (NS_FAILED(mCache->GetBuffer(CACHE_KEY, &buf, &size))) {
return;
}
@ -709,9 +709,6 @@ public:
beginning = end + 1;
end = strchr(beginning, ';');
}
// Should we use free() or delete[] here? See bug 684700.
free(buf);
}
virtual void
@ -953,7 +950,7 @@ gfxFT2FontList::FindFontsInOmnijar(FontNameCache *aCache)
mozilla::scache::StartupCache* cache =
mozilla::scache::StartupCache::GetSingleton();
char *cachedModifiedTimeBuf;
UniquePtr<char[]> cachedModifiedTimeBuf;
uint32_t longSize;
int64_t jarModifiedTime;
if (cache &&
@ -964,7 +961,7 @@ gfxFT2FontList::FindFontsInOmnijar(FontNameCache *aCache)
{
nsCOMPtr<nsIFile> jarFile = Omnijar::GetPath(Omnijar::Type::GRE);
jarFile->GetLastModifiedTime(&jarModifiedTime);
if (jarModifiedTime > *(int64_t*)cachedModifiedTimeBuf) {
if (jarModifiedTime > *(int64_t*)cachedModifiedTimeBuf.get()) {
jarChanged = true;
}
}

View File

@ -15,6 +15,7 @@
using namespace JS;
using namespace mozilla::scache;
using mozilla::UniquePtr;
// We only serialize scripts with system principals. So we don't serialize the
// principals when writing a script. Instead, when reading it back, we set the
@ -23,14 +24,13 @@ nsresult
ReadCachedScript(StartupCache* cache, nsACString& uri, JSContext* cx,
nsIPrincipal* systemPrincipal, MutableHandleScript scriptp)
{
nsAutoArrayPtr<char> buf;
UniquePtr<char[]> buf;
uint32_t len;
nsresult rv = cache->GetBuffer(PromiseFlatCString(uri).get(),
getter_Transfers(buf), &len);
nsresult rv = cache->GetBuffer(PromiseFlatCString(uri).get(), &buf, &len);
if (NS_FAILED(rv))
return rv; // don't warn since NOT_AVAILABLE is an ok error
scriptp.set(JS_DecodeScript(cx, buf, len));
scriptp.set(JS_DecodeScript(cx, buf.get(), len));
if (!scriptp)
return NS_ERROR_OUT_OF_MEMORY;
return NS_OK;

View File

@ -373,16 +373,16 @@ public:
* Copy member into a new buffer if uncompressed.
* @return a buffer with whole zip member. It is caller's responsibility to free() it.
*/
T* Forget() {
mozilla::UniquePtr<T[]> Forget() {
if (!mReturnBuf)
return nullptr;
// In uncompressed mmap case, give up buffer
if (mAutoBuf.get() == mReturnBuf) {
mReturnBuf = nullptr;
return (T*) mAutoBuf.release();
return mozilla::UniquePtr<T[]>(reinterpret_cast<T*>(mAutoBuf.release()));
}
T *ret = (T*) malloc(Length());
memcpy(ret, mReturnBuf, Length());
auto ret = mozilla::MakeUnique<T[]>(Length());
memcpy(ret.get(), mReturnBuf, Length());
mReturnBuf = nullptr;
return ret;
}

View File

@ -284,7 +284,7 @@ namespace {
nsresult
GetBufferFromZipArchive(nsZipArchive *zip, bool doCRC, const char* id,
char** outbuf, uint32_t* length)
UniquePtr<char[]>* outbuf, uint32_t* length)
{
if (!zip)
return NS_ERROR_NOT_AVAILABLE;
@ -303,7 +303,7 @@ GetBufferFromZipArchive(nsZipArchive *zip, bool doCRC, const char* id,
// NOTE: this will not find a new entry until it has been written to disk!
// Consumer should take ownership of the resulting buffer.
nsresult
StartupCache::GetBuffer(const char* id, char** outbuf, uint32_t* length)
StartupCache::GetBuffer(const char* id, UniquePtr<char[]>* outbuf, uint32_t* length)
{
PROFILER_LABEL_FUNC(js::ProfileEntry::Category::OTHER);
@ -315,8 +315,8 @@ StartupCache::GetBuffer(const char* id, char** outbuf, uint32_t* length)
nsDependentCString idStr(id);
mTable.Get(idStr, &entry);
if (entry) {
*outbuf = new char[entry->size];
memcpy(*outbuf, entry->data, entry->size);
*outbuf = MakeUnique<char[]>(entry->size);
memcpy(outbuf->get(), entry->data, entry->size);
*length = entry->size;
return NS_OK;
}
@ -754,7 +754,10 @@ StartupCacheWrapper::GetBuffer(const char* id, char** outbuf, uint32_t* length)
if (!sc) {
return NS_ERROR_NOT_INITIALIZED;
}
return sc->GetBuffer(id, outbuf, length);
UniquePtr<char[]> buf;
nsresult rv = sc->GetBuffer(id, &buf, length);
*outbuf = buf.release();
return rv;
}
nsresult

View File

@ -20,6 +20,7 @@
#include "mozilla/Attributes.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/UniquePtr.h"
/**
* The StartupCache is a persistent cache of simple key-value pairs,
@ -112,7 +113,7 @@ public:
// StartupCache methods. See above comments for a more detailed description.
// Returns a buffer that was previously stored, caller takes ownership.
nsresult GetBuffer(const char* id, char** outbuf, uint32_t* length);
nsresult GetBuffer(const char* id, UniquePtr<char[]>* outbuf, uint32_t* length);
// Stores a buffer. Caller keeps ownership, we make a copy.
nsresult PutBuffer(const char* id, const char* inbuf, uint32_t length);