2
* This file is part of the KDE libraries
3
* Copyright (C) 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
4
* Copyright (C) 2007-2009 Torch Mobile, Inc. All rights reserved
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Library General Public
8
* License as published by the Free Software Foundation; either
9
* version 2 of the License, or (at your option) any later version.
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Library General Public License for more details.
16
* You should have received a copy of the GNU Library General Public License
17
* along with this library; see the file COPYING.LIB. If not, write to
18
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
* Boston, MA 02110-1301, USA.
23
#ifndef FastMallocWince_h
24
#define FastMallocWince_h
30
#include "MemoryManager.h"
34
void* fastMalloc(size_t n);
35
void* fastCalloc(size_t n_elements, size_t element_size);
36
void fastFree(void* p);
37
void* fastRealloc(void* p, size_t n);
38
void* fastZeroedMalloc(size_t n);
39
// These functions return 0 if an allocation fails.
40
void* tryFastMalloc(size_t n);
41
void* tryFastZeroedMalloc(size_t n);
42
void* tryFastCalloc(size_t n_elements, size_t element_size);
43
void* tryFastRealloc(void* p, size_t n);
44
char* fastStrDup(const char*);
47
void fastMallocForbid();
48
void fastMallocAllow();
51
#if !defined(USE_SYSTEM_MALLOC) || !USE_SYSTEM_MALLOC
53
#define malloc(n) fastMalloc(n)
54
#define calloc(n_elements, element_size) fastCalloc(n_elements, element_size)
55
#define realloc(p, n) fastRealloc(p, n)
56
#define free(p) fastFree(p)
57
#define strdup(p) fastStrDup(p)
61
#define strdup(p) _strdup(p)
70
#if !defined(USE_SYSTEM_MALLOC) || !USE_SYSTEM_MALLOC
71
static inline void* __cdecl operator new(size_t s) { return fastMalloc(s); }
72
static inline void __cdecl operator delete(void* p) { fastFree(p); }
73
static inline void* __cdecl operator new[](size_t s) { return fastMalloc(s); }
74
static inline void __cdecl operator delete[](void* p) { fastFree(p); }
75
static inline void* operator new(size_t s, const std::nothrow_t&) throw() { return fastMalloc(s); }
76
static inline void operator delete(void* p, const std::nothrow_t&) throw() { fastFree(p); }
77
static inline void* operator new[](size_t s, const std::nothrow_t&) throw() { return fastMalloc(s); }
78
static inline void operator delete[](void* p, const std::nothrow_t&) throw() { fastFree(p); }
82
// This defines a type which holds an unsigned integer and is the same
83
// size as the minimally aligned memory allocation.
84
typedef unsigned long long AllocAlignmentInteger;
87
enum AllocType { // Start with an unusual number instead of zero, because zero is common.
88
AllocTypeMalloc = 0x375d6750, // Encompasses fastMalloc, fastZeroedMalloc, fastCalloc, fastRealloc.
89
AllocTypeClassNew, // Encompasses class operator new from FastAllocBase.
90
AllocTypeClassNewArray, // Encompasses class operator new[] from FastAllocBase.
91
AllocTypeFastNew, // Encompasses fastNew.
92
AllocTypeFastNewArray, // Encompasses fastNewArray.
93
AllocTypeNew, // Encompasses global operator new.
94
AllocTypeNewArray // Encompasses global operator new[].
99
#if ENABLE(FAST_MALLOC_MATCH_VALIDATION)
101
// Malloc validation is a scheme whereby a tag is attached to an
102
// allocation which identifies how it was originally allocated.
103
// This allows us to verify that the freeing operation matches the
104
// allocation operation. If memory is allocated with operator new[]
105
// but freed with free or delete, this system would detect that.
106
// In the implementation here, the tag is an integer prepended to
107
// the allocation memory which is assigned one of the AllocType
108
// enumeration values. An alternative implementation of this
109
// scheme could store the tag somewhere else or ignore it.
110
// Users of FastMalloc don't need to know or care how this tagging
115
// Return the AllocType tag associated with the allocated block p.
116
inline AllocType fastMallocMatchValidationType(const void* p)
118
const AllocAlignmentInteger* type = static_cast<const AllocAlignmentInteger*>(p) - 1;
119
return static_cast<AllocType>(*type);
122
// Return the address of the AllocType tag associated with the allocated block p.
123
inline AllocAlignmentInteger* fastMallocMatchValidationValue(void* p)
125
return reinterpret_cast<AllocAlignmentInteger*>(static_cast<char*>(p) - sizeof(AllocAlignmentInteger));
128
// Set the AllocType tag to be associaged with the allocated block p.
129
inline void setFastMallocMatchValidationType(void* p, AllocType allocType)
131
AllocAlignmentInteger* type = static_cast<AllocAlignmentInteger*>(p) - 1;
132
*type = static_cast<AllocAlignmentInteger>(allocType);
135
// Handle a detected alloc/free mismatch. By default this calls CRASH().
136
void fastMallocMatchFailed(void* p);
138
} // namespace Internal
140
// This is a higher level function which is used by FastMalloc-using code.
141
inline void fastMallocMatchValidateMalloc(void* p, Internal::AllocType allocType)
146
Internal::setFastMallocMatchValidationType(p, allocType);
149
// This is a higher level function which is used by FastMalloc-using code.
150
inline void fastMallocMatchValidateFree(void* p, Internal::AllocType allocType)
155
if (Internal::fastMallocMatchValidationType(p) != allocType)
156
Internal::fastMallocMatchFailed(p);
157
Internal::setFastMallocMatchValidationType(p, Internal::AllocTypeMalloc); // Set it to this so that fastFree thinks it's OK.
162
inline void fastMallocMatchValidateMalloc(void*, Internal::AllocType)
166
inline void fastMallocMatchValidateFree(void*, Internal::AllocType)
176
#endif // FastMallocWince_h