From 7971b7b2491b36517c87cd8cabf4446e7940fc99 Mon Sep 17 00:00:00 2001 From: Jim Blandy Date: Tue, 19 Aug 2014 14:21:33 -0700 Subject: [PATCH] Bug 1053932: Require all Debugger.Memory Assorters to provide a running total. r=terrence --- js/src/vm/DebuggerMemory.cpp | 45 ++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/js/src/vm/DebuggerMemory.cpp b/js/src/vm/DebuggerMemory.cpp index 594b747a9c4..c65c45e3d35 100644 --- a/js/src/vm/DebuggerMemory.cpp +++ b/js/src/vm/DebuggerMemory.cpp @@ -280,6 +280,9 @@ struct Census { // bool count(Census &census, const Node &node); // Categorize and count |node| as appropriate for this kind of assorter. // +// size_t total() const; +// Return the number of times 'count' has been called. +// // bool report(Census &census, MutableHandleValue report); // Construct a JavaScript object reporting the counts this assorter has // seen, and store it in |report|. @@ -289,23 +292,25 @@ struct Census { // The simplest assorter: count everything, and return a tally form. class Tally { - size_t counter; + size_t total_; public: - Tally(Census &census) : counter(0) { } - Tally(Tally &&rhs) : counter(rhs.counter) { } - Tally &operator=(Tally &&rhs) { counter = rhs.counter; return *this; } + Tally(Census &census) : total_(0) { } + Tally(Tally &&rhs) : total_(rhs.total_) { } + Tally &operator=(Tally &&rhs) { total_ = rhs.total_; return *this; } bool init(Census &census) { return true; } bool count(Census &census, const Node &node) { - counter++; + total_++; return true; } + size_t total() const { return total_; } + bool report(Census &census, MutableHandleValue report) { RootedObject obj(census.cx, NewBuiltinClassInstance(census.cx, &JSObject::class_)); - RootedValue countValue(census.cx, NumberValue(counter)); + RootedValue countValue(census.cx, NumberValue(total_)); if (!obj || !JSObject::defineProperty(census.cx, obj, census.cx->names().count, countValue)) { @@ -327,6 +332,7 @@ template class ByJSType { + size_t total_; EachObject objects; EachScript scripts; EachString strings; @@ -334,13 +340,15 @@ class ByJSType { public: ByJSType(Census &census) - : objects(census), + : total_(0), + objects(census), scripts(census), strings(census), other(census) { } ByJSType(ByJSType &&rhs) - : objects(Move(rhs.objects)), + : total_(rhs.total_), + objects(Move(rhs.objects)), scripts(move(rhs.scripts)), strings(move(rhs.strings)), other(move(rhs.other)) @@ -360,6 +368,7 @@ class ByJSType { } bool count(Census &census, const Node &node) { + total_++; if (node.is()) return objects.count(census, node); if (node.is() || node.is() || node.is()) @@ -409,6 +418,8 @@ class ByJSType { template class ByObjectClass { + size_t total_; + // A hash policy that compares js::Classes by name. struct HashPolicy { typedef const js::Class *Lookup; @@ -428,8 +439,10 @@ class ByObjectClass { EachOther other; public: - ByObjectClass(Census &census) : other(census) { } - ByObjectClass(ByObjectClass &&rhs) : table(Move(rhs.table)), other(Move(rhs.other)) { } + ByObjectClass(Census &census) : total_(0), other(census) { } + ByObjectClass(ByObjectClass &&rhs) + : total_(rhs.total_), table(Move(rhs.table)), other(Move(rhs.other)) + { } ByObjectClass &operator=(ByObjectClass &&rhs) { MOZ_ASSERT(&rhs != this); this->~ByObjectClass(); @@ -440,6 +453,7 @@ class ByObjectClass { bool init(Census &census) { return table.init() && other.init(census); } bool count(Census &census, const Node &node) { + total_++; if (!node.is()) return other.count(census, node); @@ -454,6 +468,8 @@ class ByObjectClass { return p->value().count(census, node); } + size_t total() const { return total_; } + bool report(Census &census, MutableHandleValue report) { JSContext *cx = census.cx; @@ -501,6 +517,8 @@ class ByObjectClass { // An assorter that categorizes nodes by their ubi::Node::typeName. template class ByUbinodeType { + size_t total_; + // Note that, because ubi::Node::typeName promises to return a specific // pointer, not just any string whose contents are correct, we can use their // addresses as hash table keys. @@ -508,8 +526,8 @@ class ByUbinodeType { Table table; public: - ByUbinodeType(Census &census) { } - ByUbinodeType(ByUbinodeType &&rhs) : table(Move(rhs.table)) { } + ByUbinodeType(Census &census) : total_(0) { } + ByUbinodeType(ByUbinodeType &&rhs) : total_(rhs.total_), table(Move(rhs.table)) { } ByUbinodeType &operator=(ByUbinodeType &&rhs) { MOZ_ASSERT(&rhs != this); this->~ByUbinodeType(); @@ -520,6 +538,7 @@ class ByUbinodeType { bool init(Census &census) { return table.init(); } bool count(Census &census, const Node &node) { + total_++; const jschar *key = node.typeName(); typename Table::AddPtr p = table.lookupForAdd(key); if (!p) { @@ -531,6 +550,8 @@ class ByUbinodeType { return p->value().count(census, node); } + size_t total() const { return total_; } + bool report(Census &census, MutableHandleValue report) { JSContext *cx = census.cx;