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 "v8.h" | |
29 #include "isolate.h" | |
30 #include "allocation.h" | |
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. | |
36 #include "../include/v8stdint.h" | 28 #include "../include/v8stdint.h" |
37 #include "globals.h" | 29 #include "globals.h" |
38 #include "checks.h" | 30 #include "checks.h" |
39 #include "allocation.h" | 31 #include "allocation.h" |
40 #include "utils.h" | 32 #include "utils.h" |
41 */ | |
42 | 33 |
43 namespace v8 { | 34 namespace v8 { |
44 namespace internal { | 35 namespace internal { |
45 | 36 |
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 | |
78 void* Malloced::New(size_t size) { | 37 void* Malloced::New(size_t size) { |
79 ASSERT(NativeAllocationChecker::allocation_allowed()); | |
80 void* result = malloc(size); | 38 void* result = malloc(size); |
81 if (result == NULL) { | 39 if (result == NULL) { |
82 v8::internal::FatalProcessOutOfMemory("Malloced operator new"); | 40 v8::internal::FatalProcessOutOfMemory("Malloced operator new"); |
83 } | 41 } |
84 return result; | 42 return result; |
85 } | 43 } |
86 | 44 |
87 | 45 |
88 void Malloced::Delete(void* p) { | 46 void Malloced::Delete(void* p) { |
89 free(p); | 47 free(p); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 char* StrNDup(const char* str, int n) { | 93 char* StrNDup(const char* str, int n) { |
136 int length = StrLength(str); | 94 int length = StrLength(str); |
137 if (n < length) length = n; | 95 if (n < length) length = n; |
138 char* result = NewArray<char>(length + 1); | 96 char* result = NewArray<char>(length + 1); |
139 memcpy(result, str, length); | 97 memcpy(result, str, length); |
140 result[length] = '\0'; | 98 result[length] = '\0'; |
141 return result; | 99 return result; |
142 } | 100 } |
143 | 101 |
144 | 102 |
145 void Isolate::PreallocatedStorageInit(size_t size) { | |
146 ASSERT(free_list_.next_ == &free_list_); | |
147 ASSERT(free_list_.previous_ == &free_list_); | |
148 PreallocatedStorage* free_chunk = | |
149 reinterpret_cast<PreallocatedStorage*>(new char[size]); | |
150 free_list_.next_ = free_list_.previous_ = free_chunk; | |
151 free_chunk->next_ = free_chunk->previous_ = &free_list_; | |
152 free_chunk->size_ = size - sizeof(PreallocatedStorage); | |
153 preallocated_storage_preallocated_ = true; | |
154 } | |
155 | |
156 | |
157 void* Isolate::PreallocatedStorageNew(size_t size) { | |
158 if (!preallocated_storage_preallocated_) { | |
159 return FreeStoreAllocationPolicy::New(size); | |
160 } | |
161 ASSERT(free_list_.next_ != &free_list_); | |
162 ASSERT(free_list_.previous_ != &free_list_); | |
163 | |
164 size = (size + kPointerSize - 1) & ~(kPointerSize - 1); | |
165 // Search for exact fit. | |
166 for (PreallocatedStorage* storage = free_list_.next_; | |
167 storage != &free_list_; | |
168 storage = storage->next_) { | |
169 if (storage->size_ == size) { | |
170 storage->Unlink(); | |
171 storage->LinkTo(&in_use_list_); | |
172 return reinterpret_cast<void*>(storage + 1); | |
173 } | |
174 } | |
175 // Search for first fit. | |
176 for (PreallocatedStorage* storage = free_list_.next_; | |
177 storage != &free_list_; | |
178 storage = storage->next_) { | |
179 if (storage->size_ >= size + sizeof(PreallocatedStorage)) { | |
180 storage->Unlink(); | |
181 storage->LinkTo(&in_use_list_); | |
182 PreallocatedStorage* left_over = | |
183 reinterpret_cast<PreallocatedStorage*>( | |
184 reinterpret_cast<char*>(storage + 1) + size); | |
185 left_over->size_ = storage->size_ - size - sizeof(PreallocatedStorage); | |
186 ASSERT(size + left_over->size_ + sizeof(PreallocatedStorage) == | |
187 storage->size_); | |
188 storage->size_ = size; | |
189 left_over->LinkTo(&free_list_); | |
190 return reinterpret_cast<void*>(storage + 1); | |
191 } | |
192 } | |
193 // Allocation failure. | |
194 ASSERT(false); | |
195 return NULL; | |
196 } | |
197 | |
198 | |
199 // We don't attempt to coalesce. | |
200 void Isolate::PreallocatedStorageDelete(void* p) { | |
201 if (p == NULL) { | |
202 return; | |
203 } | |
204 if (!preallocated_storage_preallocated_) { | |
205 FreeStoreAllocationPolicy::Delete(p); | |
206 return; | |
207 } | |
208 PreallocatedStorage* storage = reinterpret_cast<PreallocatedStorage*>(p) - 1; | |
209 ASSERT(storage->next_->previous_ == storage); | |
210 ASSERT(storage->previous_->next_ == storage); | |
211 storage->Unlink(); | |
212 storage->LinkTo(&free_list_); | |
213 } | |
214 | |
215 | |
216 void PreallocatedStorage::LinkTo(PreallocatedStorage* other) { | 103 void PreallocatedStorage::LinkTo(PreallocatedStorage* other) { |
217 next_ = other->next_; | 104 next_ = other->next_; |
218 other->next_->previous_ = this; | 105 other->next_->previous_ = this; |
219 previous_ = other; | 106 previous_ = other; |
220 other->next_ = this; | 107 other->next_ = this; |
221 } | 108 } |
222 | 109 |
223 | 110 |
224 void PreallocatedStorage::Unlink() { | 111 void PreallocatedStorage::Unlink() { |
225 next_->previous_ = previous_; | 112 next_->previous_ = previous_; |
226 previous_->next_ = next_; | 113 previous_->next_ = next_; |
227 } | 114 } |
228 | 115 |
229 | 116 |
230 PreallocatedStorage::PreallocatedStorage(size_t size) | 117 PreallocatedStorage::PreallocatedStorage(size_t size) |
231 : size_(size) { | 118 : size_(size) { |
232 previous_ = next_ = this; | 119 previous_ = next_ = this; |
233 } | 120 } |
234 | 121 |
235 } } // namespace v8::internal | 122 } } // namespace v8::internal |
OLD | NEW |