Skip to content

Commit

Permalink
Add the ability for the Context to "forget" about slab allocations.
Browse files Browse the repository at this point in the history
This is useful for projects that create one long-lived Context object for all
their GTIRB needs, and the object is destroyed at program shutdown. That
scenario results in doing a lot of wasted effort deallocating objects when the
OS will be reclaiming that memory anyway.
  • Loading branch information
AaronBallman committed May 7, 2020
1 parent 329b177 commit bc9fbf2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
9 changes: 9 additions & 0 deletions include/gtirb/Allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,15 @@ template <typename T> class SpecificBumpPtrAllocator {
/// Allocate space for an array of objects without constructing them.
T* Allocate(size_t num = 1) { return Allocator.Allocate<T>(num); }

/// Forgets all allocations from the underlying allocator, effectively
/// leaking the memory. This is useful when the allocator is no longer needed
/// and the operating system will be reclaiming the memory (such as at
// program shutdown time).
void ForgetAllocations() {
Allocator.Slabs.clear();
Allocator.CustomSizedSlabs.clear();
}

private:
/// Call the destructor of each allocated object and deallocate all but the
/// current slab and reset the current pointer to the beginning of it, freeing
Expand Down
5 changes: 5 additions & 0 deletions include/gtirb/Context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ class GTIRB_EXPORT_API Context {
Context();
~Context();

/// \brief Forgets all arena allocations held by this \ref Context object.
/// This can be useful under circumstances where leaking the memory is
/// acceptable, such as when shutting a program down.
void ForgetAllocations();

/// \brief Create an object of type \ref T.
///
/// \tparam NodeTy The type of object for which to allocate memory.
Expand Down
12 changes: 12 additions & 0 deletions src/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ Context::~Context() = default;

void Context::unregisterNode(const Node* N) { UuidMap.erase(N->getUUID()); }

void Context::ForgetAllocations() {
NodeAllocator.ForgetAllocations();
CodeBlockAllocator.ForgetAllocations();
ByteIntervalAllocator.ForgetAllocations();
DataBlockAllocator.ForgetAllocations();
IrAllocator.ForgetAllocations();
ModuleAllocator.ForgetAllocations();
ProxyBlockAllocator.ForgetAllocations();
SectionAllocator.ForgetAllocations();
SymbolAllocator.ForgetAllocations();
}

const Node* Context::findNode(const UUID& ID) const {
auto Iter = UuidMap.find(ID);
return Iter != UuidMap.end() ? Iter->second : nullptr;
Expand Down

0 comments on commit bc9fbf2

Please sign in to comment.