Bug 735615 - Remove AutoClean from mozglue/linkers/Utils.h r=glandium

This commit is contained in:
Andrew Quartey 2012-07-30 14:17:53 -04:00
parent bc94267076
commit eb11829813
4 changed files with 30 additions and 126 deletions

View File

@ -74,7 +74,8 @@ MappableExtractFile::Create(const char *name, Zip *zip, Zip::Stream *stream)
"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) {
@ -86,13 +87,15 @@ MappableExtractFile::Create(const char *name, Zip *zip, Zip::Stream *stream)
}
}
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());
@ -341,8 +344,8 @@ 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;
@ -523,7 +526,8 @@ MappableSeekableZStream::stats(const char *when, const char *name) const
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++) {

View File

@ -10,6 +10,7 @@
#include "Zip.h"
#include "SeekableZStream.h"
#include "mozilla/RefPtr.h"
#include "mozilla/Scoped.h"
#include "zlib.h"
/**
@ -109,15 +110,15 @@ private:
* 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;
@ -155,7 +156,7 @@ private:
mozilla::RefPtr<Zip> zip;
/* Decompression buffer */
AutoDeletePtr<_MappableBuffer> buffer;
mozilla::ScopedDeletePtr<_MappableBuffer> buffer;
/* Zlib data */
z_stream zStream;
@ -193,7 +194,7 @@ private:
mozilla::RefPtr<Zip> zip;
/* Decompression buffer */
AutoDeletePtr<_MappableBuffer> buffer;
mozilla::ScopedDeletePtr<_MappableBuffer> buffer;
/* Seekable ZStream */
SeekableZStream zStream;
@ -236,7 +237,7 @@ private:
/* 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;

View File

@ -10,6 +10,7 @@
#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,
@ -77,71 +78,6 @@ 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
@ -149,52 +85,11 @@ struct name: public AutoClean<Traits<T> > \
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;
typedef mozilla::Scoped<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);
/**
* 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

View File

@ -9,6 +9,7 @@
#include <fcntl.h>
#include <errno.h>
#include "mozilla/Assertions.h"
#include "mozilla/Scoped.h"
#include "SeekableZStream.h"
#include "Utils.h"
#include "Logging.h"
@ -24,7 +25,8 @@ int main(int argc, char* argv[])
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;
@ -53,7 +55,8 @@ int main(int argc, char* argv[])
}
/* 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;
@ -93,7 +96,8 @@ int main(int argc, char* argv[])
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;