Bug 1800050 - Use UniqueFreePtr in StartupCache::PutBuffer. r=nbp

Differential Revision: https://phabricator.services.mozilla.com/D163453
This commit is contained in:
Tooru Fujisawa 2022-12-01 10:46:34 +00:00
parent d17cafec50
commit e5c2ee72c8
8 changed files with 28 additions and 16 deletions

View file

@ -30,6 +30,7 @@
#include "mozilla/scache/StartupCacheUtils.h" #include "mozilla/scache/StartupCacheUtils.h"
#include "mozilla/Telemetry.h" #include "mozilla/Telemetry.h"
#include "mozilla/RefPtr.h" #include "mozilla/RefPtr.h"
#include "mozilla/UniquePtrExtensions.h"
#include "mozilla/intl/LocaleService.h" #include "mozilla/intl/LocaleService.h"
using namespace mozilla; using namespace mozilla;
@ -285,7 +286,7 @@ nsresult nsXULPrototypeCache::FinishOutputStream(CacheType cacheType,
nsCOMPtr<nsIOutputStream> outputStream = do_QueryInterface(storageStream); nsCOMPtr<nsIOutputStream> outputStream = do_QueryInterface(storageStream);
outputStream->Close(); outputStream->Close();
UniquePtr<char[]> buf; UniqueFreePtr<char[]> buf;
uint32_t len; uint32_t len;
rv = NewBufferFromStorageStream(storageStream, &buf, &len); rv = NewBufferFromStorageStream(storageStream, &buf, &len);
NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_SUCCESS(rv, rv);
@ -425,7 +426,8 @@ nsresult nsXULPrototypeCache::BeginCaching(nsIURI* aURI) {
} }
if (NS_SUCCEEDED(rv)) { if (NS_SUCCEEDED(rv)) {
auto putBuf = MakeUnique<char[]>(len); auto putBuf = UniqueFreePtr<char[]>(
reinterpret_cast<char*>(malloc(sizeof(char) * len)));
rv = inputStream->Read(putBuf.get(), len, &amtRead); rv = inputStream->Read(putBuf.get(), len, &amtRead);
if (NS_SUCCEEDED(rv) && len == amtRead) if (NS_SUCCEEDED(rv) && len == amtRead)
rv = startupCache->PutBuffer(kXULCacheInfoKey, std::move(putBuf), len); rv = startupCache->PutBuffer(kXULCacheInfoKey, std::move(putBuf), len);

View file

@ -746,7 +746,7 @@ class FontNameCache {
LOG(("putting FontNameCache to " CACHE_KEY ", length %zu", LOG(("putting FontNameCache to " CACHE_KEY ", length %zu",
buf.Length() + 1)); buf.Length() + 1));
mCache->PutBuffer(CACHE_KEY, UniquePtr<char[]>(ToNewCString(buf)), mCache->PutBuffer(CACHE_KEY, UniqueFreePtr<char[]>(ToNewCString(buf)),
buf.Length() + 1); buf.Length() + 1);
mWriteNeeded = false; mWriteNeeded = false;
} }
@ -1563,7 +1563,8 @@ void gfxFT2FontList::WriteCache() {
mozilla::scache::StartupCache::GetSingleton(); mozilla::scache::StartupCache::GetSingleton();
if (cache && mJarModifiedTime > 0) { if (cache && mJarModifiedTime > 0) {
const size_t bufSize = sizeof(mJarModifiedTime); const size_t bufSize = sizeof(mJarModifiedTime);
auto buf = MakeUnique<char[]>(bufSize); auto buf = UniqueFreePtr<char[]>(
reinterpret_cast<char*>(malloc(sizeof(char) * bufSize)));
LittleEndian::writeInt64(buf.get(), mJarModifiedTime); LittleEndian::writeInt64(buf.get(), mJarModifiedTime);
LOG(("WriteCache: putting Jar, length %zu", bufSize)); LOG(("WriteCache: putting Jar, length %zu", bufSize));

View file

@ -66,7 +66,7 @@ nsresult WriteCachedStencil(StartupCache* cache, nsACString& cachePath,
} }
// Move the vector buffer into a unique pointer buffer. // Move the vector buffer into a unique pointer buffer.
UniquePtr<char[]> buf( UniqueFreePtr<char[]> buf(
reinterpret_cast<char*>(buffer.extractOrCopyRawBuffer())); reinterpret_cast<char*>(buffer.extractOrCopyRawBuffer()));
nsresult rv = cache->PutBuffer(PromiseFlatCString(cachePath).get(), nsresult rv = cache->PutBuffer(PromiseFlatCString(cachePath).get(),
std::move(buf), size); std::move(buf), size);

View file

@ -412,7 +412,8 @@ nsresult StartupCache::GetBuffer(const char* id, const char** outbuf,
Span<const char> compressed = Span( Span<const char> compressed = Span(
mCacheData.get<char>().get() + mCacheEntriesBaseOffset + value.mOffset, mCacheData.get<char>().get() + mCacheEntriesBaseOffset + value.mOffset,
value.mCompressedSize); value.mCompressedSize);
value.mData = MakeUnique<char[]>(value.mUncompressedSize); value.mData = UniqueFreePtr<char[]>(reinterpret_cast<char*>(
malloc(sizeof(char) * value.mUncompressedSize)));
Span<char> uncompressed = Span(value.mData.get(), value.mUncompressedSize); Span<char> uncompressed = Span(value.mData.get(), value.mUncompressedSize);
MMAP_FAULT_HANDLER_BEGIN_BUFFER(uncompressed.Elements(), MMAP_FAULT_HANDLER_BEGIN_BUFFER(uncompressed.Elements(),
uncompressed.Length()) uncompressed.Length())
@ -453,7 +454,7 @@ nsresult StartupCache::GetBuffer(const char* id, const char** outbuf,
} }
// Makes a copy of the buffer, client retains ownership of inbuf. // Makes a copy of the buffer, client retains ownership of inbuf.
nsresult StartupCache::PutBuffer(const char* id, UniquePtr<char[]>&& inbuf, nsresult StartupCache::PutBuffer(const char* id, UniqueFreePtr<char[]>&& inbuf,
uint32_t len) MOZ_NO_THREAD_SAFETY_ANALYSIS { uint32_t len) MOZ_NO_THREAD_SAFETY_ANALYSIS {
NS_ASSERTION(NS_IsMainThread(), NS_ASSERTION(NS_IsMainThread(),
"Startup cache only available on main thread"); "Startup cache only available on main thread");

View file

@ -27,6 +27,7 @@
#include "mozilla/Mutex.h" #include "mozilla/Mutex.h"
#include "mozilla/Result.h" #include "mozilla/Result.h"
#include "mozilla/UniquePtr.h" #include "mozilla/UniquePtr.h"
#include "mozilla/UniquePtrExtensions.h"
/** /**
* The StartupCache is a persistent cache of simple key-value pairs, * The StartupCache is a persistent cache of simple key-value pairs,
@ -83,7 +84,7 @@ namespace mozilla {
namespace scache { namespace scache {
struct StartupCacheEntry { struct StartupCacheEntry {
UniquePtr<char[]> mData; UniqueFreePtr<char[]> mData;
uint32_t mOffset; uint32_t mOffset;
uint32_t mCompressedSize; uint32_t mCompressedSize;
uint32_t mUncompressedSize; uint32_t mUncompressedSize;
@ -101,7 +102,7 @@ struct StartupCacheEntry {
mRequestedOrder(0), mRequestedOrder(0),
mRequested(false) {} mRequested(false) {}
StartupCacheEntry(UniquePtr<char[]> aData, size_t aLength, StartupCacheEntry(UniqueFreePtr<char[]> aData, size_t aLength,
int32_t aRequestedOrder) int32_t aRequestedOrder)
: mData(std::move(aData)), : mData(std::move(aData)),
mOffset(0), mOffset(0),
@ -148,7 +149,7 @@ class StartupCache : public nsIMemoryReporter {
nsresult GetBuffer(const char* id, const char** outbuf, uint32_t* length); nsresult GetBuffer(const char* id, const char** outbuf, uint32_t* length);
// Stores a buffer. Caller yields ownership. // Stores a buffer. Caller yields ownership.
nsresult PutBuffer(const char* id, UniquePtr<char[]>&& inbuf, nsresult PutBuffer(const char* id, UniqueFreePtr<char[]>&& inbuf,
uint32_t length); uint32_t length);
// Removes the cache file. // Removes the cache file.

View file

@ -66,7 +66,8 @@ nsresult NewObjectOutputWrappedStorageStream(
} }
nsresult NewBufferFromStorageStream(nsIStorageStream* storageStream, nsresult NewBufferFromStorageStream(nsIStorageStream* storageStream,
UniquePtr<char[]>* buffer, uint32_t* len) { UniqueFreePtr<char[]>* buffer,
uint32_t* len) {
nsresult rv; nsresult rv;
nsCOMPtr<nsIInputStream> inputStream; nsCOMPtr<nsIInputStream> inputStream;
rv = storageStream->NewInputStream(0, getter_AddRefs(inputStream)); rv = storageStream->NewInputStream(0, getter_AddRefs(inputStream));
@ -78,7 +79,8 @@ nsresult NewBufferFromStorageStream(nsIStorageStream* storageStream,
NS_ENSURE_TRUE(avail64 <= UINT32_MAX, NS_ERROR_FILE_TOO_BIG); NS_ENSURE_TRUE(avail64 <= UINT32_MAX, NS_ERROR_FILE_TOO_BIG);
uint32_t avail = (uint32_t)avail64; uint32_t avail = (uint32_t)avail64;
auto temp = MakeUnique<char[]>(avail); auto temp = UniqueFreePtr<char[]>(
reinterpret_cast<char*>(malloc(sizeof(char) * avail)));
uint32_t read; uint32_t read;
rv = inputStream->Read(temp.get(), avail, &read); rv = inputStream->Read(temp.get(), avail, &read);
if (NS_SUCCEEDED(rv) && avail != read) rv = NS_ERROR_UNEXPECTED; if (NS_SUCCEEDED(rv) && avail != read) rv = NS_ERROR_UNEXPECTED;

View file

@ -10,6 +10,7 @@
#include "nsIObjectInputStream.h" #include "nsIObjectInputStream.h"
#include "nsIObjectOutputStream.h" #include "nsIObjectOutputStream.h"
#include "mozilla/UniquePtr.h" #include "mozilla/UniquePtr.h"
#include "mozilla/UniquePtrExtensions.h"
class nsIURI; class nsIURI;
@ -33,7 +34,8 @@ nsresult NewObjectOutputWrappedStorageStream(
// allocated with 'new []'. After calling this function, the caller would // allocated with 'new []'. After calling this function, the caller would
// typically call StartupCache::PutBuffer with the returned buffer. // typically call StartupCache::PutBuffer with the returned buffer.
nsresult NewBufferFromStorageStream(nsIStorageStream* storageStream, nsresult NewBufferFromStorageStream(nsIStorageStream* storageStream,
UniquePtr<char[]>* buffer, uint32_t* len); UniqueFreePtr<char[]>* buffer,
uint32_t* len);
nsresult ResolveURI(nsIURI* in, nsIURI** out); nsresult ResolveURI(nsIURI* in, nsIURI** out);

View file

@ -23,6 +23,7 @@
#include "mozilla/Maybe.h" #include "mozilla/Maybe.h"
#include "mozilla/Printf.h" #include "mozilla/Printf.h"
#include "mozilla/UniquePtr.h" #include "mozilla/UniquePtr.h"
#include "mozilla/UniquePtrExtensions.h"
#include "nsNetCID.h" #include "nsNetCID.h"
#include "nsIURIMutator.h" #include "nsIURIMutator.h"
@ -83,7 +84,8 @@ TEST_F(TestStartupCache, StartupWriteRead) {
const char* outbuf; const char* outbuf;
uint32_t len; uint32_t len;
rv = sc->PutBuffer(id, UniquePtr<char[]>(strdup(buf)), strlen(buf) + 1); rv = sc->PutBuffer(id, mozilla::UniqueFreePtr<char[]>(strdup(buf)),
strlen(buf) + 1);
EXPECT_NS_SUCCEEDED(rv); EXPECT_NS_SUCCEEDED(rv);
rv = sc->GetBuffer(id, &outbuf, &len); rv = sc->GetBuffer(id, &outbuf, &len);
@ -108,7 +110,8 @@ TEST_F(TestStartupCache, WriteInvalidateRead) {
StartupCache* sc = StartupCache::GetSingleton(); StartupCache* sc = StartupCache::GetSingleton();
ASSERT_TRUE(sc); ASSERT_TRUE(sc);
rv = sc->PutBuffer(id, UniquePtr<char[]>(strdup(buf)), strlen(buf) + 1); rv = sc->PutBuffer(id, mozilla::UniqueFreePtr<char[]>(strdup(buf)),
strlen(buf) + 1);
EXPECT_NS_SUCCEEDED(rv); EXPECT_NS_SUCCEEDED(rv);
sc->InvalidateCache(); sc->InvalidateCache();
@ -153,7 +156,7 @@ TEST_F(TestStartupCache, WriteObject) {
rv = objectOutput->WriteObject(objQI, true); rv = objectOutput->WriteObject(objQI, true);
EXPECT_NS_SUCCEEDED(rv); EXPECT_NS_SUCCEEDED(rv);
UniquePtr<char[]> buf; mozilla::UniqueFreePtr<char[]> buf;
uint32_t len; uint32_t len;
NewBufferFromStorageStream(storageStream, &buf, &len); NewBufferFromStorageStream(storageStream, &buf, &len);