NAMD
ObjectArena.h
Go to the documentation of this file.
1 
7 /*
8  ObjectArena template - semi-contiguous storage management
9 */
10 
11 #ifndef OBJECTARENA_H
12 #define OBJECTARENA_H
13 
14 #include "ResizeArray.h"
15 
16 template <class Type> class ObjectArena {
17  public:
18  ObjectArena(void) : blockSize(1024), alignment(1), pos(0), end(0) { };
19  ~ObjectArena(void) {
20  int i;
21  for( i = 0; i < blocks.size(); ++i ) delete [] blocks[i];
22  }
23  void setBlockSize(int n) { blockSize = n; }
24 /*
25  void setAlignment(int n) { alignment = n; }
26 
27  inline Type* getNewArray(int n) {
28  Type *rpos;
29  if ( n > (blockSize/2) ) {
30  rpos = new Type[n+((alignment-1)/sizeof(Type))];
31  blocks.add(rpos);
32  while ( ((long)rpos) & (alignment-1) ) ++rpos;
33  } else {
34  while ( ((long)pos) & (alignment-1) ) ++pos;
35  rpos = pos;
36  if ( ( pos += n ) > end ) {
37  pos = new Type[blockSize];
38  blocks.add(pos);
39  end = pos + blockSize;
40  while ( ((long)pos) & (alignment-1) ) ++pos;
41  rpos = pos;
42  pos += n;
43  }
44  }
45  return rpos;
46  }
47 */
48 
49  inline Type* getNewArray(int n) {
50  Type *rpos = pos;
51  if ( n > (blockSize/2) ) {
52  rpos = new Type[n];
53  blocks.add(rpos);
54  } else if ( ( pos += n ) > end ) {
55  rpos = pos = new Type[blockSize];
56  blocks.add(pos);
57  end = rpos + blockSize;
58  pos += n;
59  }
60  return rpos;
61  }
62 
63  private:
64  int blockSize;
65  int alignment;
66  ResizeArray<Type*> blocks;
67  Type *pos, *end;
68 };
69 
70 #endif
Type * getNewArray(int n)
Definition: ObjectArena.h:49
ObjectArena(void)
Definition: ObjectArena.h:18
void setBlockSize(int n)
Definition: ObjectArena.h:23
~ObjectArena(void)
Definition: ObjectArena.h:19
int add(const Elem &elem)
Definition: ResizeArray.h:97
int size(void) const
Definition: ResizeArray.h:127