OpenASIP 2.2
Loading...
Searching...
No Matches
SafePointer.hh
Go to the documentation of this file.
1/*
2 Copyright (c) 2002-2009 Tampere University.
3
4 This file is part of TTA-Based Codesign Environment (TCE).
5
6 Permission is hereby granted, free of charge, to any person obtaining a
7 copy of this software and associated documentation files (the "Software"),
8 to deal in the Software without restriction, including without limitation
9 the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 and/or sell copies of the Software, and to permit persons to whom the
11 Software is furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
23 */
24/**
25 * @file SafePointer.hh
26 *
27 * Declaration of SafePointer class and utility classes and types related
28 * to ReferenceManager.
29 *
30 * @author Pekka Jääskeläinen 2003 (pjaaskel-no.spam-cs.tut.fi)
31 * @note reviewed 29 Aug 2003 by rm, ml, ac, tr
32 *
33 * @note rating: yellow
34 */
35
36#ifndef TTA_SAFEPOINTER_HH
37#define TTA_SAFEPOINTER_HH
38
39#include <cstddef> // NULL
40#include <set>
41#include <map>
42#include <list>
43#include <iterator>
44#include <sstream>
45
46//#include "hash_map.hh"
47#include "Application.hh"
48#include "ReferenceKey.hh"
49#include "Exception.hh" // IllegalParameters, UnresolvedReference
50
51namespace TPEF {
52
53class SafePointable;
54
55/**
56 *
57 * Contains classes, utility classes and types related to reference managing.
58 *
59 */
60namespace ReferenceManager {
61
62class SafePointer;
63
64///////////////////////////////////////////////////////////////////////////////
65// SafePointerList
66///////////////////////////////////////////////////////////////////////////////
67/**
68 * List of SafePointers that are all pointing to the same object.
69 *
70 */
72public:
73 /// Type of the container to hold the SafePointers in.
74 typedef std::list<SafePointer*> SafePointerListType;
75
76 /// Type of the list length value.
77 typedef std::string::size_type LengthType;
78
81
82 virtual ~SafePointerList();
83
84 void setReference(const SafePointable* obj);
86
87 void append(SafePointer* newSafePointer);
88 void append(SafePointerList* anotherSafePointerList);
89
91
92 void cleanup();
93 void cleanupDead();
94
96
97private:
98 /// Object that SafePointers in this list are pointing to.
100
101 /// Container for SafePointers.
103};
104
105/**
106 * Class containing all the hash functions if hash_map is
107 * used. However the use of hash_map will make program very slow
108 * unless MapTools::containsValue() is not optimised for
109 * hash_tables or that function should be circumvented.
110 */
112public:
113 size_t operator()(const SectionIndexKey& key) const {
114 return key.index()^
115 (static_cast<size_t>(key.sectionId()) <<
116 ((sizeof(size_t) - sizeof(SectionId)) *
118 }
119
120 size_t operator()(const SectionOffsetKey& key) const {
121 return key.offset()^
122 (static_cast<size_t>(key.sectionId()) <<
123 ((sizeof(size_t) - sizeof(SectionId)) *
125 }
126
127 size_t operator()(const FileOffsetKey& key) const {
128 return key.fileOffset();
129 }
130
131 size_t operator()(const SectionKey& key) const {
132 return key.sectionId();
133 }
134
135 size_t operator()(const SafePointable* const & key) const {
136 return reinterpret_cast<size_t>(key);
137 }
138};
139
140// TODO: for hash map implementation check SafePointer.cc line 424 TODO
141// MapTools::containsValue call takes very long time for hash maps..
142
143/// Unordered set of SafePointers.
144typedef std::set<SafePointer*> SafePointerSet;
145
146/// Map for SafePointers that are requested using SectionIndexKeys.
147typedef std::map<SectionIndexKey, SafePointerList*> SectionIndexMap;
148//typedef hash_map<SectionIndexKey, SafePointerList*,
149// HashFunctions> SectionIndexMap;
150
151/// Map for SafePointers that are requested using SectionOffsetKeys.
152typedef std::map<SectionOffsetKey, SafePointerList*> SectionOffsetMap;
153//typedef hash_map<SectionOffsetKey, SafePointerList*,
154// HashFunctions> SectionOffsetMap;
155
156/// Map for SafePointers that are requested using FileOffsetKeys.
157typedef std::map<FileOffsetKey, SafePointerList*> FileOffsetMap;
158//typedef hash_map<FileOffsetKey, SafePointerList*,
159// HashFunctions> FileOffsetMap;
160
161/// Map for SafePointers that are requested using SectionKeys.
162typedef std::map<SectionKey, SafePointerList*> SectionMap;
163//typedef hash_map<SectionKey, SafePointerList*,
164// HashFunctions> SectionMap;
165
166
167/// Map for resolved references, that is SafePointers that are pointing to
168/// the created object.
169typedef std::map<const SafePointable*, SafePointerList*> ReferenceMap;
170//typedef hash_map<const SafePointable*, SafePointerList*,
171// HashFunctions> ReferenceMap;
172
173
174///////////////////////////////////////////////////////////////////////////////
175// SafePointer
176///////////////////////////////////////////////////////////////////////////////
177/**
178 * Indirection for object references.
179 *
180 * Allows updating references later on, thus allows referencing objects that
181 * are created later. Helps with the dangling pointers problem, too.
182 *
183 * References to objects that are not created yet are made with different kind
184 * of keys. These keys refer for example to sections and offsets in source
185 * binary file while reading the binary. After the binary is read and the
186 * object model of binary is constructed, these keys and keytables have no use.
187 */
189public:
190 virtual ~SafePointer();
191
194
195 static bool isAlive(SafePointer* pointerToCheck);
196 static bool isReferenced(const SafePointable* object);
197
198 static void addObjectReference(
199 SectionIndexKey key, const SafePointable* obj);
200 static void addObjectReference(
201 SectionOffsetKey key, const SafePointable* obj);
202 static void addObjectReference(FileOffsetKey key, const SafePointable* obj);
203 static void addObjectReference(SectionKey key, const SafePointable* obj);
204
208 static SectionKey sectionKeyFor(const SafePointable* obj);
209
210 static void notifyDeleted(const SafePointable* obj);
211 static void notifyDeleted(SafePointer* safePointer);
212
213 static void resolve();
214
215 static void cleanupKeyTables();
216 static void cleanup();
217
218 template <typename MapType>
219 static bool unresolvedReferences(const MapType& mapToCheck,
220 const ReferenceKey **unresolvedKey);
221
223 const SafePointer* old, SafePointable* obj);
224
226 SafePointable* newObj, SafePointable* oldObj);
227
228 /// The default SafePointer that is used in null references.
229 static const SafePointer null;
230
231#ifndef NDEBUG
232#ifndef DOXYGEN_SHOULD_SKIP_THIS
233
234/**
235 * Creates SafePointer with debug data of creation place of pointer.
236 *
237 * Just calls debugCreate method, with full parameters.
238 *
239 * Creation place of SafePointer is very important to know
240 * because it's very hard to find where SafePointer that points
241 * to wrong place was created.
242 */
243#define CREATE_SAFEPOINTER(x) \
244 ReferenceManager::SafePointer::debugCreate( x , __FILE__, __LINE__)
245
246 // Debug methods... don't delete.
247
248 // methods for testing
249 static const SectionIndexMap* SIMap();
250 static const SafePointerList* SIMapAt(SectionIndexKey k);
251 static const SectionOffsetMap* SOMap();
252 static const SafePointerList* SOMapAt(SectionOffsetKey k);
253 static const FileOffsetMap* FOMap();
254 static const SafePointerList* FOMapAt(FileOffsetKey k);
255 static const ReferenceMap* RMap();
256 static const SafePointerList* RMapAt(SafePointable* k);
257 static const SectionMap* SMap();
258 static const SafePointerList* SMapAt(SectionKey k);
259
260 // methods to set and get debug string
261 void setDebugString(std::string aStr);
262 std::string debugString() const;
263
264 template <typename ObjType>
265 static SafePointer* debugCreate(ObjType obj, const char *file, int line);
266
267#endif // DOXYGEN_SHOULD_SKIP_THIS
268
269#else
270
271/**
272 * Creates SafePointer without debug data.
273 *
274 * Just calls genericCreate method.
275 */
276#define CREATE_SAFEPOINTER(x) \
277 ReferenceManager::SafePointer::genericCreate(x)
278#endif
279
280 template <typename ObjType>
281 static SafePointer* genericCreate(ObjType obj);
282
283
284protected:
289 SafePointer(SafePointable* object);
290
291 template <typename KeyType, typename MapType>
293 const KeyType& key,
294 MapType& destinationMap,
295 SafePointer* newSafePointer);
296
297 template <typename KeyType, typename MapType>
299 const KeyType& key, MapType& destinationMap, const SafePointable* obj);
300
301 template <typename KeyType, typename MapType>
302 static KeyType genericKeyFor(const SafePointable* obj, MapType& sourceMap);
303
304 template <typename MapType>
306 MapType& sourceMap,
307 std::set<SafePointerList*>&
308 listsToDelete);
309
310private:
311
312 /// The reference to the real object.
314
315 /// Map of SafePointers that are requested using SectionIndexKeys.
317
318 /// Map of SafePointers that are requested using SectionOffsetKeys.
320
321 /// Map of SafePointers that are requested using FileOffsetKeys.
323
324 /// Map of SafePointers that are requested using SectionKeys.
326
327 /// Map of SafePointers that have resolved references.
329
330 /// Set that cointains all alive (not deleted) SafePointers for extra
331 /// safety.
333
334 // KeyFor cache stuff to make genericKeyFor function to work O(1) speed
335 // after first call
336
337 /// Key type for cache, void* is pointer to key map
338 /// (sectioMap_, sectionOffsetMap, ...)
339 typedef std::pair<const SafePointable*, void*> KeyForCacheKey;
340 typedef std::map<KeyForCacheKey,const ReferenceKey*> KeyForCacheMap;
341
342 /// Map for cache.
344
345 /// Assignment not allowed.
347
348 /// Copying not allowed.
350
351#ifndef NDEBUG
352 std::string debugString_;
353#endif
354
355};
356
357} // namespace ReferenceManager
358}
359
360#include "SafePointer.icc"
361
362#endif
const Byte BYTE_BITWIDTH
Definition BaseType.hh:136
size_t operator()(const SectionIndexKey &key) const
size_t operator()(const FileOffsetKey &key) const
size_t operator()(const SectionOffsetKey &key) const
size_t operator()(const SafePointable *const &key) const
size_t operator()(const SectionKey &key) const
std::list< SafePointer * > SafePointerListType
Type of the container to hold the SafePointers in.
std::string::size_type LengthType
Type of the list length value.
void append(SafePointer *newSafePointer)
SafePointable * reference_
Object that SafePointers in this list are pointing to.
void setReference(const SafePointable *obj)
SafePointerListType list_
Container for SafePointers.
SafePointer & operator=(SafePointer &)
Assignment not allowed.
static void safelyCleanupKeyTable(MapType &sourceMap, std::set< SafePointerList * > &listsToDelete)
SafePointable * pointer() const
SafePointable * object_
The reference to the real object.
void genericRegisterPointer(const KeyType &key, MapType &destinationMap, SafePointer *newSafePointer)
static bool isAlive(SafePointer *pointerToCheck)
static void addObjectReference(SectionIndexKey key, const SafePointable *obj)
static SectionOffsetMap * sectionOffsetMap_
Map of SafePointers that are requested using SectionOffsetKeys.
static const SafePointer * replaceReference(const SafePointer *old, SafePointable *obj)
static const SafePointer null
The default SafePointer that is used in null references.
static FileOffsetMap * fileOffsetMap_
Map of SafePointers that are requested using FileOffsetKeys.
static FileOffsetKey fileOffsetKeyFor(const SafePointable *obj)
static KeyType genericKeyFor(const SafePointable *obj, MapType &sourceMap)
static SectionOffsetKey sectionOffsetKeyFor(const SafePointable *obj)
void setPointer(SafePointable *object)
static void notifyDeleted(const SafePointable *obj)
static bool unresolvedReferences(const MapType &mapToCheck, const ReferenceKey **unresolvedKey)
static SafePointerSet * aliveSafePointers_
Set that cointains all alive (not deleted) SafePointers for extra safety.
static SectionIndexKey sectionIndexKeyFor(const SafePointable *obj)
static SectionKey sectionKeyFor(const SafePointable *obj)
std::pair< const SafePointable *, void * > KeyForCacheKey
Key type for cache, void* is pointer to key map (sectioMap_, sectionOffsetMap, ......
static void genericAddObjectReference(const KeyType &key, MapType &destinationMap, const SafePointable *obj)
static SectionMap * sectionMap_
Map of SafePointers that are requested using SectionKeys.
static ReferenceMap * referenceMap_
Map of SafePointers that have resolved references.
static SectionIndexMap * sectionIndexMap_
Map of SafePointers that are requested using SectionIndexKeys.
SafePointer(SafePointer &)
Copying not allowed.
std::map< KeyForCacheKey, const ReferenceKey * > KeyForCacheMap
static void replaceAllReferences(SafePointable *newObj, SafePointable *oldObj)
static bool isReferenced(const SafePointable *object)
static SafePointer * genericCreate(ObjType obj)
static KeyForCacheMap * keyForCache_
Map for cache.
std::map< SectionKey, SafePointerList * > SectionMap
Map for SafePointers that are requested using SectionKeys.
std::map< SectionIndexKey, SafePointerList * > SectionIndexMap
Map for SafePointers that are requested using SectionIndexKeys.
std::map< SectionOffsetKey, SafePointerList * > SectionOffsetMap
Map for SafePointers that are requested using SectionOffsetKeys.
std::map< const SafePointable *, SafePointerList * > ReferenceMap
Map for resolved references, that is SafePointers that are pointing to the created object.
std::map< FileOffsetKey, SafePointerList * > FileOffsetMap
Map for SafePointers that are requested using FileOffsetKeys.
std::set< SafePointer * > SafePointerSet
Unordered set of SafePointers.
HalfWord SectionId
Type for storing binary file section ids.