| 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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 } | 46 } |
| 47 | 47 |
| 48 | 48 |
| 49 void Malloced::FatalProcessOutOfMemory() { | 49 void Malloced::FatalProcessOutOfMemory() { |
| 50 V8::FatalProcessOutOfMemory("Out of memory"); | 50 V8::FatalProcessOutOfMemory("Out of memory"); |
| 51 } | 51 } |
| 52 | 52 |
| 53 | 53 |
| 54 #ifdef DEBUG | 54 #ifdef DEBUG |
| 55 | 55 |
| 56 static void* invalid = static_cast<void*>(NULL); | 56 static void* const invalid = static_cast<void*>(NULL); |
| 57 | 57 |
| 58 void* Embedded::operator new(size_t size) { | 58 void* Embedded::operator new(size_t size) { |
| 59 UNREACHABLE(); | 59 UNREACHABLE(); |
| 60 return invalid; | 60 return invalid; |
| 61 } | 61 } |
| 62 | 62 |
| 63 | 63 |
| 64 void Embedded::operator delete(void* p) { | 64 void Embedded::operator delete(void* p) { |
| 65 UNREACHABLE(); | 65 UNREACHABLE(); |
| 66 } | 66 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 90 | 90 |
| 91 char* StrNDup(const char* str, int n) { | 91 char* StrNDup(const char* str, int n) { |
| 92 int length = StrLength(str); | 92 int length = StrLength(str); |
| 93 if (n < length) length = n; | 93 if (n < length) length = n; |
| 94 char* result = NewArray<char>(length + 1); | 94 char* result = NewArray<char>(length + 1); |
| 95 memcpy(result, str, length * kCharSize); | 95 memcpy(result, str, length * kCharSize); |
| 96 result[length] = '\0'; | 96 result[length] = '\0'; |
| 97 return result; | 97 return result; |
| 98 } | 98 } |
| 99 | 99 |
| 100 | 100 StorageData::StorageData() |
| 101 int NativeAllocationChecker::allocation_disallowed_ = 0; | 101 :in_use_list_(0), |
| 102 | 102 free_list_(0), |
| 103 | 103 preallocated_(false), |
| 104 PreallocatedStorage PreallocatedStorage::in_use_list_(0); | 104 #ifdef DEBUG |
| 105 PreallocatedStorage PreallocatedStorage::free_list_(0); | 105 rset_used_(true), |
| 106 bool PreallocatedStorage::preallocated_ = false; | 106 #endif |
| 107 allocation_disallowed_(0) { |
| 108 } |
| 107 | 109 |
| 108 | 110 |
| 109 void PreallocatedStorage::Init(size_t size) { | 111 void PreallocatedStorage::Init(size_t size) { |
| 112 StorageData& storage_data = v8_context()->storage_data_; |
| 113 PreallocatedStorage & free_list_ = storage_data.free_list_; |
| 110 ASSERT(free_list_.next_ == &free_list_); | 114 ASSERT(free_list_.next_ == &free_list_); |
| 111 ASSERT(free_list_.previous_ == &free_list_); | 115 ASSERT(free_list_.previous_ == &free_list_); |
| 112 PreallocatedStorage* free_chunk = | 116 PreallocatedStorage* free_chunk = |
| 113 reinterpret_cast<PreallocatedStorage*>(new char[size]); | 117 reinterpret_cast<PreallocatedStorage*>(new char[size]); |
| 114 free_list_.next_ = free_list_.previous_ = free_chunk; | 118 free_list_.next_ = free_list_.previous_ = free_chunk; |
| 115 free_chunk->next_ = free_chunk->previous_ = &free_list_; | 119 free_chunk->next_ = free_chunk->previous_ = &free_list_; |
| 116 free_chunk->size_ = size - sizeof(PreallocatedStorage); | 120 free_chunk->size_ = size - sizeof(PreallocatedStorage); |
| 117 preallocated_ = true; | 121 storage_data.preallocated_ = true; |
| 118 } | 122 } |
| 119 | 123 |
| 120 | 124 |
| 121 void* PreallocatedStorage::New(size_t size) { | 125 void* PreallocatedStorage::New(size_t size) { |
| 122 if (!preallocated_) { | 126 StorageData& storage_data = v8_context()->storage_data_; |
| 127 |
| 128 if (!storage_data.preallocated_) { |
| 123 return FreeStoreAllocationPolicy::New(size); | 129 return FreeStoreAllocationPolicy::New(size); |
| 124 } | 130 } |
| 131 |
| 132 PreallocatedStorage & free_list_ = storage_data.free_list_; |
| 133 PreallocatedStorage & in_use_list_ = storage_data.in_use_list_; |
| 134 |
| 125 ASSERT(free_list_.next_ != &free_list_); | 135 ASSERT(free_list_.next_ != &free_list_); |
| 126 ASSERT(free_list_.previous_ != &free_list_); | 136 ASSERT(free_list_.previous_ != &free_list_); |
| 127 size = (size + kPointerSize - 1) & ~(kPointerSize - 1); | 137 size = (size + kPointerSize - 1) & ~(kPointerSize - 1); |
| 128 // Search for exact fit. | 138 // Search for exact fit. |
| 129 for (PreallocatedStorage* storage = free_list_.next_; | 139 for (PreallocatedStorage* storage = free_list_.next_; |
| 130 storage != &free_list_; | 140 storage != &free_list_; |
| 131 storage = storage->next_) { | 141 storage = storage->next_) { |
| 132 if (storage->size_ == size) { | 142 if (storage->size_ == size) { |
| 133 storage->Unlink(); | 143 storage->Unlink(); |
| 134 storage->LinkTo(&in_use_list_); | 144 storage->LinkTo(&in_use_list_); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 157 ASSERT(false); | 167 ASSERT(false); |
| 158 return NULL; | 168 return NULL; |
| 159 } | 169 } |
| 160 | 170 |
| 161 | 171 |
| 162 // We don't attempt to coalesce. | 172 // We don't attempt to coalesce. |
| 163 void PreallocatedStorage::Delete(void* p) { | 173 void PreallocatedStorage::Delete(void* p) { |
| 164 if (p == NULL) { | 174 if (p == NULL) { |
| 165 return; | 175 return; |
| 166 } | 176 } |
| 167 if (!preallocated_) { | 177 StorageData& storage_data = v8_context()->storage_data_; |
| 178 if (!storage_data.preallocated_) { |
| 168 FreeStoreAllocationPolicy::Delete(p); | 179 FreeStoreAllocationPolicy::Delete(p); |
| 169 return; | 180 return; |
| 170 } | 181 } |
| 171 PreallocatedStorage* storage = reinterpret_cast<PreallocatedStorage*>(p) - 1; | 182 PreallocatedStorage* storage = reinterpret_cast<PreallocatedStorage*>(p) - 1; |
| 172 ASSERT(storage->next_->previous_ == storage); | 183 ASSERT(storage->next_->previous_ == storage); |
| 173 ASSERT(storage->previous_->next_ == storage); | 184 ASSERT(storage->previous_->next_ == storage); |
| 174 storage->Unlink(); | 185 storage->Unlink(); |
| 175 storage->LinkTo(&free_list_); | 186 storage->LinkTo(&storage_data.free_list_); |
| 176 } | 187 } |
| 177 | 188 |
| 178 | 189 |
| 179 void PreallocatedStorage::LinkTo(PreallocatedStorage* other) { | 190 void PreallocatedStorage::LinkTo(PreallocatedStorage* other) { |
| 180 next_ = other->next_; | 191 next_ = other->next_; |
| 181 other->next_->previous_ = this; | 192 other->next_->previous_ = this; |
| 182 previous_ = other; | 193 previous_ = other; |
| 183 other->next_ = this; | 194 other->next_ = this; |
| 184 } | 195 } |
| 185 | 196 |
| 186 | 197 |
| 187 void PreallocatedStorage::Unlink() { | 198 void PreallocatedStorage::Unlink() { |
| 188 next_->previous_ = previous_; | 199 next_->previous_ = previous_; |
| 189 previous_->next_ = next_; | 200 previous_->next_ = next_; |
| 190 } | 201 } |
| 191 | 202 |
| 192 | 203 |
| 193 PreallocatedStorage::PreallocatedStorage(size_t size) | 204 PreallocatedStorage::PreallocatedStorage(size_t size) |
| 194 : size_(size) { | 205 : size_(size) { |
| 195 previous_ = next_ = this; | 206 previous_ = next_ = this; |
| 196 } | 207 } |
| 197 | 208 |
| 198 } } // namespace v8::internal | 209 } } // namespace v8::internal |
| OLD | NEW |