1*fce4895dSRene Gollent /*
2*fce4895dSRene Gollent * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3*fce4895dSRene Gollent * Distributed under the terms of the MIT License.
4*fce4895dSRene Gollent */
5*fce4895dSRene Gollent
6*fce4895dSRene Gollent
7*fce4895dSRene Gollent #include "StackFrameValueInfos.h"
8*fce4895dSRene Gollent
9*fce4895dSRene Gollent #include <new>
10*fce4895dSRene Gollent
11*fce4895dSRene Gollent #include "FunctionID.h"
12*fce4895dSRene Gollent #include "Type.h"
13*fce4895dSRene Gollent #include "TypeComponentPath.h"
14*fce4895dSRene Gollent #include "ValueLocation.h"
15*fce4895dSRene Gollent
16*fce4895dSRene Gollent
17*fce4895dSRene Gollent struct StackFrameValueInfos::Key {
18*fce4895dSRene Gollent ObjectID* variable;
19*fce4895dSRene Gollent TypeComponentPath* path;
20*fce4895dSRene Gollent
KeyStackFrameValueInfos::Key21*fce4895dSRene Gollent Key(ObjectID* variable, TypeComponentPath* path)
22*fce4895dSRene Gollent :
23*fce4895dSRene Gollent variable(variable),
24*fce4895dSRene Gollent path(path)
25*fce4895dSRene Gollent {
26*fce4895dSRene Gollent }
27*fce4895dSRene Gollent
HashValueStackFrameValueInfos::Key28*fce4895dSRene Gollent uint32 HashValue() const
29*fce4895dSRene Gollent {
30*fce4895dSRene Gollent return variable->HashValue() ^ path->HashValue();
31*fce4895dSRene Gollent }
32*fce4895dSRene Gollent
operator ==StackFrameValueInfos::Key33*fce4895dSRene Gollent bool operator==(const Key& other) const
34*fce4895dSRene Gollent {
35*fce4895dSRene Gollent return *variable == *other.variable && *path == *other.path;
36*fce4895dSRene Gollent }
37*fce4895dSRene Gollent };
38*fce4895dSRene Gollent
39*fce4895dSRene Gollent
40*fce4895dSRene Gollent struct StackFrameValueInfos::InfoEntry : Key {
41*fce4895dSRene Gollent Type* type;
42*fce4895dSRene Gollent ValueLocation* location;
43*fce4895dSRene Gollent InfoEntry* next;
44*fce4895dSRene Gollent
InfoEntryStackFrameValueInfos::InfoEntry45*fce4895dSRene Gollent InfoEntry(ObjectID* variable, TypeComponentPath* path)
46*fce4895dSRene Gollent :
47*fce4895dSRene Gollent Key(variable, path),
48*fce4895dSRene Gollent type(NULL),
49*fce4895dSRene Gollent location(NULL)
50*fce4895dSRene Gollent {
51*fce4895dSRene Gollent variable->AcquireReference();
52*fce4895dSRene Gollent path->AcquireReference();
53*fce4895dSRene Gollent }
54*fce4895dSRene Gollent
~InfoEntryStackFrameValueInfos::InfoEntry55*fce4895dSRene Gollent ~InfoEntry()
56*fce4895dSRene Gollent {
57*fce4895dSRene Gollent SetInfo(NULL, NULL);
58*fce4895dSRene Gollent variable->ReleaseReference();
59*fce4895dSRene Gollent path->ReleaseReference();
60*fce4895dSRene Gollent }
61*fce4895dSRene Gollent
62*fce4895dSRene Gollent
SetInfoStackFrameValueInfos::InfoEntry63*fce4895dSRene Gollent void SetInfo(Type* type, ValueLocation* location)
64*fce4895dSRene Gollent {
65*fce4895dSRene Gollent if (type != NULL)
66*fce4895dSRene Gollent type->AcquireReference();
67*fce4895dSRene Gollent if (location != NULL)
68*fce4895dSRene Gollent location->AcquireReference();
69*fce4895dSRene Gollent
70*fce4895dSRene Gollent if (this->type != NULL)
71*fce4895dSRene Gollent this->type->ReleaseReference();
72*fce4895dSRene Gollent if (this->location != NULL)
73*fce4895dSRene Gollent this->location->ReleaseReference();
74*fce4895dSRene Gollent
75*fce4895dSRene Gollent this->type = type;
76*fce4895dSRene Gollent this->location = location;
77*fce4895dSRene Gollent }
78*fce4895dSRene Gollent };
79*fce4895dSRene Gollent
80*fce4895dSRene Gollent
81*fce4895dSRene Gollent struct StackFrameValueInfos::InfoEntryHashDefinition {
82*fce4895dSRene Gollent typedef Key KeyType;
83*fce4895dSRene Gollent typedef InfoEntry ValueType;
84*fce4895dSRene Gollent
HashKeyStackFrameValueInfos::InfoEntryHashDefinition85*fce4895dSRene Gollent size_t HashKey(const Key& key) const
86*fce4895dSRene Gollent {
87*fce4895dSRene Gollent return key.HashValue();
88*fce4895dSRene Gollent }
89*fce4895dSRene Gollent
HashStackFrameValueInfos::InfoEntryHashDefinition90*fce4895dSRene Gollent size_t Hash(const InfoEntry* value) const
91*fce4895dSRene Gollent {
92*fce4895dSRene Gollent return value->HashValue();
93*fce4895dSRene Gollent }
94*fce4895dSRene Gollent
CompareStackFrameValueInfos::InfoEntryHashDefinition95*fce4895dSRene Gollent bool Compare(const Key& key, const InfoEntry* value) const
96*fce4895dSRene Gollent {
97*fce4895dSRene Gollent return key == *value;
98*fce4895dSRene Gollent }
99*fce4895dSRene Gollent
GetLinkStackFrameValueInfos::InfoEntryHashDefinition100*fce4895dSRene Gollent InfoEntry*& GetLink(InfoEntry* value) const
101*fce4895dSRene Gollent {
102*fce4895dSRene Gollent return value->next;
103*fce4895dSRene Gollent }
104*fce4895dSRene Gollent };
105*fce4895dSRene Gollent
106*fce4895dSRene Gollent
StackFrameValueInfos()107*fce4895dSRene Gollent StackFrameValueInfos::StackFrameValueInfos()
108*fce4895dSRene Gollent :
109*fce4895dSRene Gollent fValues(NULL)
110*fce4895dSRene Gollent {
111*fce4895dSRene Gollent }
112*fce4895dSRene Gollent
113*fce4895dSRene Gollent
~StackFrameValueInfos()114*fce4895dSRene Gollent StackFrameValueInfos::~StackFrameValueInfos()
115*fce4895dSRene Gollent {
116*fce4895dSRene Gollent _Cleanup();
117*fce4895dSRene Gollent }
118*fce4895dSRene Gollent
119*fce4895dSRene Gollent
120*fce4895dSRene Gollent status_t
Init()121*fce4895dSRene Gollent StackFrameValueInfos::Init()
122*fce4895dSRene Gollent {
123*fce4895dSRene Gollent fValues = new(std::nothrow) ValueTable;
124*fce4895dSRene Gollent if (fValues == NULL)
125*fce4895dSRene Gollent return B_NO_MEMORY;
126*fce4895dSRene Gollent
127*fce4895dSRene Gollent return fValues->Init();
128*fce4895dSRene Gollent }
129*fce4895dSRene Gollent
130*fce4895dSRene Gollent
131*fce4895dSRene Gollent bool
GetInfo(ObjectID * variable,const TypeComponentPath * path,Type ** _type,ValueLocation ** _location) const132*fce4895dSRene Gollent StackFrameValueInfos::GetInfo(ObjectID* variable,
133*fce4895dSRene Gollent const TypeComponentPath* path, Type** _type,
134*fce4895dSRene Gollent ValueLocation** _location) const
135*fce4895dSRene Gollent {
136*fce4895dSRene Gollent InfoEntry* entry = fValues->Lookup(
137*fce4895dSRene Gollent Key(variable, (TypeComponentPath*)path));
138*fce4895dSRene Gollent if (entry == NULL)
139*fce4895dSRene Gollent return false;
140*fce4895dSRene Gollent
141*fce4895dSRene Gollent if (_type != NULL) {
142*fce4895dSRene Gollent entry->type->AcquireReference();
143*fce4895dSRene Gollent *_type = entry->type;
144*fce4895dSRene Gollent }
145*fce4895dSRene Gollent
146*fce4895dSRene Gollent if (_location != NULL) {
147*fce4895dSRene Gollent entry->location->AcquireReference();
148*fce4895dSRene Gollent *_location = entry->location;
149*fce4895dSRene Gollent }
150*fce4895dSRene Gollent
151*fce4895dSRene Gollent return true;
152*fce4895dSRene Gollent }
153*fce4895dSRene Gollent
154*fce4895dSRene Gollent
155*fce4895dSRene Gollent bool
HasInfo(ObjectID * variable,const TypeComponentPath * path) const156*fce4895dSRene Gollent StackFrameValueInfos::HasInfo(ObjectID* variable,
157*fce4895dSRene Gollent const TypeComponentPath* path) const
158*fce4895dSRene Gollent {
159*fce4895dSRene Gollent return fValues->Lookup(Key(variable, (TypeComponentPath*)path)) != NULL;
160*fce4895dSRene Gollent }
161*fce4895dSRene Gollent
162*fce4895dSRene Gollent
163*fce4895dSRene Gollent status_t
SetInfo(ObjectID * variable,TypeComponentPath * path,Type * type,ValueLocation * location)164*fce4895dSRene Gollent StackFrameValueInfos::SetInfo(ObjectID* variable, TypeComponentPath* path,
165*fce4895dSRene Gollent Type* type, ValueLocation* location)
166*fce4895dSRene Gollent {
167*fce4895dSRene Gollent InfoEntry* entry = fValues->Lookup(Key(variable, path));
168*fce4895dSRene Gollent if (entry == NULL) {
169*fce4895dSRene Gollent entry = new(std::nothrow) InfoEntry(variable, path);
170*fce4895dSRene Gollent if (entry == NULL)
171*fce4895dSRene Gollent return B_NO_MEMORY;
172*fce4895dSRene Gollent fValues->Insert(entry);
173*fce4895dSRene Gollent }
174*fce4895dSRene Gollent
175*fce4895dSRene Gollent entry->SetInfo(type, location);
176*fce4895dSRene Gollent return B_OK;
177*fce4895dSRene Gollent }
178*fce4895dSRene Gollent
179*fce4895dSRene Gollent
180*fce4895dSRene Gollent void
_Cleanup()181*fce4895dSRene Gollent StackFrameValueInfos::_Cleanup()
182*fce4895dSRene Gollent {
183*fce4895dSRene Gollent if (fValues != NULL) {
184*fce4895dSRene Gollent InfoEntry* entry = fValues->Clear(true);
185*fce4895dSRene Gollent
186*fce4895dSRene Gollent while (entry != NULL) {
187*fce4895dSRene Gollent InfoEntry* next = entry->next;
188*fce4895dSRene Gollent delete entry;
189*fce4895dSRene Gollent entry = next;
190*fce4895dSRene Gollent }
191*fce4895dSRene Gollent
192*fce4895dSRene Gollent delete fValues;
193*fce4895dSRene Gollent fValues = NULL;
194*fce4895dSRene Gollent }
195*fce4895dSRene Gollent }
196