#ifdef SUPPRESSED
#endif
#include <stdint.h>
#include "../Inputs/system-header-simulator-cxx.h"
void error();
void *malloc(size_t);
inline uintptr_t alignAddr(const void *Addr, size_t Alignment) {
return (((uintptr_t)Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1));
}
inline size_t alignmentAdjustment(const void *Ptr, size_t Alignment) {
return alignAddr(Ptr, Alignment) - (uintptr_t)Ptr;
}
inline void *safe_malloc(size_t Sz) {
void *Result = malloc(Sz);
if (Result == nullptr)
error();
return Result;
}
class MallocAllocator {
public:
void *Allocate(size_t Size, size_t ) {
return safe_malloc(Size);
}
};
class BumpPtrAllocator {
public:
void *Allocate(size_t Size, size_t Alignment) {
BytesAllocated += Size;
size_t Adjustment = alignmentAdjustment(CurPtr, Alignment);
size_t SizeToAllocate = Size;
size_t PaddedSize = SizeToAllocate + Alignment - 1;
uintptr_t AlignedAddr = alignAddr(Allocator.Allocate(PaddedSize, 0),
Alignment);
char *AlignedPtr = (char*)AlignedAddr;
return AlignedPtr;
}
private:
char *CurPtr = nullptr;
size_t BytesAllocated = 0;
MallocAllocator Allocator;
};
class ASTContext;
void *operator new(size_t Bytes, const ASTContext &C, size_t Alignment = 8);
void *operator new[](size_t Bytes, const ASTContext &C, size_t Alignment = 8);
class ASTContext {
public:
void *Allocate(size_t Size, unsigned Align = 8) const {
return BumpAlloc.Allocate(Size, Align);
}
template <typename T>
T *Allocate(size_t Num = 1) const {
return static_cast<T *>(Allocate(Num * sizeof(T), alignof(T)));
}
private:
mutable BumpPtrAllocator BumpAlloc;
};
inline void *operator new(size_t Bytes, const ASTContext &C,
size_t Alignment ) {
return C.Allocate(Bytes, Alignment);
}
inline void *operator new[](size_t Bytes, const ASTContext &C,
size_t Alignment ) {
return C.Allocate(Bytes, Alignment);
}
void *operator new(size_t Bytes, ASTContext &C,
size_t Alignment = 8) noexcept {
return ::operator new(Bytes, C, Alignment);
}
class A {
public:
void setValue(int value) { Value = value; }
private:
int Value;
};
void f(const ASTContext &C) {
A *a = new (C) A;
a->setValue(13);
#ifndef SUPPRESSED
#endif
}
void g(const ASTContext &C) {
A *a = new (C) A[1];
a[0].setValue(13);
#ifndef SUPPRESSED
#endif
}