Generated on Mon Nov 30 23:53:32 2009 for Gecode by doxygen 1.6.1

allocators.hpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Filip Konvicka <filip.konvicka@logis.cz>
00005  *
00006  *  Copyright:
00007  *     LOGIS, s.r.o., 2009
00008  *
00009  *  Last modified:
00010  *     $Date: 2009-09-08 21:10:29 +0200 (Tue, 08 Sep 2009) $ by $Author: schulte $
00011  *     $Revision: 9692 $
00012  *
00013  *  Permission is hereby granted, free of charge, to any person obtaining
00014  *  a copy of this software and associated documentation files (the
00015  *  "Software"), to deal in the Software without restriction, including
00016  *  without limitation the rights to use, copy, modify, merge, publish,
00017  *  distribute, sublicense, and/or sell copies of the Software, and to
00018  *  permit persons to whom the Software is furnished to do so, subject to
00019  *  the following conditions:
00020  *
00021  *  The above copyright notice and this permission notice shall be
00022  *  included in all copies or substantial portions of the Software.
00023  *
00024  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00026  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00027  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00028  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00029  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00030  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00031  */
00032 
00033 #include <limits>
00034 
00035 namespace Gecode {
00036 
00037   template<class T> struct space_allocator;
00038 
00153   template<> 
00154   struct space_allocator<void> {
00155     typedef void*       pointer;
00156     typedef const void* const_pointer;
00157     typedef void        value_type;
00158     template<class U> struct rebind {
00159       typedef space_allocator<U> other;
00160     };
00161   };
00162 
00172   template<class T> 
00173   struct space_allocator {
00175     typedef T value_type;
00177     typedef size_t size_type;
00179     typedef ptrdiff_t difference_type;
00181     typedef T* pointer;
00183     typedef T const* const_pointer;
00185     typedef T& reference;
00187     typedef T const&  const_reference;
00189     template<class U> struct rebind { 
00191       typedef space_allocator<U> other;
00192     };
00193 
00195     Space& space;
00196 
00201     space_allocator(Space& space) throw() : space(space) {}
00206     space_allocator(space_allocator const& al) throw() : space(al.space) {}
00211     template<class U> 
00212     space_allocator(space_allocator<U> const& al) throw() : space(al.space) {}
00213 
00215     pointer address(reference x) const { return &x; }
00217     const_pointer address(const_reference x) const { return &x; }
00219     size_type max_size() const throw() {
00220       return std::numeric_limits<size_type>::max() / 
00221         (sizeof(T)>0 ? sizeof(T) : 1);
00222     }
00231     pointer allocate(size_type count) {
00232       return static_cast<pointer>(space.ralloc(sizeof(T)*count));
00233     }
00234 
00245     pointer allocate(size_type count, const void * const hint) {
00246       (void) hint;
00247       return allocate(count);
00248     }
00249 
00251     void deallocate(pointer p, size_type count) {
00252       space.rfree(static_cast<void*>(p), count);
00253     }
00254 
00255     /*
00256      * \brief Constructs an object 
00257      *
00258      * Constructs an object of type \a T with the initial value of \a t 
00259      * at the location specified by \a element. This function calls 
00260      * the <i>placement new()</i> operator.
00261      */
00262     void construct(pointer element, const_reference t) {
00263       new (element) T(t);
00264     }
00265 
00267     void destroy(pointer element) {
00268       element->~T();
00269     }
00270   };
00271 
00278   template<class T1, class T2>
00279   bool operator==(space_allocator<T1> const& al1, 
00280                   space_allocator<T2> const& al2) throw() {
00281     return &al1.space == &al2.space;
00282   }
00283 
00290   template<class T1, class T2>
00291   bool operator!=(space_allocator<T1> const& al1, 
00292                   space_allocator<T2> const& al2) throw() {
00293     return &al1.space != &al2.space;
00294   }
00295 
00296 
00297   template<class T> struct region_allocator;
00298 
00305   template<> 
00306   struct region_allocator<void> {
00307     typedef void* pointer;
00308     typedef const void* const_pointer;
00309     typedef void value_type;
00310     template<class U> struct rebind {
00311       typedef region_allocator<U> other;
00312     };
00313   };
00314 
00323   template<class T> 
00324   struct region_allocator {
00326     typedef T value_type;
00328     typedef size_t size_type;
00330     typedef ptrdiff_t difference_type;
00332     typedef T* pointer;
00334     typedef T const*  const_pointer;
00336     typedef T&  reference;
00338     typedef T const& const_reference;
00339 
00341     template<class U> struct rebind { 
00343      typedef region_allocator<U> other;
00344     };
00345 
00347     Region& region;
00348 
00353     region_allocator(Region& region) throw() 
00354       : region(region) {}
00359     region_allocator(region_allocator const& al) throw() 
00360       : region(al.region) {}
00365     template<class U> 
00366     region_allocator(region_allocator<U> const& al) throw() 
00367       : region(al.region) {}
00368 
00370     pointer address(reference x) const { return &x; }
00372     const_pointer address(const_reference x) const { return &x; }
00374     size_type max_size() const throw() {
00375       return std::numeric_limits<size_type>::max() 
00376         / (sizeof(T)>0 ? sizeof(T) : 1);
00377     }
00378 
00387     pointer allocate(size_type count) {
00388       return static_cast<pointer>(region.ralloc(sizeof(T)*count));
00389     }
00390 
00402     pointer allocate(size_type count, const void * const hint) {
00403       (void) hint;
00404       return allocate(count);
00405     }
00406     
00415     void deallocate(pointer* p, size_type count) {
00416       region.rfree(static_cast<void*>(p), count);
00417     }
00418 
00426      void construct(pointer element, const_reference t) {
00427       new (element) T(t);
00428     }
00429 
00431     void destroy(pointer element) {
00432       element->~T();
00433     }
00434   };
00435 
00436   /*
00437    * \brief Tests two region allocators for equality
00438    *
00439    * Two allocators are equal when each can release storage allocated 
00440    * from the other.
00441    */
00442   template<class T1, class T2>
00443   bool operator==(region_allocator<T1> const& al1, 
00444                   region_allocator<T2> const& al2) throw() {
00445     return &al1.region == &al2.region;
00446   }
00447 
00448   /*
00449    * \brief Tests two region allocators for inequality
00450    *
00451    * Two allocators are equal when each can release storage allocated
00452    * from the other.
00453    */
00454   template<class T1, class T2>
00455   bool operator!=(region_allocator<T1> const& al1, 
00456                   region_allocator<T2> const& al2) throw() {
00457     return &al1.region != &al2.region;
00458   }
00459 
00460 }
00461 
00462 // STATISTICS: kernel-other