Index: lib/Demangle/ItaniumDemangle.cpp =================================================================== --- lib/Demangle/ItaniumDemangle.cpp +++ lib/Demangle/ItaniumDemangle.cpp @@ -1945,40 +1945,53 @@ struct BlockMeta { BlockMeta* Next; size_t Current; + + BlockMeta(BlockMeta *N) : Next(N), + Current(Alignment - + (reinterpret_cast(this + 1) & (Alignment - 1))) { } + void *getTail() { return reinterpret_cast(this + 1) + Current; } }; + static constexpr size_t Alignment = 16; static constexpr size_t AllocSize = 4096; - static constexpr size_t UsableAllocSize = AllocSize - sizeof(BlockMeta); + static constexpr size_t UsableAllocSize = AllocSize - sizeof(BlockMeta) - + Alignment; - alignas(16) char InitialBuffer[AllocSize]; + char InitialBuffer[AllocSize]; BlockMeta* BlockList = nullptr; void grow() { char* NewMeta = new char[AllocSize]; - BlockList = new (NewMeta) BlockMeta{BlockList, 0}; + BlockList = new (NewMeta) BlockMeta(BlockList); } void* allocateMassive(size_t NBytes) { - NBytes += sizeof(BlockMeta); - BlockMeta* NewMeta = reinterpret_cast(new char[NBytes]); - BlockList->Next = new (NewMeta) BlockMeta{BlockList->Next, 0}; - return static_cast(NewMeta + 1); + size_t AllocSz = NBytes + sizeof(BlockMeta) + Alignment; + char *Mem = new char[AllocSz]; + BlockList->Next = new (Mem) BlockMeta(BlockList->Next); + void *Result = BlockList->getTail(); + BlockList->Current += NBytes; + return Result; + } + + static constexpr size_t getAlignedSize(size_t Sz) { + return (Sz + (Alignment - 1)) & ~(Alignment - 1); } public: BumpPointerAllocator() - : BlockList(new (InitialBuffer) BlockMeta{nullptr, 0}) {} + : BlockList(new (InitialBuffer) BlockMeta(nullptr)) {} void* allocate(size_t N) { - N = (N + 15u) & ~15u; + N = getAlignedSize(N); if (N + BlockList->Current >= UsableAllocSize) { if (N > UsableAllocSize) return allocateMassive(N); grow(); } + void *Result = BlockList->getTail(); BlockList->Current += N; - return static_cast(reinterpret_cast(BlockList + 1) + - BlockList->Current - N); + return Result; } void reset() { @@ -1988,7 +2001,7 @@ if (reinterpret_cast(Tmp) != InitialBuffer) delete[] reinterpret_cast(Tmp); } - BlockList = new (InitialBuffer) BlockMeta{nullptr, 0}; + BlockList = new (InitialBuffer) BlockMeta(nullptr); } ~BumpPointerAllocator() { reset(); }