Bug 735615 - Remove AutoClean from mozglue/linkers/Utils.h r=glandium
authorAndrew Quartey<andrew.quartey@gmail.com>
Mon, 30 Jul 2012 14:17:53 -0400
changeset 100862 3c34aa0c81f7541a12c64ab32132f7c26908239a
parent 100861 034d26a9a87398e19239ac4f1995a6235e191cf1
child 100863 883bdf118b52331224bc8a3c0cc4540810331b31
push idunknown
push userunknown
push dateunknown
reviewersglandium
bugs735615
milestone17.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 735615 - Remove AutoClean from mozglue/linkers/Utils.h r=glandium
mozglue/linker/Mappable.cpp
mozglue/linker/Mappable.h
mozglue/linker/Utils.h
mozglue/linker/szip.cpp
--- a/mozglue/linker/Mappable.cpp
+++ b/mozglue/linker/Mappable.cpp
@@ -69,35 +69,38 @@ Mappable *
 MappableExtractFile::Create(const char *name, Zip *zip, Zip::Stream *stream)
 {
   const char *cachePath = getenv("MOZ_LINKER_CACHE");
   if (!cachePath || !*cachePath) {
     log("Warning: MOZ_LINKER_EXTRACT is set, but not MOZ_LINKER_CACHE; "
         "not extracting");
     return NULL;
   }
-  AutoDeleteArray<char> path = new char[strlen(cachePath) + strlen(name) + 2];
+  mozilla::ScopedDeleteArray<char> path;
+  path = new char[strlen(cachePath) + strlen(name) + 2];
   sprintf(path, "%s/%s", cachePath, name);
   struct stat cacheStat;
   if (stat(path, &cacheStat) == 0) {
     struct stat zipStat;
     stat(zip->GetName(), &zipStat);
     if (cacheStat.st_mtime > zipStat.st_mtime) {
       debug("Reusing %s", static_cast<char *>(path));
       return MappableFile::Create(path);
     }
   }
   debug("Extracting to %s", static_cast<char *>(path));
-  AutoCloseFD fd = open(path, O_TRUNC | O_RDWR | O_CREAT | O_NOATIME,
-                              S_IRUSR | S_IWUSR);
+  AutoCloseFD fd;
+  fd = open(path, O_TRUNC | O_RDWR | O_CREAT | O_NOATIME,
+                  S_IRUSR | S_IWUSR);
   if (fd == -1) {
     log("Couldn't open %s to decompress library", path.get());
     return NULL;
   }
-  AutoUnlinkFile file = path.forget();
+  AutoUnlinkFile file;
+  file = path.forget();
   if (stream->GetType() == Zip::Stream::DEFLATE) {
     if (ftruncate(fd, stream->GetUncompressedSize()) == -1) {
       log("Couldn't ftruncate %s to decompress library", file.get());
       return NULL;
     }
     /* Map the temporary file for use as inflate buffer */
     MappedPtr buffer(::mmap(NULL, stream->GetUncompressedSize(), PROT_WRITE,
                             MAP_SHARED, fd, 0), stream->GetUncompressedSize());
@@ -336,18 +339,18 @@ MappableDeflate::finalize()
   zip = NULL;
 }
 
 Mappable *
 MappableSeekableZStream::Create(const char *name, Zip *zip,
                                 Zip::Stream *stream)
 {
   MOZ_ASSERT(stream->GetType() == Zip::Stream::STORE);
-  AutoDeletePtr<MappableSeekableZStream> mappable =
-    new MappableSeekableZStream(zip);
+  mozilla::ScopedDeletePtr<MappableSeekableZStream> mappable;
+  mappable = new MappableSeekableZStream(zip);
 
   if (pthread_mutex_init(&mappable->mutex, NULL))
     return NULL;
 
   if (!mappable->zStream.Init(stream->GetBuffer()))
     return NULL;
 
   mappable->buffer = _MappableBuffer::Create(name,
@@ -518,17 +521,18 @@ MappableSeekableZStream::ensure(const vo
 void
 MappableSeekableZStream::stats(const char *when, const char *name) const
 {
   size_t nEntries = zStream.GetChunksNum();
   debug("%s: %s; %ld/%ld chunks decompressed",
         name, when, chunkAvailNum, nEntries);
 
   size_t len = 64;
-  AutoDeleteArray<char> map = new char[len + 3];
+  mozilla::ScopedDeleteArray<char> map;
+  map = new char[len + 3];
   map[0] = '[';
 
   for (size_t i = 0, j = 1; i < nEntries; i++, j++) {
     map[j] = chunkAvail[i] ? '*' : '_';
     if ((j == len) || (i == nEntries - 1)) {
       map[j + 1] = ']';
       map[j + 2] = '\0';
       debug("%s", static_cast<char *>(map));
--- a/mozglue/linker/Mappable.h
+++ b/mozglue/linker/Mappable.h
@@ -5,16 +5,17 @@
 #ifndef Mappable_h
 #define Mappable_h
 
 #include <sys/types.h>
 #include <pthread.h>
 #include "Zip.h"
 #include "SeekableZStream.h"
 #include "mozilla/RefPtr.h"
+#include "mozilla/Scoped.h"
 #include "zlib.h"
 
 /**
  * Abstract class to handle mmap()ing from various kind of entities, such as
  * plain files or Zip entries. The virtual members are meant to act as the
  * equivalent system functions, with a few differences:
  * - mapped memory is always MAP_PRIVATE, even though a given implementation
  *   may use something different internally.
@@ -104,25 +105,25 @@ public:
 private:
   MappableExtractFile(int fd, char *path)
   : MappableFile(fd), path(path), pid(getpid()) { }
 
   /**
    * AutoUnlinkFile keeps track or a file name and removes (unlinks) the file
    * when the instance is destroyed.
    */
-  struct AutoUnlinkFileTraits: public AutoDeleteArrayTraits<char>
+  struct AutoUnlinkFileTraits: public mozilla::ScopedDeleteArrayTraits<char>
   {
-    static void clean(char *value)
+    static void release(char *value)
     {
       unlink(value);
-      AutoDeleteArrayTraits<char>::clean(value);
+      mozilla::ScopedDeleteArrayTraits<char>::release(value);
     }
   };
-  typedef AutoClean<AutoUnlinkFileTraits> AutoUnlinkFile;
+  typedef mozilla::Scoped<AutoUnlinkFileTraits> AutoUnlinkFile;
 
   /* Extracted file */
   AutoUnlinkFile path;
 
   /* Id of the process that initialized the instance */
   pid_t pid;
 };
 
@@ -150,17 +151,17 @@ public:
 
 private:
   MappableDeflate(_MappableBuffer *buf, Zip *zip, Zip::Stream *stream);
 
   /* Zip reference */
   mozilla::RefPtr<Zip> zip;
 
   /* Decompression buffer */
-  AutoDeletePtr<_MappableBuffer> buffer;
+  mozilla::ScopedDeletePtr<_MappableBuffer> buffer;
 
   /* Zlib data */
   z_stream zStream;
 };
 
 /**
  * Mappable implementation for seekable zStreams.
  * Inflates the mapped bits in a temporary buffer, on demand.
@@ -188,17 +189,17 @@ public:
 
 private:
   MappableSeekableZStream(Zip *zip);
 
   /* Zip reference */
   mozilla::RefPtr<Zip> zip;
 
   /* Decompression buffer */
-  AutoDeletePtr<_MappableBuffer> buffer;
+  mozilla::ScopedDeletePtr<_MappableBuffer> buffer;
 
   /* Seekable ZStream */
   SeekableZStream zStream;
 
   /* Keep track of mappings performed with MappableSeekableZStream::mmap so
    * that they can be realized by MappableSeekableZStream::ensure.
    * Values stored in the struct are those passed to mmap */
   struct LazyMap
@@ -231,17 +232,17 @@ private:
     }
   };
 
   /* List of all mappings */
   std::vector<LazyMap> lazyMaps;
 
   /* Array keeping track of which chunks have already been decompressed.
    * Each value is the number of pages decompressed for the given chunk. */
-  AutoDeleteArray<unsigned char> chunkAvail;
+  mozilla::ScopedDeleteArray<unsigned char> chunkAvail;
 
   /* Number of chunks that have already been decompressed. */
   size_t chunkAvailNum;
 
   /* Mutex protecting decompression */
   pthread_mutex_t mutex;
 };
 
--- a/mozglue/linker/Utils.h
+++ b/mozglue/linker/Utils.h
@@ -5,16 +5,17 @@
 #ifndef Utils_h
 #define Utils_h
 
 #include <stdint.h>
 #include <stddef.h>
 #include <sys/mman.h>
 #include <unistd.h>
 #include "mozilla/Assertions.h"
+#include "mozilla/Scoped.h"
 
 /**
  * On architectures that are little endian and that support unaligned reads,
  * we can use direct type, but on others, we want to have a special class
  * to handle conversion and alignment issues.
  */
 #if !defined(DEBUG) && (defined(__i386__) || defined(__x86_64__))
 typedef uint16_t le_uint16;
@@ -72,134 +73,28 @@ private:
 
 /**
  * Type definitions
  */
 typedef le_to_cpu<unsigned char> le_uint16;
 typedef le_to_cpu<le_uint16> le_uint32;
 #endif
 
-/**
- * AutoClean is a helper to create RAII wrappers
- * The Traits class is expected to look like the following:
- *   struct Traits {
- *     // Define the type of the value stored in the wrapper
- *     typedef value_type type;
- *     // Returns the value corresponding to the uninitialized or freed state
- *     const static type None();
- *     // Cleans up resources corresponding to the wrapped value
- *     const static void clean(type);
- *   }
- */
-template <typename Traits>
-class AutoClean
-{
-  typedef typename Traits::type T;
-public:
-  AutoClean(): value(Traits::None()) { }
-  AutoClean(const T& value): value(value) { }
-  ~AutoClean()
-  {
-    if (value != Traits::None())
-      Traits::clean(value);
-  }
-
-  operator const T&() const { return value; }
-  const T& operator->() const { return value; }
-  const T& get() const { return value; }
-
-  T forget()
-  {
-    T _value = value;
-    value = Traits::None();
-    return _value;
-  }
-
-  bool operator ==(T other) const
-  {
-    return value == other;
-  }
-
-  AutoClean& operator =(T other)
-  {
-    if (value != Traits::None())
-      Traits::clean(value);
-    value = other;
-    return *this;
-  }
-
-private:
-  T value;
-};
-
-/**
- * AUTOCLEAN_TEMPLATE defines a templated class derived from AutoClean
- * This allows to implement templates such as AutoFreePtr.
- */
-#define AUTOCLEAN_TEMPLATE(name, Traits) \
-template <typename T> \
-struct name: public AutoClean<Traits<T> > \
-{ \
-  using AutoClean<Traits<T> >::operator =; \
-  name(): AutoClean<Traits<T> >() { } \
-  name(typename Traits<T>::type ptr): AutoClean<Traits<T> >(ptr) { } \
-}
 
 /**
  * AutoCloseFD is a RAII wrapper for POSIX file descriptors
  */
 struct AutoCloseFDTraits
 {
   typedef int type;
-  static int None() { return -1; }
-  static void clean(int fd) { close(fd); }
+  static int empty() { return -1; }
+  static void release(int fd) { close(fd); }
 };
-typedef AutoClean<AutoCloseFDTraits> AutoCloseFD;
-
-/**
- * AutoFreePtr is a RAII wrapper for pointers that need to be free()d.
- *
- *   struct S { ... };
- *   AutoFreePtr<S> foo = malloc(sizeof(S));
- *   AutoFreePtr<char> bar = strdup(str);
- */
-template <typename T>
-struct AutoFreePtrTraits
-{
-  typedef T *type;
-  static T *None() { return NULL; }
-  static void clean(T *ptr) { free(ptr); }
-};
-AUTOCLEAN_TEMPLATE(AutoFreePtr, AutoFreePtrTraits);
+typedef mozilla::Scoped<AutoCloseFDTraits> AutoCloseFD;
 
-/**
- * AutoDeletePtr is a RAII wrapper for pointers that need to be deleted.
- *
- *   struct S { ... };
- *   AutoDeletePtr<S> foo = new S();
- */
-template <typename T>
-struct AutoDeletePtrTraits: public AutoFreePtrTraits<T>
-{
-  static void clean(T *ptr) { delete ptr; }
-};
-AUTOCLEAN_TEMPLATE(AutoDeletePtr, AutoDeletePtrTraits);
-
-/**
- * AutoDeleteArray is a RAII wrapper for pointers that need to be delete[]ed.
- *
- *   struct S { ... };
- *   AutoDeleteArray<S> foo = new S[42];
- */
-template <typename T>
-struct AutoDeleteArrayTraits: public AutoFreePtrTraits<T>
-{
-  static void clean(T *ptr) { delete [] ptr; }
-};
-AUTOCLEAN_TEMPLATE(AutoDeleteArray, AutoDeleteArrayTraits);
 
 /**
  * MappedPtr is a RAII wrapper for mmap()ed memory. It can be used as
  * a simple void * or unsigned char *.
  *
  * It is defined as a derivative of a template that allows to use a
  * different unmapping strategy.
  */
--- a/mozglue/linker/szip.cpp
+++ b/mozglue/linker/szip.cpp
@@ -4,32 +4,34 @@
 
 #include <algorithm>
 #include <sys/stat.h>
 #include <cstring>
 #include <zlib.h>
 #include <fcntl.h>
 #include <errno.h>
 #include "mozilla/Assertions.h"
+#include "mozilla/Scoped.h"
 #include "SeekableZStream.h"
 #include "Utils.h"
 #include "Logging.h"
 
 static const size_t CHUNK = 16384;
 
 /* Generate a seekable compressed stream. */
 
 int main(int argc, char* argv[])
 {
   if (argc != 3 || !argv[1] || !argv[2] || (strcmp(argv[1], argv[2]) == 0)) {
     log("usage: %s file_to_compress out_file", argv[0]);
     return 1;
   }
 
-  AutoCloseFD origFd = open(argv[1], O_RDONLY);
+  AutoCloseFD origFd;
+  origFd = open(argv[1], O_RDONLY);
   if (origFd == -1) {
     log("Couldn't open %s: %s", argv[1], strerror(errno));
     return 1;
   }
 
   struct stat st;
   int ret = fstat(origFd, &st);
   if (ret == -1) {
@@ -48,17 +50,18 @@ int main(int argc, char* argv[])
   MappedPtr origBuf;
   origBuf.Assign(mmap(NULL, origSize, PROT_READ, MAP_PRIVATE, origFd, 0), origSize);
   if (origBuf == MAP_FAILED) {
     log("Couldn't mmap %s: %s", argv[1], strerror(errno));
     return 1;
   }
 
   /* Create the compressed file */
-  AutoCloseFD outFd = open(argv[2], O_RDWR | O_CREAT | O_TRUNC, 0666);
+  AutoCloseFD outFd;
+  outFd = open(argv[2], O_RDWR | O_CREAT | O_TRUNC, 0666);
   if (outFd == -1) {
     log("Couldn't open %s: %s", argv[2], strerror(errno));
     return 1;
   }
 
   /* Expected total number of chunks */
   size_t nChunks = ((origSize + CHUNK - 1) / CHUNK);
 
@@ -88,17 +91,18 @@ int main(int argc, char* argv[])
    * compressed streams */
   lseek(outFd, offset, SEEK_SET);
 
   /* Initialize zlib structure */
   z_stream zStream;
   memset(&zStream, 0, sizeof(zStream));
 
   /* Compression buffer */
-  AutoDeleteArray<Bytef> outBuf = new Bytef[CHUNK * 2];
+  mozilla::ScopedDeleteArray<Bytef> outBuf;
+  outBuf = new Bytef[CHUNK * 2];
 
   Bytef *origData = static_cast<Bytef*>(origBuf);
   size_t avail = 0;
   while (origSize) {
     avail = std::min(origSize, CHUNK);
 
     /* Compress chunk */
     ret = deflateInit(&zStream, 9);