blob: 35e68bde04bf1a84d331b4a976ec6d0a9d0d90ef [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_ALLOCATION_H_
29#define V8_ALLOCATION_H_
30
31namespace v8 { namespace internal {
32
33
34// A class that controls whether allocation is allowed. This is for
35// the C++ heap only!
36class NativeAllocationChecker {
37 public:
38 typedef enum { ALLOW, DISALLOW } NativeAllocationAllowed;
39 explicit inline NativeAllocationChecker(NativeAllocationAllowed allowed)
40 : allowed_(allowed) {
41#ifdef DEBUG
42 if (allowed == DISALLOW) {
43 allocation_disallowed_++;
44 }
45#endif
46 }
47 ~NativeAllocationChecker() {
48#ifdef DEBUG
49 if (allowed_ == DISALLOW) {
50 allocation_disallowed_--;
51 }
52#endif
53 ASSERT(allocation_disallowed_ >= 0);
54 }
55 static inline bool allocation_allowed() {
56 return allocation_disallowed_ == 0;
57 }
58 private:
59 // This static counter ensures that NativeAllocationCheckers can be nested.
60 static int allocation_disallowed_;
61 // This flag applies to this particular instance.
62 NativeAllocationAllowed allowed_;
63};
64
65
66// Superclass for classes managed with new & delete.
67class Malloced {
68 public:
69 void* operator new(size_t size) { return New(size); }
70 void operator delete(void* p) { Delete(p); }
71
72 static void FatalProcessOutOfMemory();
73 static void* New(size_t size);
74 static void Delete(void* p);
75};
76
77
78// A macro is used for defining the base class used for embedded instances.
79// The reason is some compilers allocate a minimum of one word for the
80// superclass. The macro prevents the use of new & delete in debug mode.
81// In release mode we are not willing to pay this overhead.
82
83#ifdef DEBUG
84// Superclass for classes with instances allocated inside stack
85// activations or inside other objects.
86class Embedded {
87 public:
88 void* operator new(size_t size);
89 void operator delete(void* p);
90};
91#define BASE_EMBEDDED : public Embedded
92#else
93#define BASE_EMBEDDED
94#endif
95
96
97// Superclass for classes only using statics.
98class AllStatic {
99#ifdef DEBUG
100 public:
101 void* operator new(size_t size);
102 void operator delete(void* p);
103#endif
104};
105
106
107template <typename T>
108static T* NewArray(int size) {
109 ASSERT(NativeAllocationChecker::allocation_allowed());
110 T* result = new T[size];
111 if (result == NULL) Malloced::FatalProcessOutOfMemory();
112 return result;
113}
114
115
116template <typename T>
117static void DeleteArray(T* array) {
118 delete[] array;
119}
120
121
122// The normal strdup function uses malloc. This version of StrDup
123// uses new and calls the FatalProcessOutOfMemory handler if
124// allocation fails.
125char* StrDup(const char* str);
126
127
128// Allocation policy for allocating in the C free store using malloc
129// and free. Used as the default policy for lists.
130class FreeStoreAllocationPolicy {
131 public:
132 INLINE(static void* New(size_t size)) { return Malloced::New(size); }
133 INLINE(static void Delete(void* p)) { Malloced::Delete(p); }
134};
135
136
137// Allocation policy for allocating in preallocated space.
138// Used as an allocation policy for ScopeInfo when generating
139// stack traces.
140class PreallocatedStorage : public AllStatic {
141 public:
142 explicit PreallocatedStorage(size_t size);
143 size_t size() { return size_; }
144 static void* New(size_t size);
145 static void Delete(void* p);
146
147 // Preallocate a set number of bytes.
148 static void Init(size_t size);
149
150 private:
151 size_t size_;
152 PreallocatedStorage* previous_;
153 PreallocatedStorage* next_;
154 static bool preallocated_;
155
156 static PreallocatedStorage in_use_list_;
157 static PreallocatedStorage free_list_;
158
159 void LinkTo(PreallocatedStorage* other);
160 void Unlink();
161 DISALLOW_IMPLICIT_CONSTRUCTORS(PreallocatedStorage);
162};
163
164
165} } // namespace v8::internal
166
167#endif // V8_ALLOCATION_H_