Bug 960860 - Don't include zlib.h from jsutil.h (r=njn)
authorLuke Wagner <luke@mozilla.com>
Fri, 17 Jan 2014 09:15:59 -0600
changeset 164095 56580a9e6ca61f8a762136469da802e3821f91e8
parent 164094 dbe8147a798142b5b467df1d9b631331c972634d
child 164096 9d2d2a2bea7f71b2a4b8b4807a6ec06748e29983
push id26026
push userphilringnalda@gmail.com
push dateSat, 18 Jan 2014 23:17:27 +0000
treeherdermozilla-central@61fd0f987cf2 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnjn
bugs960860
milestone29.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 960860 - Don't include zlib.h from jsutil.h (r=njn)
js/src/jsscript.cpp
js/src/jsutil.cpp
js/src/jsutil.h
js/src/jsworkers.cpp
js/src/jsworkers.h
js/src/moz.build
js/src/vm/Compression.cpp
js/src/vm/Compression.h
js/src/vm/SelfHosting.cpp
--- a/js/src/jsscript.cpp
+++ b/js/src/jsscript.cpp
@@ -28,16 +28,17 @@
 
 #include "frontend/BytecodeEmitter.h"
 #include "frontend/SharedContext.h"
 #include "gc/Marking.h"
 #include "jit/BaselineJIT.h"
 #include "jit/IonCode.h"
 #include "js/OldDebugAPI.h"
 #include "vm/ArgumentsObject.h"
+#include "vm/Compression.h"
 #include "vm/Debugger.h"
 #include "vm/Shape.h"
 #include "vm/Xdr.h"
 
 #include "jsfuninlines.h"
 #include "jsinferinlines.h"
 #include "jsobjinlines.h"
 
@@ -1234,17 +1235,17 @@ ScriptSource::setSource(const jschar *sr
 {
     JS_ASSERT(!hasSourceData());
     length_ = length;
     JS_ASSERT(!argumentsNotIncluded_);
     data.source = const_cast<jschar *>(src);
 }
 
 bool
-SourceCompressionTask::compress()
+SourceCompressionTask::work()
 {
     // A given compression token can be compressed on any thread, and the ss
     // not being ready indicates to other threads that its fields might change
     // with no lock held.
     JS_ASSERT(!ss->ready());
 
     size_t compressedLength = 0;
     size_t nbytes = sizeof(jschar) * ss->length_;
--- a/js/src/jsutil.cpp
+++ b/js/src/jsutil.cpp
@@ -22,131 +22,16 @@
 
 #include "js/Utility.h"
 
 using namespace js;
 
 using mozilla::CeilingLog2Size;
 using mozilla::PodArrayZero;
 
-#if USE_ZLIB
-static void *
-zlib_alloc(void *cx, uInt items, uInt size)
-{
-    return js_calloc(items, size);
-}
-
-static void
-zlib_free(void *cx, void *addr)
-{
-    js_free(addr);
-}
-
-Compressor::Compressor(const unsigned char *inp, size_t inplen)
-    : inp(inp),
-      inplen(inplen),
-      outbytes(0)
-{
-    JS_ASSERT(inplen > 0);
-    zs.opaque = nullptr;
-    zs.next_in = (Bytef *)inp;
-    zs.avail_in = 0;
-    zs.next_out = nullptr;
-    zs.avail_out = 0;
-    zs.zalloc = zlib_alloc;
-    zs.zfree = zlib_free;
-}
-
-
-Compressor::~Compressor()
-{
-    int ret = deflateEnd(&zs);
-    if (ret != Z_OK) {
-        // If we finished early, we can get a Z_DATA_ERROR.
-        JS_ASSERT(ret == Z_DATA_ERROR);
-        JS_ASSERT(uInt(zs.next_in - inp) < inplen || !zs.avail_out);
-    }
-}
-
-bool
-Compressor::init()
-{
-    if (inplen >= UINT32_MAX)
-        return false;
-    // zlib is slow and we'd rather be done compression sooner
-    // even if it means decompression is slower which penalizes
-    // Function.toString()
-    int ret = deflateInit(&zs, Z_BEST_SPEED);
-    if (ret != Z_OK) {
-        JS_ASSERT(ret == Z_MEM_ERROR);
-        return false;
-    }
-    return true;
-}
-
-void
-Compressor::setOutput(unsigned char *out, size_t outlen)
-{
-    JS_ASSERT(outlen > outbytes);
-    zs.next_out = out + outbytes;
-    zs.avail_out = outlen - outbytes;
-}
-
-Compressor::Status
-Compressor::compressMore()
-{
-    JS_ASSERT(zs.next_out);
-    uInt left = inplen - (zs.next_in - inp);
-    bool done = left <= CHUNKSIZE;
-    if (done)
-        zs.avail_in = left;
-    else if (zs.avail_in == 0)
-        zs.avail_in = CHUNKSIZE;
-    Bytef *oldout = zs.next_out;
-    int ret = deflate(&zs, done ? Z_FINISH : Z_NO_FLUSH);
-    outbytes += zs.next_out - oldout;
-    if (ret == Z_MEM_ERROR) {
-        zs.avail_out = 0;
-        return OOM;
-    }
-    if (ret == Z_BUF_ERROR || (done && ret == Z_OK)) {
-        JS_ASSERT(zs.avail_out == 0);
-        return MOREOUTPUT;
-    }
-    JS_ASSERT_IF(!done, ret == Z_OK);
-    JS_ASSERT_IF(done, ret == Z_STREAM_END);
-    return done ? DONE : CONTINUE;
-}
-
-bool
-js::DecompressString(const unsigned char *inp, size_t inplen, unsigned char *out, size_t outlen)
-{
-    JS_ASSERT(inplen <= UINT32_MAX);
-    z_stream zs;
-    zs.zalloc = zlib_alloc;
-    zs.zfree = zlib_free;
-    zs.opaque = nullptr;
-    zs.next_in = (Bytef *)inp;
-    zs.avail_in = inplen;
-    zs.next_out = out;
-    JS_ASSERT(outlen);
-    zs.avail_out = outlen;
-    int ret = inflateInit(&zs);
-    if (ret != Z_OK) {
-        JS_ASSERT(ret == Z_MEM_ERROR);
-        return false;
-    }
-    ret = inflate(&zs, Z_FINISH);
-    JS_ASSERT(ret == Z_STREAM_END);
-    ret = inflateEnd(&zs);
-    JS_ASSERT(ret == Z_OK);
-    return true;
-}
-#endif
-
 #ifdef DEBUG
 /* For JS_OOM_POSSIBLY_FAIL in jsutil.h. */
 JS_PUBLIC_DATA(uint32_t) OOM_maxAllocations = UINT32_MAX;
 JS_PUBLIC_DATA(uint32_t) OOM_counter = 0;
 #endif
 
 /*
  * Checks the assumption that JS_FUNC_TO_DATA_PTR and JS_DATA_TO_FUNC_PTR
--- a/js/src/jsutil.h
+++ b/js/src/jsutil.h
@@ -11,20 +11,16 @@
 #ifndef jsutil_h
 #define jsutil_h
 
 #include "mozilla/Compiler.h"
 #include "mozilla/GuardObjects.h"
 
 #include <limits.h>
 
-#ifdef USE_ZLIB
-#include <zlib.h>
-#endif
-
 #include "js/Utility.h"
 
 static JS_ALWAYS_INLINE void *
 js_memcpy(void *dst_, const void *src_, size_t len)
 {
     char *dst = (char *) dst_;
     const char *src = (const char *) src_;
     JS_ASSERT_IF(dst >= src, (size_t) (dst - src) >= len);
@@ -259,51 +255,16 @@ ClearBitArrayElement(size_t *array, size
 
 static inline void
 ClearAllBitArrayElements(size_t *array, size_t length)
 {
     for (unsigned i = 0; i < length; ++i)
         array[i] = 0;
 }
 
-#ifdef USE_ZLIB
-class Compressor
-{
-    /* Number of bytes we should hand to zlib each compressMore() call. */
-    static const size_t CHUNKSIZE = 2048;
-    z_stream zs;
-    const unsigned char *inp;
-    size_t inplen;
-    size_t outbytes;
-
-  public:
-    enum Status {
-        MOREOUTPUT,
-        DONE,
-        CONTINUE,
-        OOM
-    };
-
-    Compressor(const unsigned char *inp, size_t inplen);
-    ~Compressor();
-    bool init();
-    void setOutput(unsigned char *out, size_t outlen);
-    size_t outWritten() const { return outbytes; }
-    /* Compress some of the input. Return true if it should be called again. */
-    Status compressMore();
-};
-
-/*
- * Decompress a string. The caller must know the length of the output and
- * allocate |out| to a string of that length.
- */
-bool DecompressString(const unsigned char *inp, size_t inplen,
-                      unsigned char *out, size_t outlen);
-#endif
-
 }  /* namespace js */
 
 /* Crash diagnostics */
 #ifdef DEBUG
 # define JS_CRASH_DIAGNOSTICS 1
 #endif
 #ifdef JS_CRASH_DIAGNOSTICS
 # define JS_POISON(p, val, size) memset((p), (val), (size))
--- a/js/src/jsworkers.cpp
+++ b/js/src/jsworkers.cpp
@@ -865,17 +865,17 @@ WorkerThread::handleCompressionWorkload(
     JS_ASSERT(state.canStartCompressionTask());
     JS_ASSERT(idle());
 
     compressionTask = state.compressionWorklist.popCopy();
     compressionTask->workerThread = this;
 
     {
         AutoUnlockWorkerThreadState unlock(runtime);
-        if (!compressionTask->compress())
+        if (!compressionTask->work())
             compressionTask->setOOM();
     }
 
     compressionTask->workerThread = nullptr;
     compressionTask = nullptr;
 
     // Notify the main thread in case it is waiting for the compression to finish.
     state.notifyAll(WorkerThreadState::CONSUMER);
--- a/js/src/jsworkers.h
+++ b/js/src/jsworkers.h
@@ -408,17 +408,17 @@ struct SourceCompressionTask
 #endif
     }
 
     ~SourceCompressionTask()
     {
         complete();
     }
 
-    bool compress();
+    bool work();
     bool complete();
     void abort() { abort_ = 1; }
     bool active() const { return !!ss; }
     ScriptSource *source() { return ss; }
     const jschar *uncompressedChars() { return chars; }
     void setOOM() { oom = true; }
 };
 
--- a/js/src/moz.build
+++ b/js/src/moz.build
@@ -150,16 +150,17 @@ UNIFIED_SOURCES += [
     'jsweakmap.cpp',
     'jsworkers.cpp',
     'jswrapper.cpp',
     'perf/jsperf.cpp',
     'prmjtime.cpp',
     'vm/ArgumentsObject.cpp',
     'vm/CallNonGenericMethod.cpp',
     'vm/CharacterEncoding.cpp',
+    'vm/Compression.cpp',
     'vm/DateTime.cpp',
     'vm/Debugger.cpp',
     'vm/ErrorObject.cpp',
     'vm/ForkJoin.cpp',
     'vm/GlobalObject.cpp',
     'vm/Id.cpp',
     'vm/Interpreter.cpp',
     'vm/MemoryMetrics.cpp',
new file mode 100644
--- /dev/null
+++ b/js/src/vm/Compression.cpp
@@ -0,0 +1,127 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
+ * vim: set ts=8 sts=4 et sw=4 tw=99:
+ * 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 "vm/Compression.h"
+
+#include "js/Utility.h"
+
+using namespace js;
+
+#if USE_ZLIB
+static void *
+zlib_alloc(void *cx, uInt items, uInt size)
+{
+    return js_calloc(items, size);
+}
+
+static void
+zlib_free(void *cx, void *addr)
+{
+    js_free(addr);
+}
+
+Compressor::Compressor(const unsigned char *inp, size_t inplen)
+    : inp(inp),
+      inplen(inplen),
+      outbytes(0)
+{
+    JS_ASSERT(inplen > 0);
+    zs.opaque = nullptr;
+    zs.next_in = (Bytef *)inp;
+    zs.avail_in = 0;
+    zs.next_out = nullptr;
+    zs.avail_out = 0;
+    zs.zalloc = zlib_alloc;
+    zs.zfree = zlib_free;
+}
+
+
+Compressor::~Compressor()
+{
+    int ret = deflateEnd(&zs);
+    if (ret != Z_OK) {
+        // If we finished early, we can get a Z_DATA_ERROR.
+        JS_ASSERT(ret == Z_DATA_ERROR);
+        JS_ASSERT(uInt(zs.next_in - inp) < inplen || !zs.avail_out);
+    }
+}
+
+bool
+Compressor::init()
+{
+    if (inplen >= UINT32_MAX)
+        return false;
+    // zlib is slow and we'd rather be done compression sooner
+    // even if it means decompression is slower which penalizes
+    // Function.toString()
+    int ret = deflateInit(&zs, Z_BEST_SPEED);
+    if (ret != Z_OK) {
+        JS_ASSERT(ret == Z_MEM_ERROR);
+        return false;
+    }
+    return true;
+}
+
+void
+Compressor::setOutput(unsigned char *out, size_t outlen)
+{
+    JS_ASSERT(outlen > outbytes);
+    zs.next_out = out + outbytes;
+    zs.avail_out = outlen - outbytes;
+}
+
+Compressor::Status
+Compressor::compressMore()
+{
+    JS_ASSERT(zs.next_out);
+    uInt left = inplen - (zs.next_in - inp);
+    bool done = left <= CHUNKSIZE;
+    if (done)
+        zs.avail_in = left;
+    else if (zs.avail_in == 0)
+        zs.avail_in = CHUNKSIZE;
+    Bytef *oldout = zs.next_out;
+    int ret = deflate(&zs, done ? Z_FINISH : Z_NO_FLUSH);
+    outbytes += zs.next_out - oldout;
+    if (ret == Z_MEM_ERROR) {
+        zs.avail_out = 0;
+        return OOM;
+    }
+    if (ret == Z_BUF_ERROR || (done && ret == Z_OK)) {
+        JS_ASSERT(zs.avail_out == 0);
+        return MOREOUTPUT;
+    }
+    JS_ASSERT_IF(!done, ret == Z_OK);
+    JS_ASSERT_IF(done, ret == Z_STREAM_END);
+    return done ? DONE : CONTINUE;
+}
+
+bool
+js::DecompressString(const unsigned char *inp, size_t inplen, unsigned char *out, size_t outlen)
+{
+    JS_ASSERT(inplen <= UINT32_MAX);
+    z_stream zs;
+    zs.zalloc = zlib_alloc;
+    zs.zfree = zlib_free;
+    zs.opaque = nullptr;
+    zs.next_in = (Bytef *)inp;
+    zs.avail_in = inplen;
+    zs.next_out = out;
+    JS_ASSERT(outlen);
+    zs.avail_out = outlen;
+    int ret = inflateInit(&zs);
+    if (ret != Z_OK) {
+        JS_ASSERT(ret == Z_MEM_ERROR);
+        return false;
+    }
+    ret = inflate(&zs, Z_FINISH);
+    JS_ASSERT(ret == Z_STREAM_END);
+    ret = inflateEnd(&zs);
+    JS_ASSERT(ret == Z_OK);
+    return true;
+}
+#endif /* USE_ZLIB */
+
new file mode 100644
--- /dev/null
+++ b/js/src/vm/Compression.h
@@ -0,0 +1,54 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
+ * vim: set ts=8 sts=4 et sw=4 tw=99:
+ * 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/. */
+
+#ifndef vm_Compression_h
+#define vm_Compression_h
+
+#ifdef USE_ZLIB
+
+#include <zlib.h>
+
+#include "jstypes.h"
+
+namespace js {
+
+class Compressor
+{
+    /* Number of bytes we should hand to zlib each compressMore() call. */
+    static const size_t CHUNKSIZE = 2048;
+    z_stream zs;
+    const unsigned char *inp;
+    size_t inplen;
+    size_t outbytes;
+
+  public:
+    enum Status {
+        MOREOUTPUT,
+        DONE,
+        CONTINUE,
+        OOM
+    };
+
+    Compressor(const unsigned char *inp, size_t inplen);
+    ~Compressor();
+    bool init();
+    void setOutput(unsigned char *out, size_t outlen);
+    size_t outWritten() const { return outbytes; }
+    /* Compress some of the input. Return true if it should be called again. */
+    Status compressMore();
+};
+
+/*
+ * Decompress a string. The caller must know the length of the output and
+ * allocate |out| to a string of that length.
+ */
+bool DecompressString(const unsigned char *inp, size_t inplen,
+                      unsigned char *out, size_t outlen);
+
+} /* namespace js */
+
+#endif /* USE_ZLIB */
+#endif /* vm_Compression_h */
--- a/js/src/vm/SelfHosting.cpp
+++ b/js/src/vm/SelfHosting.cpp
@@ -12,16 +12,17 @@
 #include "jshashutil.h"
 #include "jsobj.h"
 #include "jswrapper.h"
 #include "selfhosted.out.h"
 
 #include "builtin/Intl.h"
 #include "builtin/TypedObject.h"
 #include "gc/Marking.h"
+#include "vm/Compression.h"
 #include "vm/ForkJoin.h"
 #include "vm/Interpreter.h"
 
 #include "jsfuninlines.h"
 #include "jsscriptinlines.h"
 
 #include "vm/BooleanObject-inl.h"
 #include "vm/NumberObject-inl.h"