diff options
| author | Yong He <yonghe@outlook.com> | 2018-02-20 23:55:51 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-02-20 23:55:51 -0500 |
| commit | 01c4134d33cea3a4f98fad9248584278fd4bc452 (patch) | |
| tree | 8ca6b0f7e844fbf56cb1991284aff3ebbcc8aa89 /source/slang/ir.h | |
| parent | 51cdcad24b5271ac8c0f816174c6a760e264ed9e (diff) | |
| parent | 4cf46e5c8b2af8a4ea4db15cd402aae4145a614c (diff) | |
Merge pull request #417 from csyonghe/leakfix
Fix IR memory leaks.
Diffstat (limited to 'source/slang/ir.h')
| -rw-r--r-- | source/slang/ir.h | 42 |
1 files changed, 40 insertions, 2 deletions
diff --git a/source/slang/ir.h b/source/slang/ir.h index 0dc9daa9a..fdca73a83 100644 --- a/source/slang/ir.h +++ b/source/slang/ir.h @@ -10,6 +10,7 @@ #include "../core/basic.h" #include "source-loc.h" +#include "memory_pool.h" namespace Slang { @@ -114,10 +115,24 @@ enum IRDecorationOp : uint16_t kIRDecorationOp_TargetIntrinsic, }; +// represents an object allocated in an IR memory pool +struct IRObject +{ + bool isDestroyed = false; + virtual void dispose() + { + isDestroyed = true; + } + virtual ~IRObject() + { + isDestroyed = true; + } +}; + // A "decoration" that gets applied to an instruction. // These usually don't affect semantics, but are useful // for preserving high-level source information. -struct IRDecoration +struct IRDecoration : public IRObject { // Next decoration attached to the same instruction IRDecoration* next; @@ -132,7 +147,7 @@ typedef Type IRType; struct IRBlock; // Base class for values in the IR -struct IRValue +struct IRValue : public IRObject { // The operation that this value represents IROp op; @@ -169,6 +184,8 @@ struct IRValue // Free a value (which needs to have been removed // from its parent, had its uses eliminated, etc.) void deallocate(); + + virtual void dispose() override; }; // Values that are contained in a doubly-linked @@ -480,6 +497,11 @@ struct IRGlobalValue : IRValue void removeFromParent(); void moveToEnd(); + virtual void dispose() override + { + IRValue::dispose(); + mangledName = String(); + } }; /// @brief A global value that potentially holds executable code. @@ -529,6 +551,12 @@ struct IRFunc : IRGlobalValueWithCode // which are actually the parameters of the first // block. IRParam* getFirstParam(); + + virtual void dispose() override + { + IRGlobalValueWithCode::dispose(); + genericDecls = decltype(genericDecls)(); + } }; // A module is a parent to functions, global variables, types, etc. @@ -536,6 +564,8 @@ struct IRModule : RefObject { // The compilation session in use. Session* session; + MemoryPool memoryPool; + List<IRObject*> irObjectsToFree; // list of ir objects to run destructor upon destruction // A list of all the functions and other // global values declared in this module. @@ -544,6 +574,14 @@ struct IRModule : RefObject IRGlobalValue* getFirstGlobalValue() { return firstGlobalValue; } IRGlobalValue* getlastGlobalValue() { return lastGlobalValue; } + + ~IRModule() + { + for (auto val : irObjectsToFree) + if (!val->isDestroyed) + val->dispose(); + irObjectsToFree = List<IRObject*>(); + } }; void printSlangIRAssembly(StringBuilder& builder, IRModule* module); |
