Skip to content

Commit f971b34

Browse files
authored
[NFC] Refactor manual size calculations in memory allocation (swiftlang#33256)
1 parent 0153cff commit f971b34

File tree

5 files changed

+13
-19
lines changed

5 files changed

+13
-19
lines changed

lib/AST/ASTContext.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2447,10 +2447,9 @@ Type TupleType::get(ArrayRef<TupleTypeElt> Fields, const ASTContext &C) {
24472447
}
24482448
}
24492449

2450+
size_t bytes = totalSizeToAlloc<TupleTypeElt>(Fields.size());
24502451
// TupleType will copy the fields list into ASTContext owned memory.
2451-
void *mem = C.Allocate(sizeof(TupleType) +
2452-
sizeof(TupleTypeElt) * Fields.size(),
2453-
alignof(TupleType), arena);
2452+
void *mem = C.Allocate(bytes, alignof(TupleType), arena);
24542453
auto New = new (mem) TupleType(Fields, IsCanonical ? &C : nullptr, properties,
24552454
hasElementWithOwnership);
24562455
C.getImpl().getArena(arena).TupleTypes.InsertNode(New, InsertPos);
@@ -2653,7 +2652,7 @@ BoundGenericType *BoundGenericType::get(NominalTypeDecl *TheDecl,
26532652
newType = new (mem) BoundGenericClassType(
26542653
theClass, Parent, GenericArgs, IsCanonical ? &C : nullptr, properties);
26552654
} else if (auto theStruct = dyn_cast<StructDecl>(TheDecl)) {
2656-
auto sz =BoundGenericStructType::totalSizeToAlloc<Type>(GenericArgs.size());
2655+
auto sz = BoundGenericStructType::totalSizeToAlloc<Type>(GenericArgs.size());
26572656
auto mem = C.Allocate(sz, alignof(BoundGenericStructType), arena);
26582657
newType = new (mem) BoundGenericStructType(
26592658
theStruct, Parent, GenericArgs, IsCanonical ? &C : nullptr, properties);

lib/AST/Attr.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1679,8 +1679,9 @@ DifferentiableAttr::create(AbstractFunctionDecl *original, bool implicit,
16791679
IndexSubset *parameterIndices,
16801680
GenericSignature derivativeGenSig) {
16811681
auto &ctx = original->getASTContext();
1682-
void *mem = ctx.Allocate(sizeof(DifferentiableAttr),
1683-
alignof(DifferentiableAttr));
1682+
1683+
size_t size = totalSizeToAlloc<ParsedAutoDiffParameter>(0);
1684+
void *mem = ctx.Allocate(size, alignof(DifferentiableAttr));
16841685
return new (mem) DifferentiableAttr(original, implicit, atLoc, baseRange,
16851686
linear, parameterIndices, derivativeGenSig);
16861687
}

lib/AST/Decl.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4480,10 +4480,8 @@ bool ClassDecl::walkSuperclasses(
44804480
EnumCaseDecl *EnumCaseDecl::create(SourceLoc CaseLoc,
44814481
ArrayRef<EnumElementDecl *> Elements,
44824482
DeclContext *DC) {
4483-
void *buf = DC->getASTContext()
4484-
.Allocate(sizeof(EnumCaseDecl) +
4485-
sizeof(EnumElementDecl*) * Elements.size(),
4486-
alignof(EnumCaseDecl));
4483+
size_t bytes = totalSizeToAlloc<EnumElementDecl *>(Elements.size());
4484+
void *buf = DC->getASTContext().Allocate(bytes, alignof(EnumCaseDecl));
44874485
return ::new (buf) EnumCaseDecl(CaseLoc, Elements, DC);
44884486
}
44894487

lib/AST/Expr.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,9 +1206,8 @@ InOutExpr::InOutExpr(SourceLoc operLoc, Expr *subExpr, Type baseType,
12061206

12071207
SequenceExpr *SequenceExpr::create(ASTContext &ctx, ArrayRef<Expr*> elements) {
12081208
assert(elements.size() & 1 && "even number of elements in sequence");
1209-
void *Buffer = ctx.Allocate(sizeof(SequenceExpr) +
1210-
elements.size() * sizeof(Expr*),
1211-
alignof(SequenceExpr));
1209+
size_t bytes = totalSizeToAlloc<Expr *>(elements.size());
1210+
void *Buffer = ctx.Allocate(bytes, alignof(SequenceExpr));
12121211
return ::new(Buffer) SequenceExpr(elements);
12131212
}
12141213

lib/AST/ImportCache.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,9 @@ ImportCache::getImportSet(ASTContext &ctx,
139139
// getImportedModulesForLookup().
140140
if (ImportSet *result = ImportSets.FindNodeOrInsertPos(ID, InsertPos))
141141
return *result;
142-
143-
void *mem = ctx.Allocate(
144-
sizeof(ImportSet) +
145-
sizeof(ModuleDecl::ImportedModule) * topLevelImports.size() +
146-
sizeof(ModuleDecl::ImportedModule) * transitiveImports.size(),
147-
alignof(ImportSet), AllocationArena::Permanent);
142+
143+
size_t bytes = ImportSet::totalSizeToAlloc<ModuleDecl::ImportedModule>(topLevelImports.size() + transitiveImports.size());
144+
void *mem = ctx.Allocate(bytes, alignof(ImportSet), AllocationArena::Permanent);
148145

149146
auto *result = new (mem) ImportSet(hasHeaderImportModule,
150147
topLevelImports,

0 commit comments

Comments
 (0)