Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Allocator.h
1 /*
2 Copyright (c) 2006, Michael Kazhdan and Matthew Bolitho
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7 
8 Redistributions of source code must retain the above copyright notice, this list of
9 conditions and the following disclaimer. Redistributions in binary form must reproduce
10 the above copyright notice, this list of conditions and the following disclaimer
11 in the documentation and/or other materials provided with the distribution.
12 
13 Neither the name of the Johns Hopkins University nor the names of its contributors
14 may be used to endorse or promote products derived from this software without specific
15 prior written permission.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
18 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
20 SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26 DAMAGE.
27 */
28 
29 #ifndef ALLOCATOR_INCLUDED
30 #define ALLOCATOR_INCLUDED
31 #include <vector>
32 
34 public:
35  int index,remains;
36 };
45 template<class T>
46 class AllocatorT{
47  size_t blockSize;
48  size_t index,remains;
49  std::vector<T*> memory;
50 public:
51  AllocatorT(void){
52  blockSize=index=remains=0;
53  }
54  ~AllocatorT(void){
55  reset();
56  }
57 
60  void reset(void){
61  for(size_t i=0;i<memory.size();i++){delete[] memory[i];}
62  memory.clear();
63  blockSize=index=remains=0;
64  }
66  AllocatorState getState(void) const{
68  s.index=index;
69  s.remains=remains;
70  return s;
71  }
72 
73 
78  void rollBack(void){
79  if( !memory.empty() ){
80  for(size_t i=0;i<memory.size();i++){
81  for(int j=0;j<blockSize;j++){
82  memory[i][j].~T();
83  new(&memory[i][j]) T();
84  }
85  }
86  index=0;
87  remains=blockSize;
88  }
89  }
94  void rollBack(const AllocatorState& state){
95  if(state.index<index || (state.index==index && state.remains<remains)){
96  if(state.index<index){
97  for(int j=state.remains;j<blockSize;j++){
98  memory[state.index][j].~T();
99  new(&memory[state.index][j]) T();
100  }
101  for(int i=state.index+1;i<index-1;i++){
102  for(int j=0;j<blockSize;j++){
103  memory[i][j].~T();
104  new(&memory[i][j]) T();
105  }
106  }
107  for(int j=0;j<remains;j++){
108  memory[index][j].~T();
109  new(&memory[index][j]) T();
110  }
111  index=state.index;
112  remains=state.remains;
113  }
114  else{
115  for(int j=0;j<state.remains;j<remains){
116  memory[index][j].~T();
117  new(&memory[index][j]) T();
118  }
119  remains=state.remains;
120  }
121  }
122  }
123 
126  void set( int blockSize){
127  reset();
128  this->blockSize=blockSize;
129  index=-1;
130  remains=0;
131  }
132 
138  T* newElements( size_t elements=1){
139  T* mem;
140  if(!elements){return NULL;}
141  if(elements>blockSize){
142  std::cerr << "Allocator Error, elements bigger than block-size: " << elements << ">" << "blockSize" << std::endl;
143  return NULL;
144  }
145  if(remains<elements){
146  if(index==memory.size()-1){
147  mem=new T[blockSize];
148  if(!mem){fprintf(stderr,"Failed to allocate memory\n");exit(0);}
149  memory.push_back(mem);
150  }
151  index++;
152  remains=blockSize;
153  }
154  mem=&(memory[index][blockSize-remains]);
155  remains-=elements;
156  return mem;
157  }
158 };
159 #endif // ALLOCATOR_INCLUDE
void set(int blockSize)
Definition: Allocator.h:126
void reset(void)
Definition: Allocator.h:60
void rollBack(void)
Definition: Allocator.h:78
AllocatorState getState(void) const
Definition: Allocator.h:66
void rollBack(const AllocatorState &state)
Definition: Allocator.h:94
T * newElements(size_t elements=1)
Definition: Allocator.h:138