Generated on Wed Sep 5 2012 18:52:01 for Gecode by doxygen 1.8.1.1
allocators.hpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Filip Konvicka <filip.konvicka@logis.cz>
5  *
6  * Copyright:
7  * LOGIS, s.r.o., 2009
8  *
9  * Bugfixes provided by:
10  * Gustavo Gutierrez
11  *
12  * Last modified:
13  * $Date: 2010-10-11 21:10:44 +1100 (Mon, 11 Oct 2010) $ by $Author: tack $
14  * $Revision: 11514 $
15  *
16  * Permission is hereby granted, free of charge, to any person obtaining
17  * a copy of this software and associated documentation files (the
18  * "Software"), to deal in the Software without restriction, including
19  * without limitation the rights to use, copy, modify, merge, publish,
20  * distribute, sublicense, and/or sell copies of the Software, and to
21  * permit persons to whom the Software is furnished to do so, subject to
22  * the following conditions:
23  *
24  * The above copyright notice and this permission notice shall be
25  * included in all copies or substantial portions of the Software.
26  *
27  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34  */
35 
36 #include <limits>
37 
38 namespace Gecode {
39 
40  template<class T> struct space_allocator;
41 
156  template<>
157  struct space_allocator<void> {
158  typedef void* pointer;
159  typedef const void* const_pointer;
160  typedef void value_type;
162  template<class U> struct rebind {
164  };
165  };
166 
176  template<class T>
179  typedef T value_type;
181  typedef size_t size_type;
183  typedef ptrdiff_t difference_type;
185  typedef T* pointer;
187  typedef T const* const_pointer;
189  typedef T& reference;
191  typedef T const& const_reference;
193  template<class U> struct rebind {
196  };
197 
200 
210  space_allocator(space_allocator const& al) throw() : space(al.space) {}
216  (void) al;
217  assert(&space == &al.space);
218  return *this;
219  }
224  template<class U>
225  space_allocator(space_allocator<U> const& al) throw() : space(al.space) {}
226 
228  pointer address(reference x) const { return &x; }
230  const_pointer address(const_reference x) const { return &x; }
232  size_type max_size() const throw() {
234  (sizeof(T)>0 ? sizeof(T) : 1);
235  }
245  return static_cast<pointer>(space.ralloc(sizeof(T)*count));
246  }
247 
258  pointer allocate(size_type count, const void * const hint) {
259  (void) hint;
260  return allocate(count);
261  }
262 
265  space.rfree(static_cast<void*>(p), count);
266  }
267 
268  /*
269  * \brief Constructs an object
270  *
271  * Constructs an object of type \a T with the initial value of \a t
272  * at the location specified by \a element. This function calls
273  * the <i>placement new()</i> operator.
274  */
276  new (element) T(t);
277  }
278 
281  element->~T();
282  }
283  };
284 
291  template<class T1, class T2>
292  bool operator==(space_allocator<T1> const& al1,
293  space_allocator<T2> const& al2) throw() {
294  return &al1.space == &al2.space;
295  }
296 
303  template<class T1, class T2>
304  bool operator!=(space_allocator<T1> const& al1,
305  space_allocator<T2> const& al2) throw() {
306  return &al1.space != &al2.space;
307  }
308 
309 
310  template<class T> struct region_allocator;
311 
318  template<>
319  struct region_allocator<void> {
320  typedef void* pointer;
321  typedef const void* const_pointer;
322  typedef void value_type;
324  template<class U> struct rebind {
326  };
327  };
328 
337  template<class T>
340  typedef T value_type;
342  typedef size_t size_type;
344  typedef ptrdiff_t difference_type;
346  typedef T* pointer;
348  typedef T const* const_pointer;
350  typedef T& reference;
352  typedef T const& const_reference;
353 
355  template<class U> struct rebind {
358  };
359 
362 
368  : region(region) {}
373  region_allocator(region_allocator const& al) throw()
374  : region(al.region) {}
379  template<class U>
381  : region(al.region) {}
382 
384  pointer address(reference x) const { return &x; }
386  const_pointer address(const_reference x) const { return &x; }
388  size_type max_size() const throw() {
390  / (sizeof(T)>0 ? sizeof(T) : 1);
391  }
392 
402  return static_cast<pointer>(region.ralloc(sizeof(T)*count));
403  }
404 
416  pointer allocate(size_type count, const void * const hint) {
417  (void) hint;
418  return allocate(count);
419  }
420 
430  region.rfree(static_cast<void*>(p), count);
431  }
432 
441  new (element) T(t);
442  }
443 
446  element->~T();
447  }
448  };
449 
450  /*
451  * \brief Tests two region allocators for equality
452  *
453  * Two allocators are equal when each can release storage allocated
454  * from the other.
455  */
456  template<class T1, class T2>
458  region_allocator<T2> const& al2) throw() {
459  return &al1.region == &al2.region;
460  }
461 
462  /*
463  * \brief Tests two region allocators for inequality
464  *
465  * Two allocators are equal when each can release storage allocated
466  * from the other.
467  */
468  template<class T1, class T2>
470  region_allocator<T2> const& al2) throw() {
471  return &al1.region != &al2.region;
472  }
473 
474 }
475 
476 // STATISTICS: kernel-other