1 //===--- Allocator.h - Simple memory allocation abstraction -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the MallocAllocator and BumpPtrAllocator interfaces.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SUPPORT_ALLOCATOR_H
15 #define LLVM_SUPPORT_ALLOCATOR_H
17 #include "llvm/Support/AlignOf.h"
22 class MallocAllocator {
29 void *Allocate(size_t Size, size_t /*Alignment*/) { return malloc(Size); }
32 T *Allocate() { return static_cast<T*>(malloc(sizeof(T))); }
35 T *Allocate(size_t Num) {
36 return static_cast<T*>(malloc(sizeof(T)*Num));
39 void Deallocate(void *Ptr) { free(Ptr); }
41 void PrintStats() const {}
44 /// BumpPtrAllocator - This allocator is useful for containers that need very
45 /// simple memory allocation strategies. In particular, this just keeps
46 /// allocating memory, and never deletes it until the entire block is dead. This
47 /// makes allocation speedy, but must only be used when the trade-off is ok.
48 class BumpPtrAllocator {
49 BumpPtrAllocator(const BumpPtrAllocator &); // do not implement
50 void operator=(const BumpPtrAllocator &); // do not implement
59 void *Allocate(size_t Size, size_t Alignment);
63 return static_cast<T*>(Allocate(sizeof(T),AlignOf<T>::Alignment));
67 T *Allocate(size_t Num) {
68 return static_cast<T*>(Allocate(Num * sizeof(T), AlignOf<T>::Alignment));
71 void Deallocate(void * /*Ptr*/) {}
73 void PrintStats() const;
76 } // end namespace llvm