OLD | NEW |
1 // Copyright 2008 the V8 project authors. All rights reserved. | 1 // Copyright 2008 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 10 matching lines...) Expand all Loading... |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 #ifndef V8_ALLOCATION_H_ | 28 #ifndef V8_ALLOCATION_H_ |
29 #define V8_ALLOCATION_H_ | 29 #define V8_ALLOCATION_H_ |
30 | 30 |
| 31 #include "v8-global-context.h" |
| 32 |
31 namespace v8 { | 33 namespace v8 { |
32 namespace internal { | 34 namespace internal { |
33 | 35 |
34 | |
35 // A class that controls whether allocation is allowed. This is for | |
36 // the C++ heap only! | |
37 class NativeAllocationChecker { | |
38 public: | |
39 typedef enum { ALLOW, DISALLOW } NativeAllocationAllowed; | |
40 explicit inline NativeAllocationChecker(NativeAllocationAllowed allowed) | |
41 : allowed_(allowed) { | |
42 #ifdef DEBUG | |
43 if (allowed == DISALLOW) { | |
44 allocation_disallowed_++; | |
45 } | |
46 #endif | |
47 } | |
48 ~NativeAllocationChecker() { | |
49 #ifdef DEBUG | |
50 if (allowed_ == DISALLOW) { | |
51 allocation_disallowed_--; | |
52 } | |
53 #endif | |
54 ASSERT(allocation_disallowed_ >= 0); | |
55 } | |
56 static inline bool allocation_allowed() { | |
57 return allocation_disallowed_ == 0; | |
58 } | |
59 private: | |
60 // This static counter ensures that NativeAllocationCheckers can be nested. | |
61 static int allocation_disallowed_; | |
62 // This flag applies to this particular instance. | |
63 NativeAllocationAllowed allowed_; | |
64 }; | |
65 | |
66 | |
67 // Superclass for classes managed with new & delete. | 36 // Superclass for classes managed with new & delete. |
68 class Malloced { | 37 class Malloced { |
69 public: | 38 public: |
70 void* operator new(size_t size) { return New(size); } | 39 void* operator new(size_t size) { return New(size); } |
71 void operator delete(void* p) { Delete(p); } | 40 void operator delete(void* p) { Delete(p); } |
72 | 41 |
73 static void FatalProcessOutOfMemory(); | 42 static void FatalProcessOutOfMemory(); |
74 static void* New(size_t size); | 43 static void* New(size_t size); |
75 static void Delete(void* p); | 44 static void Delete(void* p); |
76 }; | 45 }; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 | 97 |
129 | 98 |
130 // Allocation policy for allocating in the C free store using malloc | 99 // Allocation policy for allocating in the C free store using malloc |
131 // and free. Used as the default policy for lists. | 100 // and free. Used as the default policy for lists. |
132 class FreeStoreAllocationPolicy { | 101 class FreeStoreAllocationPolicy { |
133 public: | 102 public: |
134 INLINE(static void* New(size_t size)) { return Malloced::New(size); } | 103 INLINE(static void* New(size_t size)) { return Malloced::New(size); } |
135 INLINE(static void Delete(void* p)) { Malloced::Delete(p); } | 104 INLINE(static void Delete(void* p)) { Malloced::Delete(p); } |
136 }; | 105 }; |
137 | 106 |
138 | |
139 // Allocation policy for allocating in preallocated space. | 107 // Allocation policy for allocating in preallocated space. |
140 // Used as an allocation policy for ScopeInfo when generating | 108 // Used as an allocation policy for ScopeInfo when generating |
141 // stack traces. | 109 // stack traces. |
142 class PreallocatedStorage : public AllStatic { | 110 class PreallocatedStorage : public AllStatic { |
143 public: | 111 public: |
144 explicit PreallocatedStorage(size_t size); | 112 explicit PreallocatedStorage(size_t size); |
145 size_t size() { return size_; } | 113 size_t size() { return size_; } |
146 static void* New(size_t size); | 114 static void* New(size_t size); |
147 static void Delete(void* p); | 115 static void Delete(void* p); |
148 | 116 |
149 // Preallocate a set number of bytes. | 117 // Preallocate a set number of bytes. |
150 static void Init(size_t size); | 118 static void Init(size_t size); |
151 | 119 |
152 private: | 120 private: |
153 size_t size_; | 121 size_t size_; |
154 PreallocatedStorage* previous_; | 122 PreallocatedStorage* previous_; |
155 PreallocatedStorage* next_; | 123 PreallocatedStorage* next_; |
156 static bool preallocated_; | |
157 | |
158 static PreallocatedStorage in_use_list_; | |
159 static PreallocatedStorage free_list_; | |
160 | 124 |
161 void LinkTo(PreallocatedStorage* other); | 125 void LinkTo(PreallocatedStorage* other); |
162 void Unlink(); | 126 void Unlink(); |
163 DISALLOW_IMPLICIT_CONSTRUCTORS(PreallocatedStorage); | 127 DISALLOW_IMPLICIT_CONSTRUCTORS(PreallocatedStorage); |
164 }; | 128 }; |
165 | 129 |
| 130 class StorageData { |
| 131 private: |
| 132 // This static counter ensures that NativeAllocationCheckers can be nested. |
| 133 int allocation_disallowed_; |
| 134 bool preallocated_; |
| 135 |
| 136 PreallocatedStorage in_use_list_; |
| 137 PreallocatedStorage free_list_; |
| 138 #ifdef DEBUG |
| 139 private: |
| 140 bool rset_used_; // state of the remembered set |
| 141 #endif |
| 142 |
| 143 StorageData(); |
| 144 friend class V8Context; |
| 145 friend class PreallocatedStorage; |
| 146 friend class NativeAllocationChecker; |
| 147 friend class Page; |
| 148 DISALLOW_COPY_AND_ASSIGN(StorageData); |
| 149 }; |
| 150 |
| 151 // A class that controls whether allocation is allowed. This is for |
| 152 // the C++ heap only! |
| 153 class NativeAllocationChecker { |
| 154 public: |
| 155 typedef enum { ALLOW, DISALLOW } NativeAllocationAllowed; |
| 156 explicit inline NativeAllocationChecker(NativeAllocationAllowed allowed) |
| 157 : allowed_(allowed) { |
| 158 #ifdef DEBUG |
| 159 if (allowed == DISALLOW) { |
| 160 v8_context()->storage_data_.allocation_disallowed_++; |
| 161 } |
| 162 #endif |
| 163 } |
| 164 ~NativeAllocationChecker() { |
| 165 #ifdef DEBUG |
| 166 if (allowed_ == DISALLOW) { |
| 167 v8_context()->storage_data_.allocation_disallowed_--; |
| 168 } |
| 169 #endif |
| 170 ASSERT(v8_context()->storage_data_.allocation_disallowed_ >= 0); |
| 171 } |
| 172 static inline bool allocation_allowed() { |
| 173 V8Context * const v8context = v8_context(); |
| 174 return v8context ? |
| 175 v8context->storage_data_.allocation_disallowed_ == 0: true; |
| 176 } |
| 177 private: |
| 178 // This static counter ensures that NativeAllocationCheckers can be nested. |
| 179 static int allocation_disallowed_; |
| 180 // This flag applies to this particular instance. |
| 181 NativeAllocationAllowed allowed_; |
| 182 }; |
166 | 183 |
167 } } // namespace v8::internal | 184 } } // namespace v8::internal |
168 | 185 |
169 #endif // V8_ALLOCATION_H_ | 186 #endif // V8_ALLOCATION_H_ |
OLD | NEW |