vfs: check userland buffers before reading them.
[haiku.git] / src / kits / debugger / model / StackFrameValueInfos.cpp
blobf2b637da1a0d392de72edc82db9175522ea4af62
1 /*
2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
7 #include "StackFrameValueInfos.h"
9 #include <new>
11 #include "FunctionID.h"
12 #include "Type.h"
13 #include "TypeComponentPath.h"
14 #include "ValueLocation.h"
17 struct StackFrameValueInfos::Key {
18 ObjectID* variable;
19 TypeComponentPath* path;
21 Key(ObjectID* variable, TypeComponentPath* path)
23 variable(variable),
24 path(path)
28 uint32 HashValue() const
30 return variable->HashValue() ^ path->HashValue();
33 bool operator==(const Key& other) const
35 return *variable == *other.variable && *path == *other.path;
40 struct StackFrameValueInfos::InfoEntry : Key {
41 Type* type;
42 ValueLocation* location;
43 InfoEntry* next;
45 InfoEntry(ObjectID* variable, TypeComponentPath* path)
47 Key(variable, path),
48 type(NULL),
49 location(NULL)
51 variable->AcquireReference();
52 path->AcquireReference();
55 ~InfoEntry()
57 SetInfo(NULL, NULL);
58 variable->ReleaseReference();
59 path->ReleaseReference();
63 void SetInfo(Type* type, ValueLocation* location)
65 if (type != NULL)
66 type->AcquireReference();
67 if (location != NULL)
68 location->AcquireReference();
70 if (this->type != NULL)
71 this->type->ReleaseReference();
72 if (this->location != NULL)
73 this->location->ReleaseReference();
75 this->type = type;
76 this->location = location;
81 struct StackFrameValueInfos::InfoEntryHashDefinition {
82 typedef Key KeyType;
83 typedef InfoEntry ValueType;
85 size_t HashKey(const Key& key) const
87 return key.HashValue();
90 size_t Hash(const InfoEntry* value) const
92 return value->HashValue();
95 bool Compare(const Key& key, const InfoEntry* value) const
97 return key == *value;
100 InfoEntry*& GetLink(InfoEntry* value) const
102 return value->next;
107 StackFrameValueInfos::StackFrameValueInfos()
109 fValues(NULL)
114 StackFrameValueInfos::~StackFrameValueInfos()
116 _Cleanup();
120 status_t
121 StackFrameValueInfos::Init()
123 fValues = new(std::nothrow) ValueTable;
124 if (fValues == NULL)
125 return B_NO_MEMORY;
127 return fValues->Init();
131 bool
132 StackFrameValueInfos::GetInfo(ObjectID* variable,
133 const TypeComponentPath* path, Type** _type,
134 ValueLocation** _location) const
136 InfoEntry* entry = fValues->Lookup(
137 Key(variable, (TypeComponentPath*)path));
138 if (entry == NULL)
139 return false;
141 if (_type != NULL) {
142 entry->type->AcquireReference();
143 *_type = entry->type;
146 if (_location != NULL) {
147 entry->location->AcquireReference();
148 *_location = entry->location;
151 return true;
155 bool
156 StackFrameValueInfos::HasInfo(ObjectID* variable,
157 const TypeComponentPath* path) const
159 return fValues->Lookup(Key(variable, (TypeComponentPath*)path)) != NULL;
163 status_t
164 StackFrameValueInfos::SetInfo(ObjectID* variable, TypeComponentPath* path,
165 Type* type, ValueLocation* location)
167 InfoEntry* entry = fValues->Lookup(Key(variable, path));
168 if (entry == NULL) {
169 entry = new(std::nothrow) InfoEntry(variable, path);
170 if (entry == NULL)
171 return B_NO_MEMORY;
172 fValues->Insert(entry);
175 entry->SetInfo(type, location);
176 return B_OK;
180 void
181 StackFrameValueInfos::_Cleanup()
183 if (fValues != NULL) {
184 InfoEntry* entry = fValues->Clear(true);
186 while (entry != NULL) {
187 InfoEntry* next = entry->next;
188 delete entry;
189 entry = next;
192 delete fValues;
193 fValues = NULL;