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
Родитель d17cafec50
Коммит e5c2ee72c8
8 изменённых файлов: 28 добавлений и 16 удалений

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

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

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

@ -746,7 +746,7 @@ class FontNameCache {
LOG(("putting FontNameCache to " CACHE_KEY ", length %zu",
buf.Length() + 1));
mCache->PutBuffer(CACHE_KEY, UniquePtr<char[]>(ToNewCString(buf)),
mCache->PutBuffer(CACHE_KEY, UniqueFreePtr<char[]>(ToNewCString(buf)),
buf.Length() + 1);
mWriteNeeded = false;
}
@ -1563,7 +1563,8 @@ void gfxFT2FontList::WriteCache() {
mozilla::scache::StartupCache::GetSingleton();
if (cache && mJarModifiedTime > 0) {
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);
LOG(("WriteCache: putting Jar, length %zu", bufSize));

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

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

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

@ -412,7 +412,8 @@ nsresult StartupCache::GetBuffer(const char* id, const char** outbuf,
Span<const char> compressed = Span(
mCacheData.get<char>().get() + mCacheEntriesBaseOffset + value.mOffset,
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);
MMAP_FAULT_HANDLER_BEGIN_BUFFER(uncompressed.Elements(),
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.
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 {
NS_ASSERTION(NS_IsMainThread(),
"Startup cache only available on main thread");

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

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

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

@ -66,7 +66,8 @@ nsresult NewObjectOutputWrappedStorageStream(
}
nsresult NewBufferFromStorageStream(nsIStorageStream* storageStream,
UniquePtr<char[]>* buffer, uint32_t* len) {
UniqueFreePtr<char[]>* buffer,
uint32_t* len) {
nsresult rv;
nsCOMPtr<nsIInputStream> 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);
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;
rv = inputStream->Read(temp.get(), avail, &read);
if (NS_SUCCEEDED(rv) && avail != read) rv = NS_ERROR_UNEXPECTED;

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

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

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

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