source: ThirdParty/mpqc_open/src/lib/util/group/pool.cc@ 47b463

Action_Thermostats Add_AtomRandomPerturbation Add_RotateAroundBondAction Add_SelectAtomByNameAction Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_StructOpt_integration_tests AutomationFragmentation_failures Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_ChronosMutex Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion GeometryObjects Gui_displays_atomic_force_velocity IndependentFragmentGrids_IntegrationTest JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks RotateToPrincipalAxisSystem_UndoRedo StoppableMakroAction Subpackage_levmar Subpackage_vmg ThirdParty_MPQC_rebuilt_buildsystem TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps Ubuntu_1604_changes stable
Last change on this file since 47b463 was 860145, checked in by Frederik Heber <heber@…>, 8 years ago

Merge commit '0b990dfaa8c6007a996d030163a25f7f5fc8a7e7' as 'ThirdParty/mpqc_open'

  • Property mode set to 100644
File size: 8.9 KB
Line 
1//
2// pool.cc
3//
4// Copyright (C) 1996 Limit Point Systems, Inc.
5//
6// Author: Curtis Janssen <cljanss@limitpt.com>
7// Maintainer: LPS
8//
9// This file is part of the SC Toolkit.
10//
11// The SC Toolkit is free software; you can redistribute it and/or modify
12// it under the terms of the GNU Library General Public License as published by
13// the Free Software Foundation; either version 2, or (at your option)
14// any later version.
15//
16// The SC Toolkit is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU Library General Public License for more details.
20//
21// You should have received a copy of the GNU Library General Public License
22// along with the SC Toolkit; see the file COPYING.LIB. If not, write to
23// the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24//
25// The U.S. Government is granted a limited license as per AL 91-7.
26//
27
28#ifdef __GNUC__
29#pragma implementation
30#endif
31
32#include <util/misc/formio.h>
33#include <util/group/pool.h>
34
35using namespace std;
36using namespace sc;
37
38void
39PoolData::check(void* lower_bound, void* upper_bound)
40{
41 if ((void*)this < lower_bound || (void*)this >= upper_bound) {
42 ExEnv::errn() << scprintf("PoolData::check: this out of bounds\n");
43 abort();
44 }
45 if (next_) {
46 if ((void*)next_ < lower_bound || (void*)next_ >= upper_bound) {
47 ExEnv::errn() << scprintf("PoolData::check: next_ out of bounds\n");
48 abort();
49 }
50 if (next_->prev_ != this) {
51 ExEnv::errn() << scprintf("PoolData::check: next pd doesn't point back\n");
52 abort();
53 }
54 if ((char*)next_ != (char*)this + size_ + PoolData_aligned_size) {
55 ExEnv::errn() << scprintf("PoolData::check: next_ not consistent with size\n");
56 abort();
57 }
58 if (free_ && next_->free_) {
59 ExEnv::errn() << scprintf("PoolData::check: free and next is free\n");
60 abort();
61 }
62 }
63 if (prev_) {
64 if ((void*)prev_ < lower_bound || (void*)prev_ >= upper_bound) {
65 ExEnv::errn() << scprintf("PoolData::check: prev_ out of bounds\n");
66 abort();
67 }
68 if (prev_->next_ != this) {
69 ExEnv::errn() << scprintf("PoolData::check: prev pd doesn't point back\n");
70 abort();
71 }
72 if (free_ && prev_->free_) {
73 ExEnv::errn() << scprintf("PoolData::check: free and prev is free\n");
74 abort();
75 }
76 }
77 if (free_) {
78 PoolData* n = f.next_free_;
79 PoolData* p = f.prev_free_;
80 if (n) {
81 if ((void*)n < lower_bound || (void*)n >= upper_bound) {
82 ExEnv::errn() << scprintf("PoolData::check: next free out of bounds\n");
83 abort();
84 }
85 if (n->f.prev_free_ != this) {
86 ExEnv::errn() << scprintf(
87 "PoolData::check: next free pd doesn't point back\n");
88 abort();
89 }
90 }
91 if (p) {
92 if ((void*)p < lower_bound || (void*)p >= upper_bound) {
93 ExEnv::errn() << scprintf("PoolData::check: prev free out of bounds\n");
94 abort();
95 }
96 if (p->f.next_free_ != this) {
97 ExEnv::errn() << scprintf(
98 "PoolData::check: prev free pd doesn't point back\n");
99 abort();
100 }
101 }
102 }
103}
104
105Pool::Pool(size_t size):
106 size_(size)
107{
108
109 // Initialize the first and last members of the data list.
110 firstdatum_ = (PoolData*)align_pool_data((void*)((char*)this+sizeof(Pool)));
111
112 if ((char*)this + size <= (char*) firstdatum_) {
113 ExEnv::errn() << scprintf("Pool::Pool: not given enough space\n");
114 abort();
115 }
116
117 size_t firstdatum_size = align_pool_data_downward((size_t)
118 (((char*)this+size)
119 - (char*)firstdatum_));
120 new(firstdatum_) PoolData(firstdatum_size);
121
122 firstdatum_->prev_next(0,0);
123
124 // Initialize the free lists.
125 int i;
126 for (i=0; i<freelist_size; i++) freelist_[i] = 0;
127 freelist_add(firstdatum_);
128}
129
130void
131Pool::freelist_add(PoolData*d)
132{
133 int slot = freelist_find_slot(d->size_);
134 d->free_ = 1;
135 PoolData* tmp = freelist_[slot];
136 d->next_free(tmp);
137 d->prev_free(0);
138 freelist_[slot] = d;
139 if (tmp) tmp->prev_free(d);
140#ifdef DEBUG_POOL
141 d->check();
142 if (d->next()) d->next()->check();
143 if (d->prev()) d->prev()->check();
144#endif
145}
146
147void
148Pool::freelist_del(PoolData*d)
149{
150 if (d->next_free()) d->next_free()->prev_free(d->prev_free());
151 if (d->prev_free()) d->prev_free()->next_free(d->next_free());
152 else {
153 int slot = freelist_find_slot(d->size_);
154 freelist_[slot] = d->next_free();
155 }
156 d->free_ = 0;
157#ifdef DEBUG_POOL
158 d->check();
159 if (d->next()) d->next()->check();
160 if (d->prev()) d->prev()->check();
161#endif
162}
163
164int
165Pool::freelist_find_slot(size_t size)
166{
167 int slot = 0;
168 size_t mask = ~ (size_t)0;
169 while(mask & size) {
170 slot++;
171 mask <<= 1;
172 }
173 return slot;
174}
175
176void*
177Pool::allocate(size_t size)
178{
179 int slot = freelist_find_slot(size);
180 for (int i=slot; i<freelist_size; i++) {
181 PoolData* j;
182 for (j=freelist_[i]; j; j = j->next_free()) {
183 if (j->size_ >= size) {
184 freelist_del(j);
185 // Maybe need to break this chunk into two pieces.
186 if (j->size_ > size + PoolData_aligned_size) {
187 PoolData* freechunk = (PoolData*)((char*)j
188 + PoolData_aligned_size
189 + size);
190 new(freechunk) PoolData(j->size_ - size);
191 freechunk->prev_next(j,j->next());
192 if (freechunk->next()) freechunk->next()->prev(freechunk);
193 j->size_ = size;
194 j->next(freechunk);
195 freelist_add(freechunk);
196 }
197#ifdef DEBUG_POOL
198 j->check();
199 if (j->next()) j->next()->check();
200 if (j->prev()) j->prev()->check();
201#endif
202 return j->data();
203 }
204 }
205 }
206 return 0;
207}
208
209void
210Pool::release(void* v)
211{
212 PoolData *d = voidptr_to_pd(v);
213 if (d->prev() && d->prev()->free_) {
214 freelist_del(d->prev());
215 d->prev()->size_ += d->size_ + PoolData_aligned_size;
216 d->prev()->next(d->next());
217 if (d->next()) d->next()->prev(d->prev());
218 d->set_magic(0);
219 d = d->prev();
220 }
221 if (d->next() && d->next()->free_) {
222 freelist_del(d->next());
223 d->next()->set_magic(0);
224 d->size_ += d->next()->size_ + PoolData_aligned_size;
225 if (d->next()->next()) d->next()->next()->prev(d);
226 d->next(d->next()->next());
227 }
228 freelist_add(d);
229}
230
231static void
232print_pooldata(ostream&o,PoolData*d,int free)
233{
234 PoolData *next,*prev;
235 if (free) {
236 next = d->next_free();
237 prev = d->prev_free();
238 }
239 else {
240 next = d->next();
241 prev = d->prev();
242 }
243
244 o << scprintf(" PoolData: size=%d", d->size_);
245 if (d->free_) o << scprintf(", free");
246 else o << scprintf(", allocated");
247 if (!prev) o << scprintf(", first");
248 if (!next) o << scprintf(", last");
249 o << endl;
250 if (next) print_pooldata(o,next,free);
251}
252
253void
254Pool::print(ostream&o)
255{
256 o << scprintf("Memory Pool:\n");
257 o << scprintf(" data chain:\n");
258 print_pooldata(o,firstdatum_,0);
259 for (int i=0; i<freelist_size; i++) {
260 if (freelist_[i]) {
261 o << scprintf(" freelist[%d]:\n",i);
262 print_pooldata(o,freelist_[i],1);
263 }
264 }
265}
266
267void
268Pool::check()
269{
270 // The bit lost at the beginning to Pool and alignment.
271 size_t start = (size_t)
272 ((char*)align_pool_data((void*)((char*)this + sizeof(Pool)))
273 - (char*)this);
274
275 // The bit lost at the end to alignment.
276 size_t end = (size_t)
277 ((char*)this + size_
278 - (char*) align_pool_data_downward((size_t)((void*)this)
279 +size_));
280
281 size_t size = start + end;
282
283 PoolData *j;
284 for (j=firstdatum_; j; j = j->next()) {
285 j->check(this,(void*)((char*)this+size_));
286 size += PoolData_aligned_size + j->size_;
287 }
288
289 if (size != size_) {
290 ExEnv::errn() << scprintf("Pool::check(): inconsistent sizes\n");
291 ExEnv::errn() << scprintf(" computed: %d\n",size);
292 ExEnv::errn() << scprintf(" actual: %d\n",size_);
293 abort();
294 }
295
296 // make sure that all data is accounted for
297 for (j=firstdatum_; j; j = j->next()) {
298 j->flags_ = 1;
299 }
300 for (int i=0; i<freelist_size; i++) {
301 for (j=freelist_[i]; j; j = j->next_free()) {
302 j->flags_ = 0;
303 }
304 }
305 for (j=firstdatum_; j; j = j->next()) {
306 if (j->free_ && j->flags_) {
307 ExEnv::errn() << scprintf("Pool::check: free data not in freelist\n");
308 abort();
309 }
310 }
311}
312
313/////////////////////////////////////////////////////////////////////////////
314
315// Local Variables:
316// mode: c++
317// c-file-style: "CLJ"
318// End:
Note: See TracBrowser for help on using the repository browser.