OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 DeleteSegment(segment_head_, segment_head_->size()); | 161 DeleteSegment(segment_head_, segment_head_->size()); |
162 segment_head_ = NULL; | 162 segment_head_ = NULL; |
163 } | 163 } |
164 } | 164 } |
165 | 165 |
166 | 166 |
167 Address Zone::NewExpand(int size) { | 167 Address Zone::NewExpand(int size) { |
168 // Make sure the requested size is already properly aligned and that | 168 // Make sure the requested size is already properly aligned and that |
169 // there isn't enough room in the Zone to satisfy the request. | 169 // there isn't enough room in the Zone to satisfy the request. |
170 ASSERT(size == RoundDown(size, kAlignment)); | 170 ASSERT(size == RoundDown(size, kAlignment)); |
171 ASSERT(position_ + size > limit_); | 171 ASSERT(size > limit_ - position_); |
172 | 172 |
173 // Compute the new segment size. We use a 'high water mark' | 173 // Compute the new segment size. We use a 'high water mark' |
174 // strategy, where we increase the segment size every time we expand | 174 // strategy, where we increase the segment size every time we expand |
175 // except that we employ a maximum segment size when we delete. This | 175 // except that we employ a maximum segment size when we delete. This |
176 // is to avoid excessive malloc() and free() overhead. | 176 // is to avoid excessive malloc() and free() overhead. |
177 Segment* head = segment_head_; | 177 Segment* head = segment_head_; |
178 int old_size = (head == NULL) ? 0 : head->size(); | 178 int old_size = (head == NULL) ? 0 : head->size(); |
179 static const int kSegmentOverhead = sizeof(Segment) + kAlignment; | 179 static const int kSegmentOverhead = sizeof(Segment) + kAlignment; |
180 int new_size = kSegmentOverhead + size + (old_size << 1); | 180 int new_size_no_overhead = size + (old_size << 1); |
| 181 int new_size = kSegmentOverhead + new_size_no_overhead; |
| 182 // Guard against integer overflow. |
| 183 if (new_size_no_overhead < size || new_size < kSegmentOverhead) { |
| 184 V8::FatalProcessOutOfMemory("Zone"); |
| 185 return NULL; |
| 186 } |
181 if (new_size < kMinimumSegmentSize) { | 187 if (new_size < kMinimumSegmentSize) { |
182 new_size = kMinimumSegmentSize; | 188 new_size = kMinimumSegmentSize; |
183 } else if (new_size > kMaximumSegmentSize) { | 189 } else if (new_size > kMaximumSegmentSize) { |
184 // Limit the size of new segments to avoid growing the segment size | 190 // Limit the size of new segments to avoid growing the segment size |
185 // exponentially, thus putting pressure on contiguous virtual address space. | 191 // exponentially, thus putting pressure on contiguous virtual address space. |
186 // All the while making sure to allocate a segment large enough to hold the | 192 // All the while making sure to allocate a segment large enough to hold the |
187 // requested size. | 193 // requested size. |
188 new_size = Max(kSegmentOverhead + size, kMaximumSegmentSize); | 194 new_size = Max(kSegmentOverhead + size, kMaximumSegmentSize); |
189 } | 195 } |
190 Segment* segment = NewSegment(new_size); | 196 Segment* segment = NewSegment(new_size); |
191 if (segment == NULL) { | 197 if (segment == NULL) { |
192 V8::FatalProcessOutOfMemory("Zone"); | 198 V8::FatalProcessOutOfMemory("Zone"); |
193 return NULL; | 199 return NULL; |
194 } | 200 } |
195 | 201 |
196 // Recompute 'top' and 'limit' based on the new segment. | 202 // Recompute 'top' and 'limit' based on the new segment. |
197 Address result = RoundUp(segment->start(), kAlignment); | 203 Address result = RoundUp(segment->start(), kAlignment); |
198 position_ = result + size; | 204 position_ = result + size; |
| 205 // Check for address overflow. |
| 206 if (position_ < result) { |
| 207 V8::FatalProcessOutOfMemory("Zone"); |
| 208 return NULL; |
| 209 } |
199 limit_ = segment->end(); | 210 limit_ = segment->end(); |
200 ASSERT(position_ <= limit_); | 211 ASSERT(position_ <= limit_); |
201 return result; | 212 return result; |
202 } | 213 } |
203 | 214 |
204 | 215 |
205 } } // namespace v8::internal | 216 } } // namespace v8::internal |
OLD | NEW |