AllocatorInline.cxx
1 // Copyright (C) 2001-2010 Vivien Mallet, INRIA
2 // Author(s): Vivien Mallet, Marc Fragu
3 //
4 // This file is part of the linear-algebra library Seldon,
5 // http://seldon.sourceforge.net/.
6 //
7 // Seldon is free software; you can redistribute it and/or modify it under the
8 // terms of the GNU Lesser General Public License as published by the Free
9 // Software Foundation; either version 2.1 of the License, or (at your option)
10 // any later version.
11 //
12 // Seldon is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
15 // more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with Seldon. If not, see http://www.gnu.org/licenses/.
19 
20 
21 #ifndef SELDON_FILE_ALLOCATOR_INLINE_CXX
22 
23 #include "Allocator.hxx"
24 
25 namespace Seldon
26 {
27 
28 
30  // MALLOCALLOC //
32 
33 
34  template <class T>
35  inline typename MallocAlloc<T>::pointer
36  MallocAlloc<T>::allocate(size_t num, void* h)
37  {
38  return static_cast<pointer>( malloc(num * sizeof(T)) );
39  }
40 
41  template <class T>
42  inline void MallocAlloc<T>::deallocate(pointer data, size_t num, void* h)
43  {
44  free(data);
45  }
46 
47  template <class T>
48  inline void* MallocAlloc<T>::reallocate(pointer data, size_t num, void* h)
49  {
50  return realloc(reinterpret_cast<void*>(data), num * sizeof(T));
51  }
52 
53  template <class T>
54  inline void MallocAlloc<T>::memoryset(pointer data, char c, size_t num)
55  {
56  memset(reinterpret_cast<void*>(data), c, num);
57  }
58 
59  template <class T>
60  inline void
61  MallocAlloc<T>::memorycpy(pointer datat, pointer datas, size_t num)
62  {
63  memcpy(reinterpret_cast<void*>(datat), reinterpret_cast<void*>(datas),
64  num * sizeof(T));
65  }
66 
67 
69  // CALLOCALLOC //
71 
72 
73  template <class T>
74  inline typename CallocAlloc<T>::pointer
75  CallocAlloc<T>::allocate(size_t num, void* h)
76  {
77  return static_cast<pointer>( calloc(num, sizeof(T)) );
78  }
79 
80  template <class T>
81  inline void CallocAlloc<T>::deallocate(pointer data, size_t num, void* h)
82  {
83  free(data);
84  }
85 
86  template <class T>
87  inline void* CallocAlloc<T>::reallocate(pointer data, size_t num, void* h)
88  {
89  return realloc(reinterpret_cast<void*>(data), num * sizeof(T));
90  }
91 
92  template <class T>
93  inline void CallocAlloc<T>::memoryset(pointer data, char c, size_t num)
94  {
95  memset(reinterpret_cast<void*>(data), c, num);
96  }
97 
98  template <class T>
99  inline void
100  CallocAlloc<T>::memorycpy(pointer datat, pointer datas, size_t num)
101  {
102  memcpy(reinterpret_cast<void*>(datat), reinterpret_cast<void*>(datas),
103  num * sizeof(T));
104  }
105 
106 
108  // NEWALLOC //
110 
111 
112  template <class T>
113  inline typename NewAlloc<T>::pointer
114  NewAlloc<T>::allocate(size_t num, void* h)
115  {
116  return static_cast<pointer>(new T[num]);
117  }
118 
119  template <class T>
120  inline void NewAlloc<T>::deallocate(pointer data, size_t num, void* h)
121  {
122  delete [] data;
123  }
124 
125  template <class T>
126  inline void* NewAlloc<T>::reallocate(pointer data, size_t num, void* h)
127  {
128  if (data != NULL)
129  delete [] data;
130 
131  return (new T[num]);
132  }
133 
134  template <class T>
135  inline void NewAlloc<T>::memoryset(pointer data, char c, size_t num)
136  {
137  memset(reinterpret_cast<void*>(data), c, num);
138  }
139 
140  template <class T>
141  inline void
142  NewAlloc<T>::memorycpy(pointer datat, pointer datas, size_t num)
143  {
144  for (size_t i = 0; i < num; i++)
145  datat[i] = datas[i];
146  }
147 
148 
150  // NANALLOC //
152 
153 
154  template <class T>
155  inline void NaNAlloc<T>::deallocate(pointer data, size_t num, void* h)
156  {
157  free(data);
158  }
159 
160  template <class T>
161  inline void NaNAlloc<T>::memoryset(pointer data, char c, size_t num)
162  {
163  memset(reinterpret_cast<void*>(data), c, num);
164  }
165 
166  template <class T>
167  inline void
168  NaNAlloc<T>::memorycpy(pointer datat, pointer datas, size_t num)
169  {
170  memcpy(reinterpret_cast<void*>(datat), reinterpret_cast<void*>(datas),
171  num * sizeof(T));
172  }
173 
174 
175 } // namespace Seldon.
176 
177 #define SELDON_FILE_ALLOCATOR_INLINE_CXX
178 #endif
Seldon
Seldon namespace.
Definition: Array.cxx:24