src/Utility/Pool.cpp

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 /***************************************************************************
00003  *
00004  * The IPPL Framework
00005  * 
00006  * This program was prepared by PSI. 
00007  * All rights in the program are reserved by PSI.
00008  * Neither PSI nor the author(s)
00009  * makes any warranty, express or implied, or assumes any liability or
00010  * responsibility for the use of this software
00011  *
00012  * Visit http://www.acl.lanl.gov/POOMS for more details
00013  *
00014  ***************************************************************************/
00015 
00016 // -*- C++ -*-
00017 /***************************************************************************
00018  *
00019  * The IPPL Framework
00020  * 
00021  *
00022  * Visit http://people.web.psi.ch/adelmann/ for more details
00023  *
00024  ***************************************************************************/
00025 
00026 // include files
00027 #include "Utility/Pool.h"
00028 #include "Utility/PAssert.h"
00029 #include "Profile/Profiler.h"
00030 
00031 #include <stdlib.h>
00032 
00033 // Null ctor creates an invalid Pool.
00034 Pool::Pool()
00035   : outstandingAllocs(0),          // Number of blocks given to user
00036     bsize(0),                      // The size of each block
00037     nblock(0),                     // Number of blocks
00038     head(0)                        // the first one.
00039 {
00040 }
00041 
00042 // Make a new Pool for objects of a given size.
00043 Pool::Pool(size_t sz)
00044   : outstandingAllocs(0),          // Number of blocks given to user
00045     bsize(round_to_align(sz)),     // The size of each block
00046     nblock(blocks_in_page(bsize)), // Number of blocks
00047     head(0)                        // the first one.
00048 {
00049 }
00050 
00051 // Destroy the Pool.
00052 Pool::~Pool()
00053 {
00054   PInsist(outstandingAllocs==0,"Not all of the pooled memory was freed!");
00055 
00056   // Loop over the allocated chunks and free them.
00057   vector<char*>::iterator p, pend = chunks.end();
00058   for (p = chunks.begin(); p != pend; ++p)
00059     delete [] *p;
00060 }
00061 
00062 // Grow a Pool.
00063 void Pool::grow()
00064 {
00065   TAU_PROFILE("Pool::grow()", "void ()", TAU_UTILITY);
00066   size_t alloc_this;
00067   if ( bsize>page() )
00068     alloc_this = bsize;
00069   else
00070     alloc_this = page();
00071 
00072   char *start = new char[alloc_this];     // Allocate aligned space.
00073   chunks.push_back(start);                // Put it in list of chunks.
00074   char *last  = start + (nblock-1)*bsize; // Get a pointer to the last one.
00075 
00076   for (char *p=start; p!=last; p+=bsize)  // For all but the last one
00077     ((Link*)p)->next = (Link*)(p+bsize);  //   point to the next.
00078   ((Link*)last)->next = head;             // The last points to the head
00079   head = (Link*)start;                    // Reset the head to the first.
00080 }
00081 
00082 /***************************************************************************
00083  * $RCSfile: Pool.cpp,v $   $Author: adelmann $
00084  * $Revision: 1.1.1.1 $   $Date: 2003/01/23 07:40:33 $
00085  * IPPL_VERSION_ID: $Id: Pool.cpp,v 1.1.1.1 2003/01/23 07:40:33 adelmann Exp $ 
00086  ***************************************************************************/

Generated on Mon Jan 16 13:23:59 2006 for IPPL by  doxygen 1.4.6