@@ -46,21 +46,28 @@ class ArenaAllocator {
46
46
}
47
47
}
48
48
49
- void *alloc (size_t Size) {
49
+ template <typename T, typename ... Args> T *alloc (Args &&... ConstructorArgs) {
50
+
51
+ size_t Size = sizeof (T);
50
52
assert (Size < Unit);
51
53
assert (Head && Head->Buf );
52
54
53
- uint8_t *P = Head->Buf + Head->Used ;
54
- Head->Used += Size;
55
+ size_t P = (size_t )Head->Buf + Head->Used ;
56
+ uintptr_t AlignedP =
57
+ (((size_t )P + alignof (T) - 1 ) & ~(size_t )(alignof (T) - 1 ));
58
+ uint8_t *PP = (uint8_t *)AlignedP;
59
+ size_t Adjustment = AlignedP - P;
60
+
61
+ Head->Used += Size + Adjustment;
55
62
if (Head->Used < Unit)
56
- return P ;
63
+ return new (PP) T (std::forward<Args>(ConstructorArgs)...) ;
57
64
58
65
AllocatorNode *NewHead = new AllocatorNode;
59
66
NewHead->Buf = new uint8_t [ArenaAllocator::Unit];
60
67
NewHead->Next = Head;
61
68
Head = NewHead;
62
69
NewHead->Used = Size;
63
- return NewHead->Buf ;
70
+ return new ( NewHead->Buf ) T (std::forward<Args>(ConstructorArgs)...) ;
64
71
}
65
72
66
73
private:
@@ -84,8 +91,6 @@ static void outputSpaceIfNecessary(OutputStream &OS) {
84
91
OS << " " ;
85
92
}
86
93
87
- void *operator new (size_t Size, ArenaAllocator &A) { return A.alloc (Size); }
88
-
89
94
// Storage classes
90
95
enum Qualifiers : uint8_t {
91
96
Q_None = 0 ,
@@ -374,7 +379,7 @@ static void outputName(OutputStream &OS, const Name *TheName) {
374
379
namespace {
375
380
376
381
Type *Type::clone (ArenaAllocator &Arena) const {
377
- return new ( Arena) Type (*this );
382
+ return Arena. alloc < Type> (*this );
378
383
}
379
384
380
385
// Write the "first half" of a given type.
@@ -471,7 +476,7 @@ void Type::outputPre(OutputStream &OS) {
471
476
void Type::outputPost (OutputStream &OS) {}
472
477
473
478
Type *PointerType::clone (ArenaAllocator &Arena) const {
474
- return new ( Arena) PointerType (*this );
479
+ return Arena. alloc < PointerType> (*this );
475
480
}
476
481
477
482
void PointerType::outputPre (OutputStream &OS) {
@@ -509,7 +514,7 @@ void PointerType::outputPost(OutputStream &OS) {
509
514
}
510
515
511
516
Type *FunctionType::clone (ArenaAllocator &Arena) const {
512
- return new ( Arena) FunctionType (*this );
517
+ return Arena. alloc < FunctionType> (*this );
513
518
}
514
519
515
520
void FunctionType::outputPre (OutputStream &OS) {
@@ -536,7 +541,7 @@ void FunctionType::outputPost(OutputStream &OS) {
536
541
}
537
542
538
543
Type *UdtType::clone (ArenaAllocator &Arena) const {
539
- return new ( Arena) UdtType (*this );
544
+ return Arena. alloc < UdtType> (*this );
540
545
}
541
546
542
547
void UdtType::outputPre (OutputStream &OS) {
@@ -561,7 +566,7 @@ void UdtType::outputPre(OutputStream &OS) {
561
566
}
562
567
563
568
Type *ArrayType::clone (ArenaAllocator &Arena) const {
564
- return new ( Arena) ArrayType (*this );
569
+ return Arena. alloc < ArrayType> (*this );
565
570
}
566
571
567
572
void ArrayType::outputPre (OutputStream &OS) {
@@ -659,9 +664,9 @@ class Demangler {
659
664
void Demangler::parse () {
660
665
// MSVC-style mangled symbols must start with '?'.
661
666
if (!MangledName.consumeFront (" ?" )) {
662
- SymbolName = new ( Arena) Name;
667
+ SymbolName = Arena. alloc < Name>() ;
663
668
SymbolName->Str = MangledName;
664
- SymbolType = new ( Arena) Type;
669
+ SymbolType = Arena. alloc < Type>() ;
665
670
SymbolType->Prim = PrimTy::Unknown;
666
671
}
667
672
@@ -832,7 +837,7 @@ Name *Demangler::demangleName() {
832
837
Name *Head = nullptr ;
833
838
834
839
while (!MangledName.consumeFront (" @" )) {
835
- Name *Elem = new ( Arena) Name;
840
+ Name *Elem = Arena. alloc < Name>() ;
836
841
837
842
assert (!Error);
838
843
demangleNamePiece (*Elem, Head == nullptr );
@@ -1239,7 +1244,7 @@ ReferenceKind Demangler::demangleReferenceKind() {
1239
1244
}
1240
1245
1241
1246
Type *Demangler::demangleFunctionEncoding () {
1242
- FunctionType *FTy = new ( Arena) FunctionType;
1247
+ FunctionType *FTy = Arena. alloc < FunctionType>() ;
1243
1248
1244
1249
FTy->Prim = PrimTy::Function;
1245
1250
FTy->FunctionClass = (FuncClass)demangleFunctionClass ();
@@ -1265,7 +1270,7 @@ Type *Demangler::demangleFunctionEncoding() {
1265
1270
1266
1271
// Reads a primitive type.
1267
1272
Type *Demangler::demangleBasicType () {
1268
- Type *Ty = new ( Arena) Type;
1273
+ Type *Ty = Arena. alloc < Type>() ;
1269
1274
1270
1275
switch (MangledName.popFront ()) {
1271
1276
case ' X' :
@@ -1335,7 +1340,7 @@ Type *Demangler::demangleBasicType() {
1335
1340
}
1336
1341
1337
1342
UdtType *Demangler::demangleClassType () {
1338
- UdtType *UTy = new ( Arena) UdtType;
1343
+ UdtType *UTy = Arena. alloc < UdtType>() ;
1339
1344
1340
1345
switch (MangledName.popFront ()) {
1341
1346
case ' T' :
@@ -1365,7 +1370,7 @@ UdtType *Demangler::demangleClassType() {
1365
1370
// <pointer-type> ::= E? <pointer-cvr-qualifiers> <ext-qualifiers> <type>
1366
1371
// # the E is required for 64-bit non-static pointers
1367
1372
PointerType *Demangler::demanglePointerType () {
1368
- PointerType *Pointer = new ( Arena) PointerType;
1373
+ PointerType *Pointer = Arena. alloc < PointerType>() ;
1369
1374
1370
1375
Pointer->Quals = Q_None;
1371
1376
switch (MangledName.popFront ()) {
@@ -1392,7 +1397,7 @@ PointerType *Demangler::demanglePointerType() {
1392
1397
}
1393
1398
1394
1399
if (MangledName.consumeFront (" 6" )) {
1395
- FunctionType *FTy = new ( Arena) FunctionType;
1400
+ FunctionType *FTy = Arena. alloc < FunctionType>() ;
1396
1401
FTy->Prim = PrimTy::Function;
1397
1402
FTy->CallConvention = demangleCallingConvention ();
1398
1403
@@ -1435,12 +1440,12 @@ ArrayType *Demangler::demangleArrayType() {
1435
1440
return nullptr ;
1436
1441
}
1437
1442
1438
- ArrayType *ATy = new ( Arena) ArrayType;
1443
+ ArrayType *ATy = Arena. alloc < ArrayType>() ;
1439
1444
ArrayType *Dim = ATy;
1440
1445
for (int I = 0 ; I < Dimension; ++I) {
1441
1446
Dim->Prim = PrimTy::Array;
1442
1447
Dim->ArrayDimension = demangleNumber ();
1443
- Dim->NextDimension = new ( Arena) ArrayType;
1448
+ Dim->NextDimension = Arena. alloc < ArrayType>() ;
1444
1449
Dim = Dim->NextDimension ;
1445
1450
}
1446
1451
@@ -1476,15 +1481,15 @@ ParamList Demangler::demangleParameterList() {
1476
1481
}
1477
1482
MangledName = MangledName.dropFront ();
1478
1483
1479
- *Current = new ( Arena) ParamList;
1484
+ *Current = Arena. alloc < ParamList>() ;
1480
1485
(*Current)->Current = BackRef[N]->clone (Arena);
1481
1486
Current = &(*Current)->Next ;
1482
1487
continue ;
1483
1488
}
1484
1489
1485
1490
size_t ArrayDimension = MangledName.size ();
1486
1491
1487
- *Current = new ( Arena) ParamList;
1492
+ *Current = Arena. alloc < ParamList>() ;
1488
1493
(*Current)->Current = demangleType (QualifierMangleMode::Drop);
1489
1494
1490
1495
// Single-letter types are ignored for backreferences because
0 commit comments