| 1 | // Copyright (c) 2013, 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 | #ifndef RUNTIME_VM_ALLOCATION_H_ | 
|---|
| 6 | #define RUNTIME_VM_ALLOCATION_H_ | 
|---|
| 7 |  | 
|---|
| 8 | #include "platform/allocation.h" | 
|---|
| 9 | #include "platform/assert.h" | 
|---|
| 10 | #include "vm/globals.h" | 
|---|
| 11 |  | 
|---|
| 12 | namespace dart { | 
|---|
| 13 |  | 
|---|
| 14 | // Forward declarations. | 
|---|
| 15 | class ThreadState; | 
|---|
| 16 | class Zone; | 
|---|
| 17 |  | 
|---|
| 18 | // Stack resources subclass from this base class. The VM will ensure that the | 
|---|
| 19 | // destructors of these objects are called before the stack is unwound past the | 
|---|
| 20 | // objects location on the stack. Use stack resource objects if objects | 
|---|
| 21 | // need to be destroyed even in the case of exceptions when a Longjump is done | 
|---|
| 22 | // to a stack frame above the frame where these objects were allocated. | 
|---|
| 23 | class StackResource { | 
|---|
| 24 | public: | 
|---|
| 25 | explicit StackResource(ThreadState* thread) : thread_(NULL), previous_(NULL) { | 
|---|
| 26 | Init(thread); | 
|---|
| 27 | } | 
|---|
| 28 |  | 
|---|
| 29 | virtual ~StackResource(); | 
|---|
| 30 |  | 
|---|
| 31 | // The thread that owns this resource. | 
|---|
| 32 | ThreadState* thread() const { return thread_; } | 
|---|
| 33 |  | 
|---|
| 34 | // Destroy stack resources of thread until top exit frame. | 
|---|
| 35 | static void Unwind(ThreadState* thread) { UnwindAbove(thread, NULL); } | 
|---|
| 36 | // Destroy stack resources of thread above new_top, exclusive. | 
|---|
| 37 | static void UnwindAbove(ThreadState* thread, StackResource* new_top); | 
|---|
| 38 |  | 
|---|
| 39 | private: | 
|---|
| 40 | void Init(ThreadState* thread); | 
|---|
| 41 |  | 
|---|
| 42 | ThreadState* thread_; | 
|---|
| 43 | StackResource* previous_; | 
|---|
| 44 |  | 
|---|
| 45 | DISALLOW_ALLOCATION(); | 
|---|
| 46 | DISALLOW_IMPLICIT_CONSTRUCTORS(StackResource); | 
|---|
| 47 | }; | 
|---|
| 48 |  | 
|---|
| 49 | // Zone allocated objects cannot be individually deallocated, but have | 
|---|
| 50 | // to rely on the destructor of Zone which is called when the Zone | 
|---|
| 51 | // goes out of scope to reclaim memory. | 
|---|
| 52 | class ZoneAllocated { | 
|---|
| 53 | public: | 
|---|
| 54 | ZoneAllocated() {} | 
|---|
| 55 |  | 
|---|
| 56 | // Implicitly allocate the object in the current zone. | 
|---|
| 57 | void* operator new(uword size); | 
|---|
| 58 |  | 
|---|
| 59 | // Allocate the object in the given zone, which must be the current zone. | 
|---|
| 60 | void* operator new(uword size, Zone* zone); | 
|---|
| 61 |  | 
|---|
| 62 | // Ideally, the delete operator should be protected instead of | 
|---|
| 63 | // public, but unfortunately the compiler sometimes synthesizes | 
|---|
| 64 | // (unused) destructors for classes derived from ZoneObject, which | 
|---|
| 65 | // require the operator to be visible. MSVC requires the delete | 
|---|
| 66 | // operator to be public. | 
|---|
| 67 |  | 
|---|
| 68 | // Disallow explicit deallocation of nodes. Nodes can only be | 
|---|
| 69 | // deallocated by invoking DeleteAll() on the zone they live in. | 
|---|
| 70 | void operator delete(void* pointer) { UNREACHABLE(); } | 
|---|
| 71 |  | 
|---|
| 72 | private: | 
|---|
| 73 | DISALLOW_COPY_AND_ASSIGN(ZoneAllocated); | 
|---|
| 74 | }; | 
|---|
| 75 |  | 
|---|
| 76 | }  // namespace dart | 
|---|
| 77 |  | 
|---|
| 78 | #endif  // RUNTIME_VM_ALLOCATION_H_ | 
|---|
| 79 |  | 
|---|