]>
git.saurik.com Git - apple/javascriptcore.git/blob - wtf/wince/FastMallocWinCE.h
2 * Copyright (C) 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2007-2009 Torch Mobile, Inc. All rights reserved
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 #ifndef WTF_FastMallocWinCE_h
23 #define WTF_FastMallocWinCE_h
29 #include "MemoryManager.h"
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 // These functions return 0 if an allocation fails.
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*);
46 void fastMallocForbid();
47 void fastMallocAllow();
50 #if !defined(USE_SYSTEM_MALLOC) || !USE_SYSTEM_MALLOC
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)
60 #define strdup(p) _strdup(p)
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
); }
81 // This defines a type which holds an unsigned integer and is the same
82 // size as the minimally aligned memory allocation.
83 typedef unsigned long long AllocAlignmentInteger
;
86 enum AllocType
{ // Start with an unusual number instead of zero, because zero is common.
87 AllocTypeMalloc
= 0x375d6750, // Encompasses fastMalloc, fastZeroedMalloc, fastCalloc, fastRealloc.
88 AllocTypeClassNew
, // Encompasses class operator new from FastAllocBase.
89 AllocTypeClassNewArray
, // Encompasses class operator new[] from FastAllocBase.
90 AllocTypeFastNew
, // Encompasses fastNew.
91 AllocTypeFastNewArray
, // Encompasses fastNewArray.
92 AllocTypeNew
, // Encompasses global operator new.
93 AllocTypeNewArray
// Encompasses global operator new[].
98 #if ENABLE(FAST_MALLOC_MATCH_VALIDATION)
100 // Malloc validation is a scheme whereby a tag is attached to an
101 // allocation which identifies how it was originally allocated.
102 // This allows us to verify that the freeing operation matches the
103 // allocation operation. If memory is allocated with operator new[]
104 // but freed with free or delete, this system would detect that.
105 // In the implementation here, the tag is an integer prepended to
106 // the allocation memory which is assigned one of the AllocType
107 // enumeration values. An alternative implementation of this
108 // scheme could store the tag somewhere else or ignore it.
109 // Users of FastMalloc don't need to know or care how this tagging
114 // Return the AllocType tag associated with the allocated block p.
115 inline AllocType
fastMallocMatchValidationType(const void* p
)
117 const AllocAlignmentInteger
* type
= static_cast<const AllocAlignmentInteger
*>(p
) - 1;
118 return static_cast<AllocType
>(*type
);
121 // Return the address of the AllocType tag associated with the allocated block p.
122 inline AllocAlignmentInteger
* fastMallocMatchValidationValue(void* p
)
124 return reinterpret_cast<AllocAlignmentInteger
*>(static_cast<char*>(p
) - sizeof(AllocAlignmentInteger
));
127 // Set the AllocType tag to be associaged with the allocated block p.
128 inline void setFastMallocMatchValidationType(void* p
, AllocType allocType
)
130 AllocAlignmentInteger
* type
= static_cast<AllocAlignmentInteger
*>(p
) - 1;
131 *type
= static_cast<AllocAlignmentInteger
>(allocType
);
134 // Handle a detected alloc/free mismatch. By default this calls CRASH().
135 void fastMallocMatchFailed(void* p
);
137 } // namespace Internal
139 // This is a higher level function which is used by FastMalloc-using code.
140 inline void fastMallocMatchValidateMalloc(void* p
, Internal::AllocType allocType
)
145 Internal::setFastMallocMatchValidationType(p
, allocType
);
148 // This is a higher level function which is used by FastMalloc-using code.
149 inline void fastMallocMatchValidateFree(void* p
, Internal::AllocType allocType
)
154 if (Internal::fastMallocMatchValidationType(p
) != allocType
)
155 Internal::fastMallocMatchFailed(p
);
156 Internal::setFastMallocMatchValidationType(p
, Internal::AllocTypeMalloc
); // Set it to this so that fastFree thinks it's OK.
161 inline void fastMallocMatchValidateMalloc(void*, Internal::AllocType
)
165 inline void fastMallocMatchValidateFree(void*, Internal::AllocType
)
175 #endif // WTF_FastMallocWinCE_h