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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
60 // Computes the address of the nth byte in this segment. | 60 // Computes the address of the nth byte in this segment. |
61 Address address(int n) const { | 61 Address address(int n) const { |
62 return Address(this) + n; | 62 return Address(this) + n; |
63 } | 63 } |
64 | 64 |
65 Segment* next_; | 65 Segment* next_; |
66 int size_; | 66 int size_; |
67 }; | 67 }; |
68 | 68 |
69 | 69 |
70 Zone::Zone() | 70 Zone::Zone(Isolate* isolate) |
71 : zone_excess_limit_(256 * MB), | 71 : zone_excess_limit_(256 * MB), |
72 segment_bytes_allocated_(0), | 72 segment_bytes_allocated_(0), |
73 position_(0), | 73 position_(0), |
74 limit_(0), | 74 limit_(0), |
75 scope_nesting_(0), | 75 scope_nesting_(0), |
76 segment_head_(NULL) { | 76 segment_head_(NULL), |
77 isolate_(isolate) { | |
77 } | 78 } |
78 unsigned Zone::allocation_size_ = 0; | 79 unsigned Zone::allocation_size_ = 0; |
79 | 80 |
80 ZoneScope::~ZoneScope() { | 81 ZoneScope::~ZoneScope() { |
81 ASSERT_EQ(Isolate::Current(), isolate_); | 82 if (ShouldDeleteOnExit()) zone_->Destroy(); |
82 if (ShouldDeleteOnExit()) isolate_->zone()->DeleteAll(); | 83 zone_->scope_nesting_--; |
83 isolate_->zone()->scope_nesting_--; | |
84 } | 84 } |
85 | 85 |
86 | 86 |
87 // Creates a new segment, sets it size, and pushes it to the front | 87 // Creates a new segment, sets it size, and pushes it to the front |
88 // of the segment chain. Returns the new segment. | 88 // of the segment chain. Returns the new segment. |
89 Segment* Zone::NewSegment(int size) { | 89 Segment* Zone::NewSegment(int size) { |
90 Segment* result = reinterpret_cast<Segment*>(Malloced::New(size)); | 90 Segment* result = reinterpret_cast<Segment*>(Malloced::New(size)); |
91 adjust_segment_bytes_allocated(size); | 91 adjust_segment_bytes_allocated(size); |
92 if (result != NULL) { | 92 if (result != NULL) { |
93 result->Initialize(segment_head_, size); | 93 result->Initialize(segment_head_, size); |
94 segment_head_ = result; | 94 segment_head_ = result; |
95 } | 95 } |
96 return result; | 96 return result; |
97 } | 97 } |
98 | 98 |
99 | 99 |
100 // Deletes the given segment. Does not touch the segment chain. | 100 // Deletes the given segment. Does not touch the segment chain. |
101 void Zone::DeleteSegment(Segment* segment, int size) { | 101 void Zone::DeleteSegment(Segment* segment, int size) { |
102 adjust_segment_bytes_allocated(-size); | 102 adjust_segment_bytes_allocated(-size); |
103 Malloced::Delete(segment); | 103 Malloced::Delete(segment); |
104 } | 104 } |
105 | 105 |
106 | 106 |
107 void Zone::Destroy() { | |
108 if (isolate_->runtime_zone() == this) | |
danno
2012/06/14 14:22:19
Yikes! Please move the DeleteAll(DELETE_ALL_BLOCKS
sanjoy
2012/06/15 09:24:31
Moved deletion to the destructor.
| |
109 DeleteAllButOne(); | |
110 else | |
111 DeleteAll(); | |
112 } | |
113 | |
114 | |
107 void Zone::DeleteAll() { | 115 void Zone::DeleteAll() { |
108 #ifdef DEBUG | 116 #ifdef DEBUG |
109 // Constant byte value used for zapping dead memory in debug mode. | 117 // Constant byte value used for zapping dead memory in debug mode. |
110 static const unsigned char kZapDeadByte = 0xcd; | 118 static const unsigned char kZapDeadByte = 0xcd; |
111 #endif | 119 #endif |
120 Segment* current = segment_head_; | |
121 while (current != NULL) { | |
122 Segment* next = current->next(); | |
123 int size = current->size(); | |
124 #ifdef DEBUG | |
125 // Zap the entire current segment (including the header). | |
126 memset(current, kZapDeadByte, size); | |
127 #endif | |
128 DeleteSegment(current, size); | |
129 current = next; | |
130 } | |
131 } | |
132 | |
133 | |
134 void Zone::DeleteAllButOne() { | |
danno
2012/06/14 14:22:19
Just add a enum flag to DeleteAll that handles the
sanjoy
2012/06/15 09:24:31
Moved deletion to the destructor.
| |
135 #ifdef DEBUG | |
136 // Constant byte value used for zapping dead memory in debug mode. | |
137 static const unsigned char kZapDeadByte = 0xcd; | |
138 #endif | |
112 | 139 |
113 // Find a segment with a suitable size to keep around. | 140 // Find a segment with a suitable size to keep around. |
114 Segment* keep = segment_head_; | 141 Segment* keep = segment_head_; |
115 while (keep != NULL && keep->size() > kMaximumKeptSegmentSize) { | 142 while (keep != NULL && keep->size() > kMaximumKeptSegmentSize) { |
116 keep = keep->next(); | 143 keep = keep->next(); |
117 } | 144 } |
118 | 145 |
119 // Traverse the chained list of segments, zapping (in debug mode) | 146 // Traverse the chained list of segments, zapping (in debug mode) |
120 // and freeing every segment except the one we wish to keep. | 147 // and freeing every segment except the one we wish to keep. |
121 Segment* current = segment_head_; | 148 Segment* current = segment_head_; |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
207 V8::FatalProcessOutOfMemory("Zone"); | 234 V8::FatalProcessOutOfMemory("Zone"); |
208 return NULL; | 235 return NULL; |
209 } | 236 } |
210 limit_ = segment->end(); | 237 limit_ = segment->end(); |
211 ASSERT(position_ <= limit_); | 238 ASSERT(position_ <= limit_); |
212 return result; | 239 return result; |
213 } | 240 } |
214 | 241 |
215 | 242 |
216 } } // namespace v8::internal | 243 } } // namespace v8::internal |
OLD | NEW |