Generated on for Gecode by doxygen 1.17.0
block-allocator.hpp
Go to the documentation of this file.
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Christian Schulte <schulte@gecode.dev>
5 *
6 * Copyright:
7 * Christian Schulte, 2004
8 *
9 * This file is part of Gecode, the generic constraint
10 * development environment:
11 * http://www.gecode.dev
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of this software and associated documentation files (the
15 * "Software"), to deal in the Software without restriction, including
16 * without limitation the rights to use, copy, modify, merge, publish,
17 * distribute, sublicense, and/or sell copies of the Software, and to
18 * permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 *
32 */
33
34namespace Gecode { namespace Support {
35
44 template<class T, class A, int blocksize = 512>
46 private:
48 A& a;
50 class Block {
51 public:
52 T b[blocksize];
53 Block* next;
54 };
56 Block* b;
58 T* n;
60 size_t _size;
62 void allocate(void);
63 public:
69 A& allocator(void);
71 T* operator ()(void);
73 size_t size(void) const;
74 };
75
83 template<class T, class A, int blocksize = 512>
85 public:
87 static void* operator new(size_t s, BlockAllocator<T,A,blocksize>& ba);
89 static void operator delete(void*, BlockAllocator<T,A,blocksize>& ba);
91 static void operator delete(void*);
92 };
93
94
95
96 template<class T, class A, int blocksize>
97 forceinline
99 b = static_cast<Block*>(a.ralloc(sizeof(Block)));
100 b->next = nullptr;
101 n = &b->b[blocksize];
102 _size = sizeof(Block);
103 }
104
105 template<class T, class A, int blocksize>
106 forceinline
108 while (b != nullptr) {
109 Block* f = b; b = b->next;
110 a.rfree(f,sizeof(Block));
111 }
112 }
113
114 template<class T, class A, int blocksize>
115 forceinline A&
117 return a;
118 }
119
120 template<class T, class A, int blocksize>
121 forceinline T*
123 T* t = --n;
124 if (t == &b->b[0])
125 allocate();
126 return t;
127 }
128
129 template<class T, class A, int blocksize>
130 void
131 BlockAllocator<T,A,blocksize>::allocate(void) {
132 // Allocate another block
133 Block* nb = static_cast<Block*>(a.ralloc(sizeof(Block)));
134 nb->next = b; b = nb;
135 n = &nb->b[blocksize];
136 _size += sizeof(Block);
137 }
138
139 template<class T, class A, int blocksize>
140 forceinline size_t
142 return _size;
143 }
144
145
146
147 template<class T, class A, int blocksize>
148 forceinline void
152 template<class T, class A, int blocksize>
153 forceinline void
156 template<class T, class A, int blocksize>
157 forceinline void*
162
163}}
164
165// STATISTICS: support-any
Manage memory organized into block lists (allocator).
A & allocator(void)
Return allocator used.
~BlockAllocator(void)
Free all allocated blocks.
size_t size(void) const
Return size of memory required by allocator.
T * operator()(void)
Return memory of size required by T.
Client for block allocator of type T.
Support algorithms and datastructures
Gecode toplevel namespace
IntPropLevel ba(IntPropLevel ipl)
Extract basic or advanced from propagation level.
Definition ipl.hpp:43