| 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 |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | |
| 5 | #include "vm/allocation.h" |
| 6 | |
| 7 | #include "platform/assert.h" |
| 8 | #include "vm/isolate.h" |
| 9 | #include "vm/thread.h" |
| 10 | #include "vm/zone.h" |
| 11 | |
| 12 | namespace dart { |
| 13 | |
| 14 | static void* Allocate(uword size, Zone* zone) { |
| 15 | ASSERT(zone != NULL); |
| 16 | if (size > static_cast<uword>(kIntptrMax)) { |
| 17 | FATAL1("ZoneAllocated object has unexpectedly large size %"Pu "", size); |
| 18 | } |
| 19 | return reinterpret_cast<void*>(zone->AllocUnsafe(size)); |
| 20 | } |
| 21 | |
| 22 | void* ZoneAllocated::operator new(uword size) { |
| 23 | return Allocate(size, Thread::Current()->zone()); |
| 24 | } |
| 25 | |
| 26 | void* ZoneAllocated::operator new(uword size, Zone* zone) { |
| 27 | ASSERT(Thread::Current()->ZoneIsOwnedByThread(zone)); |
| 28 | return Allocate(size, zone); |
| 29 | } |
| 30 | |
| 31 | StackResource::~StackResource() { |
| 32 | if (thread_ != NULL) { |
| 33 | StackResource* top = thread_->top_resource(); |
| 34 | ASSERT(top == this); |
| 35 | thread_->set_top_resource(previous_); |
| 36 | } |
| 37 | #if defined(DEBUG) |
| 38 | if (thread_ != NULL) { |
| 39 | ASSERT(Thread::Current() == thread_); |
| 40 | } |
| 41 | #endif |
| 42 | } |
| 43 | |
| 44 | void StackResource::Init(ThreadState* thread) { |
| 45 | // We can only have longjumps and exceptions when there is a current |
| 46 | // thread and isolate. If there is no current thread, we don't need to |
| 47 | // protect this case. |
| 48 | // TODO(23807): Eliminate this special case. |
| 49 | if (thread != NULL) { |
| 50 | ASSERT(Thread::Current() == thread); |
| 51 | thread_ = thread; |
| 52 | previous_ = thread_->top_resource(); |
| 53 | ASSERT((previous_ == NULL) || (previous_->thread_ == thread)); |
| 54 | thread_->set_top_resource(this); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | void StackResource::UnwindAbove(ThreadState* thread, StackResource* new_top) { |
| 59 | StackResource* current_resource = thread->top_resource(); |
| 60 | while (current_resource != new_top) { |
| 61 | current_resource->~StackResource(); |
| 62 | current_resource = thread->top_resource(); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | } // namespace dart |
| 67 |