00001 // User-visible allocators -*- C++ -*- 00002 /* 00003 Galois, a framework to exploit amorphous data-parallelism in irregular 00004 programs. 00005 00006 Copyright (C) 2011, The University of Texas at Austin. All rights reserved. 00007 UNIVERSITY EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES CONCERNING THIS SOFTWARE 00008 AND DOCUMENTATION, INCLUDING ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR ANY 00009 PARTICULAR PURPOSE, NON-INFRINGEMENT AND WARRANTIES OF PERFORMANCE, AND ANY 00010 WARRANTY THAT MIGHT OTHERWISE ARISE FROM COURSE OF DEALING OR USAGE OF TRADE. 00011 NO WARRANTY IS EITHER EXPRESS OR IMPLIED WITH RESPECT TO THE USE OF THE 00012 SOFTWARE OR DOCUMENTATION. Under no circumstances shall University be liable 00013 for incidental, special, indirect, direct or consequential damages or loss of 00014 profits, interruption of business, or related expenses which may arise from use 00015 of Software or Documentation, including but not limited to those resulting from 00016 defects in Software and/or Documentation, or loss or inaccuracy of data of any 00017 kind. 00018 */ 00019 00020 #ifndef GALOIS_MEM_H 00021 #define GALOIS_MEM_H 00022 00023 #include "Galois/Runtime/mm/mem.h" 00024 00025 #include <boost/utility.hpp> 00026 00027 extern "C" void* hoard_malloc(size_t); 00028 extern "C" void hoard_free(void*); 00029 00030 namespace Galois { 00031 00032 typedef GaloisRuntime::MM::SimpleBumpPtr<GaloisRuntime::MM::FreeListHeap<GaloisRuntime::MM::SystemBaseAlloc> > ItAllocBaseTy; 00033 00034 typedef GaloisRuntime::MM::ExternRefGaloisAllocator<char, ItAllocBaseTy> PerIterAllocTy; 00035 00036 template<typename Ty> 00037 class Allocator; 00038 00039 template<> 00040 class Allocator<void> { 00041 public: 00042 typedef size_t size_type; 00043 typedef ptrdiff_t difference_type; 00044 typedef void* pointer; 00045 typedef const void* const_pointer; 00046 typedef void value_type; 00047 00048 template<typename Other> 00049 struct rebind { typedef Allocator<Other> other; }; 00050 }; 00051 00052 template<typename Ty> 00053 class Allocator { 00054 inline void destruct(char*) {} 00055 inline void destruct(wchar_t*) { } 00056 template<typename T> inline void destruct(T* t) { t->~T(); } 00057 00058 public: 00059 typedef size_t size_type; 00060 typedef ptrdiff_t difference_type; 00061 typedef Ty *pointer; 00062 typedef const Ty *const_pointer; 00063 typedef Ty& reference; 00064 typedef const Ty& const_reference; 00065 typedef Ty value_type; 00066 00067 template<class Other> 00068 struct rebind { typedef Allocator<Other> other; }; 00069 00070 Allocator() throw() { } 00071 Allocator(const Allocator&) throw() { } 00072 template <class U> Allocator(const Allocator<U>&) throw() { } 00073 ~Allocator() { } 00074 00075 pointer address(reference val) const { return &val; } 00076 const_pointer address(const_reference val) const { return &val; } 00077 00078 pointer allocate(size_type size) { 00079 if (size > max_size()) 00080 throw std::bad_alloc(); 00081 #ifdef SOLARIS 00082 return static_cast<Ty*>(malloc(size * sizeof(Ty))); 00083 #else 00084 return static_cast<Ty*>(hoard_malloc(size * sizeof(Ty))); 00085 #endif 00086 } 00087 00088 void deallocate(pointer p, size_type) { 00089 //::operator delete(p); 00090 #ifdef SOLARIS 00091 free(p); 00092 #else 00093 hoard_free(p); 00094 #endif 00095 } 00096 00097 size_type max_size() const throw() { 00098 return size_t(-1) / sizeof(Ty); 00099 } 00100 00101 void construct(pointer p, const Ty& val) { 00102 new(p) Ty(val); 00103 } 00104 00105 void destroy(pointer p) { 00106 destruct(p); 00107 } 00108 }; 00109 00110 template<typename T1, typename T2> 00111 inline bool operator==(const Allocator<T1>&, const Allocator<T2>&) throw() { 00112 return true; 00113 } 00114 00115 template<typename T1, typename T2> 00116 inline bool operator!=(const Allocator<T1>&, const Allocator<T2>&) throw() { 00117 return false; 00118 } 00119 } 00120 00121 #endif