ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
Allocator.h
Go to the documentation of this file.
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:
36 };
45 template<class T>
46 class Allocator
47 {
48  int blockSize;
49  int index , remains;
50  std::vector< T* > memory;
51 public:
52  Allocator( void ){ blockSize = index = remains = 0; }
53  ~Allocator( void ){ reset(); }
54 
57  void reset( void )
58  {
59  for(size_t i=0;i<memory.size();i++){delete[] memory[i];}
60  memory.clear();
61  blockSize=index=remains=0;
62  }
64  AllocatorState getState( void ) const
65  {
67  s.index=index;
68  s.remains=remains;
69  return s;
70  }
71 
72 
77  void rollBack(void)
78  {
79  if( memory.size() )
80  {
81  for( size_t i=0 ; i<memory.size() ; i++ )
82  {
83  for( int j=0 ; j<blockSize ; j++ )
84  {
85  memory[i][j].~T();
86  new(&memory[i][j]) T();
87  }
88  }
89  index=0;
90  remains=blockSize;
91  }
92  }
97  void rollBack(const AllocatorState& state){
98  if(state.index<index || (state.index==index && state.remains<remains)){
99  if(state.index<index){
100  for(int j=state.remains;j<blockSize;j++){
101  memory[state.index][j].~T();
102  new(&memory[state.index][j]) T();
103  }
104  for(int i=state.index+1;i<index-1;i++){
105  for(int j=0;j<blockSize;j++){
106  memory[i][j].~T();
107  new(&memory[i][j]) T();
108  }
109  }
110  for(int j=0;j<remains;j++){
111  memory[index][j].~T();
112  new(&memory[index][j]) T();
113  }
114  index=state.index;
115  remains=state.remains;
116  }
117  else{
118  for(int j=0;j<state.remains;j<remains){
119  memory[index][j].~T();
120  new(&memory[index][j]) T();
121  }
122  remains=state.remains;
123  }
124  }
125  }
126 
129  void set( int blockSize)
130  {
131  reset();
132  this->blockSize = blockSize;
133  index=-1;
134  remains=0;
135  }
136 
142  T* newElements( int elements=1 )
143  {
144  T* mem;
145  if( !elements ) return NULL;
146  if( elements>blockSize ) fprintf( stderr , "[ERROR] Allocator: elements bigger than block-size: %d>%d\n" , elements , blockSize ) , exit( 0 );
147  if( remains<elements )
148  {
149  if( index==memory.size()-1 )
150  {
151  mem = new T[blockSize];
152  if( !mem ) fprintf( stderr , "[ERROR] Failed to allocate memory\n" ) , exit(0);
153  memory.push_back( mem );
154  }
155  index++;
156  remains=blockSize;
157  }
158  mem = &(memory[index][blockSize-remains]);
159  remains -= elements;
160  return mem;
161  }
162 };
163 #endif // ALLOCATOR_INCLUDE
#define NULL
void rollBack(const AllocatorState &state)
Definition: Allocator.h:97
void set(int blockSize)
Definition: Allocator.h:129
Allocator(void)
Definition: Allocator.h:52
void rollBack(void)
Definition: Allocator.h:77
AllocatorState getState(void) const
Definition: Allocator.h:64
void reset(void)
Definition: Allocator.h:57
T * newElements(int elements=1)
Definition: Allocator.h:142
~Allocator(void)
Definition: Allocator.h:53