Skip to content
This repository was archived by the owner on Nov 12, 2022. It is now read-only.

Commit 0ff4db7

Browse files
author
bors-servo
authored
Auto merge of #409 - aeweston98:aew-issue7084, r=jdm
Use ObjectPrivateVisitor derived class in CollectServoSizes to measure heap usage of DOM objects This PR contains the updates in rust-mozjs necessary for [servo issue 7084](servo/servo#7084). A new class which derives from ObjectPrivateVisitor was added. It is used in CollectServoSizes, with an instance being passed into AddServoSizeOf instead of a nullptr. An additional function was added (InitializeMemoryReporter) which sets the value of a global function pointer, which is used to check if we want to measure the heap usage for a particular JSObject. <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-mozjs/409) <!-- Reviewable:end -->
2 parents f584529 + a9566ed commit 0ff4db7

File tree

3 files changed

+58
-6
lines changed

3 files changed

+58
-6
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "mozjs"
33
description = "Rust bindings to the Mozilla SpiderMonkey JavaScript engine."
44
repository = "https://github.com/servo/rust-mozjs"
5-
version = "0.4.2"
5+
version = "0.5.0"
66
authors = ["The Servo Project Developers"]
77
build = "build.rs"
88
license = "MPL-2.0"

src/glue.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,8 @@ extern "C" {
261261
pub fn AppendToAutoObjectVector(v: *mut AutoObjectVector,
262262
obj: *mut JSObject) -> bool;
263263
pub fn DeleteAutoObjectVector(v: *mut AutoObjectVector);
264-
pub fn CollectServoSizes(rt: *mut JSRuntime, sizes: *mut ServoSizes) -> bool;
264+
pub fn CollectServoSizes(rt: *mut JSRuntime, sizes: *mut ServoSizes, get_size: Option<unsafe extern "C" fn (obj: *mut JSObject) -> usize>) -> bool;
265+
pub fn InitializeMemoryReporter(want_to_measure: Option<unsafe extern "C" fn (obj: *mut JSObject) -> bool>);
265266
pub fn CallIdTracer(trc: *mut JSTracer, idp: *mut Heap<jsid>,
266267
name: *const ::libc::c_char);
267268
pub fn CallValueTracer(trc: *mut JSTracer, valuep: *mut Heap<Value>,

src/jsglue.cpp

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
#include "js/Principals.h"
2222
#include "assert.h"
2323

24+
typedef bool(*WantToMeasure)(JSObject *obj);
25+
typedef size_t(*GetSize)(JSObject *obj);
26+
27+
WantToMeasure gWantToMeasure = nullptr;
28+
2429
struct ProxyTraps {
2530
bool (*enter)(JSContext *cx, JS::HandleObject proxy, JS::HandleId id,
2631
js::BaseProxyHandler::Action action, bool *bp);
@@ -437,6 +442,45 @@ class ForwardingProxyHandler : public js::BaseProxyHandler
437442
}
438443
};
439444

445+
class ServoDOMVisitor : public JS::ObjectPrivateVisitor {
446+
public:
447+
size_t sizeOfIncludingThis(nsISupports *aSupports) {
448+
449+
JSObject* obj = (JSObject*)aSupports;
450+
size_t result = 0;
451+
452+
if (get_size != nullptr && obj != nullptr) {
453+
result = (*get_size)(obj);
454+
}
455+
456+
return result;
457+
}
458+
459+
GetSize get_size;
460+
461+
ServoDOMVisitor(GetSize gs, GetISupportsFun getISupports)
462+
: ObjectPrivateVisitor(getISupports)
463+
, get_size(gs)
464+
{}
465+
};
466+
467+
bool
468+
ShouldMeasureObject(JSObject* obj, nsISupports** iface) {
469+
470+
if (obj == nullptr) {
471+
return false;
472+
}
473+
474+
bool want_to_measure = (*gWantToMeasure)(obj);
475+
476+
if (want_to_measure) {
477+
*iface = (nsISupports*)obj;
478+
return true;
479+
}
480+
return false;
481+
}
482+
483+
440484
extern "C" {
441485

442486
JSPrincipals*
@@ -805,11 +849,18 @@ static size_t MallocSizeOf(const void* aPtr)
805849
}
806850

807851
bool
808-
CollectServoSizes(JSRuntime *rt, JS::ServoSizes *sizes)
852+
CollectServoSizes(JSRuntime *rt, JS::ServoSizes *sizes, GetSize gs)
809853
{
810-
mozilla::PodZero(sizes);
811-
return JS::AddServoSizeOf(rt, MallocSizeOf,
812-
/* ObjectPrivateVisitor = */ nullptr, sizes);
854+
mozilla::PodZero(sizes);
855+
856+
ServoDOMVisitor sdv(gs, ShouldMeasureObject);
857+
858+
return JS::AddServoSizeOf(rt, MallocSizeOf, &sdv, sizes);
859+
}
860+
861+
void
862+
InitializeMemoryReporter(WantToMeasure wtm){
863+
gWantToMeasure = wtm;
813864
}
814865

815866
void

0 commit comments

Comments
 (0)