| OLD | NEW |
| 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 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 16 matching lines...) Expand all Loading... |
| 27 | 27 |
| 28 #include "v8.h" | 28 #include "v8.h" |
| 29 | 29 |
| 30 #include "zone-inl.h" | 30 #include "zone-inl.h" |
| 31 | 31 |
| 32 namespace v8 { namespace internal { | 32 namespace v8 { namespace internal { |
| 33 | 33 |
| 34 | 34 |
| 35 Address Zone::position_ = 0; | 35 Address Zone::position_ = 0; |
| 36 Address Zone::limit_ = 0; | 36 Address Zone::limit_ = 0; |
| 37 int Zone::zone_excess_limit_ = 256 * MB; |
| 38 int Zone::segment_bytes_allocated_ = 0; |
| 37 | 39 |
| 38 bool AssertNoZoneAllocation::allow_allocation_ = true; | 40 bool AssertNoZoneAllocation::allow_allocation_ = true; |
| 39 | 41 |
| 40 int ZoneScope::nesting_ = 0; | 42 int ZoneScope::nesting_ = 0; |
| 41 | 43 |
| 42 // Segments represent chunks of memory: They have starting address | 44 // Segments represent chunks of memory: They have starting address |
| 43 // (encoded in the this pointer) and a size in bytes. Segments are | 45 // (encoded in the this pointer) and a size in bytes. Segments are |
| 44 // chained together forming a LIFO structure with the newest segment | 46 // chained together forming a LIFO structure with the newest segment |
| 45 // available as Segment::head(). Segments are allocated using malloc() | 47 // available as Segment::head(). Segments are allocated using malloc() |
| 46 // and de-allocated using free(). | 48 // and de-allocated using free(). |
| 47 | 49 |
| 48 class Segment { | 50 class Segment { |
| 49 public: | 51 public: |
| 50 Segment* next() const { return next_; } | 52 Segment* next() const { return next_; } |
| 51 void clear_next() { next_ = NULL; } | 53 void clear_next() { next_ = NULL; } |
| 52 | 54 |
| 53 int size() const { return size_; } | 55 int size() const { return size_; } |
| 54 int capacity() const { return size_ - sizeof(Segment); } | 56 int capacity() const { return size_ - sizeof(Segment); } |
| 55 | 57 |
| 56 Address start() const { return address(sizeof(Segment)); } | 58 Address start() const { return address(sizeof(Segment)); } |
| 57 Address end() const { return address(size_); } | 59 Address end() const { return address(size_); } |
| 58 | 60 |
| 59 static Segment* head() { return head_; } | 61 static Segment* head() { return head_; } |
| 60 static void set_head(Segment* head) { head_ = head; } | 62 static void set_head(Segment* head) { head_ = head; } |
| 61 | 63 |
| 62 // Creates a new segment, sets it size, and pushes it to the front | 64 // Creates a new segment, sets it size, and pushes it to the front |
| 63 // of the segment chain. Returns the new segment. | 65 // of the segment chain. Returns the new segment. |
| 64 static Segment* New(int size) { | 66 static Segment* New(int size) { |
| 65 Segment* result = reinterpret_cast<Segment*>(Malloced::New(size)); | 67 Segment* result = reinterpret_cast<Segment*>(Malloced::New(size)); |
| 68 Zone::segment_bytes_allocated_ += size; |
| 66 if (result != NULL) { | 69 if (result != NULL) { |
| 67 result->next_ = head_; | 70 result->next_ = head_; |
| 68 result->size_ = size; | 71 result->size_ = size; |
| 69 head_ = result; | 72 head_ = result; |
| 70 } | 73 } |
| 71 return result; | 74 return result; |
| 72 } | 75 } |
| 73 | 76 |
| 74 // Deletes the given segment. Does not touch the segment chain. | 77 // Deletes the given segment. Does not touch the segment chain. |
| 75 static void Delete(Segment* segment) { | 78 static void Delete(Segment* segment, int size) { |
| 79 Zone::segment_bytes_allocated_ -= size; |
| 76 Malloced::Delete(segment); | 80 Malloced::Delete(segment); |
| 77 } | 81 } |
| 78 | 82 |
| 83 static int bytes_allocated() { return bytes_allocated_; } |
| 84 |
| 79 private: | 85 private: |
| 80 // Computes the address of the nth byte in this segment. | 86 // Computes the address of the nth byte in this segment. |
| 81 Address address(int n) const { | 87 Address address(int n) const { |
| 82 return Address(this) + n; | 88 return Address(this) + n; |
| 83 } | 89 } |
| 84 | 90 |
| 85 static Segment* head_; | 91 static Segment* head_; |
| 92 static int bytes_allocated_; |
| 86 Segment* next_; | 93 Segment* next_; |
| 87 int size_; | 94 int size_; |
| 88 }; | 95 }; |
| 89 | 96 |
| 90 | 97 |
| 91 Segment* Segment::head_ = NULL; | 98 Segment* Segment::head_ = NULL; |
| 99 int Segment::bytes_allocated_ = 0; |
| 92 | 100 |
| 93 | 101 |
| 94 void Zone::DeleteAll() { | 102 void Zone::DeleteAll() { |
| 95 #ifdef DEBUG | 103 #ifdef DEBUG |
| 96 // Constant byte value used for zapping dead memory in debug mode. | 104 // Constant byte value used for zapping dead memory in debug mode. |
| 97 static const unsigned char kZapDeadByte = 0xcd; | 105 static const unsigned char kZapDeadByte = 0xcd; |
| 98 #endif | 106 #endif |
| 99 | 107 |
| 100 // Find a segment with a suitable size to keep around. | 108 // Find a segment with a suitable size to keep around. |
| 101 Segment* keep = Segment::head(); | 109 Segment* keep = Segment::head(); |
| 102 while (keep != NULL && keep->size() > kMaximumKeptSegmentSize) { | 110 while (keep != NULL && keep->size() > kMaximumKeptSegmentSize) { |
| 103 keep = keep->next(); | 111 keep = keep->next(); |
| 104 } | 112 } |
| 105 | 113 |
| 106 // Traverse the chained list of segments, zapping (in debug mode) | 114 // Traverse the chained list of segments, zapping (in debug mode) |
| 107 // and freeing every segment except the one we wish to keep. | 115 // and freeing every segment except the one we wish to keep. |
| 108 Segment* current = Segment::head(); | 116 Segment* current = Segment::head(); |
| 109 while (current != NULL) { | 117 while (current != NULL) { |
| 110 Segment* next = current->next(); | 118 Segment* next = current->next(); |
| 111 if (current == keep) { | 119 if (current == keep) { |
| 112 // Unlink the segment we wish to keep from the list. | 120 // Unlink the segment we wish to keep from the list. |
| 113 current->clear_next(); | 121 current->clear_next(); |
| 114 } else { | 122 } else { |
| 123 int size = current->size(); |
| 115 #ifdef DEBUG | 124 #ifdef DEBUG |
| 116 // Zap the entire current segment (including the header). | 125 // Zap the entire current segment (including the header). |
| 117 memset(current, kZapDeadByte, current->size()); | 126 memset(current, kZapDeadByte, size); |
| 118 #endif | 127 #endif |
| 119 Segment::Delete(current); | 128 Segment::Delete(current, size); |
| 120 } | 129 } |
| 121 current = next; | 130 current = next; |
| 122 } | 131 } |
| 123 | 132 |
| 124 // If we have found a segment we want to keep, we must recompute the | 133 // If we have found a segment we want to keep, we must recompute the |
| 125 // variables 'position' and 'limit' to prepare for future allocate | 134 // variables 'position' and 'limit' to prepare for future allocate |
| 126 // attempts. Otherwise, we must clear the position and limit to | 135 // attempts. Otherwise, we must clear the position and limit to |
| 127 // force a new segment to be allocated on demand. | 136 // force a new segment to be allocated on demand. |
| 128 if (keep != NULL) { | 137 if (keep != NULL) { |
| 129 Address start = keep->start(); | 138 Address start = keep->start(); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 // Recompute 'top' and 'limit' based on the new segment. | 171 // Recompute 'top' and 'limit' based on the new segment. |
| 163 Address result = RoundUp(segment->start(), kAlignment); | 172 Address result = RoundUp(segment->start(), kAlignment); |
| 164 position_ = result + size; | 173 position_ = result + size; |
| 165 limit_ = segment->end(); | 174 limit_ = segment->end(); |
| 166 ASSERT(position_ <= limit_); | 175 ASSERT(position_ <= limit_); |
| 167 return result; | 176 return result; |
| 168 } | 177 } |
| 169 | 178 |
| 170 | 179 |
| 171 } } // namespace v8::internal | 180 } } // namespace v8::internal |
| OLD | NEW |