1 // 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 #include <stdlib.h> 29 30 #include "v8.h" 31 32 namespace v8 { 33 namespace internal { 34 35 36 void* Malloced::New(size_t size) { 37 ASSERT(NativeAllocationChecker::allocation_allowed()); 38 void* result = malloc(size); 39 if (result == NULL) V8::FatalProcessOutOfMemory("Malloced operator new"); 40 return result; 41 } 42 43 44 void Malloced::Delete(void* p) { 45 free(p); 46 } 47 48 49 void Malloced::FatalProcessOutOfMemory() { 50 V8::FatalProcessOutOfMemory("Out of memory"); 51 } 52 53 54 #ifdef DEBUG 55 56 static void* invalid = static_cast<void*>(NULL); 57 58 void* Embedded::operator new(size_t size) { 59 UNREACHABLE(); 60 return invalid; 61 } 62 63 64 void Embedded::operator delete(void* p) { 65 UNREACHABLE(); 66 } 67 68 69 void* AllStatic::operator new(size_t size) { 70 UNREACHABLE(); 71 return invalid; 72 } 73 74 75 void AllStatic::operator delete(void* p) { 76 UNREACHABLE(); 77 } 78 79 #endif 80 81 82 char* StrDup(const char* str) { 83 int length = StrLength(str); 84 char* result = NewArray<char>(length + 1); 85 memcpy(result, str, length * kCharSize); 86 result[length] = '\0'; 87 return result; 88 } 89 90 91 char* StrNDup(const char* str, int n) { 92 int length = StrLength(str); 93 if (n < length) length = n; 94 char* result = NewArray<char>(length + 1); 95 memcpy(result, str, length * kCharSize); 96 result[length] = '\0'; 97 return result; 98 } 99 100 101 int NativeAllocationChecker::allocation_disallowed_ = 0; 102 103 104 PreallocatedStorage PreallocatedStorage::in_use_list_(0); 105 PreallocatedStorage PreallocatedStorage::free_list_(0); 106 bool PreallocatedStorage::preallocated_ = false; 107 108 109 void PreallocatedStorage::Init(size_t size) { 110 ASSERT(free_list_.next_ == &free_list_); 111 ASSERT(free_list_.previous_ == &free_list_); 112 PreallocatedStorage* free_chunk = 113 reinterpret_cast<PreallocatedStorage*>(new char[size]); 114 free_list_.next_ = free_list_.previous_ = free_chunk; 115 free_chunk->next_ = free_chunk->previous_ = &free_list_; 116 free_chunk->size_ = size - sizeof(PreallocatedStorage); 117 preallocated_ = true; 118 } 119 120 121 void* PreallocatedStorage::New(size_t size) { 122 if (!preallocated_) { 123 return FreeStoreAllocationPolicy::New(size); 124 } 125 ASSERT(free_list_.next_ != &free_list_); 126 ASSERT(free_list_.previous_ != &free_list_); 127 size = (size + kPointerSize - 1) & ~(kPointerSize - 1); 128 // Search for exact fit. 129 for (PreallocatedStorage* storage = free_list_.next_; 130 storage != &free_list_; 131 storage = storage->next_) { 132 if (storage->size_ == size) { 133 storage->Unlink(); 134 storage->LinkTo(&in_use_list_); 135 return reinterpret_cast<void*>(storage + 1); 136 } 137 } 138 // Search for first fit. 139 for (PreallocatedStorage* storage = free_list_.next_; 140 storage != &free_list_; 141 storage = storage->next_) { 142 if (storage->size_ >= size + sizeof(PreallocatedStorage)) { 143 storage->Unlink(); 144 storage->LinkTo(&in_use_list_); 145 PreallocatedStorage* left_over = 146 reinterpret_cast<PreallocatedStorage*>( 147 reinterpret_cast<char*>(storage + 1) + size); 148 left_over->size_ = storage->size_ - size - sizeof(PreallocatedStorage); 149 ASSERT(size + left_over->size_ + sizeof(PreallocatedStorage) == 150 storage->size_); 151 storage->size_ = size; 152 left_over->LinkTo(&free_list_); 153 return reinterpret_cast<void*>(storage + 1); 154 } 155 } 156 // Allocation failure. 157 ASSERT(false); 158 return NULL; 159 } 160 161 162 // We don't attempt to coalesce. 163 void PreallocatedStorage::Delete(void* p) { 164 if (p == NULL) { 165 return; 166 } 167 if (!preallocated_) { 168 FreeStoreAllocationPolicy::Delete(p); 169 return; 170 } 171 PreallocatedStorage* storage = reinterpret_cast<PreallocatedStorage*>(p) - 1; 172 ASSERT(storage->next_->previous_ == storage); 173 ASSERT(storage->previous_->next_ == storage); 174 storage->Unlink(); 175 storage->LinkTo(&free_list_); 176 } 177 178 179 void PreallocatedStorage::LinkTo(PreallocatedStorage* other) { 180 next_ = other->next_; 181 other->next_->previous_ = this; 182 previous_ = other; 183 other->next_ = this; 184 } 185 186 187 void PreallocatedStorage::Unlink() { 188 next_->previous_ = previous_; 189 previous_->next_ = next_; 190 } 191 192 193 PreallocatedStorage::PreallocatedStorage(size_t size) 194 : size_(size) { 195 previous_ = next_ = this; 196 } 197 198 } } // namespace v8::internal 199