|
Sierra Toolkit
Version of the Day
|
00001 /*--------------------------------------------------------------------*/ 00002 /* Copyright 2005 Sandia Corporation. */ 00003 /* Under the terms of Contract DE-AC04-94AL85000, there is a */ 00004 /* non-exclusive license for use of this work by or on behalf */ 00005 /* of the U.S. Government. Export of this program may require */ 00006 /* a license from the United States Government. */ 00007 /*--------------------------------------------------------------------*/ 00008 00009 #include <stk_util/util/Pool.hpp> 00010 00011 namespace stk_classic { 00012 namespace util { 00013 00014 Pool::Pool(unsigned int sz) 00015 : chunks(NULL), 00016 esize(sz<sizeof(Link) ? sizeof(Link) : sz), 00017 head(NULL) 00018 { 00019 } 00020 00021 Pool::~Pool() 00022 { 00023 //free all chunks 00024 Chunk* n = chunks; 00025 while(n) { 00026 Chunk* p = n; 00027 n = n->next; 00028 delete p; 00029 } 00030 } 00031 00032 void 00033 Pool::grow() 00034 { 00035 //allocate new chunk, organize it as a linked list of elements of size 'esize' 00036 Chunk* n = new Chunk; 00037 n->next = chunks; 00038 chunks = n; 00039 00040 const int nelem = Chunk::size/esize; 00041 char* start = n->mem; 00042 char* last = &start[ (nelem-1)*esize ]; 00043 for(char* p=start; p<last; p+=esize) { 00044 reinterpret_cast<Link*>(p)->next = reinterpret_cast<Link*>(p+esize); 00045 } 00046 reinterpret_cast<Link*>(last)->next = NULL; 00047 head = reinterpret_cast<Link*>(start); 00048 } 00049 00050 }//namespace util 00051 }//namespace stk_classic 00052