Generated on Thu Jan 16 2025 00:00:00 for Gecode by doxygen 1.14.0
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 * This file is part of Gecode, the generic constraint
13 * development environment:
14 * http://www.gecode.org
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
38namespace Gecode {
39
40 template<class T> struct space_allocator;
41
148
149
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 }
220
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; }
232 size_type max_size(void) const throw() {
233 return std::numeric_limits<size_type>::max() /
234 (sizeof(T)>0 ? sizeof(T) : 1);
235 }
236
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>
293 space_allocator<T2> const& al2) throw() {
294 return &al1.space == &al2.space;
295 }
296
303 template<class T1, class T2>
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
369
374 : region(al.region) {}
375
379 template<class U>
381 : region(al.region) {}
382
384 pointer address(reference x) const { return &x; }
388 size_type max_size(void) const throw() {
389 return std::numeric_limits<size_type>::max()
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-memory
Handle to region.
Definition region.hpp:55
Computation spaces.
Definition core.hpp:1744
Gecode toplevel namespace
void count(Home home, const IntVarArgs &x, int n, IntRelType irt, int m, IntPropLevel ipl=IPL_DEF)
Post propagator for .
Definition count.cpp:40
void element(Home home, IntSharedArray n, IntVar x0, IntVar x1, IntPropLevel ipl=IPL_DEF)
Post domain consistent propagator for .
Definition element.cpp:39
Post propagator for SetVar x
Definition set.hh:773
bool operator==(const FloatVal &x, const FloatVal &y)
Definition val.hpp:294
bool operator!=(const FloatVal &x, const FloatVal &y)
Definition val.hpp:317
Rebinding helper (returns the type of a similar allocator for type U).
region_allocator< U > other
The allocator type for U.
Rebinding helper (returns the type of a similar allocator for type U)
Allocator that allocates memory from a region.
size_type max_size(void) const
Returns the largest size for which a call to allocate might succeed.
region_allocator(region_allocator< U > const &al)
Copy from other instantiation.
void construct(pointer element, const_reference t)
Constructs an object.
void deallocate(pointer p, size_type count)
Deallocates storage.
pointer allocate(size_type count, const void *const hint)
Allocates storage.
void destroy(pointer element)
Calls the destructor on the object pointed to by element.
pointer allocate(size_type count)
Allocates storage.
region_allocator(region_allocator const &al)
Copy construction.
const_pointer address(const_reference x) const
Convert a const reference x to a const pointer.
pointer address(reference x) const
Convert a reference x to a pointer.
Rebinding helper (returns the type of a similar allocator for type U).
space_allocator< U > other
The allocator type for U.
Rebinding helper (returns the type of a similar allocator for type U)
Allocator that allocates memory from a space heap.
void construct(pointer element, const_reference t)
space_allocator(space_allocator const &al)
Copy construction.
space_allocator & operator=(space_allocator const &al)
Assignment operator.
pointer allocate(size_type count, const void *const hint)
Allocates storage.
space_allocator(space_allocator< U > const &al)
Copy from other instantiation.
pointer address(reference x) const
Convert a reference x to a pointer.
void destroy(pointer element)
Calls the destructor on the object pointed to by element.
const_pointer address(const_reference x) const
Convert a const reference x to a const pointer.
size_type max_size(void) const
Returns the largest size for which a call to allocate might succeed.
void deallocate(pointer p, size_type count)
Deallocates the storage obtained by a call to allocate() with arguments count and p.
pointer allocate(size_type count)
Allocates storage.