~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/3rdparty/webkit/JavaScriptCore/wtf/wince/FastMallocWince.h

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2009-11-02 18:30:08 UTC
  • mfrom: (1.2.2 upstream)
  • mto: (15.2.5 experimental)
  • mto: This revision was merged to the branch mainline in revision 88.
  • Revision ID: james.westby@ubuntu.com-20091102183008-b6a4gcs128mvfb3m
Tags: upstream-4.6.0~beta1
ImportĀ upstreamĀ versionĀ 4.6.0~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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.
 
20
 *
 
21
 */
 
22
 
 
23
#ifndef FastMallocWince_h
 
24
#define FastMallocWince_h
 
25
 
 
26
#include <new.h>
 
27
 
 
28
#ifdef __cplusplus
 
29
#include <new>
 
30
#include "MemoryManager.h"
 
31
extern "C" {
 
32
#endif
 
33
 
 
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*);
 
45
 
 
46
#ifndef NDEBUG
 
47
void fastMallocForbid();
 
48
void fastMallocAllow();
 
49
#endif
 
50
 
 
51
#if !defined(USE_SYSTEM_MALLOC) || !USE_SYSTEM_MALLOC
 
52
 
 
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)
 
58
 
 
59
#else
 
60
 
 
61
#define strdup(p)                           _strdup(p)
 
62
 
 
63
#endif
 
64
 
 
65
#ifdef __cplusplus
 
66
}
 
67
#endif
 
68
 
 
69
#ifdef __cplusplus
 
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); }
 
79
#endif
 
80
 
 
81
namespace WTF {
 
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;
 
85
 
 
86
    namespace Internal {
 
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[].
 
95
        };
 
96
    }
 
97
 
 
98
 
 
99
#if ENABLE(FAST_MALLOC_MATCH_VALIDATION)
 
100
 
 
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
 
111
    // is implemented.
 
112
 
 
113
    namespace Internal {
 
114
 
 
115
        // Return the AllocType tag associated with the allocated block p.
 
116
        inline AllocType fastMallocMatchValidationType(const void* p)
 
117
        {
 
118
            const AllocAlignmentInteger* type = static_cast<const AllocAlignmentInteger*>(p) - 1;
 
119
            return static_cast<AllocType>(*type);
 
120
        }
 
121
 
 
122
        // Return the address of the AllocType tag associated with the allocated block p.
 
123
        inline AllocAlignmentInteger* fastMallocMatchValidationValue(void* p)
 
124
        {
 
125
            return reinterpret_cast<AllocAlignmentInteger*>(static_cast<char*>(p) - sizeof(AllocAlignmentInteger));
 
126
        }
 
127
 
 
128
        // Set the AllocType tag to be associaged with the allocated block p.
 
129
        inline void setFastMallocMatchValidationType(void* p, AllocType allocType)
 
130
        {
 
131
            AllocAlignmentInteger* type = static_cast<AllocAlignmentInteger*>(p) - 1;
 
132
            *type = static_cast<AllocAlignmentInteger>(allocType);
 
133
        }
 
134
 
 
135
        // Handle a detected alloc/free mismatch. By default this calls CRASH().
 
136
        void fastMallocMatchFailed(void* p);
 
137
 
 
138
    } // namespace Internal
 
139
 
 
140
    // This is a higher level function which is used by FastMalloc-using code.
 
141
    inline void fastMallocMatchValidateMalloc(void* p, Internal::AllocType allocType)
 
142
    {
 
143
        if (!p)
 
144
            return;
 
145
 
 
146
        Internal::setFastMallocMatchValidationType(p, allocType);
 
147
    }
 
148
 
 
149
    // This is a higher level function which is used by FastMalloc-using code.
 
150
    inline void fastMallocMatchValidateFree(void* p, Internal::AllocType allocType)
 
151
    {
 
152
        if (!p)
 
153
            return;
 
154
 
 
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.
 
158
    }
 
159
 
 
160
#else
 
161
 
 
162
    inline void fastMallocMatchValidateMalloc(void*, Internal::AllocType)
 
163
    {
 
164
    }
 
165
 
 
166
    inline void fastMallocMatchValidateFree(void*, Internal::AllocType)
 
167
    {
 
168
    }
 
169
 
 
170
#endif
 
171
 
 
172
} // namespace WTF
 
173
 
 
174
#endif
 
175
 
 
176
#endif // FastMallocWince_h
 
177