2012-01-11 14:10:55 +04:00
|
|
|
/* 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 Utils_h
|
|
|
|
#define Utils_h
|
|
|
|
|
2016-09-12 15:15:24 +03:00
|
|
|
#include <pthread.h>
|
2012-01-11 14:10:55 +04:00
|
|
|
#include <stdint.h>
|
2012-01-20 12:48:44 +04:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <unistd.h>
|
2012-02-13 18:49:45 +04:00
|
|
|
#include "mozilla/Assertions.h"
|
2012-07-30 22:17:53 +04:00
|
|
|
#include "mozilla/Scoped.h"
|
2012-01-11 14:10:55 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2012-02-22 11:12:15 +04:00
|
|
|
#if !defined(DEBUG) && (defined(__i386__) || defined(__x86_64__))
|
2012-01-11 14:10:55 +04:00
|
|
|
typedef uint16_t le_uint16;
|
|
|
|
typedef uint32_t le_uint32;
|
|
|
|
#else
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Template that allows to find an unsigned int type from a (computed) bit size
|
|
|
|
*/
|
|
|
|
template <int s> struct UInt { };
|
|
|
|
template <> struct UInt<16> { typedef uint16_t Type; };
|
|
|
|
template <> struct UInt<32> { typedef uint32_t Type; };
|
|
|
|
|
|
|
|
/**
|
2012-02-22 11:12:15 +04:00
|
|
|
* Template to access 2 n-bit sized words as a 2*n-bit sized word, doing
|
2012-01-11 14:10:55 +04:00
|
|
|
* conversion from little endian and avoiding alignment issues.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
class le_to_cpu
|
|
|
|
{
|
|
|
|
public:
|
2012-02-22 11:12:15 +04:00
|
|
|
typedef typename UInt<16 * sizeof(T)>::Type Type;
|
|
|
|
|
|
|
|
operator Type() const
|
2012-01-11 14:10:55 +04:00
|
|
|
{
|
|
|
|
return (b << (sizeof(T) * 8)) | a;
|
|
|
|
}
|
2012-02-22 11:12:15 +04:00
|
|
|
|
|
|
|
const le_to_cpu& operator =(const Type &v)
|
|
|
|
{
|
|
|
|
a = v & ((1 << (sizeof(T) * 8)) - 1);
|
|
|
|
b = v >> (sizeof(T) * 8);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
le_to_cpu() { }
|
|
|
|
le_to_cpu(const Type &v)
|
|
|
|
{
|
|
|
|
operator =(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
const le_to_cpu& operator +=(const Type &v)
|
|
|
|
{
|
|
|
|
return operator =(operator Type() + v);
|
|
|
|
}
|
|
|
|
|
|
|
|
const le_to_cpu& operator ++(int)
|
|
|
|
{
|
|
|
|
return operator =(operator Type() + 1);
|
|
|
|
}
|
|
|
|
|
2012-01-11 14:10:55 +04:00
|
|
|
private:
|
|
|
|
T a, b;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Type definitions
|
|
|
|
*/
|
|
|
|
typedef le_to_cpu<unsigned char> le_uint16;
|
|
|
|
typedef le_to_cpu<le_uint16> le_uint32;
|
|
|
|
#endif
|
|
|
|
|
2012-01-20 12:48:50 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* AutoCloseFD is a RAII wrapper for POSIX file descriptors
|
|
|
|
*/
|
|
|
|
struct AutoCloseFDTraits
|
|
|
|
{
|
|
|
|
typedef int type;
|
2012-07-30 22:17:53 +04:00
|
|
|
static int empty() { return -1; }
|
2013-01-03 19:45:50 +04:00
|
|
|
static void release(int fd) { if (fd != -1) close(fd); }
|
2012-01-20 12:48:50 +04:00
|
|
|
};
|
2012-07-30 22:17:53 +04:00
|
|
|
typedef mozilla::Scoped<AutoCloseFDTraits> AutoCloseFD;
|
2012-01-20 12:48:50 +04:00
|
|
|
|
2012-11-07 11:02:53 +04:00
|
|
|
/**
|
|
|
|
* AutoCloseFILE is a RAII wrapper for POSIX streams
|
|
|
|
*/
|
|
|
|
struct AutoCloseFILETraits
|
|
|
|
{
|
|
|
|
typedef FILE *type;
|
2013-11-11 23:15:46 +04:00
|
|
|
static FILE *empty() { return nullptr; }
|
2013-01-03 19:45:50 +04:00
|
|
|
static void release(FILE *f) { if (f) fclose(f); }
|
2012-11-07 11:02:53 +04:00
|
|
|
};
|
|
|
|
typedef mozilla::Scoped<AutoCloseFILETraits> AutoCloseFILE;
|
2012-01-11 14:10:55 +04:00
|
|
|
|
2012-01-20 12:48:44 +04:00
|
|
|
/**
|
2013-06-27 04:35:49 +04:00
|
|
|
* Page alignment helpers
|
2012-01-20 12:48:44 +04:00
|
|
|
*/
|
2013-06-27 04:35:49 +04:00
|
|
|
static inline size_t PageSize()
|
|
|
|
{
|
|
|
|
return 4096;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uintptr_t AlignedPtr(uintptr_t ptr, size_t alignment)
|
|
|
|
{
|
|
|
|
return ptr & ~(alignment - 1);
|
|
|
|
}
|
|
|
|
|
2012-01-20 12:48:44 +04:00
|
|
|
template <typename T>
|
2013-06-27 04:35:49 +04:00
|
|
|
static inline T *AlignedPtr(T *ptr, size_t alignment)
|
|
|
|
{
|
|
|
|
return reinterpret_cast<T *>(
|
|
|
|
AlignedPtr(reinterpret_cast<uintptr_t>(ptr), alignment));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static inline T PageAlignedPtr(T ptr)
|
|
|
|
{
|
|
|
|
return AlignedPtr(ptr, PageSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uintptr_t AlignedEndPtr(uintptr_t ptr, size_t alignment)
|
|
|
|
{
|
|
|
|
return AlignedPtr(ptr + alignment - 1, alignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static inline T *AlignedEndPtr(T *ptr, size_t alignment)
|
|
|
|
{
|
|
|
|
return reinterpret_cast<T *>(
|
|
|
|
AlignedEndPtr(reinterpret_cast<uintptr_t>(ptr), alignment));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static inline T PageAlignedEndPtr(T ptr)
|
|
|
|
{
|
|
|
|
return AlignedEndPtr(ptr, PageSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline size_t AlignedSize(size_t size, size_t alignment)
|
|
|
|
{
|
|
|
|
return (size + alignment - 1) & ~(alignment - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline size_t PageAlignedSize(size_t size)
|
|
|
|
{
|
|
|
|
return AlignedSize(size, PageSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool IsAlignedPtr(uintptr_t ptr, size_t alignment)
|
|
|
|
{
|
|
|
|
return ptr % alignment == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static inline bool IsAlignedPtr(T *ptr, size_t alignment)
|
|
|
|
{
|
|
|
|
return IsAlignedPtr(reinterpret_cast<uintptr_t>(ptr), alignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static inline bool IsPageAlignedPtr(T ptr)
|
|
|
|
{
|
|
|
|
return IsAlignedPtr(ptr, PageSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool IsAlignedSize(size_t size, size_t alignment)
|
|
|
|
{
|
|
|
|
return size % alignment == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool IsPageAlignedSize(size_t size)
|
|
|
|
{
|
|
|
|
return IsAlignedSize(size, PageSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline size_t PageNumber(size_t size)
|
|
|
|
{
|
|
|
|
return (size + PageSize() - 1) / PageSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MemoryRange stores a pointer, size pair.
|
|
|
|
*/
|
|
|
|
class MemoryRange
|
2012-01-20 12:48:44 +04:00
|
|
|
{
|
|
|
|
public:
|
2013-06-27 04:35:49 +04:00
|
|
|
MemoryRange(void *buf, size_t length): buf(buf), length(length) { }
|
2012-01-20 12:48:44 +04:00
|
|
|
|
2012-01-20 12:49:03 +04:00
|
|
|
void Assign(void *b, size_t len) {
|
2012-01-20 12:48:44 +04:00
|
|
|
buf = b;
|
|
|
|
length = len;
|
|
|
|
}
|
|
|
|
|
2013-06-27 04:35:49 +04:00
|
|
|
void Assign(const MemoryRange& other) {
|
|
|
|
buf = other.buf;
|
|
|
|
length = other.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *get() const
|
2012-01-20 12:48:44 +04:00
|
|
|
{
|
2013-06-27 04:35:49 +04:00
|
|
|
return buf;
|
2012-01-20 12:48:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
operator void *() const
|
|
|
|
{
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator unsigned char *() const
|
|
|
|
{
|
|
|
|
return reinterpret_cast<unsigned char *>(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator ==(void *ptr) const {
|
|
|
|
return buf == ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator ==(unsigned char *ptr) const {
|
|
|
|
return buf == ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *operator +(off_t offset) const
|
|
|
|
{
|
|
|
|
return reinterpret_cast<char *>(buf) + offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether the given address is within the mapped range
|
|
|
|
*/
|
|
|
|
bool Contains(void *ptr) const
|
|
|
|
{
|
|
|
|
return (ptr >= buf) && (ptr < reinterpret_cast<char *>(buf) + length);
|
|
|
|
}
|
|
|
|
|
2012-01-20 12:48:44 +04:00
|
|
|
/**
|
|
|
|
* Returns the length of the mapped range
|
|
|
|
*/
|
|
|
|
size_t GetLength() const
|
|
|
|
{
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2013-06-27 04:35:49 +04:00
|
|
|
static MemoryRange mmap(void *addr, size_t length, int prot, int flags,
|
|
|
|
int fd, off_t offset) {
|
|
|
|
return MemoryRange(::mmap(addr, length, prot, flags, fd, offset), length);
|
|
|
|
}
|
|
|
|
|
2012-01-20 12:48:44 +04:00
|
|
|
private:
|
|
|
|
void *buf;
|
|
|
|
size_t length;
|
|
|
|
};
|
|
|
|
|
2013-06-27 04:35:49 +04:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
class GenericMappedPtr: public MemoryRange
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
GenericMappedPtr(void *buf, size_t length): MemoryRange(buf, length) { }
|
|
|
|
GenericMappedPtr(const MemoryRange& other): MemoryRange(other) { }
|
|
|
|
GenericMappedPtr(): MemoryRange(MAP_FAILED, 0) { }
|
|
|
|
|
|
|
|
void Assign(void *b, size_t len) {
|
|
|
|
if (get() != MAP_FAILED)
|
|
|
|
static_cast<T *>(this)->munmap(get(), GetLength());
|
|
|
|
MemoryRange::Assign(b, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Assign(const MemoryRange& other) {
|
|
|
|
Assign(other.get(), other.GetLength());
|
|
|
|
}
|
|
|
|
|
|
|
|
~GenericMappedPtr()
|
|
|
|
{
|
|
|
|
if (get() != MAP_FAILED)
|
|
|
|
static_cast<T *>(this)->munmap(get(), GetLength());
|
|
|
|
}
|
|
|
|
|
2014-10-07 02:42:18 +04:00
|
|
|
void release()
|
|
|
|
{
|
|
|
|
MemoryRange::Assign(MAP_FAILED, 0);
|
|
|
|
}
|
2013-06-27 04:35:49 +04:00
|
|
|
};
|
|
|
|
|
2012-01-20 12:48:44 +04:00
|
|
|
struct MappedPtr: public GenericMappedPtr<MappedPtr>
|
|
|
|
{
|
|
|
|
MappedPtr(void *buf, size_t length)
|
|
|
|
: GenericMappedPtr<MappedPtr>(buf, length) { }
|
2013-06-27 04:35:49 +04:00
|
|
|
MappedPtr(const MemoryRange& other)
|
|
|
|
: GenericMappedPtr<MappedPtr>(other) { }
|
2012-01-20 12:48:44 +04:00
|
|
|
MappedPtr(): GenericMappedPtr<MappedPtr>() { }
|
|
|
|
|
2012-02-22 11:12:15 +04:00
|
|
|
private:
|
|
|
|
friend class GenericMappedPtr<MappedPtr>;
|
2012-01-20 12:48:44 +04:00
|
|
|
void munmap(void *buf, size_t length)
|
|
|
|
{
|
|
|
|
::munmap(buf, length);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-01-20 12:48:44 +04:00
|
|
|
/**
|
|
|
|
* UnsizedArray is a way to access raw arrays of data in memory.
|
|
|
|
*
|
|
|
|
* struct S { ... };
|
|
|
|
* UnsizedArray<S> a(buf);
|
|
|
|
* UnsizedArray<S> b; b.Init(buf);
|
|
|
|
*
|
|
|
|
* This is roughly equivalent to
|
|
|
|
* const S *a = reinterpret_cast<const S *>(buf);
|
2013-11-11 23:15:46 +04:00
|
|
|
* const S *b = nullptr; b = reinterpret_cast<const S *>(buf);
|
2012-01-20 12:48:44 +04:00
|
|
|
*
|
|
|
|
* An UnsizedArray has no known length, and it's up to the caller to make
|
|
|
|
* sure the accessed memory is mapped and makes sense.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
class UnsizedArray
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef size_t idx_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructors and Initializers
|
|
|
|
*/
|
2013-11-11 23:15:46 +04:00
|
|
|
UnsizedArray(): contents(nullptr) { }
|
2012-01-20 12:48:44 +04:00
|
|
|
UnsizedArray(const void *buf): contents(reinterpret_cast<const T *>(buf)) { }
|
|
|
|
|
|
|
|
void Init(const void *buf)
|
|
|
|
{
|
2013-11-11 23:15:46 +04:00
|
|
|
MOZ_ASSERT(contents == nullptr);
|
2012-01-20 12:48:44 +04:00
|
|
|
contents = reinterpret_cast<const T *>(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the nth element of the array
|
|
|
|
*/
|
|
|
|
const T &operator[](const idx_t index) const
|
|
|
|
{
|
2012-02-13 18:49:45 +04:00
|
|
|
MOZ_ASSERT(contents);
|
2012-01-20 12:48:44 +04:00
|
|
|
return contents[index];
|
|
|
|
}
|
|
|
|
|
2013-03-06 10:30:07 +04:00
|
|
|
operator const T *() const
|
|
|
|
{
|
|
|
|
return contents;
|
|
|
|
}
|
2012-01-20 12:48:44 +04:00
|
|
|
/**
|
|
|
|
* Returns whether the array points somewhere
|
|
|
|
*/
|
|
|
|
operator bool() const
|
|
|
|
{
|
2013-11-11 23:15:46 +04:00
|
|
|
return contents != nullptr;
|
2012-01-20 12:48:44 +04:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
const T *contents;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Array, like UnsizedArray, is a way to access raw arrays of data in memory.
|
|
|
|
* Unlike UnsizedArray, it has a known length, and is enumerable with an
|
|
|
|
* iterator.
|
|
|
|
*
|
|
|
|
* struct S { ... };
|
|
|
|
* Array<S> a(buf, len);
|
|
|
|
* UnsizedArray<S> b; b.Init(buf, len);
|
|
|
|
*
|
|
|
|
* In the above examples, len is the number of elements in the array. It is
|
|
|
|
* also possible to initialize an Array with the buffer size:
|
|
|
|
*
|
|
|
|
* Array<S> c; c.InitSize(buf, size);
|
|
|
|
*
|
|
|
|
* It is also possible to initialize an Array in two steps, only providing
|
|
|
|
* one data at a time:
|
|
|
|
*
|
|
|
|
* Array<S> d;
|
|
|
|
* d.Init(buf);
|
|
|
|
* d.Init(len); // or d.InitSize(size);
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
class Array: public UnsizedArray<T>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef typename UnsizedArray<T>::idx_t idx_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructors and Initializers
|
|
|
|
*/
|
|
|
|
Array(): UnsizedArray<T>(), length(0) { }
|
|
|
|
Array(const void *buf, const idx_t length)
|
|
|
|
: UnsizedArray<T>(buf), length(length) { }
|
|
|
|
|
|
|
|
void Init(const void *buf)
|
|
|
|
{
|
|
|
|
UnsizedArray<T>::Init(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Init(const idx_t len)
|
|
|
|
{
|
2012-02-13 18:49:45 +04:00
|
|
|
MOZ_ASSERT(length == 0);
|
2012-01-20 12:48:44 +04:00
|
|
|
length = len;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InitSize(const idx_t size)
|
|
|
|
{
|
|
|
|
Init(size / sizeof(T));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Init(const void *buf, const idx_t len)
|
|
|
|
{
|
|
|
|
UnsizedArray<T>::Init(buf);
|
|
|
|
Init(len);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InitSize(const void *buf, const idx_t size)
|
|
|
|
{
|
|
|
|
UnsizedArray<T>::Init(buf);
|
|
|
|
InitSize(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the nth element of the array
|
|
|
|
*/
|
|
|
|
const T &operator[](const idx_t index) const
|
|
|
|
{
|
2012-02-13 18:49:45 +04:00
|
|
|
MOZ_ASSERT(index < length);
|
|
|
|
MOZ_ASSERT(operator bool());
|
2012-01-20 12:48:44 +04:00
|
|
|
return UnsizedArray<T>::operator[](index);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the number of elements in the array
|
|
|
|
*/
|
|
|
|
idx_t numElements() const
|
|
|
|
{
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether the array points somewhere and has at least one element.
|
|
|
|
*/
|
|
|
|
operator bool() const
|
|
|
|
{
|
|
|
|
return (length > 0) && UnsizedArray<T>::operator bool();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Iterator for an Array. Use is similar to that of STL const_iterators:
|
|
|
|
*
|
|
|
|
* struct S { ... };
|
|
|
|
* Array<S> a(buf, len);
|
|
|
|
* for (Array<S>::iterator it = a.begin(); it < a.end(); ++it) {
|
|
|
|
* // Do something with *it.
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
class iterator
|
|
|
|
{
|
|
|
|
public:
|
2013-11-11 23:15:46 +04:00
|
|
|
iterator(): item(nullptr) { }
|
2012-01-20 12:48:44 +04:00
|
|
|
|
|
|
|
const T &operator *() const
|
|
|
|
{
|
|
|
|
return *item;
|
|
|
|
}
|
|
|
|
|
|
|
|
const T *operator ->() const
|
|
|
|
{
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2012-06-07 12:41:59 +04:00
|
|
|
iterator &operator ++()
|
2012-01-20 12:48:44 +04:00
|
|
|
{
|
2012-06-07 12:41:59 +04:00
|
|
|
++item;
|
|
|
|
return *this;
|
2012-01-20 12:48:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<(const iterator &other) const
|
|
|
|
{
|
|
|
|
return item < other.item;
|
|
|
|
}
|
|
|
|
protected:
|
|
|
|
friend class Array<T>;
|
|
|
|
iterator(const T &item): item(&item) { }
|
|
|
|
|
|
|
|
private:
|
|
|
|
const T *item;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an iterator pointing at the beginning of the Array
|
|
|
|
*/
|
|
|
|
iterator begin() const {
|
|
|
|
if (length)
|
|
|
|
return iterator(UnsizedArray<T>::operator[](0));
|
|
|
|
return iterator();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an iterator pointing past the end of the Array
|
|
|
|
*/
|
|
|
|
iterator end() const {
|
|
|
|
if (length)
|
|
|
|
return iterator(UnsizedArray<T>::operator[](length));
|
|
|
|
return iterator();
|
|
|
|
}
|
2012-06-07 12:41:59 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reverse iterator for an Array. Use is similar to that of STL
|
|
|
|
* const_reverse_iterators:
|
|
|
|
*
|
|
|
|
* struct S { ... };
|
|
|
|
* Array<S> a(buf, len);
|
|
|
|
* for (Array<S>::reverse_iterator it = a.rbegin(); it < a.rend(); ++it) {
|
|
|
|
* // Do something with *it.
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
class reverse_iterator
|
|
|
|
{
|
|
|
|
public:
|
2013-11-11 23:15:46 +04:00
|
|
|
reverse_iterator(): item(nullptr) { }
|
2012-06-07 12:41:59 +04:00
|
|
|
|
|
|
|
const T &operator *() const
|
|
|
|
{
|
|
|
|
const T *tmp = item;
|
|
|
|
return *--tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
const T *operator ->() const
|
|
|
|
{
|
|
|
|
return &operator*();
|
|
|
|
}
|
|
|
|
|
|
|
|
reverse_iterator &operator ++()
|
|
|
|
{
|
|
|
|
--item;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<(const reverse_iterator &other) const
|
|
|
|
{
|
|
|
|
return item > other.item;
|
|
|
|
}
|
|
|
|
protected:
|
|
|
|
friend class Array<T>;
|
|
|
|
reverse_iterator(const T &item): item(&item) { }
|
|
|
|
|
|
|
|
private:
|
|
|
|
const T *item;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a reverse iterator pointing at the end of the Array
|
|
|
|
*/
|
|
|
|
reverse_iterator rbegin() const {
|
|
|
|
if (length)
|
|
|
|
return reverse_iterator(UnsizedArray<T>::operator[](length));
|
|
|
|
return reverse_iterator();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a reverse iterator pointing past the beginning of the Array
|
|
|
|
*/
|
|
|
|
reverse_iterator rend() const {
|
|
|
|
if (length)
|
|
|
|
return reverse_iterator(UnsizedArray<T>::operator[](0));
|
|
|
|
return reverse_iterator();
|
|
|
|
}
|
2012-01-20 12:48:44 +04:00
|
|
|
private:
|
|
|
|
idx_t length;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transforms a pointer-to-function to a pointer-to-object pointing at the
|
|
|
|
* same address.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
void *FunctionPtr(T func)
|
|
|
|
{
|
|
|
|
union {
|
|
|
|
void *ptr;
|
|
|
|
T func;
|
|
|
|
} f;
|
|
|
|
f.func = func;
|
|
|
|
return f.ptr;
|
|
|
|
}
|
|
|
|
|
2016-09-12 15:15:24 +03:00
|
|
|
class AutoLock {
|
|
|
|
public:
|
|
|
|
AutoLock(pthread_mutex_t *mutex): mutex(mutex)
|
|
|
|
{
|
|
|
|
if (pthread_mutex_lock(mutex))
|
|
|
|
MOZ_CRASH("pthread_mutex_lock failed");
|
|
|
|
}
|
|
|
|
~AutoLock()
|
|
|
|
{
|
|
|
|
if (pthread_mutex_unlock(mutex))
|
|
|
|
MOZ_CRASH("pthread_mutex_unlock failed");
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
pthread_mutex_t *mutex;
|
|
|
|
};
|
|
|
|
|
2012-01-11 14:10:55 +04:00
|
|
|
#endif /* Utils_h */
|
2012-01-20 12:48:44 +04:00
|
|
|
|