#ifndef LLVM_SUPPORT_ALLOCATOR_H
#define LLVM_SUPPORT_ALLOCATOR_H
#include "llvm/Support/AlignOf.h"
#include "llvm/Support/DataTypes.h"
#include "llvm/Support/MathExtras.h"
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdlib>
namespace llvm {
template <typename T> struct ReferenceAdder { typedef T& result; };
template <typename T> struct ReferenceAdder<T&> { typedef T result; };
class MallocAllocator {
public:
MallocAllocator() {}
~MallocAllocator() {}
void Reset() {}
void *Allocate(size_t Size, size_t ) { return malloc(Size); }
template <typename T>
T *Allocate() { return static_cast<T*>(malloc(sizeof(T))); }
template <typename T>
T *Allocate(size_t Num) {
return static_cast<T*>(malloc(sizeof(T)*Num));
}
void Deallocate(const void *Ptr) { free(const_cast<void*>(Ptr)); }
template <typename T>
void Deallocate(T *Ptr, size_t Num = 1) {
Deallocate(static_cast<const void *>(Ptr), Num * sizeof(T));
}
void Deallocate(const void *Ptr, size_t ) {
free(const_cast<void*>(Ptr));
}
void PrintStats() const {}
};
class MemSlab {
public:
size_t Size;
MemSlab *NextPtr;
};
class SlabAllocator {
public:
virtual ~SlabAllocator();
virtual MemSlab *Allocate(size_t Size) = 0;
virtual void Deallocate(MemSlab *Slab) = 0;
};
class MallocSlabAllocator : public SlabAllocator {
MallocAllocator Allocator;
public:
MallocSlabAllocator() : Allocator() { }
virtual ~MallocSlabAllocator();
virtual MemSlab *Allocate(size_t Size) LLVM_OVERRIDE;
virtual void Deallocate(MemSlab *Slab) LLVM_OVERRIDE;
};
class BumpPtrAllocator {
BumpPtrAllocator(const BumpPtrAllocator &) LLVM_DELETED_FUNCTION;
void operator=(const BumpPtrAllocator &) LLVM_DELETED_FUNCTION;
size_t SlabSize;
size_t SizeThreshold;
MallocSlabAllocator DefaultSlabAllocator;
SlabAllocator &Allocator;
MemSlab *CurSlab;
char *CurPtr;
char *End;
size_t BytesAllocated;
static char *AlignPtr(char *Ptr, size_t Alignment);
void StartNewSlab();
void DeallocateSlabs(MemSlab *Slab);
template<typename T> friend class SpecificBumpPtrAllocator;
public:
BumpPtrAllocator(size_t size = 4096, size_t threshold = 4096);
BumpPtrAllocator(size_t size, size_t threshold, SlabAllocator &allocator);
~BumpPtrAllocator();
void Reset();
void *Allocate(size_t Size, size_t Alignment);
template <typename T>
T *Allocate() {
return static_cast<T*>(Allocate(sizeof(T),AlignOf<T>::Alignment));
}
template <typename T>
T *Allocate(size_t Num) {
return static_cast<T*>(Allocate(Num * sizeof(T), AlignOf<T>::Alignment));
}
template <typename T>
T *Allocate(size_t Num, size_t Alignment) {
size_t EltSize = (sizeof(T)+Alignment-1)&(-Alignment);
return static_cast<T*>(Allocate(Num * EltSize, Alignment));
}
void Deallocate(const void * ) {}
unsigned GetNumSlabs() const;
void PrintStats() const;
size_t getTotalMemory() const;
};
template <typename T>
class SpecificBumpPtrAllocator {
BumpPtrAllocator Allocator;
public:
SpecificBumpPtrAllocator(size_t size = 4096, size_t threshold = 4096)
: Allocator(size, threshold) {}
SpecificBumpPtrAllocator(size_t size, size_t threshold,
SlabAllocator &allocator)
: Allocator(size, threshold, allocator) {}
~SpecificBumpPtrAllocator() {
DestroyAll();
}
void DestroyAll() {
MemSlab *Slab = Allocator.CurSlab;
while (Slab) {
char *End = Slab == Allocator.CurSlab ? Allocator.CurPtr :
(char *)Slab + Slab->Size;
for (char *Ptr = (char*)(Slab+1); Ptr < End; Ptr += sizeof(T)) {
Ptr = Allocator.AlignPtr(Ptr, alignOf<T>());
if (Ptr + sizeof(T) <= End)
reinterpret_cast<T*>(Ptr)->~T();
}
Slab = Slab->NextPtr;
}
Allocator.Reset();
}
T *Allocate(size_t num = 1) {
return Allocator.Allocate<T>(num);
}
};
}
inline void *operator new(size_t Size, llvm::BumpPtrAllocator &Allocator) {
struct S {
char c;
union {
double D;
long double LD;
long long L;
void *P;
} x;
};
return Allocator.Allocate(Size, std::min((size_t)llvm::NextPowerOf2(Size),
offsetof(S, x)));
}
inline void operator delete(void *, llvm::BumpPtrAllocator &) {}
#endif // LLVM_SUPPORT_ALLOCATOR_H