Path: blob/devel/ElmerGUI/netgen/libsrc/general/optmem.cpp
3206 views
/**************************************************************************/1/* File: optmem.cc */2/* Author: Joachim Schoeberl */3/* Date: 04. Apr. 97 */4/**************************************************************************/56/*7Abstract data type ARRAY8*/91011#include <mystdlib.h>12#include <myadt.hpp>1314namespace netgen15{16//using namespace netgen;1718BlockAllocator :: BlockAllocator (unsigned asize, unsigned ablocks)19: bablocks (0)20{21if (asize < sizeof(void*))22asize = sizeof(void*);23size = asize;24blocks = ablocks;25freelist = NULL;26}2728BlockAllocator :: ~BlockAllocator ()29{30//for (unsigned i = 0; i < bablocks.Size(); i++)31for (int i = 0; i < bablocks.Size(); i++)32delete [] bablocks[i];33}3435void * BlockAllocator :: Alloc ()36{37// return new char[size];38if (!freelist)39{40// cout << "freelist = " << freelist << endl;41// cout << "BlockAlloc: " << size*blocks << endl;42char * hcp = new char [size * blocks];43bablocks.Append (hcp);44bablocks.Last() = hcp;45for (unsigned i = 0; i < blocks-1; i++)46*(void**)&(hcp[i * size]) = &(hcp[ (i+1) * size]);47*(void**)&(hcp[(blocks-1)*size]) = NULL;48freelist = hcp;49}5051void * p = freelist;52freelist = *(void**)freelist;53return p;54}5556/*57void BlockAllocator :: Free (void * p)58{59*(void**)p = freelist;60freelist = p;61}62*/63}646566