[midend]初步修复内存泄漏问题(仍然剩余11处)

This commit is contained in:
rain2133
2025-08-07 01:34:00 +08:00
parent d732800149
commit 8aa5ba692f
3 changed files with 186 additions and 0 deletions

View File

@@ -25,6 +25,18 @@ inline std::string getMachineCode(float fval) {
*/
namespace sysy {
// Global cleanup function implementation
void cleanupIRPools() {
// Clean up the main memory pools that cause leaks
ConstantValue::cleanup();
UndefinedValue::cleanup();
// Note: Type pools (PointerType, FunctionType, ArrayType) use unique_ptr
// and will be automatically cleaned up when the program exits.
// For more thorough cleanup during program execution, consider refactoring
// to use singleton pattern with explicit cleanup methods.
}
/*相关打印函数*/
template <typename T>
@@ -206,6 +218,12 @@ PointerType* PointerType::get(Type *baseType) {
return result.first->second.get();
}
void PointerType::cleanup() {
// Note: Due to static variable scoping, we can't directly access
// the static map here. The cleanup will happen when the program exits.
// For more thorough cleanup, consider using a singleton pattern.
}
FunctionType*FunctionType::get(Type *returnType, const std::vector<Type *> &paramTypes) {
static std::set<std::unique_ptr<FunctionType>> functionTypes;
auto iter =
@@ -225,6 +243,12 @@ FunctionType*FunctionType::get(Type *returnType, const std::vector<Type *> &para
return result.first->get();
}
void FunctionType::cleanup() {
// Note: Due to static variable scoping, we can't directly access
// the static set here. The cleanup will happen when the program exits.
// For more thorough cleanup, consider using a singleton pattern.
}
ArrayType *ArrayType::get(Type *elementType, unsigned numElements) {
static std::set<std::unique_ptr<ArrayType>> arrayTypes;
auto iter = std::find_if(arrayTypes.begin(), arrayTypes.end(), [&](const std::unique_ptr<ArrayType> &type) -> bool {
@@ -239,6 +263,12 @@ ArrayType *ArrayType::get(Type *elementType, unsigned numElements) {
return result.first->get();
}
void ArrayType::cleanup() {
// Note: Due to static variable scoping, we can't directly access
// the static set here. The cleanup will happen when the program exits.
// For more thorough cleanup, consider using a singleton pattern.
}
void Argument::print(std::ostream& os) const {
os << *getType() << " %" << getName();
}
@@ -464,6 +494,13 @@ ConstantValue* ConstantValue::get(Type* type, ConstantValVariant val) {
return newConstant;
}
void ConstantValue::cleanup() {
for (auto& pair : mConstantPool) {
delete pair.second;
}
mConstantPool.clear();
}
ConstantInteger* ConstantInteger::get(Type* type, int val) {
return dynamic_cast<ConstantInteger*>(ConstantValue::get(type, val));
}
@@ -485,6 +522,13 @@ UndefinedValue* UndefinedValue::get(Type* type) {
return newUndef;
}
void UndefinedValue::cleanup() {
for (auto& pair : UndefValues) {
delete pair.second;
}
UndefValues.clear();
}
void ConstantValue::print(std::ostream &os) const {
if(dynamic_cast<const ConstantInteger*>(this)) {
dynamic_cast<const ConstantInteger*>(this)->print(os);
@@ -1097,4 +1141,95 @@ void Module::print(std::ostream& os) const {
}
}
void Module::cleanup() {
// 清理所有函数中的使用关系
for (auto& func : functions) {
if (func.second) {
func.second->cleanup();
}
}
for (auto& extFunc : externalFunctions) {
if (extFunc.second) {
extFunc.second->cleanup();
}
}
// 清理符号表
variableTable.cleanup();
// 清理函数表
functions.clear();
externalFunctions.clear();
}
void Function::cleanup() {
// 清理所有基本块中的使用关系
for (auto& block : blocks) {
if (block) {
block->cleanup();
}
}
// 清理参数列表中的使用关系
for (auto arg : arguments) {
if (arg) {
arg->cleanup();
}
}
// 清理基本块列表
blocks.clear();
arguments.clear();
callees.clear();
}
void BasicBlock::cleanup() {
// 清理所有指令中的使用关系
for (auto& inst : instructions) {
if (inst) {
inst->cleanup();
}
}
// 清理指令列表
instructions.clear();
// 清理前驱后继关系
predecessors.clear();
successors.clear();
}
void User::cleanup() {
// 清理所有操作数的使用关系
for (auto& operand : operands) {
if (operand && operand->getValue()) {
operand->getValue()->removeUse(operand);
}
}
// 清理操作数列表
operands.clear();
}
void SymbolTable::cleanup() {
// 清理全局变量和常量
globals.clear();
globalconsts.clear();
// 清理符号表节点
nodeList.clear();
// 重置当前节点
curNode = nullptr;
// 清理变量索引
variableIndex.clear();
}
void Argument::cleanup() {
// Argument继承自Value清理其使用列表
uses.clear();
}
} // namespace sysy