Generated on Tue May 22 09:40:09 2018 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     m.acquire();
00046     if (c != nullptr) {
00047       assert(n_c > 0U);
00048       n = c; c = c->next; n_c--;
00049     } else {
00050       n = new Region::Chunk;
00051     }
00052     n->reset();
00053     m.release();
00054     return n;
00055   }
00056   void
00057   Region::Pool::chunk(Chunk* u) {
00058     m.acquire();
00059     if (n_c == Kernel::MemoryConfig::n_hc_cache) {
00060       delete u;
00061     } else {
00062       u->next = c; c = u;
00063       n_c++;
00064     }
00065     m.release();
00066   }
00067   Region::Pool::~Pool(void) {
00068     m.acquire();
00069     // If that were the case there is 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     m.release();
00077   }
00078 
00079   Region::Pool& Region::pool(void) {
00080     static Region::Pool _p;
00081     return _p;
00082   }
00083 
00084   void*
00085   Region::heap_alloc(size_t s) {
00086     void* p = heap.ralloc(s);
00087     if (hi == nullptr) {
00088       hi = p;
00089       assert(!Support::marked(hi));
00090     } else if (!Support::marked(hi)) {
00091       HeapInfo* h = static_cast<HeapInfo*>
00092         (heap.ralloc(sizeof(HeapInfo)+(4-1)*sizeof(void*)));
00093       h->n=2; h->size=4;
00094       h->blocks[0]=hi; h->blocks[1]=p;
00095       hi = Support::mark(h);
00096     } else {
00097       HeapInfo* h = static_cast<HeapInfo*>(Support::unmark(hi));
00098       if (h->n == h->size) {
00099         HeapInfo* n = static_cast<HeapInfo*>
00100           (heap.ralloc(sizeof(HeapInfo)+(2*h->n-1)*sizeof(void*)));
00101         n->size = 2*h->n;
00102         n->n = h->n;
00103         memcpy(&n->blocks[0], &h->blocks[0], h->n*sizeof(void*));
00104         hi = Support::mark(n);
00105         heap.rfree(h);
00106         h = n;
00107       }
00108       h->blocks[h->n++] = p;
00109     }
00110     return p;
00111   }
00112 
00113   void
00114   Region::heap_free(void) {
00115     assert(hi != nullptr);
00116     if (Support::marked(hi)) {
00117       HeapInfo* h = static_cast<HeapInfo*>(Support::unmark(hi));
00118       for (unsigned int i=h->n; i--; )
00119         heap.rfree(h->blocks[i]);
00120       heap.rfree(h);
00121     } else {
00122       heap.rfree(hi);
00123     }
00124   }
00125 
00126 }
00127 
00128 // STATISTICS: kernel-memory