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 |
11 // with the distribution. | 11 // with the distribution. |
12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
15 // | 15 // |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
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 #include <stdlib.h> | 28 #include "v8.h" |
| 29 #include "isolate.h" |
| 30 #include "allocation.h" |
29 | 31 |
| 32 /* TODO(isolates): this is what's included in bleeding_edge |
| 33 including of v8.h was replaced with these in |
| 34 http://codereview.chromium.org/5005001/ |
| 35 we need Isolate and Isolate needs a lot more so I'm including v8.h back. |
30 #include "../include/v8stdint.h" | 36 #include "../include/v8stdint.h" |
31 #include "globals.h" | 37 #include "globals.h" |
32 #include "checks.h" | 38 #include "checks.h" |
33 #include "allocation.h" | 39 #include "allocation.h" |
34 #include "utils.h" | 40 #include "utils.h" |
| 41 */ |
35 | 42 |
36 namespace v8 { | 43 namespace v8 { |
37 namespace internal { | 44 namespace internal { |
38 | 45 |
| 46 #ifdef DEBUG |
| 47 |
| 48 NativeAllocationChecker::NativeAllocationChecker( |
| 49 NativeAllocationChecker::NativeAllocationAllowed allowed) |
| 50 : allowed_(allowed) { |
| 51 if (allowed == DISALLOW) { |
| 52 Isolate* isolate = Isolate::Current(); |
| 53 isolate->set_allocation_disallowed(isolate->allocation_disallowed() + 1); |
| 54 } |
| 55 } |
| 56 |
| 57 |
| 58 NativeAllocationChecker::~NativeAllocationChecker() { |
| 59 Isolate* isolate = Isolate::Current(); |
| 60 if (allowed_ == DISALLOW) { |
| 61 isolate->set_allocation_disallowed(isolate->allocation_disallowed() - 1); |
| 62 } |
| 63 ASSERT(isolate->allocation_disallowed() >= 0); |
| 64 } |
| 65 |
| 66 |
| 67 bool NativeAllocationChecker::allocation_allowed() { |
| 68 // TODO(isolates): either find a way to make this work that doesn't |
| 69 // require initializing an isolate before we can use malloc or drop |
| 70 // it completely. |
| 71 return true; |
| 72 // return Isolate::Current()->allocation_disallowed() == 0; |
| 73 } |
| 74 |
| 75 #endif // DEBUG |
| 76 |
| 77 |
39 void* Malloced::New(size_t size) { | 78 void* Malloced::New(size_t size) { |
40 ASSERT(NativeAllocationChecker::allocation_allowed()); | 79 ASSERT(NativeAllocationChecker::allocation_allowed()); |
41 void* result = malloc(size); | 80 void* result = malloc(size); |
42 if (result == NULL) { | 81 if (result == NULL) { |
43 v8::internal::FatalProcessOutOfMemory("Malloced operator new"); | 82 v8::internal::FatalProcessOutOfMemory("Malloced operator new"); |
44 } | 83 } |
45 return result; | 84 return result; |
46 } | 85 } |
47 | 86 |
48 | 87 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 char* StrNDup(const char* str, int n) { | 135 char* StrNDup(const char* str, int n) { |
97 int length = StrLength(str); | 136 int length = StrLength(str); |
98 if (n < length) length = n; | 137 if (n < length) length = n; |
99 char* result = NewArray<char>(length + 1); | 138 char* result = NewArray<char>(length + 1); |
100 memcpy(result, str, length); | 139 memcpy(result, str, length); |
101 result[length] = '\0'; | 140 result[length] = '\0'; |
102 return result; | 141 return result; |
103 } | 142 } |
104 | 143 |
105 | 144 |
106 int NativeAllocationChecker::allocation_disallowed_ = 0; | 145 void Isolate::PreallocatedStorageInit(size_t size) { |
107 | |
108 | |
109 PreallocatedStorage PreallocatedStorage::in_use_list_(0); | |
110 PreallocatedStorage PreallocatedStorage::free_list_(0); | |
111 bool PreallocatedStorage::preallocated_ = false; | |
112 | |
113 | |
114 void PreallocatedStorage::Init(size_t size) { | |
115 ASSERT(free_list_.next_ == &free_list_); | 146 ASSERT(free_list_.next_ == &free_list_); |
116 ASSERT(free_list_.previous_ == &free_list_); | 147 ASSERT(free_list_.previous_ == &free_list_); |
117 PreallocatedStorage* free_chunk = | 148 PreallocatedStorage* free_chunk = |
118 reinterpret_cast<PreallocatedStorage*>(new char[size]); | 149 reinterpret_cast<PreallocatedStorage*>(new char[size]); |
119 free_list_.next_ = free_list_.previous_ = free_chunk; | 150 free_list_.next_ = free_list_.previous_ = free_chunk; |
120 free_chunk->next_ = free_chunk->previous_ = &free_list_; | 151 free_chunk->next_ = free_chunk->previous_ = &free_list_; |
121 free_chunk->size_ = size - sizeof(PreallocatedStorage); | 152 free_chunk->size_ = size - sizeof(PreallocatedStorage); |
122 preallocated_ = true; | 153 preallocated_storage_preallocated_ = true; |
123 } | 154 } |
124 | 155 |
125 | 156 |
126 void* PreallocatedStorage::New(size_t size) { | 157 void* Isolate::PreallocatedStorageNew(size_t size) { |
127 if (!preallocated_) { | 158 if (!preallocated_storage_preallocated_) { |
128 return FreeStoreAllocationPolicy::New(size); | 159 return FreeStoreAllocationPolicy::New(size); |
129 } | 160 } |
130 ASSERT(free_list_.next_ != &free_list_); | 161 ASSERT(free_list_.next_ != &free_list_); |
131 ASSERT(free_list_.previous_ != &free_list_); | 162 ASSERT(free_list_.previous_ != &free_list_); |
132 | 163 |
133 size = (size + kPointerSize - 1) & ~(kPointerSize - 1); | 164 size = (size + kPointerSize - 1) & ~(kPointerSize - 1); |
134 // Search for exact fit. | 165 // Search for exact fit. |
135 for (PreallocatedStorage* storage = free_list_.next_; | 166 for (PreallocatedStorage* storage = free_list_.next_; |
136 storage != &free_list_; | 167 storage != &free_list_; |
137 storage = storage->next_) { | 168 storage = storage->next_) { |
(...skipping 21 matching lines...) Expand all Loading... |
159 return reinterpret_cast<void*>(storage + 1); | 190 return reinterpret_cast<void*>(storage + 1); |
160 } | 191 } |
161 } | 192 } |
162 // Allocation failure. | 193 // Allocation failure. |
163 ASSERT(false); | 194 ASSERT(false); |
164 return NULL; | 195 return NULL; |
165 } | 196 } |
166 | 197 |
167 | 198 |
168 // We don't attempt to coalesce. | 199 // We don't attempt to coalesce. |
169 void PreallocatedStorage::Delete(void* p) { | 200 void Isolate::PreallocatedStorageDelete(void* p) { |
170 if (p == NULL) { | 201 if (p == NULL) { |
171 return; | 202 return; |
172 } | 203 } |
173 if (!preallocated_) { | 204 if (!preallocated_storage_preallocated_) { |
174 FreeStoreAllocationPolicy::Delete(p); | 205 FreeStoreAllocationPolicy::Delete(p); |
175 return; | 206 return; |
176 } | 207 } |
177 PreallocatedStorage* storage = reinterpret_cast<PreallocatedStorage*>(p) - 1; | 208 PreallocatedStorage* storage = reinterpret_cast<PreallocatedStorage*>(p) - 1; |
178 ASSERT(storage->next_->previous_ == storage); | 209 ASSERT(storage->next_->previous_ == storage); |
179 ASSERT(storage->previous_->next_ == storage); | 210 ASSERT(storage->previous_->next_ == storage); |
180 storage->Unlink(); | 211 storage->Unlink(); |
181 storage->LinkTo(&free_list_); | 212 storage->LinkTo(&free_list_); |
182 } | 213 } |
183 | 214 |
(...skipping 11 matching lines...) Expand all Loading... |
195 previous_->next_ = next_; | 226 previous_->next_ = next_; |
196 } | 227 } |
197 | 228 |
198 | 229 |
199 PreallocatedStorage::PreallocatedStorage(size_t size) | 230 PreallocatedStorage::PreallocatedStorage(size_t size) |
200 : size_(size) { | 231 : size_(size) { |
201 previous_ = next_ = this; | 232 previous_ = next_ = this; |
202 } | 233 } |
203 | 234 |
204 } } // namespace v8::internal | 235 } } // namespace v8::internal |
OLD | NEW |