Sacado_Fad_MemPoolImp.hpp

Go to the documentation of this file.
00001 // $Id$ 
00002 // $Source$ 
00003 // @HEADER
00004 // ***********************************************************************
00005 // 
00006 //                           Sacado Package
00007 //                 Copyright (2006) Sandia Corporation
00008 // 
00009 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00010 // the U.S. Government retains certain rights in this software.
00011 // 
00012 // This library is free software; you can redistribute it and/or modify
00013 // it under the terms of the GNU Lesser General Public License as
00014 // published by the Free Software Foundation; either version 2.1 of the
00015 // License, or (at your option) any later version.
00016 //  
00017 // This library is distributed in the hope that it will be useful, but
00018 // WITHOUT ANY WARRANTY; without even the implied warranty of
00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020 // Lesser General Public License for more details.
00021 //  
00022 // You should have received a copy of the GNU Lesser General Public
00023 // License along with this library; if not, write to the Free Software
00024 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00025 // USA
00026 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
00027 // (etphipp@sandia.gov).
00028 // 
00029 // ***********************************************************************
00030 // @HEADER
00031 
00032 #include <new>
00033 
00034 inline
00035 Sacado::Fad::MemPool::MemPool(unsigned int elem_size, unsigned int n_elem,
00036             unsigned int pre_alloc) :
00037   esize(elem_size < sizeof(Link) ? sizeof(Link) : elem_size),
00038   n(n_elem),
00039   csize(esize*n+sizeof(Chunk)),
00040   chunks(NULL),
00041   head(NULL),
00042   num_chunks(0)
00043 {
00044   // Pre allocate chunks if required
00045   if (pre_alloc) 
00046     for (unsigned int i=0; i<pre_alloc; i++)
00047       grow();
00048 }
00049 
00050 inline 
00051 Sacado::Fad::MemPool::~MemPool()
00052 {
00053   Chunk * nc = chunks;
00054   while (nc != NULL) {
00055     Chunk * p = nc;
00056     nc = nc->next;
00057     operator delete((void*) p);
00058   }
00059 }
00060 
00061 inline void*
00062 Sacado::Fad::MemPool::alloc()
00063 {
00064   if (head == NULL)
00065     grow();
00066   Link *p = head;
00067   head = p->next;
00068 
00069   return p;
00070 }
00071 
00072 inline void 
00073 Sacado::Fad::MemPool::free(void *b)
00074 {
00075   if (b == NULL)
00076     return;
00077   Link *p = static_cast<Link*>(b);
00078   p->next = head;
00079   head = p;
00080 }
00081 
00082 inline void
00083 Sacado::Fad::MemPool::grow()
00084 {
00085   // Create a new chunk
00086   void *p = operator new(csize);
00087   Chunk *c = static_cast<Chunk*>(p);
00088   c->mem = static_cast<char*>(p)+sizeof(Chunk);
00089   c->next = chunks;
00090   chunks = c;
00091   ++num_chunks;
00092 
00093   // Initialize each element in a chunk
00094   char *start = c->mem;
00095   char *last = &start[(n-1)*esize];
00096   
00097   for (char *q = start; q<last; q += esize)
00098     reinterpret_cast<Link*>(q)->next = reinterpret_cast<Link*>(q+esize);
00099   reinterpret_cast<Link*>(last)->next = NULL;
00100   head = reinterpret_cast<Link*>(start);
00101 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Wed Apr 13 10:19:32 2011 for Sacado Package Browser (Single Doxygen Collection) by  doxygen 1.6.3