| 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 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 : allocation_size_(0), | 71 : allocation_size_(0), |
| 72 segment_bytes_allocated_(0), | 72 segment_bytes_allocated_(0), |
| 73 position_(0), | 73 position_(0), |
| 74 limit_(0), | 74 limit_(0), |
| 75 segment_head_(NULL), | 75 segment_head_(NULL), |
| 76 isolate_(isolate) { | 76 isolate_(isolate) { |
| 77 } | 77 } |
| 78 | 78 |
| 79 | 79 |
| 80 Zone::~Zone() { | 80 Zone::~Zone() { |
| 81 DeleteAll(); |
| 82 DeleteKeptSegment(); |
| 83 |
| 84 ASSERT(segment_bytes_allocated_ == 0); |
| 85 } |
| 86 |
| 87 |
| 88 void Zone::DeleteAll() { |
| 81 #ifdef DEBUG | 89 #ifdef DEBUG |
| 82 // Constant byte value used for zapping dead memory in debug mode. | 90 // Constant byte value used for zapping dead memory in debug mode. |
| 83 static const unsigned char kZapDeadByte = 0xcd; | 91 static const unsigned char kZapDeadByte = 0xcd; |
| 84 #endif | 92 #endif |
| 85 | 93 |
| 86 // Traverse the chained list of segments, zapping | 94 // Find a segment with a suitable size to keep around. |
| 87 // (in debug mode) and freeing every segment | 95 Segment* keep = segment_head_; |
| 88 Segment* current = segment_head_; | 96 while (keep != NULL && keep->size() > kMaximumKeptSegmentSize) { |
| 89 while (current != NULL) { | 97 keep = keep->next(); |
| 98 } |
| 99 |
| 100 // Traverse the chained list of segments, zapping (in debug mode) |
| 101 // and freeing every segment except the one we wish to keep. |
| 102 for (Segment* current = segment_head_; current != NULL; ) { |
| 90 Segment* next = current->next(); | 103 Segment* next = current->next(); |
| 91 int size = current->size(); | 104 if (current == keep) { |
| 105 // Unlink the segment we wish to keep from the list. |
| 106 current->clear_next(); |
| 107 } else { |
| 108 int size = current->size(); |
| 92 #ifdef DEBUG | 109 #ifdef DEBUG |
| 93 // Zap the entire current segment (including the header). | 110 // Zap the entire current segment (including the header). |
| 94 memset(current, kZapDeadByte, size); | 111 memset(current, kZapDeadByte, size); |
| 95 #endif | 112 #endif |
| 96 DeleteSegment(current, size); | 113 DeleteSegment(current, size); |
| 114 } |
| 97 current = next; | 115 current = next; |
| 98 } | 116 } |
| 99 | 117 |
| 100 // We must clear the position and limit to force | 118 // If we have found a segment we want to keep, we must recompute the |
| 101 // a new segment to be allocated on demand. | 119 // variables 'position' and 'limit' to prepare for future allocate |
| 102 position_ = limit_ = 0; | 120 // attempts. Otherwise, we must clear the position and limit to |
| 121 // force a new segment to be allocated on demand. |
| 122 if (keep != NULL) { |
| 123 Address start = keep->start(); |
| 124 position_ = RoundUp(start, kAlignment); |
| 125 limit_ = keep->end(); |
| 126 #ifdef DEBUG |
| 127 // Zap the contents of the kept segment (but not the header). |
| 128 memset(start, kZapDeadByte, keep->capacity()); |
| 129 #endif |
| 130 } else { |
| 131 position_ = limit_ = 0; |
| 132 } |
| 103 | 133 |
| 104 // Update the head segment. | 134 // Update the head segment to be the kept segment (if any). |
| 105 segment_head_ = NULL; | 135 segment_head_ = keep; |
| 106 } | 136 } |
| 107 | 137 |
| 108 | 138 |
| 139 void Zone::DeleteKeptSegment() { |
| 140 #ifdef DEBUG |
| 141 // Constant byte value used for zapping dead memory in debug mode. |
| 142 static const unsigned char kZapDeadByte = 0xcd; |
| 143 #endif |
| 144 |
| 145 ASSERT(segment_head_ == NULL || segment_head_->next() == NULL); |
| 146 if (segment_head_ != NULL) { |
| 147 int size = segment_head_->size(); |
| 148 #ifdef DEBUG |
| 149 // Zap the entire kept segment (including the header). |
| 150 memset(segment_head_, kZapDeadByte, size); |
| 151 #endif |
| 152 DeleteSegment(segment_head_, size); |
| 153 segment_head_ = NULL; |
| 154 } |
| 155 |
| 156 ASSERT(segment_bytes_allocated_ == 0); |
| 157 } |
| 158 |
| 159 |
| 109 // Creates a new segment, sets it size, and pushes it to the front | 160 // Creates a new segment, sets it size, and pushes it to the front |
| 110 // of the segment chain. Returns the new segment. | 161 // of the segment chain. Returns the new segment. |
| 111 Segment* Zone::NewSegment(int size) { | 162 Segment* Zone::NewSegment(int size) { |
| 112 Segment* result = reinterpret_cast<Segment*>(Malloced::New(size)); | 163 Segment* result = reinterpret_cast<Segment*>(Malloced::New(size)); |
| 113 adjust_segment_bytes_allocated(size); | 164 adjust_segment_bytes_allocated(size); |
| 114 if (result != NULL) { | 165 if (result != NULL) { |
| 115 result->Initialize(segment_head_, size); | 166 result->Initialize(segment_head_, size); |
| 116 segment_head_ = result; | 167 segment_head_ = result; |
| 117 } | 168 } |
| 118 return result; | 169 return result; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 V8::FatalProcessOutOfMemory("Zone"); | 220 V8::FatalProcessOutOfMemory("Zone"); |
| 170 return NULL; | 221 return NULL; |
| 171 } | 222 } |
| 172 limit_ = segment->end(); | 223 limit_ = segment->end(); |
| 173 ASSERT(position_ <= limit_); | 224 ASSERT(position_ <= limit_); |
| 174 return result; | 225 return result; |
| 175 } | 226 } |
| 176 | 227 |
| 177 | 228 |
| 178 } } // namespace v8::internal | 229 } } // namespace v8::internal |
| OLD | NEW |