region.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
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
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