Generated on Sat Feb 7 2015 02:01:31 for Gecode by doxygen 1.8.9.1
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.org>
5  *
6  * Copyright:
7  * Christian Schulte, 2004
8  *
9  * Last modified:
10  * $Date: 2010-07-28 16:13:53 +0200 (Wed, 28 Jul 2010) $ by $Author: schulte $
11  * $Revision: 11292 $
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 namespace Gecode { namespace Support {
39 
48  template<class T, class A, int blocksize = 512>
50  private:
52  A& a;
54  class Block {
55  public:
56  T b[blocksize];
57  Block* next;
58  };
60  Block* b;
62  T* n;
64  size_t _size;
66  void allocate(void);
67  public:
69  BlockAllocator(A& a);
71  ~BlockAllocator(void);
73  A& allocator(void);
75  T* operator ()(void);
77  size_t size(void) const;
78  };
79 
87  template<class T, class A, int blocksize = 512>
88  class BlockClient {
89  public:
91  static void* operator new(size_t s, BlockAllocator<T,A,blocksize>& ba);
93  static void operator delete(void*, BlockAllocator<T,A,blocksize>& ba);
95  static void operator delete(void*);
96  };
97 
98 
99 
100  template<class T, class A, int blocksize>
103  b = static_cast<Block*>(a.ralloc(sizeof(Block)));
104  b->next = NULL;
105  n = &b->b[blocksize];
106  _size = sizeof(Block);
107  }
108 
109  template<class T, class A, int blocksize>
112  while (b != NULL) {
113  Block* f = b; b = b->next;
114  a.rfree(f,sizeof(Block));
115  }
116  }
117 
118  template<class T, class A, int blocksize>
119  forceinline A&
121  return a;
122  }
123 
124  template<class T, class A, int blocksize>
125  forceinline T*
127  T* t = --n;
128  if (t == &b->b[0])
129  allocate();
130  return t;
131  }
132 
133  template<class T, class A, int blocksize>
134  void
136  // Allocate another block
137  Block* nb = static_cast<Block*>(a.ralloc(sizeof(Block)));
138  nb->next = b; b = nb;
139  n = &nb->b[blocksize];
140  _size += sizeof(Block);
141  }
142 
143  template<class T, class A, int blocksize>
144  forceinline size_t
146  return _size;
147  }
148 
149 
150 
151  template<class T, class A, int blocksize>
152  forceinline void
155  }
156  template<class T, class A, int blocksize>
157  forceinline void
159  }
160  template<class T, class A, int blocksize>
161  forceinline void*
164  return ba();
165  }
166 
167 }}
168 
169 // STATISTICS: support-any
NodeType t
Type of node.
Definition: bool-expr.cpp:234
T * operator()(void)
Return memory of size required by T.
~BlockAllocator(void)
Free all allocated blocks.
A & allocator(void)
Return allocator used.
size_t size(void) const
Return size of memory required by allocator.
Manage memory organized into block lists (allocator)
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
Client for block allocator of type T.
#define forceinline
Definition: config.hpp:132
struct Gecode::@518::NNF::@57::@58 b
For binary nodes (and, or, eqv)
Gecode toplevel namespace
struct Gecode::@518::NNF::@57::@59 a
For atomic nodes.