From: Jay Freeman (saurik) Date: Thu, 19 Nov 2009 05:45:22 +0000 (+0000) Subject: Fixed all of the memory leaks caused by the new identifier renamer. X-Git-Tag: v0.9.432~146 X-Git-Url: https://git.saurik.com/cycript.git/commitdiff_plain/a846a8cdfbbb59f5f545db03dd7c5fee43c2056a Fixed all of the memory leaks caused by the new identifier renamer. --- diff --git a/Output.cpp b/Output.cpp index 552768b..a3765da 100644 --- a/Output.cpp +++ b/Output.cpp @@ -432,8 +432,6 @@ void CYFunction::Output(CYOutput &out, CYFlags flags) const { if (protect) out << '('; out << "function"; - if (out.options_.verbose_) - out.out_ << ':' << static_cast(this); if (name_ != NULL) out << ' ' << *name_; out << '(' << parameters_ << ')'; diff --git a/Parser.hpp b/Parser.hpp index 73a605c..9ff1d38 100644 --- a/Parser.hpp +++ b/Parser.hpp @@ -324,8 +324,8 @@ typedef std::vector CYIdentifierUsageVector; struct CYScope { CYScope *parent_; - CYIdentifierAddressFlagsMap internal_; + CYIdentifierAddressFlagsMap internal_; CYIdentifierValueSet identifiers_; CYScope() : @@ -343,11 +343,9 @@ struct CYScope { }; struct CYProgram : - CYScope, CYThing { CYStatement *statements_; - CYIdentifierUsageVector rename_; CYProgram(CYStatement *statements) : statements_(statements) @@ -364,13 +362,12 @@ struct CYContext : apr_pool_t *pool_; CYOptions &options_; CYScope *scope_; - CYProgram *program_; + CYIdentifierUsageVector rename_; CYContext(apr_pool_t *pool, CYOptions &options) : pool_(pool), options_(options), - scope_(this), - program_(NULL) + scope_(this) { } @@ -395,11 +392,9 @@ struct CYBlock : CYThing { CYStatement *statements_; - CYScope *scope_; - CYBlock(CYStatement *statements, CYScope *scope = NULL) : - statements_(statements), - scope_(scope) + CYBlock(CYStatement *statements) : + statements_(statements) { } @@ -1372,9 +1367,7 @@ struct CYWhile : virtual void Output(CYOutput &out, CYFlags flags) const; }; -struct CYFunction : - CYScope -{ +struct CYFunction { CYIdentifier *name_; CYFunctionParameter *parameters_; CYBlock code_; @@ -1382,7 +1375,7 @@ struct CYFunction : CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) : name_(name), parameters_(parameters), - code_(statements, this) + code_(statements) { } diff --git a/Pooling.hpp b/Pooling.hpp index f4045ab..c907662 100644 --- a/Pooling.hpp +++ b/Pooling.hpp @@ -46,6 +46,8 @@ #include "Exception.hpp" #include "Standard.hpp" +#include + _finline void *operator new(size_t size, apr_pool_t *pool) { return apr_palloc(pool, size); } @@ -108,4 +110,58 @@ struct CYData { }; +template +struct CYPoolAllocator { + apr_pool_t *pool_; + + typedef Type_ value_type; + typedef value_type *pointer; + typedef const value_type *const_pointer; + typedef value_type &reference; + typedef const value_type &const_reference; + typedef std::size_t size_type; + typedef std::ptrdiff_t difference_type; + + CYPoolAllocator() : + pool_(NULL) + { + } + + template + CYPoolAllocator(const CYPoolAllocator &rhs) : + pool_(rhs.pool_) + { + } + + pointer allocate(size_type size, const void *hint = 0) { + return reinterpret_cast(apr_palloc(pool_, size)); + } + + void deallocate(pointer data, size_type size) { + } + + void construct(pointer address, const Type_ &rhs) { + new(address) Type_(rhs); + } + + void destroy(pointer address) { + address->~Type_(); + } + + template + inline bool operator==(const CYPoolAllocator &rhs) { + return pool_ == rhs.pool_; + } + + template + inline bool operator!=(const CYPoolAllocator &rhs) { + return !operator==(rhs); + } + + template + struct rebind { + typedef CYPoolAllocator other; + }; +}; + #endif/*CYPOOLING_HPP*/ diff --git a/Replace.cpp b/Replace.cpp index b54bfe3..8938704 100644 --- a/Replace.cpp +++ b/Replace.cpp @@ -338,8 +338,9 @@ void CYFunction::Replace_(CYContext &context, bool outer) { if (outer) Inject(context); - parent_ = context.scope_; - context.scope_ = this; + CYScope scope; + scope.parent_ = context.scope_; + context.scope_ = &scope; if (!outer && name_ != NULL) Inject(context); @@ -347,8 +348,8 @@ void CYFunction::Replace_(CYContext &context, bool outer) { parameters_->Replace(context); code_.Replace(context); - context.scope_ = parent_; - Scope(context, code_.statements_); + context.scope_ = scope.parent_; + scope.Scope(context, code_.statements_); } CYExpression *CYFunctionExpression::Replace(CYContext &context) { @@ -368,11 +369,9 @@ CYStatement *CYFunctionStatement::Replace(CYContext &context) { } CYIdentifier *CYIdentifier::Replace(CYContext &context) { - if (replace_ == NULL) { - replace_ = context.scope_->Lookup(context, this); - ++replace_->usage_; - } else if (replace_ != this) + if (replace_ != NULL && replace_ != this) return replace_->Replace(context); + replace_ = context.scope_->Lookup(context, this); return replace_; } @@ -478,32 +477,30 @@ namespace { } void CYProgram::Replace(CYContext &context) { - parent_ = context.scope_; - CYProgram *program(context.program_); - - context.scope_ = this; - context.program_ = this; + CYScope scope; + scope.parent_ = context.scope_; + context.scope_ = &scope; statements_ = statements_->ReplaceAll(context); - context.scope_ = parent_; - context.program_ = program; - Scope(context, statements_); + context.scope_ = scope.parent_; + + scope.Scope(context, statements_); size_t offset(0); CYCStringSet external; - for (CYIdentifierValueSet::const_iterator i(identifiers_.begin()); i != identifiers_.end(); ++i) + for (CYIdentifierValueSet::const_iterator i(scope.identifiers_.begin()); i != scope.identifiers_.end(); ++i) external.insert((*i)->Word()); IdentifierUsages usages; - if (offset < rename_.size()) - for (CYIdentifier *i(rename_[offset].identifier_); i != NULL; i = i->next_) - usages.insert(i); + if (offset < context.rename_.size()) + for (CYIdentifier *i(context.rename_[offset].identifier_); i != NULL; i = i->next_) + usages.insert(i); // XXX: totalling the probable occurrences and sorting by them would improve the result - for (CYIdentifierUsageVector::const_iterator i(rename_.begin()); i != rename_.end(); ++i, ++offset) { + for (CYIdentifierUsageVector::const_iterator i(context.rename_.begin()); i != context.rename_.end(); ++i, ++offset) { //std::cout << *i << ":" << (*i)->offset_ << std::endl; const char *name; @@ -591,7 +588,7 @@ namespace { if (lhs.flags_ != rhs.flags_) return lhs.flags_ < rhs.flags_; /*if (lhs.usage_ != rhs.usage_) - return lhs.usage_ > rhs.usage_;*/ + return lhs.usage_ < rhs.usage_;*/ return lhs.identifier_ < rhs.identifier_; } }; @@ -601,12 +598,11 @@ namespace { void CYScope::Scope(CYContext &context, CYStatement *&statements) { CYDeclarations *last(NULL), *curr(NULL); - CYProgram *program(context.program_); IdentifierOffsets offsets; for (CYIdentifierAddressFlagsMap::const_iterator i(internal_.begin()); i != internal_.end(); ++i) - if (program != NULL && i->second != CYIdentifierMagic) + if (i->second != CYIdentifierMagic) offsets.insert(IdentifierOffset(i->first, i->second)); size_t offset(0); @@ -623,10 +619,10 @@ void CYScope::Scope(CYContext &context, CYStatement *&statements) { if (offset < i->offset_) offset = i->offset_; - if (program->rename_.size() <= offset) - program->rename_.resize(offset + 1); + if (context.rename_.size() <= offset) + context.rename_.resize(offset + 1); - CYIdentifierUsage &rename(program->rename_[offset++]); + CYIdentifierUsage &rename(context.rename_[offset++]); i->identifier_->SetNext(rename.identifier_); rename.identifier_ = i->identifier_; rename.usage_ += i->identifier_->usage_ + 1; diff --git a/todo.txt b/todo.txt index 7b5bbd3..343ed26 100644 --- a/todo.txt +++ b/todo.txt @@ -24,6 +24,4 @@ the concept of NULL pooling is entirely incorrect and sad... bad... evil... need NSArray's .toString() and .toLocaleString() fail hard, as Array.prototype.to*String are Array-specific (4).toString() is legal, but I'm stripping the ()'s somehow in the serializer applyOnMainThread, when done at console, loops the cyonifier - -!! CYScope has a bunch of STL container objects that are leaking /all/ of their memory special work needs to be done to correctly handle the "arguments" symbol: Declare("arguments", ...Special)