allocators.hpp
Go to the documentation of this file.
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Filip Konvicka <filip.konvicka@logis.cz>
*
* Copyright:
* LOGIS, s.r.o., 2009
*
* Bugfixes provided by:
* Gustavo Gutierrez
*
* This file is part of Gecode, the generic constraint
* development environment:
* http://www.gecode.org
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <limits>
namespace Gecode {
template<class T> struct space_allocator;
template<>
struct space_allocator<void> {
typedef void* pointer;
typedef const void* const_pointer;
typedef void value_type;
template<class U> struct rebind {
typedef space_allocator<U> other;
};
};
template<class T>
struct space_allocator {
typedef T value_type;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef T const* const_pointer;
typedef T& reference;
typedef T const& const_reference;
template<class U> struct rebind {
typedef space_allocator<U> other;
};
Space& space;
space_allocator(Space& space) throw() : space(space) {}
space_allocator(space_allocator const& al) throw() : space(al.space) {}
space_allocator& operator =(space_allocator const& al) {
(void) al;
assert(&space == &al.space);
return *this;
}
template<class U>
space_allocator(space_allocator<U> const& al) throw() : space(al.space) {}
pointer address(reference x) const { return &x; }
const_pointer address(const_reference x) const { return &x; }
size_type max_size(void) const throw() {
return std::numeric_limits<size_type>::max() /
(sizeof(T)>0 ? sizeof(T) : 1);
}
pointer allocate(size_type count) {
return static_cast<pointer>(space.ralloc(sizeof(T)*count));
}
pointer allocate(size_type count, const void * const hint) {
(void) hint;
return allocate(count);
}
void deallocate(pointer p, size_type count) {
space.rfree(static_cast<void*>(p), count);
}
/*
* \brief Constructs an object
*
* Constructs an object of type \a T with the initial value of \a t
* at the location specified by \a element. This function calls
* the <i>placement new()</i> operator.
*/
void construct(pointer element, const_reference t) {
new (element) T(t);
}
void destroy(pointer element) {
element->~T();
}
};
template<class T1, class T2>
bool operator==(space_allocator<T1> const& al1,
space_allocator<T2> const& al2) throw() {
return &al1.space == &al2.space;
}
template<class T1, class T2>
bool operator!=(space_allocator<T1> const& al1,
space_allocator<T2> const& al2) throw() {
return &al1.space != &al2.space;
}
template<class T> struct region_allocator;
template<>
struct region_allocator<void> {
typedef void* pointer;
typedef const void* const_pointer;
typedef void value_type;
template<class U> struct rebind {
typedef region_allocator<U> other;
};
};
template<class T>
struct region_allocator {
typedef T value_type;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef T const* const_pointer;
typedef T& reference;
typedef T const& const_reference;
template<class U> struct rebind {
typedef region_allocator<U> other;
};
Region& region;
region_allocator(Region& region) throw()
: region(region) {}
region_allocator(region_allocator const& al) throw()
: region(al.region) {}
template<class U>
region_allocator(region_allocator<U> const& al) throw()
: region(al.region) {}
pointer address(reference x) const { return &x; }
const_pointer address(const_reference x) const { return &x; }
size_type max_size(void) const throw() {
return std::numeric_limits<size_type>::max()
/ (sizeof(T)>0 ? sizeof(T) : 1);
}
pointer allocate(size_type count) {
return static_cast<pointer>(region.ralloc(sizeof(T)*count));
}
pointer allocate(size_type count, const void * const hint) {
(void) hint;
return allocate(count);
}
void deallocate(pointer p, size_type count) {
region.rfree(static_cast<void*>(p), count);
}
void construct(pointer element, const_reference t) {
new (element) T(t);
}
void destroy(pointer element) {
element->~T();
}
};
/*
* \brief Tests two region allocators for equality
*
* Two allocators are equal when each can release storage allocated
* from the other.
*/
template<class T1, class T2>
bool operator==(region_allocator<T1> const& al1,
region_allocator<T2> const& al2) throw() {
return &al1.region == &al2.region;
}
/*
* \brief Tests two region allocators for inequality
*
* Two allocators are equal when each can release storage allocated
* from the other.
*/
template<class T1, class T2>
bool operator!=(region_allocator<T1> const& al1,
region_allocator<T2> const& al2) throw() {
return &al1.region != &al2.region;
}
}
// STATISTICS: kernel-memory
Gecode::Region
Handle to region.
Definition region.hpp:55
Gecode::Space
Computation spaces.
Definition core.hpp:1742
Gecode
Gecode toplevel namespace
Gecode::count
void count(Home home, const IntVarArgs &x, int n, IntRelType irt, int m, IntPropLevel ipl=IPL_DEF)
Post propagator for .
Definition count.cpp:40
Gecode::element
void element(Home home, IntSharedArray n, IntVar x0, IntVar x1, IntPropLevel ipl=IPL_DEF)
Post domain consistent propagator for .
Definition element.cpp:39
Gecode::operator==
bool operator==(const FloatVal &x, const FloatVal &y)
Definition val.hpp:294
Gecode::operator!=
bool operator!=(const FloatVal &x, const FloatVal &y)
Definition val.hpp:317
Gecode::region_allocator::rebind
Rebinding helper (returns the type of a similar allocator for type U).
Definition allocators.hpp:355
Gecode::region_allocator::rebind::other
region_allocator< U > other
The allocator type for U.
Definition allocators.hpp:357
Gecode::region_allocator< void >::rebind
Rebinding helper (returns the type of a similar allocator for type U).
Definition allocators.hpp:324
Gecode::region_allocator< void >::rebind::other
region_allocator< U > other
Definition allocators.hpp:325
Gecode::region_allocator< void >::const_pointer
const void * const_pointer
Definition allocators.hpp:321
Gecode::region_allocator< void >::value_type
void value_type
Definition allocators.hpp:322
Gecode::region_allocator< void >::pointer
void * pointer
Definition allocators.hpp:320
Gecode::region_allocator
Allocator that allocates memory from a region.
Definition allocators.hpp:338
Gecode::region_allocator< U >::region
Region & region
Definition allocators.hpp:361
Gecode::region_allocator::max_size
size_type max_size(void) const
Returns the largest size for which a call to allocate might succeed.
Definition allocators.hpp:388
Gecode::region_allocator::region_allocator
region_allocator(region_allocator< U > const &al)
Copy from other instantiation.
Definition allocators.hpp:380
Gecode::region_allocator< U >::value_type
U value_type
Definition allocators.hpp:340
Gecode::region_allocator::construct
void construct(pointer element, const_reference t)
Constructs an object.
Definition allocators.hpp:440
Gecode::region_allocator::deallocate
void deallocate(pointer p, size_type count)
Deallocates storage.
Definition allocators.hpp:429
Gecode::region_allocator< void >::region_allocator
region_allocator(Region &region)
Definition allocators.hpp:367
Gecode::region_allocator< U >::const_pointer
U const * const_pointer
Definition allocators.hpp:348
Gecode::region_allocator< U >::size_type
size_t size_type
Definition allocators.hpp:342
Gecode::region_allocator::allocate
pointer allocate(size_type count, const void *const hint)
Allocates storage.
Definition allocators.hpp:416
Gecode::region_allocator::destroy
void destroy(pointer element)
Calls the destructor on the object pointed to by element.
Definition allocators.hpp:445
Gecode::region_allocator< U >::const_reference
U const & const_reference
Definition allocators.hpp:352
Gecode::region_allocator::allocate
pointer allocate(size_type count)
Allocates storage.
Definition allocators.hpp:401
Gecode::region_allocator< U >::pointer
U * pointer
Definition allocators.hpp:346
Gecode::region_allocator::region_allocator
region_allocator(region_allocator const &al)
Copy construction.
Definition allocators.hpp:373
Gecode::region_allocator< U >::difference_type
ptrdiff_t difference_type
Definition allocators.hpp:344
Gecode::region_allocator::address
const_pointer address(const_reference x) const
Convert a const reference x to a const pointer.
Definition allocators.hpp:386
Gecode::region_allocator::address
pointer address(reference x) const
Convert a reference x to a pointer.
Definition allocators.hpp:384
Gecode::region_allocator< U >::reference
U & reference
Definition allocators.hpp:350
Gecode::space_allocator::rebind
Rebinding helper (returns the type of a similar allocator for type U).
Definition allocators.hpp:193
Gecode::space_allocator::rebind::other
space_allocator< U > other
The allocator type for U.
Definition allocators.hpp:195
Gecode::space_allocator< void >::rebind
Rebinding helper (returns the type of a similar allocator for type U).
Definition allocators.hpp:162
Gecode::space_allocator< void >::rebind::other
space_allocator< U > other
Definition allocators.hpp:163
Gecode::space_allocator< void >::const_pointer
const void * const_pointer
Definition allocators.hpp:159
Gecode::space_allocator< void >::pointer
void * pointer
Definition allocators.hpp:158
Gecode::space_allocator< void >::value_type
void value_type
Definition allocators.hpp:160
Gecode::space_allocator
Allocator that allocates memory from a space heap.
Definition allocators.hpp:177
Gecode::space_allocator::construct
void construct(pointer element, const_reference t)
Definition allocators.hpp:275
Gecode::space_allocator::space_allocator
space_allocator(space_allocator const &al)
Copy construction.
Definition allocators.hpp:210
Gecode::space_allocator< U >::reference
U & reference
Definition allocators.hpp:189
Gecode::space_allocator::operator=
space_allocator & operator=(space_allocator const &al)
Assignment operator.
Definition allocators.hpp:215
Gecode::space_allocator< U >::const_pointer
U const * const_pointer
Definition allocators.hpp:187
Gecode::space_allocator::allocate
pointer allocate(size_type count, const void *const hint)
Allocates storage.
Definition allocators.hpp:258
Gecode::space_allocator::space_allocator
space_allocator(space_allocator< U > const &al)
Copy from other instantiation.
Definition allocators.hpp:225
Gecode::space_allocator::address
pointer address(reference x) const
Convert a reference x to a pointer.
Definition allocators.hpp:228
Gecode::space_allocator< void >::space_allocator
space_allocator(Space &space)
Definition allocators.hpp:205
Gecode::space_allocator< U >::value_type
U value_type
Definition allocators.hpp:179
Gecode::space_allocator< U >::const_reference
U const & const_reference
Definition allocators.hpp:191
Gecode::space_allocator< U >::pointer
U * pointer
Definition allocators.hpp:185
Gecode::space_allocator< U >::difference_type
ptrdiff_t difference_type
Definition allocators.hpp:183
Gecode::space_allocator::destroy
void destroy(pointer element)
Calls the destructor on the object pointed to by element.
Definition allocators.hpp:280
Gecode::space_allocator::address
const_pointer address(const_reference x) const
Convert a const reference x to a const pointer.
Definition allocators.hpp:230
Gecode::space_allocator< U >::space
Space & space
Definition allocators.hpp:199
Gecode::space_allocator::max_size
size_type max_size(void) const
Returns the largest size for which a call to allocate might succeed.
Definition allocators.hpp:232
Gecode::space_allocator< U >::size_type
size_t size_type
Definition allocators.hpp:181
Gecode::space_allocator::deallocate
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
Gecode::space_allocator::allocate
pointer allocate(size_type count)
Allocates storage.
Definition allocators.hpp:244