OLD | NEW |
---|---|
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
171 void Zone::DeleteSegment(Segment* segment, int size) { | 171 void Zone::DeleteSegment(Segment* segment, int size) { |
172 adjust_segment_bytes_allocated(-size); | 172 adjust_segment_bytes_allocated(-size); |
173 Malloced::Delete(segment); | 173 Malloced::Delete(segment); |
174 } | 174 } |
175 | 175 |
176 | 176 |
177 Address Zone::NewExpand(int size) { | 177 Address Zone::NewExpand(int size) { |
178 // Make sure the requested size is already properly aligned and that | 178 // Make sure the requested size is already properly aligned and that |
179 // there isn't enough room in the Zone to satisfy the request. | 179 // there isn't enough room in the Zone to satisfy the request. |
180 ASSERT(size == RoundDown(size, kAlignment)); | 180 ASSERT(size == RoundDown(size, kAlignment)); |
181 ASSERT(size > limit_ - position_); | 181 ASSERT(size > limit_ - position_); |
Jakob Kummerow
2013/12/16 15:30:34
this subtraction is undefined behavior too
| |
182 | 182 |
183 // Compute the new segment size. We use a 'high water mark' | 183 // Compute the new segment size. We use a 'high water mark' |
184 // strategy, where we increase the segment size every time we expand | 184 // strategy, where we increase the segment size every time we expand |
185 // except that we employ a maximum segment size when we delete. This | 185 // except that we employ a maximum segment size when we delete. This |
186 // is to avoid excessive malloc() and free() overhead. | 186 // is to avoid excessive malloc() and free() overhead. |
187 Segment* head = segment_head_; | 187 Segment* head = segment_head_; |
188 int old_size = (head == NULL) ? 0 : head->size(); | 188 const size_t old_size = (head == NULL) ? 0 : head->size(); |
189 static const int kSegmentOverhead = sizeof(Segment) + kAlignment; | 189 static const size_t kSegmentOverhead = sizeof(Segment) + kAlignment; |
190 int new_size_no_overhead = size + (old_size << 1); | 190 const size_t new_size_no_overhead = size + (old_size << 1); |
191 int new_size = kSegmentOverhead + new_size_no_overhead; | 191 size_t new_size = kSegmentOverhead + new_size_no_overhead; |
192 const size_t min_new_size = kSegmentOverhead + static_cast<size_t>(size); | |
192 // Guard against integer overflow. | 193 // Guard against integer overflow. |
193 if (new_size_no_overhead < size || new_size < kSegmentOverhead) { | 194 if (new_size_no_overhead < static_cast<size_t>(size) || |
195 new_size < static_cast<size_t>(kSegmentOverhead)) { | |
194 V8::FatalProcessOutOfMemory("Zone"); | 196 V8::FatalProcessOutOfMemory("Zone"); |
195 return NULL; | 197 return NULL; |
196 } | 198 } |
197 if (new_size < kMinimumSegmentSize) { | 199 if (new_size < static_cast<size_t>(kMinimumSegmentSize)) { |
198 new_size = kMinimumSegmentSize; | 200 new_size = kMinimumSegmentSize; |
199 } else if (new_size > kMaximumSegmentSize) { | 201 } else if (new_size > static_cast<size_t>(kMaximumSegmentSize)) { |
200 // Limit the size of new segments to avoid growing the segment size | 202 // Limit the size of new segments to avoid growing the segment size |
201 // exponentially, thus putting pressure on contiguous virtual address space. | 203 // exponentially, thus putting pressure on contiguous virtual address space. |
202 // All the while making sure to allocate a segment large enough to hold the | 204 // All the while making sure to allocate a segment large enough to hold the |
203 // requested size. | 205 // requested size. |
204 new_size = Max(kSegmentOverhead + size, kMaximumSegmentSize); | 206 new_size = Max(min_new_size, static_cast<size_t>(kMaximumSegmentSize)); |
205 } | 207 } |
206 Segment* segment = NewSegment(new_size); | 208 if (new_size > INT_MAX) { |
209 V8::FatalProcessOutOfMemory("Zone"); | |
210 return NULL; | |
211 } | |
212 Segment* segment = NewSegment(static_cast<int>(new_size)); | |
207 if (segment == NULL) { | 213 if (segment == NULL) { |
208 V8::FatalProcessOutOfMemory("Zone"); | 214 V8::FatalProcessOutOfMemory("Zone"); |
209 return NULL; | 215 return NULL; |
210 } | 216 } |
211 | 217 |
212 // Recompute 'top' and 'limit' based on the new segment. | 218 // Recompute 'top' and 'limit' based on the new segment. |
213 Address result = RoundUp(segment->start(), kAlignment); | 219 Address result = RoundUp(segment->start(), kAlignment); |
214 position_ = result + size; | 220 position_ = result + size; |
215 // Check for address overflow. | 221 // Check for address overflow. |
216 if (position_ < result) { | 222 // (Should not happen since the segment is guaranteed to accomodate |
223 // size bytes + header and alignment padding) | |
224 if (reinterpret_cast<uintptr_t>(position_) | |
225 < reinterpret_cast<uintptr_t>(result)) { | |
217 V8::FatalProcessOutOfMemory("Zone"); | 226 V8::FatalProcessOutOfMemory("Zone"); |
218 return NULL; | 227 return NULL; |
219 } | 228 } |
220 limit_ = segment->end(); | 229 limit_ = segment->end(); |
221 ASSERT(position_ <= limit_); | 230 ASSERT(position_ <= limit_); |
Jakob Kummerow
2013/12/16 15:30:34
this comparison is undefined behavior too
| |
222 return result; | 231 return result; |
223 } | 232 } |
224 | 233 |
225 | 234 |
226 } } // namespace v8::internal | 235 } } // namespace v8::internal |
OLD | NEW |