#ifndef WTF_FastMalloc_h
#define WTF_FastMalloc_h
#include "Platform.h"
#include "PossiblyNull.h"
#include <stdlib.h>
#include <new>
namespace WTF {
void* fastMalloc(size_t);
void* fastZeroedMalloc(size_t);
void* fastCalloc(size_t numElements, size_t elementSize);
void* fastRealloc(void*, size_t);
char* fastStrDup(const char*);
size_t fastMallocSize(const void*);
struct TryMallocReturnValue {
TryMallocReturnValue(void* data)
: m_data(data)
{
}
TryMallocReturnValue(const TryMallocReturnValue& source)
: m_data(source.m_data)
{
source.m_data = 0;
}
~TryMallocReturnValue() { ASSERT(!m_data); }
template <typename T> bool getValue(T& data) WARN_UNUSED_RETURN;
template <typename T> operator PossiblyNull<T>()
{
T value;
getValue(value);
return PossiblyNull<T>(value);
}
private:
mutable void* m_data;
};
template <typename T> bool TryMallocReturnValue::getValue(T& data)
{
union u { void* data; T target; } res;
res.data = m_data;
data = res.target;
bool returnValue = !!m_data;
m_data = 0;
return returnValue;
}
TryMallocReturnValue tryFastMalloc(size_t n);
TryMallocReturnValue tryFastZeroedMalloc(size_t n);
TryMallocReturnValue tryFastCalloc(size_t n_elements, size_t element_size);
TryMallocReturnValue tryFastRealloc(void* p, size_t n);
void fastFree(void*);
#ifndef NDEBUG
void fastMallocForbid();
void fastMallocAllow();
#endif
void releaseFastMallocFreeMemory();
struct FastMallocStatistics {
size_t reservedVMBytes;
size_t committedVMBytes;
size_t freeListBytes;
};
FastMallocStatistics fastMallocStatistics();
typedef unsigned long long AllocAlignmentInteger;
namespace Internal {
enum AllocType { AllocTypeMalloc = 0x375d6750, AllocTypeClassNew, AllocTypeClassNewArray, AllocTypeFastNew, AllocTypeFastNewArray, AllocTypeNew, AllocTypeNewArray };
enum {
ValidationPrefix = 0xf00df00d,
ValidationSuffix = 0x0badf00d
};
typedef unsigned ValidationTag;
struct ValidationHeader {
AllocType m_type;
unsigned m_size;
ValidationTag m_prefix;
unsigned m_alignment;
};
static const int ValidationBufferSize = sizeof(ValidationHeader) + sizeof(ValidationTag);
}
#if ENABLE(WTF_MALLOC_VALIDATION)
namespace Internal {
void fastMallocMatchFailed(void* p);
inline ValidationHeader* fastMallocValidationHeader(void* p)
{
return reinterpret_cast<ValidationHeader*>(static_cast<char*>(p) - sizeof(ValidationHeader));
}
inline ValidationTag* fastMallocValidationSuffix(void* p)
{
ValidationHeader* header = fastMallocValidationHeader(p);
if (header->m_prefix != static_cast<unsigned>(ValidationPrefix))
fastMallocMatchFailed(p);
return reinterpret_cast<ValidationTag*>(static_cast<char*>(p) + header->m_size);
}
inline AllocType fastMallocMatchValidationType(void* p)
{
return fastMallocValidationHeader(p)->m_type;
}
inline void setFastMallocMatchValidationType(void* p, AllocType allocType)
{
fastMallocValidationHeader(p)->m_type = allocType;
}
}
inline void fastMallocMatchValidateMalloc(void* p, Internal::AllocType allocType)
{
if (!p)
return;
Internal::setFastMallocMatchValidationType(p, allocType);
}
inline void fastMallocMatchValidateFree(void* p, Internal::AllocType)
{
if (!p)
return;
Internal::ValidationHeader* header = Internal::fastMallocValidationHeader(p);
if (header->m_prefix != static_cast<unsigned>(Internal::ValidationPrefix))
Internal::fastMallocMatchFailed(p);
if (*Internal::fastMallocValidationSuffix(p) != Internal::ValidationSuffix)
Internal::fastMallocMatchFailed(p);
Internal::setFastMallocMatchValidationType(p, Internal::AllocTypeMalloc); }
inline void fastMallocValidate(void* p)
{
if (!p)
return;
Internal::ValidationHeader* header = Internal::fastMallocValidationHeader(p);
if (header->m_prefix != static_cast<unsigned>(Internal::ValidationPrefix))
Internal::fastMallocMatchFailed(p);
if (*Internal::fastMallocValidationSuffix(p) != Internal::ValidationSuffix)
Internal::fastMallocMatchFailed(p);
}
#else
inline void fastMallocMatchValidateMalloc(void*, Internal::AllocType)
{
}
inline void fastMallocMatchValidateFree(void*, Internal::AllocType)
{
}
#endif
}
using WTF::fastCalloc;
using WTF::fastFree;
using WTF::fastMalloc;
using WTF::fastMallocSize;
using WTF::fastRealloc;
using WTF::fastStrDup;
using WTF::fastZeroedMalloc;
using WTF::tryFastCalloc;
using WTF::tryFastMalloc;
using WTF::tryFastRealloc;
using WTF::tryFastZeroedMalloc;
#ifndef NDEBUG
using WTF::fastMallocForbid;
using WTF::fastMallocAllow;
#endif
#if COMPILER(GCC) && OS(DARWIN)
#define WTF_PRIVATE_INLINE __private_extern__ inline __attribute__((always_inline))
#elif COMPILER(GCC)
#define WTF_PRIVATE_INLINE inline __attribute__((always_inline))
#elif COMPILER(MSVC) || COMPILER(RVCT)
#define WTF_PRIVATE_INLINE __forceinline
#else
#define WTF_PRIVATE_INLINE inline
#endif
#if !defined(_CRTDBG_MAP_ALLOC) && !(defined(USE_SYSTEM_MALLOC) && USE_SYSTEM_MALLOC && !PLATFORM(BREWMP))
#if ENABLE(GLOBAL_FASTMALLOC_NEW)
#if COMPILER(MSVC)
#pragma warning(push)
#pragma warning(disable: 4290) // Disable the C++ exception specification ignored warning.
#endif
WTF_PRIVATE_INLINE void* operator new(size_t size) throw (std::bad_alloc) { return fastMalloc(size); }
WTF_PRIVATE_INLINE void* operator new(size_t size, const std::nothrow_t&) throw() { return fastMalloc(size); }
WTF_PRIVATE_INLINE void operator delete(void* p) throw() { fastFree(p); }
WTF_PRIVATE_INLINE void operator delete(void* p, const std::nothrow_t&) throw() { fastFree(p); }
WTF_PRIVATE_INLINE void* operator new[](size_t size) throw (std::bad_alloc) { return fastMalloc(size); }
WTF_PRIVATE_INLINE void* operator new[](size_t size, const std::nothrow_t&) throw() { return fastMalloc(size); }
WTF_PRIVATE_INLINE void operator delete[](void* p) throw() { fastFree(p); }
WTF_PRIVATE_INLINE void operator delete[](void* p, const std::nothrow_t&) throw() { fastFree(p); }
#if COMPILER(MSVC)
#pragma warning(pop)
#endif
#endif
#endif
#endif