GTIRB  v2.3.1
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 <boost/uuid/uuid_hash.hpp>
23 #include <cstdlib>
24 #include <functional>
25 #include <map>
26 
29 
30 namespace gtirb {
31 
37 using UUID = boost::uuids::uuid;
38 
39 class Node;
40 class CfgNode;
41 class ByteInterval;
42 class CodeBlock;
43 class DataBlock;
44 class IR;
45 class Module;
46 class ProxyBlock;
47 class Section;
48 class Symbol;
49 
65  // Note: this must be declared first so it outlives the allocators. They
66  // will access the UuidMap during their destructors to unregister nodes.
67  std::map<UUID, Node*> UuidMap;
68 
69  // Allocate each node type in a separate arena.
70  mutable SpecificBumpPtrAllocator<Node> NodeAllocator;
71  mutable SpecificBumpPtrAllocator<ByteInterval> ByteIntervalAllocator;
72  mutable SpecificBumpPtrAllocator<CodeBlock> CodeBlockAllocator;
73  mutable SpecificBumpPtrAllocator<DataBlock> DataBlockAllocator;
74  mutable SpecificBumpPtrAllocator<IR> IrAllocator;
75  mutable SpecificBumpPtrAllocator<Module> ModuleAllocator;
76  mutable SpecificBumpPtrAllocator<ProxyBlock> ProxyBlockAllocator;
77  mutable SpecificBumpPtrAllocator<Section> SectionAllocator;
78  mutable SpecificBumpPtrAllocator<Symbol> SymbolAllocator;
79 
81  friend class Node;
82 
83  void registerNode(const UUID& ID, Node* N) { UuidMap[ID] = N; }
84 
85  void unregisterNode(const Node* N);
86  const Node* findNode(const UUID& ID) const;
87  Node* findNode(const UUID& ID);
88 
95  template <class T> void* Allocate() const;
96 
105  void Deallocate(void*, size_t) const {
106  // Noop -- we don't want callers to deallocate individual allocations, but
107  // should instead deallocate the entire Context object to free memory.
108  }
109 
110 public:
111  Context();
112  ~Context();
113 
117  void ForgetAllocations();
118 
126  template <typename NodeTy, typename... Args>
127  NodeTy* Create(Args&&... TheArgs) {
128  return new (Allocate<NodeTy>()) NodeTy(std::forward<Args>(TheArgs)...);
129  }
130 };
131 
132 template <> GTIRB_EXPORT_API void* Context::Allocate<Node>() const;
133 template <> GTIRB_EXPORT_API void* Context::Allocate<ByteInterval>() const;
134 template <> GTIRB_EXPORT_API void* Context::Allocate<CodeBlock>() const;
135 template <> GTIRB_EXPORT_API void* Context::Allocate<DataBlock>() const;
136 template <> GTIRB_EXPORT_API void* Context::Allocate<IR>() const;
137 template <> GTIRB_EXPORT_API void* Context::Allocate<Module>() const;
138 template <> GTIRB_EXPORT_API void* Context::Allocate<ProxyBlock>() const;
139 template <> GTIRB_EXPORT_API void* Context::Allocate<Section>() const;
140 template <> GTIRB_EXPORT_API void* Context::Allocate<Symbol>() const;
141 
142 } // namespace gtirb
143 
144 #endif // GTIRB_CONTEXT_H
gtirb::CodeBlock
A basic block.
Definition: CodeBlock.hpp:47
gtirb::Context::Create
NodeTy * Create(Args &&... TheArgs)
Create an object of type T.
Definition: Context.hpp:127
gtirb::SpecificBumpPtrAllocator
Definition: Allocator.hpp:299
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:37
gtirb::DataBlock
Represents a data object, possibly symbolic.
Definition: DataBlock.hpp:44
gtirb::Context
The context under which GTIRB operations occur.
Definition: Context.hpp:64
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
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:124
gtirb::Symbol
Represents a Symbol, which maps a name to an object in the IR.
Definition: Symbol.hpp:43
Allocator.hpp