summaryrefslogtreecommitdiffstats
path: root/JavaScriptCore/wtf/wince/FastMallocWinCE.h
blob: d91a5f2c9026115db0d6a10f123e0c7ceb16a025 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
 *  Copyright (C) 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
 *  Copyright (C) 2007-2009 Torch Mobile, Inc. All rights reserved
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 *
 */

#ifndef WTF_FastMallocWinCE_h
#define WTF_FastMallocWinCE_h

#include <new.h>

#ifdef __cplusplus
#include <new>
#include "MemoryManager.h"
extern "C" {
#endif

void* fastMalloc(size_t n);
void* fastCalloc(size_t n_elements, size_t element_size);
void fastFree(void* p);
void* fastRealloc(void* p, size_t n);
void* fastZeroedMalloc(size_t n);
// These functions return 0 if an allocation fails.
void* tryFastMalloc(size_t n);
void* tryFastZeroedMalloc(size_t n);
void* tryFastCalloc(size_t n_elements, size_t element_size);
void* tryFastRealloc(void* p, size_t n);
char* fastStrDup(const char*);

#ifndef NDEBUG
void fastMallocForbid();
void fastMallocAllow();
#endif

#if !defined(USE_SYSTEM_MALLOC) || !USE_SYSTEM_MALLOC

#define malloc(n)                           fastMalloc(n)
#define calloc(n_elements, element_size)    fastCalloc(n_elements, element_size)
#define realloc(p, n)                       fastRealloc(p, n)
#define free(p)                             fastFree(p)
#define strdup(p)                           fastStrDup(p)

#else

#define strdup(p)                           _strdup(p)

#endif

#ifdef __cplusplus
}
#endif

#ifdef __cplusplus
#if !defined(USE_SYSTEM_MALLOC) || !USE_SYSTEM_MALLOC
static inline void* __cdecl operator new(size_t s) { return fastMalloc(s); }
static inline void __cdecl operator delete(void* p) { fastFree(p); }
static inline void* __cdecl operator new[](size_t s) { return fastMalloc(s); }
static inline void __cdecl operator delete[](void* p) { fastFree(p); }
static inline void* operator new(size_t s, const std::nothrow_t&) throw() { return fastMalloc(s); }
static inline void operator delete(void* p, const std::nothrow_t&) throw() { fastFree(p); }
static inline void* operator new[](size_t s, const std::nothrow_t&) throw() { return fastMalloc(s); }
static inline void operator delete[](void* p, const std::nothrow_t&) throw() { fastFree(p); }
#endif

namespace WTF {
    // This defines a type which holds an unsigned integer and is the same
    // size as the minimally aligned memory allocation.
    typedef unsigned long long AllocAlignmentInteger;

    namespace Internal {
        enum AllocType {                    // Start with an unusual number instead of zero, because zero is common.
            AllocTypeMalloc = 0x375d6750,   // Encompasses fastMalloc, fastZeroedMalloc, fastCalloc, fastRealloc.
            AllocTypeClassNew,              // Encompasses class operator new from FastAllocBase.
            AllocTypeClassNewArray,         // Encompasses class operator new[] from FastAllocBase.
            AllocTypeFastNew,               // Encompasses fastNew.
            AllocTypeFastNewArray,          // Encompasses fastNewArray.
            AllocTypeNew,                   // Encompasses global operator new.
            AllocTypeNewArray               // Encompasses global operator new[].
        };
    }


#if ENABLE(FAST_MALLOC_MATCH_VALIDATION)

    // Malloc validation is a scheme whereby a tag is attached to an
    // allocation which identifies how it was originally allocated.
    // This allows us to verify that the freeing operation matches the
    // allocation operation. If memory is allocated with operator new[]
    // but freed with free or delete, this system would detect that.
    // In the implementation here, the tag is an integer prepended to
    // the allocation memory which is assigned one of the AllocType
    // enumeration values. An alternative implementation of this
    // scheme could store the tag somewhere else or ignore it.
    // Users of FastMalloc don't need to know or care how this tagging
    // is implemented.

    namespace Internal {

        // Return the AllocType tag associated with the allocated block p.
        inline AllocType fastMallocMatchValidationType(const void* p)
        {
            const AllocAlignmentInteger* type = static_cast<const AllocAlignmentInteger*>(p) - 1;
            return static_cast<AllocType>(*type);
        }

        // Return the address of the AllocType tag associated with the allocated block p.
        inline AllocAlignmentInteger* fastMallocMatchValidationValue(void* p)
        {
            return reinterpret_cast<AllocAlignmentInteger*>(static_cast<char*>(p) - sizeof(AllocAlignmentInteger));
        }

        // Set the AllocType tag to be associaged with the allocated block p.
        inline void setFastMallocMatchValidationType(void* p, AllocType allocType)
        {
            AllocAlignmentInteger* type = static_cast<AllocAlignmentInteger*>(p) - 1;
            *type = static_cast<AllocAlignmentInteger>(allocType);
        }

        // Handle a detected alloc/free mismatch. By default this calls CRASH().
        void fastMallocMatchFailed(void* p);

    } // namespace Internal

    // This is a higher level function which is used by FastMalloc-using code.
    inline void fastMallocMatchValidateMalloc(void* p, Internal::AllocType allocType)
    {
        if (!p)
            return;

        Internal::setFastMallocMatchValidationType(p, allocType);
    }

    // This is a higher level function which is used by FastMalloc-using code.
    inline void fastMallocMatchValidateFree(void* p, Internal::AllocType allocType)
    {
        if (!p)
            return;

        if (Internal::fastMallocMatchValidationType(p) != allocType)
            Internal::fastMallocMatchFailed(p);
        Internal::setFastMallocMatchValidationType(p, Internal::AllocTypeMalloc);  // Set it to this so that fastFree thinks it's OK.
    }

#else

    inline void fastMallocMatchValidateMalloc(void*, Internal::AllocType)
    {
    }

    inline void fastMallocMatchValidateFree(void*, Internal::AllocType)
    {
    }

#endif

} // namespace WTF

#endif

#endif // WTF_FastMallocWinCE_h