ObjectArena.h

Go to the documentation of this file.
00001 
00007 /*
00008    ObjectArena template - semi-contiguous storage management
00009 */
00010 
00011 #ifndef OBJECTARENA_H
00012 #define OBJECTARENA_H
00013 
00014 #include "ResizeArray.h"
00015 
00016 template <class Type> class ObjectArena {
00017   public:
00018     ObjectArena(void) : blockSize(1024), alignment(1), pos(0), end(0) { };
00019     ~ObjectArena(void) {
00020       int i;
00021       for( i = 0; i < blocks.size(); ++i ) delete [] blocks[i];
00022     }
00023     void setBlockSize(int n) { blockSize = n; }
00024 /*
00025     void setAlignment(int n) { alignment = n; }
00026 
00027     inline Type* getNewArray(int n) {
00028       Type *rpos;
00029       if ( n > (blockSize/2) ) {
00030         rpos = new Type[n+((alignment-1)/sizeof(Type))];
00031         blocks.add(rpos);
00032         while ( ((long)rpos) & (alignment-1) ) ++rpos;
00033       } else {
00034         while ( ((long)pos) & (alignment-1) ) ++pos;
00035         rpos = pos;
00036         if ( ( pos += n ) > end ) {
00037           pos = new Type[blockSize];
00038           blocks.add(pos);
00039           end = pos + blockSize;
00040           while ( ((long)pos) & (alignment-1) ) ++pos;
00041           rpos = pos;
00042           pos += n;
00043         }
00044       }
00045       return rpos;
00046     }
00047 */
00048 
00049     inline Type* getNewArray(int n) {
00050       Type *rpos = pos;
00051       if ( n > (blockSize/2) ) {
00052         rpos = new Type[n];
00053         blocks.add(rpos);
00054       } else if ( ( pos += n ) > end ) {
00055         rpos = pos = new Type[blockSize];
00056         blocks.add(pos);
00057         end = rpos + blockSize;
00058         pos += n;
00059       }
00060       return rpos;
00061     }
00062 
00063   private:
00064     int blockSize;
00065     int alignment;
00066     ResizeArray<Type*> blocks;
00067     Type *pos, *end;
00068 };
00069 
00070 #endif

Generated on Sun Nov 19 01:17:14 2017 for NAMD by  doxygen 1.4.7