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 25 matching lines...) Expand all Loading... |
36 | 36 |
37 // Called when allocation routines fail to allocate. | 37 // Called when allocation routines fail to allocate. |
38 // This function should not return, but should terminate the current | 38 // This function should not return, but should terminate the current |
39 // processing. | 39 // processing. |
40 void FatalProcessOutOfMemory(const char* message); | 40 void FatalProcessOutOfMemory(const char* message); |
41 | 41 |
42 // A class that controls whether allocation is allowed. This is for | 42 // A class that controls whether allocation is allowed. This is for |
43 // the C++ heap only! | 43 // the C++ heap only! |
44 class NativeAllocationChecker { | 44 class NativeAllocationChecker { |
45 public: | 45 public: |
46 typedef enum { ALLOW, DISALLOW } NativeAllocationAllowed; | 46 enum NativeAllocationAllowed { ALLOW, DISALLOW }; |
47 explicit inline NativeAllocationChecker(NativeAllocationAllowed allowed) | |
48 : allowed_(allowed) { | |
49 #ifdef DEBUG | 47 #ifdef DEBUG |
50 if (allowed == DISALLOW) { | 48 explicit NativeAllocationChecker(NativeAllocationAllowed allowed); |
51 allocation_disallowed_++; | 49 ~NativeAllocationChecker(); |
52 } | 50 static bool allocation_allowed(); |
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: | 51 private: |
67 // This static counter ensures that NativeAllocationCheckers can be nested. | |
68 static int allocation_disallowed_; | |
69 // This flag applies to this particular instance. | 52 // This flag applies to this particular instance. |
70 NativeAllocationAllowed allowed_; | 53 NativeAllocationAllowed allowed_; |
| 54 #else |
| 55 explicit inline NativeAllocationChecker(NativeAllocationAllowed allowed) {} |
| 56 static inline bool allocation_allowed() { return true; } |
| 57 #endif |
71 }; | 58 }; |
72 | 59 |
73 | 60 |
74 // Superclass for classes managed with new & delete. | 61 // Superclass for classes managed with new & delete. |
75 class Malloced { | 62 class Malloced { |
76 public: | 63 public: |
77 void* operator new(size_t size) { return New(size); } | 64 void* operator new(size_t size) { return New(size); } |
78 void operator delete(void* p) { Delete(p); } | 65 void operator delete(void* p) { Delete(p); } |
79 | 66 |
80 static void FatalProcessOutOfMemory(); | 67 static void FatalProcessOutOfMemory(); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 class FreeStoreAllocationPolicy { | 126 class FreeStoreAllocationPolicy { |
140 public: | 127 public: |
141 INLINE(static void* New(size_t size)) { return Malloced::New(size); } | 128 INLINE(static void* New(size_t size)) { return Malloced::New(size); } |
142 INLINE(static void Delete(void* p)) { Malloced::Delete(p); } | 129 INLINE(static void Delete(void* p)) { Malloced::Delete(p); } |
143 }; | 130 }; |
144 | 131 |
145 | 132 |
146 // Allocation policy for allocating in preallocated space. | 133 // Allocation policy for allocating in preallocated space. |
147 // Used as an allocation policy for ScopeInfo when generating | 134 // Used as an allocation policy for ScopeInfo when generating |
148 // stack traces. | 135 // stack traces. |
149 class PreallocatedStorage : public AllStatic { | 136 class PreallocatedStorage { |
150 public: | 137 public: |
151 explicit PreallocatedStorage(size_t size); | 138 explicit PreallocatedStorage(size_t size); |
152 size_t size() { return size_; } | 139 size_t size() { return size_; } |
153 static void* New(size_t size); | |
154 static void Delete(void* p); | |
155 | 140 |
156 // Preallocate a set number of bytes. | 141 // TODO(isolates): Get rid of these-- we'll have to change the allocator |
157 static void Init(size_t size); | 142 // interface to include a pointer to an isolate to do this |
| 143 // efficiently. |
| 144 static inline void* New(size_t size); |
| 145 static inline void Delete(void* p); |
158 | 146 |
159 private: | 147 private: |
160 size_t size_; | 148 size_t size_; |
161 PreallocatedStorage* previous_; | 149 PreallocatedStorage* previous_; |
162 PreallocatedStorage* next_; | 150 PreallocatedStorage* next_; |
163 static bool preallocated_; | |
164 | |
165 static PreallocatedStorage in_use_list_; | |
166 static PreallocatedStorage free_list_; | |
167 | 151 |
168 void LinkTo(PreallocatedStorage* other); | 152 void LinkTo(PreallocatedStorage* other); |
169 void Unlink(); | 153 void Unlink(); |
| 154 |
| 155 friend class Isolate; |
| 156 |
170 DISALLOW_IMPLICIT_CONSTRUCTORS(PreallocatedStorage); | 157 DISALLOW_IMPLICIT_CONSTRUCTORS(PreallocatedStorage); |
171 }; | 158 }; |
172 | 159 |
173 | 160 |
174 } } // namespace v8::internal | 161 } } // namespace v8::internal |
175 | 162 |
176 #endif // V8_ALLOCATION_H_ | 163 #endif // V8_ALLOCATION_H_ |
OLD | NEW |