Regress to not using the llvm namespace.
[oota-llvm.git] / include / Support / MallocAllocator.h
1 //===-- Support/MallocAllocator.h - Allocator using malloc/free -*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines MallocAllocator class, an STL compatible allocator which
11 // just uses malloc/free to get and release memory.  The default allocator uses
12 // the STL pool allocator runtime library, this explicitly avoids it.
13 //
14 // This file is used for variety of purposes, including the pool allocator
15 // project and testing, regardless of whether or not it's used directly in the
16 // LLVM code, so don't delete this from CVS if you think it's unused!
17 //
18 //===----------------------------------------------------------------------===//
19
20 #ifndef SUPPORT_MALLOCALLOCATOR_H
21 #define SUPPORT_MALLOCALLOCATOR_H
22
23 #include <cstdlib>
24 #include <memory>
25
26 template<typename T>
27 struct MallocAllocator {
28   typedef size_t size_type;
29   typedef ptrdiff_t difference_type;
30   typedef T* pointer;
31   typedef const T* const_pointer;
32   typedef T& reference;
33   typedef const T& const_reference;
34   typedef T value_type;
35   template <class U> struct rebind {
36     typedef MallocAllocator<U> other;
37   };
38
39   template<typename R>
40   MallocAllocator(const MallocAllocator<R> &) {}
41   MallocAllocator() {}
42
43   pointer address(reference x) const { return &x; }
44   const_pointer address(const_reference x) const { return &x; }
45   size_type max_size() const { return ~0 / sizeof(T); }
46   
47   static pointer allocate(size_t n, void* hint = 0) {
48     return (pointer)malloc(n*sizeof(T));
49   }
50
51   static void deallocate(pointer p, size_t n) {
52     free((void*)p);
53   }
54
55   void construct(pointer p, const T &val) {
56     new((void*)p) T(val);
57   }
58   void destroy(pointer p) {
59     p->~T();
60   }
61 };
62
63 template<typename T>
64 inline bool operator==(const MallocAllocator<T> &, const MallocAllocator<T> &) {
65   return true;
66 }
67 template<typename T>
68 inline bool operator!=(const MallocAllocator<T>&, const MallocAllocator<T>&) {
69   return false;
70 }
71
72 namespace std {
73   template<typename Type, typename Type2>
74   struct _Alloc_traits<Type, ::MallocAllocator<Type2> > {
75     static const bool _S_instanceless = true;
76     typedef ::MallocAllocator<Type> base_alloc_type;
77     typedef ::MallocAllocator<Type> _Alloc_type;
78     typedef ::MallocAllocator<Type> allocator_type;
79   };
80 }
81
82 #endif