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