blob: 394366ea4dc454f54935b48384707e3b767232da [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2008 the V8 project authors. All rights reserved.
2// 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
Ben Murdochb0fe1622011-05-05 13:52:32 +010031#include "checks.h"
32#include "globals.h"
33
Steve Blocka7e24c12009-10-30 11:49:00 +000034namespace v8 {
35namespace internal {
36
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080037// Called when allocation routines fail to allocate.
38// This function should not return, but should terminate the current
39// processing.
40void FatalProcessOutOfMemory(const char* message);
Steve Blocka7e24c12009-10-30 11:49:00 +000041
42// A class that controls whether allocation is allowed. This is for
43// the C++ heap only!
44class NativeAllocationChecker {
45 public:
46 typedef enum { ALLOW, DISALLOW } NativeAllocationAllowed;
47 explicit inline NativeAllocationChecker(NativeAllocationAllowed allowed)
48 : allowed_(allowed) {
49#ifdef DEBUG
50 if (allowed == DISALLOW) {
51 allocation_disallowed_++;
52 }
53#endif
54 }
55 ~NativeAllocationChecker() {
56#ifdef DEBUG
57 if (allowed_ == DISALLOW) {
58 allocation_disallowed_--;
59 }
60#endif
61 ASSERT(allocation_disallowed_ >= 0);
62 }
63 static inline bool allocation_allowed() {
64 return allocation_disallowed_ == 0;
65 }
66 private:
67 // This static counter ensures that NativeAllocationCheckers can be nested.
68 static int allocation_disallowed_;
69 // This flag applies to this particular instance.
70 NativeAllocationAllowed allowed_;
71};
72
73
74// Superclass for classes managed with new & delete.
75class Malloced {
76 public:
77 void* operator new(size_t size) { return New(size); }
78 void operator delete(void* p) { Delete(p); }
79
80 static void FatalProcessOutOfMemory();
81 static void* New(size_t size);
82 static void Delete(void* p);
83};
84
85
86// A macro is used for defining the base class used for embedded instances.
87// The reason is some compilers allocate a minimum of one word for the
88// superclass. The macro prevents the use of new & delete in debug mode.
89// In release mode we are not willing to pay this overhead.
90
91#ifdef DEBUG
92// Superclass for classes with instances allocated inside stack
93// activations or inside other objects.
94class Embedded {
95 public:
96 void* operator new(size_t size);
97 void operator delete(void* p);
98};
99#define BASE_EMBEDDED : public Embedded
100#else
101#define BASE_EMBEDDED
102#endif
103
104
105// Superclass for classes only using statics.
106class AllStatic {
107#ifdef DEBUG
108 public:
109 void* operator new(size_t size);
110 void operator delete(void* p);
111#endif
112};
113
114
115template <typename T>
116static T* NewArray(int size) {
117 ASSERT(NativeAllocationChecker::allocation_allowed());
118 T* result = new T[size];
119 if (result == NULL) Malloced::FatalProcessOutOfMemory();
120 return result;
121}
122
123
124template <typename T>
125static void DeleteArray(T* array) {
126 delete[] array;
127}
128
129
130// The normal strdup functions use malloc. These versions of StrDup
131// and StrNDup uses new and calls the FatalProcessOutOfMemory handler
132// if allocation fails.
133char* StrDup(const char* str);
Steve Blockd0582a62009-12-15 09:54:21 +0000134char* StrNDup(const char* str, int n);
Steve Blocka7e24c12009-10-30 11:49:00 +0000135
136
137// Allocation policy for allocating in the C free store using malloc
138// and free. Used as the default policy for lists.
139class FreeStoreAllocationPolicy {
140 public:
141 INLINE(static void* New(size_t size)) { return Malloced::New(size); }
142 INLINE(static void Delete(void* p)) { Malloced::Delete(p); }
143};
144
145
146// Allocation policy for allocating in preallocated space.
147// Used as an allocation policy for ScopeInfo when generating
148// stack traces.
149class PreallocatedStorage : public AllStatic {
150 public:
151 explicit PreallocatedStorage(size_t size);
152 size_t size() { return size_; }
153 static void* New(size_t size);
154 static void Delete(void* p);
155
156 // Preallocate a set number of bytes.
157 static void Init(size_t size);
158
159 private:
160 size_t size_;
161 PreallocatedStorage* previous_;
162 PreallocatedStorage* next_;
163 static bool preallocated_;
164
165 static PreallocatedStorage in_use_list_;
166 static PreallocatedStorage free_list_;
167
168 void LinkTo(PreallocatedStorage* other);
169 void Unlink();
170 DISALLOW_IMPLICIT_CONSTRUCTORS(PreallocatedStorage);
171};
172
173
174} } // namespace v8::internal
175
176#endif // V8_ALLOCATION_H_