Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "vm/zone.h" | 5 #include "vm/zone.h" |
| 6 | 6 |
| 7 #include "platform/assert.h" | 7 #include "platform/assert.h" |
| 8 #include "platform/utils.h" | 8 #include "platform/utils.h" |
| 9 #include "vm/dart_api_state.h" | 9 #include "vm/dart_api_state.h" |
| 10 #include "vm/flags.h" | 10 #include "vm/flags.h" |
| 11 #include "vm/handles_impl.h" | 11 #include "vm/handles_impl.h" |
| 12 #include "vm/heap.h" | 12 #include "vm/heap.h" |
| 13 #include "vm/os.h" | 13 #include "vm/os.h" |
| 14 | 14 |
| 15 namespace dart { | 15 namespace dart { |
| 16 | 16 |
| 17 // Zone segments represent chunks of memory: They have starting | 17 // Zone segments represent chunks of memory: They have starting |
| 18 // address encoded in the this pointer and a size in bytes. They are | 18 // address encoded in the this pointer and a size in bytes. They are |
| 19 // chained together to form the backing storage for an expanding zone. | 19 // chained together to form the backing storage for an expanding zone. |
| 20 class Zone::Segment { | 20 class Zone::Segment { |
| 21 public: | 21 public: |
| 22 Segment* next() const { return next_; } | 22 Segment* next() const { return next_; } |
| 23 intptr_t size() const { return size_; } | 23 intptr_t size() const { return memory_->size(); } |
| 24 | 24 |
| 25 uword start() { return address(sizeof(Segment)); } | 25 uword start() { return address(sizeof(Segment)); } |
| 26 uword end() { return address(size_); } | 26 uword end() { return address(size()); } |
|
zra
2017/06/26 20:30:00
Should this be address(size() - sizeof(Segment))?
rmacnak
2017/06/26 20:44:43
Hm, the size() calculate should exclude the Segeme
| |
| 27 | 27 |
| 28 // Allocate or delete individual segments. | 28 // Allocate or delete individual segments. |
| 29 static Segment* New(intptr_t size, Segment* next); | 29 static Segment* New(intptr_t size, Segment* next); |
| 30 static void DeleteSegmentList(Segment* segment); | 30 static void DeleteSegmentList(Segment* segment); |
| 31 static void IncrementMemoryCapacity(uintptr_t size); | 31 static void IncrementMemoryCapacity(uintptr_t size); |
| 32 static void DecrementMemoryCapacity(uintptr_t size); | 32 static void DecrementMemoryCapacity(uintptr_t size); |
| 33 | 33 |
| 34 private: | 34 private: |
| 35 VirtualMemory* memory_; | |
| 35 Segment* next_; | 36 Segment* next_; |
| 36 intptr_t size_; | |
| 37 | 37 |
| 38 // Computes the address of the nth byte in this segment. | 38 // Computes the address of the nth byte in this segment. |
| 39 uword address(int n) { return reinterpret_cast<uword>(this) + n; } | 39 uword address(int n) { return reinterpret_cast<uword>(this) + n; } |
| 40 | 40 |
| 41 static void Delete(Segment* segment) { free(segment); } | 41 static void Delete(Segment* segment) { delete segment->memory_; } |
| 42 | 42 |
| 43 DISALLOW_IMPLICIT_CONSTRUCTORS(Segment); | 43 DISALLOW_IMPLICIT_CONSTRUCTORS(Segment); |
| 44 }; | 44 }; |
| 45 | 45 |
| 46 | 46 |
| 47 Zone::Segment* Zone::Segment::New(intptr_t size, Zone::Segment* next) { | 47 Zone::Segment* Zone::Segment::New(intptr_t size, Zone::Segment* next) { |
| 48 ASSERT(size >= 0); | 48 ASSERT(size >= 0); |
| 49 Segment* result = reinterpret_cast<Segment*>(malloc(size)); | 49 size = Utils::RoundUp(size, VirtualMemory::PageSize()); |
| 50 if (result == NULL) { | 50 VirtualMemory* memory = VirtualMemory::Reserve(size); |
| 51 const bool kNotExecutable = false; | |
| 52 if ((memory == NULL) || !memory->Commit(kNotExecutable, "dart-zone")) { | |
| 51 OUT_OF_MEMORY(); | 53 OUT_OF_MEMORY(); |
| 52 } | 54 } |
| 55 Segment* result = reinterpret_cast<Segment*>(memory->address()); | |
| 53 ASSERT(Utils::IsAligned(result->start(), Zone::kAlignment)); | 56 ASSERT(Utils::IsAligned(result->start(), Zone::kAlignment)); |
| 54 #ifdef DEBUG | 57 #ifdef DEBUG |
| 55 // Zap the entire allocated segment (including the header). | 58 // Zap the entire allocated segment (including the header). |
| 56 memset(result, kZapUninitializedByte, size); | 59 memset(result, kZapUninitializedByte, size); |
| 57 #endif | 60 #endif |
| 61 result->memory_ = memory; | |
| 58 result->next_ = next; | 62 result->next_ = next; |
| 59 result->size_ = size; | |
| 60 IncrementMemoryCapacity(size); | 63 IncrementMemoryCapacity(size); |
| 61 return result; | 64 return result; |
| 62 } | 65 } |
| 63 | 66 |
| 64 | 67 |
| 65 void Zone::Segment::DeleteSegmentList(Segment* head) { | 68 void Zone::Segment::DeleteSegmentList(Segment* head) { |
| 66 Segment* current = head; | 69 Segment* current = head; |
| 67 while (current != NULL) { | 70 while (current != NULL) { |
| 68 DecrementMemoryCapacity(current->size()); | 71 DecrementMemoryCapacity(current->size()); |
| 69 Segment* next = current->next(); | 72 Segment* next = current->next(); |
| 70 #ifdef DEBUG | |
| 71 // Zap the entire current segment (including the header). | |
| 72 memset(current, kZapDeletedByte, current->size()); | |
| 73 #endif | |
| 74 Segment::Delete(current); | 73 Segment::Delete(current); |
| 75 current = next; | 74 current = next; |
| 76 } | 75 } |
| 77 } | 76 } |
| 78 | 77 |
| 79 | 78 |
| 80 void Zone::Segment::IncrementMemoryCapacity(uintptr_t size) { | 79 void Zone::Segment::IncrementMemoryCapacity(uintptr_t size) { |
| 81 Thread* current_thread = Thread::Current(); | 80 Thread* current_thread = Thread::Current(); |
| 82 if (current_thread != NULL) { | 81 if (current_thread != NULL) { |
| 83 current_thread->IncrementMemoryCapacity(size); | 82 current_thread->IncrementMemoryCapacity(size); |
| (...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 322 ASSERT(thread()->zone() == &zone_); | 321 ASSERT(thread()->zone() == &zone_); |
| 323 thread()->set_zone(zone_.previous_); | 322 thread()->set_zone(zone_.previous_); |
| 324 if (FLAG_trace_zones) { | 323 if (FLAG_trace_zones) { |
| 325 OS::PrintErr("*** Deleting Stack zone 0x%" Px "(0x%" Px ")\n", | 324 OS::PrintErr("*** Deleting Stack zone 0x%" Px "(0x%" Px ")\n", |
| 326 reinterpret_cast<intptr_t>(this), | 325 reinterpret_cast<intptr_t>(this), |
| 327 reinterpret_cast<intptr_t>(&zone_)); | 326 reinterpret_cast<intptr_t>(&zone_)); |
| 328 } | 327 } |
| 329 } | 328 } |
| 330 | 329 |
| 331 } // namespace dart | 330 } // namespace dart |
| OLD | NEW |