//-*-c++-*- #ifndef CONTAINERCC #define CONTAINERCC // Copyright (c) 1994-1996 Ohio Board of Regents and the University of // Cincinnati. All Rights Reserved. // BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY // FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT // PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, // EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR // PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE // PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME // THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. // IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING // WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR // REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR // DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL // DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM // (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED // INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF // THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER // OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. // // $Id: Container.cc,v 1.1.1.1 2007/03/15 15:45:06 rmadhoun Exp $ // //--------------------------------------------------------------------------- #include "Container.hh" #ifdef REUSE class BasicEvent; class AckRecord; class BasicTimeWarp; class BasicState; class FileData; class InFileData; class SigEvent; #ifdef REUSESTATS static int BEtotalAlloc = 0; static int BEtotalRequested = 0; static int BEtotalReused = 0; static int BEtotalReleased = 0; static int BTWtotalAlloc = 0; static int BTWtotalRequested = 0; static int BTWtotalReused = 0; static int BTWtotalReleased = 0; static int BStotalAlloc = 0; static int BStotalRequested = 0; static int BStotalReused = 0; static int BStotalReleased = 0; static int ARtotalAlloc = 0; static int ARtotalRequested = 0; static int ARtotalReused = 0; static int ARtotalReleased = 0; static int FDtotalAlloc = 0; static int FDtotalRequested = 0; static int FDtotalReused = 0; #endif // REUSESTATS // pointers for all the freepools Container* Container::freepool = NULL; unsigned Container::freesize = 0; Container* Container::freepool = NULL; unsigned Container::freesize = 0; Container* Container::freepool = NULL; unsigned Container::freesize = 0; Container* Container::freepool = NULL; unsigned Container::freesize = 0; Container* Container::freepool = NULL; unsigned Container::freesize = 0; Container* Container::freepool = NULL; unsigned Container::freesize = 0; static const unsigned containerChunk = 10; void* Container::operator new(size_t size) { register Container *newptr; if (Container::freesize == 0) { #ifdef REUSESTATS BTWtotalAlloc ++; BTWtotalRequested++; #endif Container::freesize = containerChunk; // allocate containerChunk new containers size_t chunk = containerChunk * size; Container::freepool = (Container*) new char[chunk]; // thread new memory together. for (newptr =Container:: freepool; newptr != &(Container::freepool[containerChunk - 1]); newptr++) { newptr->next = newptr + 1; } } #ifdef REUSESTATS else { BTWtotalReused++; BTWtotalRequested++; } #endif newptr = Container::freepool; Container::freepool = Container::freepool->next; Container::freesize--; return newptr; } void* Container::operator new(size_t size) { register Container *newptr; if (Container::freesize == 0) { #ifdef REUSESTATS BStotalAlloc ++; BStotalRequested++; #endif Container::freesize = containerChunk; // allocate containerChunk new containers size_t chunk = containerChunk * size; Container::freepool = (Container*) new char[chunk]; // thread new memory together. for (newptr = Container::freepool; newptr != &(Container::freepool[containerChunk - 1]); newptr++) { newptr->next = newptr + 1; // pointer arithmetic! } } #ifdef REUSESTATS else { BStotalReused++; BStotalRequested++; } #endif newptr =Container:: freepool; Container::freepool = Container::freepool->next; Container::freesize--; return newptr; } void* Container::operator new(size_t size) { register Container *newptr; if (Container::freesize == 0) { #ifdef REUSESTATS BEtotalAlloc++; BEtotalRequested++; #endif Container::freesize = containerChunk; // allocate containerChunk new containers size_t chunk = containerChunk * size; Container::freepool = (Container*) new char[chunk]; // thread new memory together. for (newptr = Container::freepool; newptr != &(Container::freepool[containerChunk - 1]); newptr++) { newptr->next = newptr + 1; // pointer arithmetic! } } #ifdef REUSESTATS else { BEtotalReused++; BEtotalRequested++; } #endif newptr = Container::freepool; Container::freepool = Container::freepool->next; Container::freesize--; return newptr; } void* Container::operator new(size_t size) { register Container *newptr; if (Container::freesize == 0) { #ifdef REUSESTATS ARtotalAlloc ++; ARtotalRequested++; #endif Container::freesize = containerChunk; // allocate containerChunk new containers size_t chunk = containerChunk * size; Container::freepool = (Container*) new char[chunk]; // thread new memory together. for (newptr = Container::freepool; newptr != &(Container::freepool[containerChunk - 1]); newptr++) { newptr->next = newptr + 1; } } #ifdef REUSESTATS else { ARtotalReused++; ARtotalRequested++; } #endif newptr = Container::freepool; Container::freepool = Container::freepool->next; Container::freesize--; return newptr; } void* Container::operator new(size_t size) { register Container *newptr; if (Container::freesize == 0) { #ifdef REUSESTATS FDtotalAlloc ++; FDtotalRequested++; #endif Container::freesize = containerChunk; // allocate containerChunk new containers size_t chunk = containerChunk * size; Container::freepool = (Container*) new char[chunk]; // thread new memory together. for (newptr = Container::freepool; newptr != &(Container::freepool[containerChunk - 1]); newptr++) { newptr->next = newptr + 1; } } #ifdef REUSESTATS else { FDtotalReused++; FDtotalRequested++; } #endif newptr = Container::freepool; Container::freepool = Container::freepool->next; Container::freesize--; return newptr; } void Container::operator delete(void* delptr) { #ifdef REUSESTATS ARtotalReleased++; #endif ((Container*)delptr)->next = Container::freepool; Container::freepool = (Container *)delptr; Container::freesize++; } void* Container::operator new(size_t size) { register Container *newptr; if (Container::freesize == 0) { #ifdef REUSESTATS FDtotalAlloc ++; FDtotalRequested++; #endif Container::freesize = containerChunk; // allocate containerChunk new containers size_t chunk = containerChunk * size; Container::freepool = (Container*) new char[chunk]; // thread new memory together. for (newptr = Container::freepool; newptr != &(Container::freepool[containerChunk - 1]); newptr++) { newptr->next = newptr + 1; } } #ifdef REUSESTATS else { FDtotalReused++; FDtotalRequested++; } #endif newptr = Container::freepool; Container::freepool = Container::freepool->next; Container::freesize--; return newptr; } void Container::operator delete(void* delptr) { #ifdef REUSESTATS ARtotalReleased++; #endif ((Container*)delptr)->next = Container::freepool; Container::freepool = (Container *)delptr; Container::freesize++; } void Container::operator delete(void* delptr) { #ifdef REUSESTATS ARtotalReleased++; #endif ((Container*)delptr)->next = Container::freepool; Container::freepool = (Container *)delptr; Container::freesize++; } void Container::operator delete(void* delptr) { #ifdef REUSESTATS BEtotalReleased++; #endif ((Container*)delptr)->next = Container::freepool; Container::freepool = (Container *)delptr; Container::freesize++; } void Container::operator delete(void* delptr) { #ifdef REUSESTATS BTWtotalReleased++; #endif ((Container*)delptr)->next = Container::freepool; Container::freepool = (Container *)delptr; Container::freesize++; } void Container::operator delete(void* delptr) { #ifdef REUSESTATS BStotalReleased++; #endif ((Container*)delptr)->next = Container::freepool; Container:: freepool = (Container *)delptr; Container::freesize++; } template inline void Container ::cleanup() { Container *delptr; while (freepool) { delptr = freepool; freepool = freepool->next; ::delete delptr; freesize = 0; } } void* Container::operator new(size_t size) { register Container *newptr; if (Container::freesize == 0) { #ifdef REUSESTATS FDtotalAlloc++; FDtotalRequested++; #endif Container::freesize = containerChunk; // allocate containerChunk new containers size_t chunk = containerChunk * size; Container::freepool = (Container*) new char[chunk]; // thread new memory together. for (newptr = Container::freepool; newptr != &(Container::freepool[containerChunk - 1]); newptr++) { newptr->next = newptr + 1; } } #ifdef REUSESTATS else { FDtotalReused++; FDtotalRequested++; } #endif newptr = Container::freepool; Container::freepool = Container::freepool->next; Container::freesize--; return (Container*)newptr; } void Container::operator delete(void* delptr) { #ifdef REUSESTATS ARtotalReleased++; #endif ((Container*)delptr)->next = Container::freepool; Container::freepool = (Container *)delptr; Container::freesize++; } #ifdef REUSESTATS void Container::printReuseStats(ostream& os) { int mallocCalls = BTWtotalAlloc + BStotalAlloc + ARtotalAlloc + BEtotalAlloc + FDtotalAlloc; int newCons = BTWtotalRequested + BEtotalRequested + BStotalRequested + ARtotalRequested + FDtotalRequested; int reusedCons = BTWtotalReused + BEtotalReused + BStotalReused + ARtotalReused + FDtotalReused; int mallocBytes = mallocCalls * containerChunk * sizeof(Container); int newBytes = newCons * sizeof(Container); int reusedBytes = reusedCons * sizeof(Container); float reusedPercent = ((float)reusedCons/(float)newCons) * 100; os << " Container reuse statistics:\n"; os << " Total memory actually allocated: " << mallocBytes << " bytes in " << mallocCalls << " chunks of " << containerChunk * sizeof(Container) << " bytes\n"; os << " Total memory requested: " << newBytes << " bytes in " << newCons << " chunks of " << sizeof(Container) << " bytes\n"; os << " Total reused memory: " << reusedBytes << " bytes in " << reusedCons << " chunks of " << sizeof(Container) << " bytes\n"; os << " Memory reuse = " << reusedPercent<< "%" << endl; } #endif // REUSESTATS #endif // REUSE #endif // CONTAINER_CC