Chris Lattner | ee0ba89 | 2003-11-04 22:38:28 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 9580ce2 | 2003-11-12 22:45:14 +0000 | [diff] [blame] | 26 | namespace llvm { |
| 27 | |
Chris Lattner | ee0ba89 | 2003-11-04 22:38:28 +0000 | [diff] [blame] | 28 | template<typename T> |
| 29 | struct MallocAllocator { |
| 30 | typedef size_t size_type; |
| 31 | typedef ptrdiff_t difference_type; |
| 32 | typedef T* pointer; |
| 33 | typedef const T* const_pointer; |
| 34 | typedef T& reference; |
| 35 | typedef const T& const_reference; |
| 36 | typedef T value_type; |
| 37 | template <class U> struct rebind { |
| 38 | typedef MallocAllocator<U> other; |
| 39 | }; |
| 40 | |
Chris Lattner | fc42785 | 2003-11-07 15:20:06 +0000 | [diff] [blame] | 41 | template<typename R> |
| 42 | MallocAllocator(const MallocAllocator<R> &) {} |
| 43 | MallocAllocator() {} |
| 44 | |
Chris Lattner | ee0ba89 | 2003-11-04 22:38:28 +0000 | [diff] [blame] | 45 | pointer address(reference x) const { return &x; } |
| 46 | const_pointer address(const_reference x) const { return &x; } |
| 47 | size_type max_size() const { return ~0 / sizeof(T); } |
| 48 | |
Chris Lattner | 59c3985 | 2003-11-07 23:20:56 +0000 | [diff] [blame] | 49 | static pointer allocate(size_t n, void* hint = 0) { |
Chris Lattner | ee0ba89 | 2003-11-04 22:38:28 +0000 | [diff] [blame] | 50 | return (pointer)malloc(n*sizeof(T)); |
| 51 | } |
| 52 | |
Chris Lattner | 59c3985 | 2003-11-07 23:20:56 +0000 | [diff] [blame] | 53 | static void deallocate(pointer p, size_t n) { |
Chris Lattner | ee0ba89 | 2003-11-04 22:38:28 +0000 | [diff] [blame] | 54 | free((void*)p); |
| 55 | } |
| 56 | |
| 57 | void construct(pointer p, const T &val) { |
| 58 | new((void*)p) T(val); |
| 59 | } |
| 60 | void destroy(pointer p) { |
| 61 | p->~T(); |
| 62 | } |
| 63 | }; |
| 64 | |
| 65 | template<typename T> |
| 66 | inline bool operator==(const MallocAllocator<T> &, const MallocAllocator<T> &) { |
| 67 | return true; |
| 68 | } |
| 69 | template<typename T> |
| 70 | inline bool operator!=(const MallocAllocator<T>&, const MallocAllocator<T>&) { |
| 71 | return false; |
| 72 | } |
Chris Lattner | 9580ce2 | 2003-11-12 22:45:14 +0000 | [diff] [blame] | 73 | } // End llvm namespace |
Chris Lattner | ee0ba89 | 2003-11-04 22:38:28 +0000 | [diff] [blame] | 74 | |
Chris Lattner | 59c3985 | 2003-11-07 23:20:56 +0000 | [diff] [blame] | 75 | namespace std { |
| 76 | template<typename Type, typename Type2> |
Chris Lattner | 9580ce2 | 2003-11-12 22:45:14 +0000 | [diff] [blame] | 77 | struct _Alloc_traits<Type, ::llvm::MallocAllocator<Type2> > { |
Chris Lattner | 59c3985 | 2003-11-07 23:20:56 +0000 | [diff] [blame] | 78 | static const bool _S_instanceless = true; |
Chris Lattner | 9580ce2 | 2003-11-12 22:45:14 +0000 | [diff] [blame] | 79 | typedef ::llvm::MallocAllocator<Type> base_alloc_type; |
| 80 | typedef ::llvm::MallocAllocator<Type> _Alloc_type; |
| 81 | typedef ::llvm::MallocAllocator<Type> allocator_type; |
Chris Lattner | 59c3985 | 2003-11-07 23:20:56 +0000 | [diff] [blame] | 82 | }; |
| 83 | } |
| 84 | |
Chris Lattner | ee0ba89 | 2003-11-04 22:38:28 +0000 | [diff] [blame] | 85 | #endif |