Generated on Thu Apr 11 13:59:16 2019 for Gecode by doxygen 1.6.3

region.cpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Christian Schulte <schulte@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Christian Schulte, 2008
00008  *
00009  *  This file is part of Gecode, the generic constraint
00010  *  development environment:
00011  *     http://www.gecode.org
00012  *
00013  *  Permission is hereby granted, free of charge, to any person obtaining
00014  *  a copy of this software and associated documentation files (the
00015  *  "Software"), to deal in the Software without restriction, including
00016  *  without limitation the rights to use, copy, modify, merge, publish,
00017  *  distribute, sublicense, and/or sell copies of the Software, and to
00018  *  permit persons to whom the Software is furnished to do so, subject to
00019  *  the following conditions:
00020  *
00021  *  The above copyright notice and this permission notice shall be
00022  *  included in all copies or substantial portions of the Software.
00023  *
00024  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00026  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00027  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00028  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00029  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00030  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00031  *
00032  */
00033 
00034 #include <gecode/kernel.hh>
00035 
00036 namespace Gecode {
00037 
00038   Region::Pool::Pool(void)
00039     : c(new Chunk), n_c(2U) {
00040     c->next = new Chunk; c->next->next = nullptr;
00041   }
00042   Region::Chunk*
00043   Region::Pool::chunk(void) {
00044     Chunk* n;
00045     {
00046       Support::Lock l(m);
00047       if (c != nullptr) {
00048         assert(n_c > 0U);
00049         n = c; c = c->next; n_c--;
00050       } else {
00051         n = new Region::Chunk;
00052       }
00053       n->reset();
00054     }
00055     return n;
00056   }
00057   void
00058   Region::Pool::chunk(Chunk* u) {
00059     Support::Lock l(m);
00060     if (n_c == Kernel::MemoryConfig::n_hc_cache) {
00061       delete u;
00062     } else {
00063       u->next = c; c = u;
00064       n_c++;
00065     }
00066   }
00067   Region::Pool::~Pool(void) {
00068     Support::Lock l(m);
00069     // If that were the case there would be a memory leak!
00070     assert(c != nullptr);
00071     do {
00072       Chunk* n=c->next;
00073       delete c;
00074       c=n;
00075     } while (c != nullptr);
00076   }
00077 
00078   Region::Pool& Region::pool(void) {
00079     static Region::Pool _p;
00080     return _p;
00081   }
00082 
00083   void*
00084   Region::heap_alloc(size_t s) {
00085     void* p = heap.ralloc(s);
00086     if (hi == nullptr) {
00087       hi = p;
00088       assert(!Support::marked(hi));
00089     } else if (!Support::marked(hi)) {
00090       HeapInfo* h = static_cast<HeapInfo*>
00091         (heap.ralloc(sizeof(HeapInfo)+(4-1)*sizeof(void*)));
00092       h->n=2; h->size=4;
00093       h->blocks[0]=hi; h->blocks[1]=p;
00094       hi = Support::mark(h);
00095     } else {
00096       HeapInfo* h = static_cast<HeapInfo*>(Support::unmark(hi));
00097       if (h->n == h->size) {
00098         HeapInfo* n = static_cast<HeapInfo*>
00099           (heap.ralloc(sizeof(HeapInfo)+(2*h->n-1)*sizeof(void*)));
00100         n->size = 2*h->n;
00101         n->n = h->n;
00102         memcpy(&n->blocks[0], &h->blocks[0], h->n*sizeof(void*));
00103         hi = Support::mark(n);
00104         heap.rfree(h);
00105         h = n;
00106       }
00107       h->blocks[h->n++] = p;
00108     }
00109     return p;
00110   }
00111 
00112   void
00113   Region::heap_free(void) {
00114     assert(hi != nullptr);
00115     if (Support::marked(hi)) {
00116       HeapInfo* h = static_cast<HeapInfo*>(Support::unmark(hi));
00117       for (unsigned int i=0U; i<h->n; i++)
00118         heap.rfree(h->blocks[i]);
00119       heap.rfree(h);
00120     } else {
00121       heap.rfree(hi);
00122     }
00123   }
00124 
00125 }
00126 
00127 // STATISTICS: kernel-memory