SkePU(integratedwithStarPU)  0.8.1
 All Classes Namespaces Files Functions Enumerations Friends Macros Groups Pages
malloc_allocator.h
1 
16 #ifndef MALLOC_ALLOCATOR_H
17 #define MALLOC_ALLOCATOR_H
18 
19 namespace skepu
20 {
21 
22 template <class T>
23 class malloc_allocator
24 {
25 public:
26  typedef T value_type;
27  typedef value_type* pointer;
28  typedef const value_type* const_pointer;
29  typedef value_type& reference;
30  typedef const value_type& const_reference;
31  typedef std::size_t size_type;
32  typedef std::ptrdiff_t difference_type;
33 
34 private:
35  void operator=(const malloc_allocator&);
36  static int count;
37 // size_type total_alloc;
38 
39 public:
40  malloc_allocator() {}
42  ~malloc_allocator() {}
43 
44 
45 public:
46  template <class U>
48 
49  template <class U>
50  struct rebind { typedef malloc_allocator<U> other; };
51 
52  pointer address(reference x) const
53  { return &x; }
54 
55  void construct(pointer p, const value_type& x)
56  {
57  new(p) value_type(x);
58  }
59 
60  void destroy(pointer p)
61  {
62  p->~value_type();
63  }
64 
65  const_pointer address(const_reference x) const
66  {
67  return &x;
68  }
69 
70  pointer allocate(size_type n, const_pointer = 0)
71  {
72  void* p=0;
73 
74 
75  #ifdef USE_PINNED_MEMORY
76  DEBUG_TEXT_LEVEL1("malloc_pinned_possible StarPU ******\n")
77  starpu_malloc((void **)&p, n * sizeof(T));
78 // starpu_data_malloc_pinned_if_possible((void **)&p, n * sizeof(T));
79  #else
80  p = std::malloc(n * sizeof(T));
81  #endif
82 
83  if (!p)
84  {
85  std::cerr<<"*** Error Malloc **************\n";
86  throw std::bad_alloc();
87  }
88  return static_cast<pointer>(p);
89  }
90 
91  void deallocate(pointer p, size_type)
92  {
93  #ifdef USE_PINNED_MEMORY
94 // starpu_data_free_pinned_if_possible(p);
95  starpu_free(p);
96  #else
97  std::free(p);
98  #endif
99  }
100 
101  size_type max_size() const
102  {
103  return static_cast<size_type>(-1) / sizeof(value_type);
104  }
105 };
106 
107 template <class T>
109 
110 template <class T>
111 inline bool operator==(const malloc_allocator<T>&, const malloc_allocator<T>&)
112 {
113  return true;
114 }
115 
116 template <class T>
117 inline bool operator!=(const malloc_allocator<T>&, const malloc_allocator<T>&)
118 {
119  return false;
120 }
121 
122 
123 template<>
124 class malloc_allocator<void>
125 {
126  typedef void value_type;
127  typedef void* pointer;
128  typedef const void* const_pointer;
129 
130  template <class U>
131  struct rebind
132  {
133  typedef malloc_allocator<U> other;
134  };
135 };
136 
137 }
138 
139 #endif
140 
A custom memory allocator used with std::vector.