fune/startupcache/StartupCacheChild.cpp
Doug Thayer 249401cc24 Bug 1651941 - Free unreferenced child StartupCache entries on send r=froydnj
This is similar to the first patch in the stack, but instead of freeing the
entries on write, we free them when we send them to the parent to be written.

Differential Revision: https://phabricator.services.mozilla.com/D83401
2020-07-22 20:31:31 +00:00

66 lines
No EOL
2 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/scache/StartupCacheChild.h"
#include "mozilla/scache/StartupCache.h"
#include "mozilla/dom/ContentParent.h"
namespace mozilla {
namespace scache {
void StartupCacheChild::Init() {
auto* cache = StartupCache::GetSingleton();
if (cache) {
Unused << cache->InitChild(this);
} else {
Send__delete__(this, AutoTArray<EntryData, 0>());
}
}
void StartupCacheChild::SendEntriesAndFinalize(StartupCache::Table& entries) {
nsTArray<EntryData> dataArray;
for (auto iter = entries.iter(); !iter.done(); iter.next()) {
const auto& key = iter.get().key();
auto& value = iter.get().value();
if (!value.mData ||
value.mRequestedOrder == kStartupCacheEntryNotRequested) {
continue;
}
auto data = dataArray.AppendElement();
MOZ_ASSERT(strnlen(key.get(), kStartupCacheKeyLengthCap) <
kStartupCacheKeyLengthCap,
"StartupCache key over the size limit.");
data->key() = nsCString(key.get());
if (value.mFlags.contains(StartupCacheEntryFlags::AddedThisSession)) {
data->data().AppendElements(
reinterpret_cast<const uint8_t*>(value.mData.get()),
value.mUncompressedSize);
}
}
Send__delete__(this, dataArray);
for (auto iter = entries.iter(); !iter.done(); iter.next()) {
auto& value = iter.get().value();
if (!value.mFlags.contains(StartupCacheEntryFlags::DoNotFree)) {
value.mData = nullptr;
}
}
}
void StartupCacheChild::ActorDestroy(ActorDestroyReason aWhy) {
auto* cache = StartupCache::GetSingleton();
if (cache) {
cache->mChildActor = nullptr;
}
}
} // namespace scache
} // namespace mozilla