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/allocation.h" | 5 #include "vm/allocation.h" |
6 | 6 |
7 #include "platform/assert.h" | 7 #include "platform/assert.h" |
8 #include "vm/isolate.h" | 8 #include "vm/isolate.h" |
| 9 #include "vm/thread.h" |
9 #include "vm/zone.h" | 10 #include "vm/zone.h" |
10 | 11 |
11 namespace dart { | 12 namespace dart { |
12 | 13 |
13 static void* Allocate(uword size, Zone* zone) { | 14 static void* Allocate(uword size, Zone* zone) { |
14 ASSERT(zone != NULL); | 15 ASSERT(zone != NULL); |
15 if (size > static_cast<uword>(kIntptrMax)) { | 16 if (size > static_cast<uword>(kIntptrMax)) { |
16 FATAL1("ZoneAllocated object has unexpectedly large size %" Pu "", size); | 17 FATAL1("ZoneAllocated object has unexpectedly large size %" Pu "", size); |
17 } | 18 } |
18 return reinterpret_cast<void*>(zone->AllocUnsafe(size)); | 19 return reinterpret_cast<void*>(zone->AllocUnsafe(size)); |
19 } | 20 } |
20 | 21 |
21 | 22 |
22 void* ZoneAllocated::operator new(uword size) { | 23 void* ZoneAllocated::operator new(uword size) { |
23 return Allocate(size, Thread::Current()->zone()); | 24 return Allocate(size, Thread::Current()->zone()); |
24 } | 25 } |
25 | 26 |
26 | 27 |
27 void* ZoneAllocated::operator new(uword size, Zone* zone) { | 28 void* ZoneAllocated::operator new(uword size, Zone* zone) { |
28 ASSERT(zone == Thread::Current()->zone()); | 29 ASSERT(zone == Thread::Current()->zone()); |
29 return Allocate(size, zone); | 30 return Allocate(size, zone); |
30 } | 31 } |
31 | 32 |
32 | 33 |
| 34 StackResource::~StackResource() { |
| 35 if (thread_ != NULL) { |
| 36 StackResource* top = thread_->top_resource(); |
| 37 ASSERT(top == this); |
| 38 thread_->set_top_resource(previous_); |
| 39 } |
| 40 #if defined(DEBUG) |
| 41 if (thread_ != NULL) { |
| 42 ASSERT(Thread::Current() == thread_); |
| 43 BaseIsolate::AssertCurrent(reinterpret_cast<BaseIsolate*>(isolate())); |
| 44 } |
| 45 #endif |
| 46 } |
| 47 |
| 48 |
| 49 Isolate* StackResource::isolate() const { |
| 50 return thread_ == NULL ? NULL : thread_->isolate(); |
| 51 } |
| 52 |
| 53 |
| 54 void StackResource::Init(Thread* thread) { |
| 55 // We can only have longjumps and exceptions when there is a current |
| 56 // thread and isolate. If there is no current thread, we don't need to |
| 57 // protect this case. |
| 58 // TODO(23807): Eliminate this special case. |
| 59 if (thread != NULL) { |
| 60 ASSERT(Thread::Current() == thread); |
| 61 thread_ = thread; |
| 62 previous_ = thread_->top_resource(); |
| 63 ASSERT((previous_ == NULL) || (previous_->thread_ == thread)); |
| 64 thread_->set_top_resource(this); |
| 65 } |
| 66 } |
| 67 |
| 68 |
33 void StackResource::UnwindAbove(Thread* thread, StackResource* new_top) { | 69 void StackResource::UnwindAbove(Thread* thread, StackResource* new_top) { |
34 StackResource* current_resource = thread->top_resource(); | 70 StackResource* current_resource = thread->top_resource(); |
35 while (current_resource != new_top) { | 71 while (current_resource != new_top) { |
36 current_resource->~StackResource(); | 72 current_resource->~StackResource(); |
37 current_resource = thread->top_resource(); | 73 current_resource = thread->top_resource(); |
38 } | 74 } |
39 } | 75 } |
40 | 76 |
41 | 77 |
42 #if defined(DEBUG) | 78 #if defined(DEBUG) |
43 NoSafepointScope::NoSafepointScope() : StackResource(Thread::Current()) { | 79 NoSafepointScope::NoSafepointScope() : StackResource(Thread::Current()) { |
44 thread()->IncrementNoSafepointScopeDepth(); | 80 thread()->IncrementNoSafepointScopeDepth(); |
45 } | 81 } |
46 | 82 |
47 | 83 |
48 NoSafepointScope::~NoSafepointScope() { | 84 NoSafepointScope::~NoSafepointScope() { |
49 thread()->DecrementNoSafepointScopeDepth(); | 85 thread()->DecrementNoSafepointScopeDepth(); |
50 } | 86 } |
51 #endif // defined(DEBUG) | 87 #endif // defined(DEBUG) |
52 | 88 |
53 } // namespace dart | 89 } // namespace dart |
OLD | NEW |