/// this ASTContext object.
LangOptions LangOpts;
- /// Allocator - The allocator object used to create AST objects.
- llvm::MallocAllocator Allocator;
-
+ /// MallocAlloc/BumpAlloc - The allocator objects used to create AST objects.
+ bool FreeMemory;
+ llvm::MallocAllocator MallocAlloc;
+ llvm::BumpPtrAllocator BumpAlloc;
public:
TargetInfo &Target;
IdentifierTable &Idents;
DeclarationNameTable DeclarationNames;
SourceManager& getSourceManager() { return SourceMgr; }
- llvm::MallocAllocator &getAllocator() { return Allocator; }
- void Deallocate(void *Ptr) { Allocator.Deallocate(Ptr); }
-
+ void *Allocate(unsigned Size, unsigned Align = 8) {
+ return FreeMemory ? MallocAlloc.Allocate(Size, Align) :
+ BumpAlloc.Allocate(Size, Align);
+ }
+ void Deallocate(void *Ptr) {
+ if (FreeMemory)
+ MallocAlloc.Deallocate(Ptr);
+ }
const LangOptions& getLangOptions() const { return LangOpts; }
FullSourceLoc getFullLoc(SourceLocation Loc) const {
QualType DependentTy;
ASTContext(const LangOptions& LOpts, SourceManager &SM, TargetInfo &t,
- IdentifierTable &idents, SelectorTable &sels,
- unsigned size_reserve=0);
+ IdentifierTable &idents, SelectorTable &sels,
+ bool FreeMemory = true, unsigned size_reserve=0);
~ASTContext();
/// @return The allocated memory. Could be NULL.
inline void *operator new(size_t Bytes, clang::ASTContext &C,
size_t Alignment = 16) throw () {
- return C.getAllocator().Allocate(Bytes, Alignment);
+ return C.Allocate(Bytes, Alignment);
}
/// @brief Placement delete companion to the new above.
///
/// the ASTContext throws in the object constructor.
inline void operator delete(void *Ptr, clang::ASTContext &C)
throw () {
- C.getAllocator().Deallocate(Ptr);
+ C.Deallocate(Ptr);
}
#endif
ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
TargetInfo &t,
IdentifierTable &idents, SelectorTable &sels,
- unsigned size_reserve) :
+ bool FreeMem, unsigned size_reserve) :
CFConstantStringTypeDecl(0), ObjCFastEnumerationStateTypeDecl(0),
- SourceMgr(SM), LangOpts(LOpts), Target(t),
+ SourceMgr(SM), LangOpts(LOpts), FreeMemory(FreeMem), Target(t),
Idents(idents), Selectors(sels)
{
if (size_reserve > 0) Types.reserve(size_reserve);
// FunctionTypeProto objects are allocated with extra bytes after them
// for a variable size array (for parameter types) at the end of them.
FunctionTypeProto *FTP =
- (FunctionTypeProto*)Allocator.Allocate(sizeof(FunctionTypeProto) +
- NumArgs*sizeof(QualType), 8);
+ (FunctionTypeProto*)Allocate(sizeof(FunctionTypeProto) +
+ NumArgs*sizeof(QualType), 8);
new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
TypeQuals, Canonical);
Types.push_back(FTP);
EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
IdentifierInfo *Id,
EnumDecl *PrevDecl) {
- void *Mem = C.getAllocator().Allocate<EnumDecl>();
- EnumDecl *Enum = new (Mem) EnumDecl(DC, L, Id);
+ EnumDecl *Enum = new (C) EnumDecl(DC, L, Id);
C.getTypeDeclType(Enum, PrevDecl);
return Enum;
}
// Zero params -> null pointer.
if (NumParams) {
- void *Mem = C.getAllocator().Allocate<ParmVarDecl*>(NumParams);
+ void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
ParamInfo = new (Mem) ParmVarDecl*[NumParams];
memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
}
// FIXME: how do I pass in Size to ASTContext::new?
unsigned Size = sizeof(TemplateParameterList) + sizeof(Decl *) * NumParams;
unsigned Align = llvm::AlignOf<TemplateParameterList>::Alignment;
- void *Mem = C.getAllocator().Allocate(Size, Align);
+ void *Mem = C.Allocate(Size, Align);
return new (Mem) TemplateParameterList(Params, NumParams);
}
assert (numdecls > 0);
unsigned size = sizeof(DeclGroup) + sizeof(Decl*) * numdecls;
unsigned alignment = llvm::AlignOf<DeclGroup>::Alignment;
- void* mem = C.getAllocator().Allocate(size, alignment);
+ void* mem = C.Allocate(size, alignment);
new (mem) DeclGroup(numdecls, decls);
return static_cast<DeclGroup*>(mem);
}
unsigned NumDecls = (unsigned) D.ReadInt();
unsigned size = sizeof(DeclGroup) + sizeof(Decl*) * NumDecls;
unsigned alignment = llvm::AlignOf<DeclGroup>::Alignment;
- DeclGroup* DG = (DeclGroup*) C.getAllocator().Allocate(size, alignment);
+ DeclGroup* DG = (DeclGroup*) C.Allocate(size, alignment);
new (DG) DeclGroup();
DG->NumDecls = NumDecls;
D.BatchReadOwnedPtrs(NumDecls, &(*DG)[0], C);
Expr **IndexExprs, unsigned NumIndexExprs,
SourceLocation ColonOrEqualLoc,
bool UsesColonSyntax, Expr *Init) {
- void *Mem = C.getAllocator().Allocate(sizeof(DesignatedInitExpr) +
- sizeof(Designator) * NumDesignators +
- sizeof(Stmt *) * (NumIndexExprs + 1),
- 8);
+ void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
+ sizeof(Designator) * NumDesignators +
+ sizeof(Stmt *) * (NumIndexExprs + 1), 8);
DesignatedInitExpr *DIE
= new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators,
ColonOrEqualLoc, UsesColonSyntax,
ASTContext *Context =
new ASTContext(PP.getLangOptions(), PP.getSourceManager(),
PP.getTargetInfo(),
- PP.getIdentifierTable(), PP.getSelectorTable());
+ PP.getIdentifierTable(), PP.getSelectorTable(),
+ FreeMemory);
TranslationUnit *TU = new TranslationUnit(*Context);
Sema S(PP, *Context, *Consumer);
Parser P(PP, S);