| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#ifndef FastMallocWince_h |
| 23 |
#define FastMallocWince_h |
| 24 |
|
| 25 |
#include <new.h> |
| 26 |
|
| 27 |
#ifdef __cplusplus |
| 28 |
#include <new> |
| 29 |
#include "MemoryManager.h" |
| 30 |
extern "C" { |
| 31 |
#endif |
| 32 |
|
| 33 |
void* fastMalloc(size_t n); |
| 34 |
void* fastCalloc(size_t n_elements, size_t element_size); |
| 35 |
void fastFree(void* p); |
| 36 |
void* fastRealloc(void* p, size_t n); |
| 37 |
void* fastZeroedMalloc(size_t n); |
| 38 |
|
| 39 |
void* tryFastMalloc(size_t n); |
| 40 |
void* tryFastZeroedMalloc(size_t n); |
| 41 |
void* tryFastCalloc(size_t n_elements, size_t element_size); |
| 42 |
void* tryFastRealloc(void* p, size_t n); |
| 43 |
char* fastStrDup(const char*); |
| 44 |
|
| 45 |
#ifndef NDEBUG |
| 46 |
void fastMallocForbid(); |
| 47 |
void fastMallocAllow(); |
| 48 |
#endif |
| 49 |
|
| 50 |
#if !defined(USE_SYSTEM_MALLOC) || !USE_SYSTEM_MALLOC |
| 51 |
|
| 52 |
#define malloc(n) fastMalloc(n) |
| 53 |
#define calloc(n_elements, element_size) fastCalloc(n_elements, element_size) |
| 54 |
#define realloc(p, n) fastRealloc(p, n) |
| 55 |
#define free(p) fastFree(p) |
| 56 |
#define strdup(p) fastStrDup(p) |
| 57 |
|
| 58 |
#else |
| 59 |
|
| 60 |
#define strdup(p) _strdup(p) |
| 61 |
|
| 62 |
#endif |
| 63 |
|
| 64 |
#ifdef __cplusplus |
| 65 |
} |
| 66 |
#endif |
| 67 |
|
| 68 |
#ifdef __cplusplus |
| 69 |
#if !defined(USE_SYSTEM_MALLOC) || !USE_SYSTEM_MALLOC |
| 70 |
static inline void* __cdecl operator new(size_t s) { return fastMalloc(s); } |
| 71 |
static inline void __cdecl operator delete(void* p) { fastFree(p); } |
| 72 |
static inline void* __cdecl operator new[](size_t s) { return fastMalloc(s); } |
| 73 |
static inline void __cdecl operator delete[](void* p) { fastFree(p); } |
| 74 |
static inline void* operator new(size_t s, const std::nothrow_t&) throw() { return fastMalloc(s); } |
| 75 |
static inline void operator delete(void* p, const std::nothrow_t&) throw() { fastFree(p); } |
| 76 |
static inline void* operator new[](size_t s, const std::nothrow_t&) throw() { return fastMalloc(s); } |
| 77 |
static inline void operator delete[](void* p, const std::nothrow_t&) throw() { fastFree(p); } |
| 78 |
#endif |
| 79 |
|
| 80 |
namespace WTF { |
| 81 |
|
| 82 |
|
| 83 |
typedef unsigned long long AllocAlignmentInteger; |
| 84 |
|
| 85 |
namespace Internal { |
| 86 |
enum AllocType { |
| 87 |
AllocTypeMalloc = 0x375d6750, |
| 88 |
AllocTypeClassNew, |
| 89 |
AllocTypeClassNewArray, |
| 90 |
AllocTypeFastNew, |
| 91 |
AllocTypeFastNewArray, |
| 92 |
AllocTypeNew, |
| 93 |
AllocTypeNewArray |
| 94 |
}; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
#if ENABLE(FAST_MALLOC_MATCH_VALIDATION) |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
namespace Internal { |
| 113 |
|
| 114 |
|
| 115 |
inline AllocType fastMallocMatchValidationType(const void* p) |
| 116 |
{ |
| 117 |
const AllocAlignmentInteger* type = static_cast<const AllocAlignmentInteger*>(p) - 1; |
| 118 |
return static_cast<AllocType>(*type); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
inline AllocAlignmentInteger* fastMallocMatchValidationValue(void* p) |
| 123 |
{ |
| 124 |
return reinterpret_cast<AllocAlignmentInteger*>(static_cast<char*>(p) - sizeof(AllocAlignmentInteger)); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
inline void setFastMallocMatchValidationType(void* p, AllocType allocType) |
| 129 |
{ |
| 130 |
AllocAlignmentInteger* type = static_cast<AllocAlignmentInteger*>(p) - 1; |
| 131 |
*type = static_cast<AllocAlignmentInteger>(allocType); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
void fastMallocMatchFailed(void* p); |
| 136 |
|
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
inline void fastMallocMatchValidateMalloc(void* p, Internal::AllocType allocType) |
| 141 |
{ |
| 142 |
if (!p) |
| 143 |
return; |
| 144 |
|
| 145 |
Internal::setFastMallocMatchValidationType(p, allocType); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
inline void fastMallocMatchValidateFree(void* p, Internal::AllocType allocType) |
| 150 |
{ |
| 151 |
if (!p) |
| 152 |
return; |
| 153 |
|
| 154 |
if (Internal::fastMallocMatchValidationType(p) != allocType) |
| 155 |
Internal::fastMallocMatchFailed(p); |
| 156 |
Internal::setFastMallocMatchValidationType(p, Internal::AllocTypeMalloc); |
| 157 |
} |
| 158 |
|
| 159 |
#else |
| 160 |
|
| 161 |
inline void fastMallocMatchValidateMalloc(void*, Internal::AllocType) |
| 162 |
{ |
| 163 |
} |
| 164 |
|
| 165 |
inline void fastMallocMatchValidateFree(void*, Internal::AllocType) |
| 166 |
{ |
| 167 |
} |
| 168 |
|
| 169 |
#endif |
| 170 |
|
| 171 |
} |
| 172 |
|
| 173 |
#endif |
| 174 |
|
| 175 |
#endif |
| 176 |
|