mFES - molecular Finite Element Solver  0.4
optmem.h
Go to the documentation of this file.
00001 /**************************************************************************/
00002 /* File:   optmem.cpp                                                     */
00003 /* Author: Joachim Schoeberl                                              */
00004 /* Date:   04. Apr. 97                                                    */
00005 /**************************************************************************/
00006 
00007 /* 
00008    Abstract data type Array
00009 */
00010 
00011 
00012 #include <mystdlib.h>
00013 #include <myadt.hpp>
00014 
00015 namespace netgen
00016 {
00017 
00018   BlockAllocator :: BlockAllocator (unsigned asize, unsigned ablocks)
00019     : bablocks (0)
00020   {
00021     if (asize < sizeof(void*))
00022       asize = sizeof(void*);
00023     size = asize;
00024     blocks = ablocks;
00025     freelist = NULL;
00026   }
00027 
00028   BlockAllocator :: ~BlockAllocator ()
00029   {
00030     for (int i = 0; i < bablocks.Size(); i++)
00031       delete [] bablocks[i];
00032   }
00033 
00034   void * BlockAllocator :: Alloc ()
00035   {
00036     //  return new char[size];
00037     if (!freelist)
00038       {
00039         // cout << "freelist = " << freelist << endl;
00040         // cout << "BlockAlloc: " << size*blocks << endl;
00041         char * hcp = new char [size * blocks];
00042         bablocks.Append (hcp);
00043         bablocks.Last() = hcp;
00044         for (unsigned i = 0; i < blocks-1; i++)
00045           *(void**)&(hcp[i * size]) = &(hcp[ (i+1) * size]);
00046         *(void**)&(hcp[(blocks-1)*size]) = NULL;
00047         freelist = hcp;
00048       }
00049 
00050     void * p = freelist;
00051     freelist = *(void**)freelist;
00052     return p;
00053   }
00054 
00055   /*
00056   void BlockAllocator :: Free (void * p)
00057   {
00058     *(void**)p = freelist;
00059     freelist = p;
00060   }
00061   */
00062 }