allocators.hpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
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
00257
00258
00259
00260
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
00438
00439
00440
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
00450
00451
00452
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