Generated on Sat Feb 7 2015 02:01:27 for Gecode by doxygen 1.8.9.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: 2013-03-07 17:39:13 +0100 (Thu, 07 Mar 2013) $ by $Author: schulte $
14  * $Revision: 13458 $
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>
177  struct space_allocator {
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 
205  space_allocator(Space& space) throw() : space(space) {}
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(void) const throw() {
234  (sizeof(T)>0 ? sizeof(T) : 1);
235  }
244  pointer allocate(size_type count) {
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 
264  void deallocate(pointer p, size_type count) {
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  */
275  void construct(pointer element, const_reference t) {
276  new (element) T(t);
277  }
278 
280  void destroy(pointer element) {
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>
338  struct region_allocator {
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 
367  region_allocator(Region& region) throw()
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(void) const throw() {
390  / (sizeof(T)>0 ? sizeof(T) : 1);
391  }
392 
401  pointer allocate(size_type count) {
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 
429  void deallocate(pointer* p, size_type count) {
430  region.rfree(static_cast<void*>(p), count);
431  }
432 
440  void construct(pointer element, const_reference t) {
441  new (element) T(t);
442  }
443 
445  void destroy(pointer element) {
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-memory
void rfree(void *p, size_t s)
Free memory previously allocated.
Definition: region.hpp:310
pointer allocate(size_type count)
Allocates storage.
Definition: allocators.hpp:244
space_allocator & operator=(space_allocator const &al)
Assignment operator.
Definition: allocators.hpp:215
void destroy(pointer element)
Calls the destructor on the object pointed to by element.
Definition: allocators.hpp:280
NodeType t
Type of node.
Definition: bool-expr.cpp:234
const_pointer address(const_reference x) const
Convert a const reference x to a const pointer.
Definition: allocators.hpp:230
pointer address(reference x) const
Convert a reference x to a pointer.
Definition: allocators.hpp:384
ptrdiff_t difference_type
Type that can represent the difference between any two pointers.
Definition: allocators.hpp:183
T & reference
Non-const reference to T.
Definition: allocators.hpp:189
space_allocator(space_allocator const &al)
Copy construction.
Definition: allocators.hpp:210
const FloatNum max
Largest allowed float value.
Definition: float.hh:831
void deallocate(pointer *p, size_type count)
Deallocates storage.
Definition: allocators.hpp:429
region_allocator< U > other
The allocator type for U.
Definition: allocators.hpp:357
void deallocate(pointer p, size_type count)
Deallocates the storage obtained by a call to allocate() with arguments count and p...
Definition: allocators.hpp:264
pointer allocate(size_type count)
Allocates storage.
Definition: allocators.hpp:401
const_pointer address(const_reference x) const
Convert a const reference x to a const pointer.
Definition: allocators.hpp:386
Allocator that allocates memory from a region.
Definition: allocators.hpp:310
T const & const_reference
Const reference to T.
Definition: allocators.hpp:352
space_allocator(Space &space)
Construction.
Definition: allocators.hpp:205
Handle to region.
Definition: region.hpp:61
Computation spaces.
Definition: core.hpp:1362
void construct(pointer element, const_reference t)
Constructs an object.
Definition: allocators.hpp:440
T & reference
Non-const reference to T.
Definition: allocators.hpp:350
Allocator that allocates memory from a space heap.
Definition: allocators.hpp:40
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:236
bool operator!=(const FloatVal &x, const FloatVal &y)
Definition: val.hpp:321
ptrdiff_t difference_type
Type that can represent the difference between any two pointers.
Definition: allocators.hpp:344
Region & region
The region that we allocate objects from.
Definition: allocators.hpp:361
size_t size_type
Type that can represent the size of the largest object.
Definition: allocators.hpp:181
void element(Home home, IntSharedArray c, IntVar x0, IntVar x1, IntConLevel)
Post domain consistent propagator for .
Definition: element.cpp:43
region_allocator(region_allocator< U > const &al)
Copy from other instantiation.
Definition: allocators.hpp:380
T const & const_reference
Const reference to T.
Definition: allocators.hpp:191
Space & space
The space that we allocate objects from.
Definition: allocators.hpp:199
T const * const_pointer
Const version of pointer.
Definition: allocators.hpp:348
void * ralloc(size_t s)
Allocate memory on space heap.
Definition: core.hpp:2359
space_allocator(space_allocator< U > const &al)
Copy from other instantiation.
Definition: allocators.hpp:225
region_allocator(Region &region)
Construction.
Definition: allocators.hpp:367
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
space_allocator< U > other
The allocator type for U.
Definition: allocators.hpp:195
void count(Home home, const IntVarArgs &x, int n, IntRelType irt, int m, IntConLevel)
Post propagator for .
Definition: count.cpp:44
pointer allocate(size_type count, const void *const hint)
Allocates storage.
Definition: allocators.hpp:416
T value_type
Type of objects the allocator creates. This is identical to T.
Definition: allocators.hpp:179
void construct(pointer element, const_reference t)
Definition: allocators.hpp:275
void destroy(pointer element)
Calls the destructor on the object pointed to by element.
Definition: allocators.hpp:445
pointer allocate(size_type count, const void *const hint)
Allocates storage.
Definition: allocators.hpp:258
size_type max_size(void) const
Returns the largest size for which a call to allocate might succeed.
Definition: allocators.hpp:388
bool operator==(const FloatVal &x, const FloatVal &y)
Definition: val.hpp:298
Gecode toplevel namespace
T * pointer
Type of pointers returned by the allocator.
Definition: allocators.hpp:346
size_t size_type
Type that can represent the size of the largest object.
Definition: allocators.hpp:342
T * pointer
Type of pointers returned by the allocator.
Definition: allocators.hpp:185
Rebinding helper (returns the type of a similar allocator for type U).
Definition: allocators.hpp:355
size_type max_size(void) const
Returns the largest size for which a call to allocate might succeed.
Definition: allocators.hpp:232
Rebinding helper (returns the type of a similar allocator for type U).
Definition: allocators.hpp:193
void * ralloc(size_t s)
Allocate memory from region.
Definition: region.hpp:302
T const * const_pointer
Const version of pointer.
Definition: allocators.hpp:187
void rfree(void *p, size_t s)
Free memory previously allocated with alloc (might be reused later)
Definition: core.hpp:2363
pointer address(reference x) const
Convert a reference x to a pointer.
Definition: allocators.hpp:228
T value_type
Type of objects the allocator creates. This is identical to T.
Definition: allocators.hpp:340
region_allocator(region_allocator const &al)
Copy construction.
Definition: allocators.hpp:373