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
00035
00036
00037
00038 #include <gecode/kernel.hh>
00039
00040 namespace Gecode {
00041
00042 void*
00043 Region::heap_alloc(size_t s) {
00044 void* p = heap.ralloc(s);
00045 if (hi == NULL) {
00046 hi = p;
00047 assert(!Support::marked(hi));
00048 } else if (!Support::marked(hi)) {
00049 HeapInfo* h = static_cast<HeapInfo*>
00050 (heap.ralloc(sizeof(HeapInfo)+4*sizeof(void*)));
00051 h->n=2; h->size=4;
00052 h->blocks[0]=hi; h->blocks[1]=p;
00053 hi = Support::mark(h);
00054 } else {
00055 HeapInfo* h = static_cast<HeapInfo*>(Support::unmark(hi));
00056 if (h->n == h->size) {
00057 HeapInfo* n = static_cast<HeapInfo*>
00058 (heap.ralloc(sizeof(HeapInfo)+2*h->n*sizeof(void*)));
00059 n->size = 2*h->n;
00060 n->n = h->n;
00061 memcpy(&n->blocks[0], &h->blocks[0], h->n*sizeof(void*));
00062 hi = Support::mark(n);
00063 h = n;
00064 }
00065 h->blocks[h->n++] = p;
00066 }
00067 return p;
00068 }
00069
00070 void
00071 Region::heap_free(void) {
00072 assert(hi != NULL);
00073 if (Support::marked(hi)) {
00074 HeapInfo* h = static_cast<HeapInfo*>(Support::unmark(hi));
00075 for (unsigned int i=h->n; i--; )
00076 heap.rfree(h->blocks[i]);
00077 heap.rfree(h);
00078 } else {
00079 heap.rfree(hi);
00080 }
00081 }
00082
00083 }
00084
00085