Flutter Engine
The Flutter Engine
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
allocation_test.cc
Go to the documentation of this 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
3// BSD-style license that can be found in the LICENSE file.
4
5#include "vm/allocation.h"
6#include "platform/assert.h"
7#include "vm/longjump.h"
8#include "vm/unit_test.h"
9
10namespace dart {
11
13 public:
14 explicit TestValueObject(int* ptr) : ptr_(ptr) {
15 EXPECT_EQ(1, *ptr_);
16 *ptr_ = 2;
17 }
18
19 virtual ~TestValueObject() {
20 EXPECT_EQ(3, *ptr_);
21 *ptr_ = 4;
22 }
23
24 int value() const { return *ptr_; }
25 virtual int GetId() const { return 3; }
26
27 private:
28 int* ptr_;
29};
30
32 public:
33 explicit TestStackResource(int* ptr)
34 : StackResource(Thread::Current()), ptr_(ptr) {
35 EXPECT_EQ(1, *ptr_);
36 *ptr_ = 2;
37 }
38
40 EXPECT_EQ(6, *ptr_);
41 *ptr_ = 7;
42 }
43
44 int value() const { return *ptr_; }
45 virtual int GetId() const { return 3; }
46
47 private:
48 int* ptr_;
49};
50
52 public:
53 explicit TestStackedStackResource(int* ptr)
54 : StackResource(Thread::Current()), ptr_(ptr) {
55 EXPECT_EQ(3, *ptr_);
56 *ptr_ = 4;
57 }
58
60 EXPECT_EQ(5, *ptr_);
61 *ptr_ = 6;
62 }
63
64 int value() const { return *ptr_; }
65
66 private:
67 int* ptr_;
68};
69
70static void StackAllocatedDestructionHelper(int* ptr) {
71 TestValueObject stacked(ptr);
72 EXPECT_EQ(2, *ptr);
73 *ptr = 3;
74}
75
76ISOLATE_UNIT_TEST_CASE(StackAllocatedDestruction) {
77 int data = 1;
79 EXPECT_EQ(4, data);
80}
81
82static void StackAllocatedLongJumpHelper(int* ptr, LongJumpScope* jump) {
83 TestValueObject stacked(ptr);
84 EXPECT_EQ(2, *ptr);
85 *ptr = 3;
86 const Error& error = Error::Handle(LanguageError::New(
87 String::Handle(String::New("StackAllocatedLongJump"))));
88 jump->Jump(1, error);
90}
91
92ISOLATE_UNIT_TEST_CASE(StackAllocatedLongJump) {
93 LongJumpScope jump;
94 int data = 1;
95 if (setjmp(*jump.Set()) == 0) {
98 } else {
99 ASSERT(Error::Handle(thread->StealStickyError()).IsLanguageError());
100 }
101 EXPECT_EQ(3, data);
102}
103
105 TestStackedStackResource stacked(ptr);
106 EXPECT_EQ(4, *ptr);
107 *ptr = 5;
108}
109
110static void StackResourceDestructionHelper(int* ptr) {
111 TestStackResource stacked(ptr);
112 EXPECT_EQ(2, *ptr);
113 *ptr = 3;
115 EXPECT_EQ(6, *ptr);
116 // Do not set data because the LongJump version does not return control here.
117}
118
119ISOLATE_UNIT_TEST_CASE(StackResourceDestruction) {
120 int data = 1;
122 EXPECT_EQ(7, data);
123}
124
126 TestStackedStackResource stacked(ptr);
127 EXPECT_EQ(4, *ptr);
128 *ptr = 5;
129 const Error& error = Error::Handle(LanguageError::New(
130 String::Handle(String::New("StackedStackResourceLongJump"))));
131 jump->Jump(1, error);
132 UNREACHABLE();
133}
134
135static void StackResourceLongJumpHelper(int* ptr, LongJumpScope* jump) {
136 TestStackResource stacked(ptr);
137 EXPECT_EQ(2, *ptr);
138 *ptr = 3;
140 UNREACHABLE();
141}
142
143ISOLATE_UNIT_TEST_CASE(StackResourceLongJump) {
145 {
146 LongJumpScope jump;
147 int data = 1;
148 if (setjmp(*jump.Set()) == 0) {
150 UNREACHABLE();
151 } else {
152 ASSERT(Error::Handle(thread->StealStickyError()).IsLanguageError());
153 }
154 EXPECT_EQ(7, data);
155 }
156 ASSERT(base == Thread::Current()->long_jump_base());
157}
158
159} // namespace dart
#define UNREACHABLE()
Definition: assert.h:248
DART_NORETURN void Jump(int value, const Error &error)
Definition: longjump.cc:22
jmp_buf * Set()
Definition: longjump.cc:16
static Object & Handle()
Definition: object.h:407
static StringPtr New(const char *cstr, Heap::Space space=Heap::kNew)
Definition: object.cc:23698
virtual int GetId() const
virtual int GetId() const
LongJumpScope * long_jump_base() const
Definition: thread_state.h:47
static Thread * Current()
Definition: thread.h:362
#define ASSERT(E)
const uint8_t uint32_t uint32_t GError ** error
Definition: dart_vm.cc:33
static void StackResourceDestructionHelper(int *ptr)
static void StackedStackResourceDestructionHelper(int *ptr)
static void StackResourceLongJumpHelper(int *ptr, LongJumpScope *jump)
ISOLATE_UNIT_TEST_CASE(StackAllocatedDestruction)
static void StackAllocatedLongJumpHelper(int *ptr, LongJumpScope *jump)
static int8_t data[kExtLength]
static void StackAllocatedDestructionHelper(int *ptr)
static void StackedStackResourceLongJumpHelper(int *ptr, LongJumpScope *jump)