Bug 621032 - Move MathCache from JSThreadData to JSCompartment (r=igor,a=blocker)
authorBill McCloskey <wmccloskey@mozilla.com>
Wed, 29 Dec 2010 17:59:02 -0800
changeset 59735 15479f7be9d81edf78e72fe43eab0cf30a64a6b2
parent 59734 f66eefcf11ff7f0849bf1912a807314ee549f714
child 59736 49ed986cf05b7a96a275f4a128ff75be51b017dd
push id1
push usershaver@mozilla.com
push dateTue, 04 Jan 2011 17:58:04 +0000
reviewersigor, blocker
bugs621032
milestone2.0b9pre
Bug 621032 - Move MathCache from JSThreadData to JSCompartment (r=igor,a=blocker)
js/src/jscntxt.cpp
js/src/jscntxt.h
js/src/jscompartment.cpp
js/src/jscompartment.h
js/src/jsmath.cpp
js/src/jstracer.cpp
--- a/js/src/jscntxt.cpp
+++ b/js/src/jscntxt.cpp
@@ -502,36 +502,25 @@ JSThreadData::init()
     nativeStackBase = GetNativeStackBase();
 
     /* Set the default size for the code cache to 16MB. */
     maxCodeCacheBytes = 16 * 1024 * 1024;
     
     return true;
 }
 
-MathCache *
-JSThreadData::allocMathCache(JSContext *cx)
-{
-    JS_ASSERT(!mathCache);
-    mathCache = new MathCache;
-    if (!mathCache)
-        js_ReportOutOfMemory(cx);
-    return mathCache;
-}
-
 void
 JSThreadData::finish()
 {
     if (dtoaState)
         js_DestroyDtoaState(dtoaState);
 
     js_FinishGSNCache(&gsnCache);
     propertyCache.~PropertyCache();
     stackSpace.finish();
-    delete mathCache;
 }
 
 void
 JSThreadData::mark(JSTracer *trc)
 {
     stackSpace.mark(trc);
 }
 
--- a/js/src/jscntxt.h
+++ b/js/src/jscntxt.h
@@ -993,26 +993,16 @@ struct JSThreadData {
     /* Base address of the native stack for the current thread. */
     jsuword             *nativeStackBase;
 
     /* List of currently pending operations on proxies. */
     JSPendingProxyOperation *pendingProxyOperation;
 
     js::ConservativeGCThreadData conservativeGC;
 
-  private:
-    js::MathCache       *mathCache;
-
-    js::MathCache *allocMathCache(JSContext *cx);
-  public:
-
-    js::MathCache *getMathCache(JSContext *cx) {
-        return mathCache ? mathCache : allocMathCache(cx);
-    }
-
     bool init();
     void finish();
     void mark(JSTracer *trc);
     void purge(JSContext *cx);
 
     /* This must be called with the GC lock held. */
     inline void triggerOperationCallback(JSRuntime *rt);
 };
--- a/js/src/jscompartment.cpp
+++ b/js/src/jscompartment.cpp
@@ -50,33 +50,41 @@
 #include "methodjit/MonoIC.h"
 
 #include "jsgcinlines.h"
 
 using namespace js;
 using namespace js::gc;
 
 JSCompartment::JSCompartment(JSRuntime *rt)
-  : rt(rt), principals(NULL), data(NULL), marked(false), debugMode(rt->debugMode),
-    anynameObject(NULL), functionNamespaceObject(NULL)
+  : rt(rt),
+    principals(NULL),
+    data(NULL),
+    marked(false),
+    debugMode(rt->debugMode),
+    anynameObject(NULL),
+    functionNamespaceObject(NULL),
+    mathCache(NULL)
 {
     JS_INIT_CLIST(&scripts);
 
-    memset(scriptsToGC, 0, sizeof(scriptsToGC));
+    PodArrayZero(scriptsToGC);
 }
 
 JSCompartment::~JSCompartment()
 {
 #if defined JS_TRACER
     FinishJIT(&traceMonitor);
 #endif
 #ifdef JS_METHODJIT
     delete jaegerCompartment;
 #endif
 
+    delete mathCache;
+
 #ifdef DEBUG
     for (size_t i = 0; i != JS_ARRAY_LENGTH(scriptsToGC); ++i)
         JS_ASSERT(!scriptsToGC[i]);
 #endif
 }
 
 bool
 JSCompartment::init()
@@ -409,8 +417,18 @@ JSCompartment::purge(JSContext *cx)
              */
             if (cx->runtime->gcRegenShapes)
                 mjit::ic::PurgeMICs(cx, script);
 # endif
         }
     }
 #endif
 }
+
+MathCache *
+JSCompartment::allocMathCache(JSContext *cx)
+{
+    JS_ASSERT(!mathCache);
+    mathCache = new MathCache;
+    if (!mathCache)
+        js_ReportOutOfMemory(cx);
+    return mathCache;
+}
--- a/js/src/jscompartment.h
+++ b/js/src/jscompartment.h
@@ -277,21 +277,39 @@ struct JS_FRIEND_API(JSCompartment) {
     bool wrap(JSContext *cx, js::PropertyDescriptor *desc);
     bool wrap(JSContext *cx, js::AutoIdVector &props);
     bool wrapException(JSContext *cx);
 
     void sweep(JSContext *cx);
     void purge(JSContext *cx);
     void finishArenaLists();
     bool arenaListsAreEmpty();
+
+  private:
+    js::MathCache                *mathCache;
+
+    js::MathCache *allocMathCache(JSContext *cx);
+
+  public:
+    js::MathCache *getMathCache(JSContext *cx) {
+        return mathCache ? mathCache : allocMathCache(cx);
+    }
 };
 
 #define JS_TRACE_MONITOR(cx)    (cx->compartment->traceMonitor)
 #define JS_SCRIPTS_TO_GC(cx)    (cx->compartment->scriptsToGC)
 
+namespace js {
+static inline MathCache *
+GetMathCache(JSContext *cx)
+{
+    return cx->compartment->getMathCache(cx);
+}
+}
+
 #ifdef DEBUG
 # define EVAL_CACHE_METER(x)    (cx->compartment->evalCacheMeter.x++)
 #else
 # define EVAL_CACHE_METER(x)    ((void) 0)
 #endif
 
 #ifdef _MSC_VER
 #pragma warning(pop)
--- a/js/src/jsmath.cpp
+++ b/js/src/jsmath.cpp
@@ -49,16 +49,17 @@
 #include "jsatom.h"
 #include "jsbuiltins.h"
 #include "jscntxt.h"
 #include "jsversion.h"
 #include "jslock.h"
 #include "jsmath.h"
 #include "jsnum.h"
 #include "jslibmath.h"
+#include "jscompartment.h"
 
 using namespace js;
 
 #ifndef M_E
 #define M_E             2.7182818284590452354
 #endif
 #ifndef M_LOG2E
 #define M_LOG2E         1.4426950408889634074
@@ -143,17 +144,17 @@ math_acos(JSContext *cx, uintN argc, Val
     if (!ValueToNumber(cx, vp[2], &x))
         return JS_FALSE;
 #if defined(SOLARIS) && defined(__GNUC__)
     if (x < -1 || 1 < x) {
         vp->setDouble(js_NaN);
         return JS_TRUE;
     }
 #endif
-    MathCache *mathCache = JS_THREAD_DATA(cx)->getMathCache(cx);
+    MathCache *mathCache = GetMathCache(cx);
     if (!mathCache)
         return JS_FALSE;
     z = mathCache->lookup(acos, x);
     vp->setDouble(z);
     return JS_TRUE;
 }
 
 static JSBool
@@ -168,17 +169,17 @@ math_asin(JSContext *cx, uintN argc, Val
     if (!ValueToNumber(cx, vp[2], &x))
         return JS_FALSE;
 #if defined(SOLARIS) && defined(__GNUC__)
     if (x < -1 || 1 < x) {
         vp->setDouble(js_NaN);
         return JS_TRUE;
     }
 #endif
-    MathCache *mathCache = JS_THREAD_DATA(cx)->getMathCache(cx);
+    MathCache *mathCache = GetMathCache(cx);
     if (!mathCache)
         return JS_FALSE;
     z = mathCache->lookup(asin, x);
     vp->setDouble(z);
     return JS_TRUE;
 }
 
 static JSBool
@@ -187,17 +188,17 @@ math_atan(JSContext *cx, uintN argc, Val
     jsdouble x, z;
 
     if (argc == 0) {
         vp->setDouble(js_NaN);
         return JS_TRUE;
     }
     if (!ValueToNumber(cx, vp[2], &x))
         return JS_FALSE;
-    MathCache *mathCache = JS_THREAD_DATA(cx)->getMathCache(cx);
+    MathCache *mathCache = GetMathCache(cx);
     if (!mathCache)
         return JS_FALSE;
     z = mathCache->lookup(atan, x);
     vp->setDouble(z);
     return JS_TRUE;
 }
 
 static inline jsdouble JS_FASTCALL
@@ -280,17 +281,17 @@ math_cos(JSContext *cx, uintN argc, Valu
     jsdouble x, z;
 
     if (argc == 0) {
         vp->setDouble(js_NaN);
         return JS_TRUE;
     }
     if (!ValueToNumber(cx, vp[2], &x))
         return JS_FALSE;
-    MathCache *mathCache = JS_THREAD_DATA(cx)->getMathCache(cx);
+    MathCache *mathCache = GetMathCache(cx);
     if (!mathCache)
         return JS_FALSE;
     z = mathCache->lookup(cos, x);
     vp->setDouble(z);
     return JS_TRUE;
 }
 
 static double
@@ -313,17 +314,17 @@ math_exp(JSContext *cx, uintN argc, Valu
     jsdouble x, z;
 
     if (argc == 0) {
         vp->setDouble(js_NaN);
         return JS_TRUE;
     }
     if (!ValueToNumber(cx, vp[2], &x))
         return JS_FALSE;
-    MathCache *mathCache = JS_THREAD_DATA(cx)->getMathCache(cx);
+    MathCache *mathCache = GetMathCache(cx);
     if (!mathCache)
         return JS_FALSE;
     z = mathCache->lookup(math_exp_body, x);
     vp->setNumber(z);
     return JS_TRUE;
 }
 
 jsdouble
@@ -360,17 +361,17 @@ math_log(JSContext *cx, uintN argc, Valu
     if (!ValueToNumber(cx, vp[2], &x))
         return JS_FALSE;
 #if defined(SOLARIS) && defined(__GNUC__)
     if (x < 0) {
         vp->setDouble(js_NaN);
         return JS_TRUE;
     }
 #endif
-    MathCache *mathCache = JS_THREAD_DATA(cx)->getMathCache(cx);
+    MathCache *mathCache = GetMathCache(cx);
     if (!mathCache)
         return JS_FALSE;
     z = mathCache->lookup(log, x);
     vp->setNumber(z);
     return JS_TRUE;
 }
 
 JSBool
@@ -607,17 +608,17 @@ math_sin(JSContext *cx, uintN argc, Valu
     jsdouble x, z;
 
     if (argc == 0) {
         vp->setDouble(js_NaN);
         return JS_TRUE;
     }
     if (!ValueToNumber(cx, vp[2], &x))
         return JS_FALSE;
-    MathCache *mathCache = JS_THREAD_DATA(cx)->getMathCache(cx);
+    MathCache *mathCache = GetMathCache(cx);
     if (!mathCache)
         return JS_FALSE;
     z = mathCache->lookup(sin, x);
     vp->setDouble(z);
     return JS_TRUE;
 }
 
 static JSBool
@@ -626,17 +627,17 @@ math_sqrt(JSContext *cx, uintN argc, Val
     jsdouble x, z;
 
     if (argc == 0) {
         vp->setDouble(js_NaN);
         return JS_TRUE;
     }
     if (!ValueToNumber(cx, vp[2], &x))
         return JS_FALSE;
-    MathCache *mathCache = JS_THREAD_DATA(cx)->getMathCache(cx);
+    MathCache *mathCache = GetMathCache(cx);
     if (!mathCache)
         return JS_FALSE;
     z = mathCache->lookup(sqrt, x);
     vp->setDouble(z);
     return JS_TRUE;
 }
 
 static JSBool
@@ -645,17 +646,17 @@ math_tan(JSContext *cx, uintN argc, Valu
     jsdouble x, z;
 
     if (argc == 0) {
         vp->setDouble(js_NaN);
         return JS_TRUE;
     }
     if (!ValueToNumber(cx, vp[2], &x))
         return JS_FALSE;
-    MathCache *mathCache = JS_THREAD_DATA(cx)->getMathCache(cx);
+    MathCache *mathCache = GetMathCache(cx);
     if (!mathCache)
         return JS_FALSE;
     z = mathCache->lookup(tan, x);
     vp->setDouble(z);
     return JS_TRUE;
 }
 
 #if JS_HAS_TOSOURCE
--- a/js/src/jstracer.cpp
+++ b/js/src/jstracer.cpp
@@ -11045,17 +11045,17 @@ TraceRecorder::callSpecializedNative(JSN
                     *argp = w.nameImmpNonGC(fp->imacropc());
                 else
                     *argp = w.nameImmpNonGC(pc);
             } else if (argtype == 'D') { /* this, as a number */
                 if (!tval.isNumber())
                     goto next_specialization;
                 *argp = this_ins;
             } else if (argtype == 'M') {
-                MathCache *mathCache = JS_THREAD_DATA(cx)->getMathCache(cx);
+                MathCache *mathCache = GetMathCache(cx);
                 if (!mathCache)
                     return RECORD_ERROR;
                 *argp = w.nameImmpNonGC(mathCache);
             } else {
                 JS_NOT_REACHED("unknown prefix arg type");
             }
             argp--;
         }