NAMD
ReserveArray.h
Go to the documentation of this file.
1 
7 /*
8  ReserveArray template
9  Uses simple contingous array allocation in a hidden manner
10  so that array object can have items added without limit
11  Suffers from memory fragmentation during resizing
12  Fast access, safe and efficient passing of encapsulated array thru
13  function arguments.
14 */
15 
16 #if 0
17 // No longer in use and probably not the best solution anyway.
18 
19 #ifndef RESERVEARRAY_H
20 #define RESERVEARRAY_H
21 
22 #define RESERVEARRAY(TYPE,NAME,RSIZE,SIZE) \
23  TYPE * NAME; \
24  ReserveArray<TYPE,RSIZE> NAME ## _reserve(SIZE,&NAME);
25 
26 template <class Elem, int reservedSize> class ReserveArray {
27 
28  Elem *allocatedStorage;
29  Elem reservedStorage[reservedSize];
30 
31  public:
32 
33  ReserveArray(int size, Elem **userStorage) {
34  if ( size > reservedSize ) {
35  *userStorage = allocatedStorage = new Elem[size];
36  } else if ( size > 0 ) {
37  allocatedStorage = 0;
38  *userStorage = reservedStorage;
39  } else {
40  allocatedStorage = 0;
41  *userStorage = 0;
42  }
43  }
44 
45  ~ReserveArray() {
46  delete [] allocatedStorage;
47  }
48 
49 };
50 
51 #endif
52 
53 #endif
54