diff options
author | Ted Kremenek <kremenek@apple.com> | 2010-11-10 05:59:39 +0000 |
---|---|---|
committer | Ted Kremenek <kremenek@apple.com> | 2010-11-10 05:59:39 +0000 |
commit | 5eec2b0bd38ad650d09c8afa01e6f3df002d9816 (patch) | |
tree | 3b2f6e5477a623dd01e8fc463adba6245df35983 /clang/lib/Parse/ParseObjc.cpp | |
parent | 2703bebc58f627a5cdf0bcb9bb205d8f552acf78 (diff) | |
download | bcm5719-llvm-5eec2b0bd38ad650d09c8afa01e6f3df002d9816.tar.gz bcm5719-llvm-5eec2b0bd38ad650d09c8afa01e6f3df002d9816.zip |
Region-allocate all AttributeList objects from a factory object instead of manually managing them
using new/delete and OwningPtrs. After memory profiling Clang, I witnessed periodic leaks of these
objects; digging deeper into the code, it was clear that our management of these objects was a mess. The ownership rules were murky at best, and not always followed. Worse, there are plenty of error paths where we could screw up.
This patch introduces AttributeList::Factory, which is a factory class that creates AttributeList
objects and then blows them away all at once. While conceptually simple, most of the changes in
this patch just have to do with migrating over to the new interface. Most of the changes have resulted in some nice simplifications.
This new strategy currently holds on to all AttributeList objects during the lifetime of the Parser
object. This is easily tunable. If we desire to have more bound the lifetime of AttributeList
objects more precisely, we can have the AttributeList::Factory object (in Parser) push/pop its
underlying allocator as we enter/leave key methods in the Parser. This means that we get
simple memory management while still having the ability to finely control memory use if necessary.
Note that because AttributeList objects are now BumpPtrAllocated, we may reduce malloc() traffic
in many large files with attributes.
This fixes the leak reported in: <rdar://problem/8650003>
llvm-svn: 118675
Diffstat (limited to 'clang/lib/Parse/ParseObjc.cpp')
-rw-r--r-- | clang/lib/Parse/ParseObjc.cpp | 21 |
1 files changed, 6 insertions, 15 deletions
diff --git a/clang/lib/Parse/ParseObjc.cpp b/clang/lib/Parse/ParseObjc.cpp index 3475602c957..d18efac5e2c 100644 --- a/clang/lib/Parse/ParseObjc.cpp +++ b/clang/lib/Parse/ParseObjc.cpp @@ -833,9 +833,9 @@ Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc, ReturnType = ParseObjCTypeName(DSRet, false); // If attributes exist before the method, parse them. - llvm::OwningPtr<AttributeList> MethodAttrs; + AttributeList *MethodAttrs = 0; if (getLang().ObjC2 && Tok.is(tok::kw___attribute)) - MethodAttrs.reset(ParseGNUAttributes()); + MethodAttrs = ParseGNUAttributes(); if (Tok.is(tok::code_completion)) { Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus, @@ -860,8 +860,7 @@ Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc, if (Tok.isNot(tok::colon)) { // If attributes exist after the method, parse them. if (getLang().ObjC2 && Tok.is(tok::kw___attribute)) - MethodAttrs.reset(addAttributeLists(MethodAttrs.take(), - ParseGNUAttributes())); + MethodAttrs = addAttributeLists(MethodAttrs, ParseGNUAttributes()); Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent); Decl *Result @@ -869,8 +868,7 @@ Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc, mType, IDecl, DSRet, ReturnType, Sel, 0, CParamInfo.data(), CParamInfo.size(), - MethodAttrs.get(), - MethodImplKind); + MethodAttrs, MethodImplKind); PD.complete(Result); return Result; } @@ -970,8 +968,7 @@ Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc, // FIXME: Add support for optional parameter list... // If attributes exist after the method, parse them. if (getLang().ObjC2 && Tok.is(tok::kw___attribute)) - MethodAttrs.reset(addAttributeLists(MethodAttrs.take(), - ParseGNUAttributes())); + MethodAttrs = addAttributeLists(MethodAttrs, ParseGNUAttributes()); if (KeyIdents.size() == 0) return 0; @@ -982,15 +979,9 @@ Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc, mType, IDecl, DSRet, ReturnType, Sel, &ArgInfos[0], CParamInfo.data(), CParamInfo.size(), - MethodAttrs.get(), + MethodAttrs, MethodImplKind, isVariadic); PD.complete(Result); - - // Delete referenced AttributeList objects. - for (llvm::SmallVectorImpl<Sema::ObjCArgInfo>::iterator - I = ArgInfos.begin(), E = ArgInfos.end(); I != E; ++I) - delete I->ArgAttrs; - return Result; } |