ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2008 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 31 | #include "checks.h" |
| 32 | #include "globals.h" |
| 33 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 34 | namespace v8 { |
| 35 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 36 | |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 37 | // Called when allocation routines fail to allocate. |
| 38 | // This function should not return, but should terminate the current |
| 39 | // processing. |
| 40 | void FatalProcessOutOfMemory(const char* message); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 41 | |
| 42 | // A class that controls whether allocation is allowed. This is for |
| 43 | // the C++ heap only! |
| 44 | class 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. |
| 75 | class 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. |
| 94 | class 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. |
| 106 | class AllStatic { |
| 107 | #ifdef DEBUG |
| 108 | public: |
| 109 | void* operator new(size_t size); |
| 110 | void operator delete(void* p); |
| 111 | #endif |
| 112 | }; |
| 113 | |
| 114 | |
| 115 | template <typename T> |
| 116 | static 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 | |
| 124 | template <typename T> |
| 125 | static void DeleteArray(T* array) { |
| 126 | delete[] array; |
| 127 | } |
| 128 | |
| 129 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 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. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 133 | char* StrDup(const char* str); |
ager@chromium.org | 01beca7 | 2009-11-24 14:29:16 +0000 | [diff] [blame] | 134 | char* StrNDup(const char* str, int n); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 135 | |
| 136 | |
| 137 | // Allocation policy for allocating in the C free store using malloc |
| 138 | // and free. Used as the default policy for lists. |
| 139 | class 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. |
| 149 | class 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_ |