GTIRB  v2.1.0
GrammaTech Intermediate Representation for Binaries: C++ API
Context.hpp
Go to the documentation of this file.
1 //===- Context.hpp ----------------------------------------------*- C++ -*-===//
2 //
3 // Copyright (C) 2020 GrammaTech, Inc.
4 //
5 // This code is licensed under the MIT license. See the LICENSE file in the
6 // project root for license terms.
7 //
8 // This project is sponsored by the Office of Naval Research, One Liberty
9 // Center, 875 N. Randolph Street, Arlington, VA 22203 under contract #
10 // N68335-17-C-0700. The content of the information does not necessarily
11 // reflect the position or policy of the Government and no official
12 // endorsement should be inferred.
13 //
14 //===----------------------------------------------------------------------===//
15 #ifndef GTIRB_CONTEXT_H
16 #define GTIRB_CONTEXT_H
17 
18 #include <gtirb/Allocator.hpp>
19 #include <gtirb/Export.hpp>
20 #include <boost/functional/hash.hpp>
21 #include <boost/uuid/uuid.hpp>
22 #include <cstdlib>
23 #include <functional>
24 #include <map>
25 
28 
29 namespace gtirb {
30 
36 using UUID = boost::uuids::uuid;
37 
38 class Node;
39 class CfgNode;
40 class ByteInterval;
41 class CodeBlock;
42 class DataBlock;
43 class IR;
44 class Module;
45 class ProxyBlock;
46 class Section;
47 class Symbol;
48 
64  // Note: this must be declared first so it outlives the allocators. They
65  // will access the UuidMap during their destructors to unregister nodes.
66  std::map<UUID, Node*> UuidMap;
67 
68  // Allocate each node type in a separate arena.
69  mutable SpecificBumpPtrAllocator<Node> NodeAllocator;
70  mutable SpecificBumpPtrAllocator<ByteInterval> ByteIntervalAllocator;
71  mutable SpecificBumpPtrAllocator<CodeBlock> CodeBlockAllocator;
72  mutable SpecificBumpPtrAllocator<DataBlock> DataBlockAllocator;
73  mutable SpecificBumpPtrAllocator<IR> IrAllocator;
74  mutable SpecificBumpPtrAllocator<Module> ModuleAllocator;
75  mutable SpecificBumpPtrAllocator<ProxyBlock> ProxyBlockAllocator;
76  mutable SpecificBumpPtrAllocator<Section> SectionAllocator;
77  mutable SpecificBumpPtrAllocator<Symbol> SymbolAllocator;
78 
80  friend class Node;
81 
82  void registerNode(const UUID& ID, Node* N) { UuidMap[ID] = N; }
83 
84  void unregisterNode(const Node* N);
85  const Node* findNode(const UUID& ID) const;
86  Node* findNode(const UUID& ID);
87 
94  template <class T> void* Allocate() const;
95 
104  void Deallocate(void*, size_t) const {
105  // Noop -- we don't want callers to deallocate individual allocations, but
106  // should instead deallocate the entire Context object to free memory.
107  }
108 
109 public:
110  Context();
111  ~Context();
112 
116  void ForgetAllocations();
117 
125  template <typename NodeTy, typename... Args>
126  NodeTy* Create(Args&&... TheArgs) {
127  return new (Allocate<NodeTy>()) NodeTy(std::forward<Args>(TheArgs)...);
128  }
129 };
130 
131 template <> GTIRB_EXPORT_API void* Context::Allocate<Node>() const;
132 template <> GTIRB_EXPORT_API void* Context::Allocate<ByteInterval>() const;
133 template <> GTIRB_EXPORT_API void* Context::Allocate<CodeBlock>() const;
134 template <> GTIRB_EXPORT_API void* Context::Allocate<DataBlock>() const;
135 template <> GTIRB_EXPORT_API void* Context::Allocate<IR>() const;
136 template <> GTIRB_EXPORT_API void* Context::Allocate<Module>() const;
137 template <> GTIRB_EXPORT_API void* Context::Allocate<ProxyBlock>() const;
138 template <> GTIRB_EXPORT_API void* Context::Allocate<Section>() const;
139 template <> GTIRB_EXPORT_API void* Context::Allocate<Symbol>() const;
140 
141 } // namespace gtirb
142 
143 namespace std {
144 
146 template <> struct hash<::gtirb::UUID> {
147  size_t operator()(const ::gtirb::UUID& UID) const {
148  return ::boost::hash<::gtirb::UUID>()(UID);
149  }
150 };
151 
152 } // namespace std
153 
154 #endif // GTIRB_CONTEXT_H
gtirb::CodeBlock
A basic block.
Definition: CodeBlock.hpp:54
gtirb::Context::Create
NodeTy * Create(Args &&... TheArgs)
Create an object of type T.
Definition: Context.hpp:126
gtirb::Node
Represents the base of the Node class hierarchy.
Definition: Node.hpp:39
gtirb::CfgNode
Represents the base of types that can be inserted into the CFG.
Definition: CfgNode.hpp:30
gtirb::UUID
boost::uuids::uuid UUID
Represents a universally unique identifier used to identify Node objects across serialization boundar...
Definition: Context.hpp:36
gtirb::DataBlock
Represents a data object, possibly symbolic.
Definition: DataBlock.hpp:44
gtirb::Context
The context under which GTIRB operations occur.
Definition: Context.hpp:63
gtirb::Section
Represents a named section of the binary.
Definition: Section.hpp:66
GTIRB_EXPORT_API
#define GTIRB_EXPORT_API
This macro controls the visibility of exported symbols (i.e. classes) in shared libraries....
Definition: Export.hpp:52
gtirb
Main namespace for the GTIRB API.
Definition: Addr.hpp:28
Export.hpp
gtirb::ProxyBlock
A placeholder that serves as the endpoint (source or target) of a CFG edge.
Definition: ProxyBlock.hpp:49
gtirb::IR
A complete internal representation consisting of Modules (Module).
Definition: IR.hpp:76
std
Definition: Addr.hpp:359
SpecificBumpPtrAllocator
Definition: Allocator.hpp:283
std::hash<::gtirb::UUID >::operator()
size_t operator()(const ::gtirb::UUID &UID) const
Definition: Context.hpp:147
gtirb::Module
Represents a single binary (library or executable).
Definition: Module.hpp:107
gtirb::ByteInterval
A contiguous region of bytes in a binary.
Definition: ByteInterval.hpp:116
gtirb::Symbol
Represents a Symbol, which maps a name to an object in the IR.
Definition: Symbol.hpp:43
Allocator.hpp