Bug 1533755 - Use RefPtr for SharedScriptData pointers. r=jandem

Leave manual refcounting in the ScriptDataTable for now since it
requires a bit of care to make the automatic types do the right thing
when sweeping.

Differential Revision: https://phabricator.services.mozilla.com/D22718

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Ted Campbell 2019-03-12 02:40:29 +00:00
Родитель 0fb55c89f0
Коммит f7d1ebcf87
2 изменённых файлов: 17 добавлений и 43 удалений

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

@ -2908,40 +2908,17 @@ SharedScriptData* js::SharedScriptData::new_(JSContext* cx, uint32_t codeLength,
inline js::ScriptBytecodeHasher::Lookup::Lookup(SharedScriptData* data)
: scriptData(data),
hash(mozilla::HashBytes(scriptData->data(), scriptData->dataLength())) {
scriptData->incRefCount();
}
inline js::ScriptBytecodeHasher::Lookup::~Lookup() {
scriptData->decRefCount();
}
hash(mozilla::HashBytes(scriptData->data(), scriptData->dataLength())) {}
bool JSScript::createSharedScriptData(JSContext* cx, uint32_t codeLength,
uint32_t noteLength, uint32_t natoms) {
MOZ_ASSERT(!scriptData());
SharedScriptData* ssd =
SharedScriptData::new_(cx, codeLength, noteLength, natoms);
if (!ssd) {
return false;
}
setScriptData(ssd);
return true;
}
void JSScript::freeScriptData() {
if (scriptData_) {
scriptData_->decRefCount();
scriptData_ = nullptr;
}
}
void JSScript::setScriptData(js::SharedScriptData* data) {
MOZ_ASSERT(!scriptData_);
scriptData_ = data;
scriptData_->incRefCount();
scriptData_ = SharedScriptData::new_(cx, codeLength, noteLength, natoms);
return !!scriptData_;
}
void JSScript::freeScriptData() { scriptData_ = nullptr; }
/*
* Takes ownership of its *ssd parameter and either adds it into the runtime's
* ScriptDataTable or frees it if a matching entry already exists.
@ -2962,20 +2939,20 @@ bool JSScript::shareScriptData(JSContext* cx) {
ScriptDataTable::AddPtr p = cx->scriptDataTable(lock).lookupForAdd(lookup);
if (p) {
MOZ_ASSERT(ssd != *p);
freeScriptData();
setScriptData(*p);
scriptData_ = *p;
} else {
if (!cx->scriptDataTable(lock).add(p, ssd)) {
freeScriptData();
ReportOutOfMemory(cx);
return false;
}
// Being in the table counts as a reference on the script data.
scriptData()->incRefCount();
ssd->AddRef();
}
// Refs: JSScript, ScriptDataTable
MOZ_ASSERT(scriptData()->refCount() >= 2);
return true;
}
@ -2989,7 +2966,7 @@ void js::SweepScriptData(JSRuntime* rt) {
for (ScriptDataTable::Enum e(table); !e.empty(); e.popFront()) {
SharedScriptData* scriptData = e.front();
if (scriptData->refCount() == 1) {
scriptData->decRefCount();
scriptData->Release();
e.removeFront();
}
}
@ -3740,9 +3717,7 @@ void JSScript::finalize(FreeOp* fop) {
fop->free_(data_);
}
if (scriptData_) {
scriptData_->decRefCount();
}
freeScriptData();
// In most cases, our LazyScript's script pointer will reference this
// script, and thus be nulled out by normal weakref processing. However, if
@ -4243,7 +4218,7 @@ JSScript* js::detail::CopyScript(JSContext* cx, HandleScript src,
if (cx->zone() != src->zoneFromAnyThread()) {
src->scriptData()->markForCrossZone(cx);
}
dst->setScriptData(src->scriptData());
dst->scriptData_ = src->scriptData();
return dst;
}

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

@ -14,6 +14,7 @@
#include "mozilla/Maybe.h"
#include "mozilla/MaybeOneOf.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/RefPtr.h"
#include "mozilla/Span.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/Utf8.h"
@ -1478,8 +1479,8 @@ class SharedScriptData {
uint32_t srcnotesLength, uint32_t natoms);
uint32_t refCount() const { return refCount_; }
void incRefCount() { refCount_++; }
void decRefCount() {
void AddRef() { refCount_++; }
void Release() {
MOZ_ASSERT(refCount_ != 0);
uint32_t remain = --refCount_;
if (remain == 0) {
@ -1543,12 +1544,11 @@ struct ScriptBytecodeHasher {
class Lookup {
friend struct ScriptBytecodeHasher;
SharedScriptData* scriptData;
RefPtr<SharedScriptData> scriptData;
HashNumber hash;
public:
explicit Lookup(SharedScriptData* data);
~Lookup();
};
static HashNumber hash(const Lookup& l) { return l.hash; }
@ -1598,7 +1598,7 @@ class JSScript : public js::gc::TenuredCell {
uint8_t* jitCodeSkipArgCheck_ = nullptr;
// Shareable script data
js::SharedScriptData* scriptData_ = nullptr;
RefPtr<js::SharedScriptData> scriptData_ = {};
// Unshared variable-length data
js::PrivateScriptData* data_ = nullptr;
@ -2537,7 +2537,6 @@ class JSScript : public js::gc::TenuredCell {
uint32_t noteLength, uint32_t natoms);
bool shareScriptData(JSContext* cx);
void freeScriptData();
void setScriptData(js::SharedScriptData* data);
public:
uint32_t getWarmUpCount() const { return warmUpCount; }