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