Point Cloud Library (PCL) 1.12.0
allocator.h
1/*
2Copyright (c) 2006, Michael Kazhdan and Matthew Bolitho
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without modification,
6are permitted provided that the following conditions are met:
7
8Redistributions of source code must retain the above copyright notice, this list of
9conditions and the following disclaimer. Redistributions in binary form must reproduce
10the above copyright notice, this list of conditions and the following disclaimer
11in the documentation and/or other materials provided with the distribution.
12
13Neither the name of the Johns Hopkins University nor the names of its contributors
14may be used to endorse or promote products derived from this software without specific
15prior written permission.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
18EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES
19OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
20SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26DAMAGE.
27*/
28
29#ifndef ALLOCATOR_INCLUDED
30#define ALLOCATOR_INCLUDED
31#include <vector>
32
33namespace pcl
34{
35 namespace poisson
36 {
38 public:
40 };
41 /** This templated class assists in memory allocation and is well suited for instances
42 * when it is known that the sequence of memory allocations is performed in a stack-based
43 * manner, so that memory allocated last is released first. It also preallocates memory
44 * in chunks so that multiple requests for small chunks of memory do not require separate
45 * system calls to the memory manager.
46 * The allocator is templated off of the class of objects that we would like it to allocate,
47 * ensuring that appropriate constructors and destructors are called as necessary.
48 */
49 template<class T>
50 class Allocator{
51 int blockSize;
52 int index,remains;
53 std::vector<T*> memory;
54 public:
55 Allocator(void){
56 blockSize=index=remains=0;
57 }
59 reset();
60 }
61
62 /** This method is the allocators destructor. It frees up any of the memory that
63 * it has allocated. */
64 void reset(void){
65 for(std::size_t i=0;i<memory.size();i++){delete[] memory[i];}
66 memory.clear();
67 blockSize=index=remains=0;
68 }
69 /** This method returns the memory state of the allocator. */
72 s.index=index;
73 s.remains=remains;
74 return s;
75 }
76
77
78 /** This method rolls back the allocator so that it makes all of the memory previously
79 * allocated available for re-allocation. Note that it does it not call the constructor
80 * again, so after this method has been called, assumptions about the state of the values
81 * in memory are no longer valid. */
82 void rollBack(void){
83 if(memory.size()){
84 for(std::size_t i=0;i<memory.size();i++){
85 for(int j=0;j<blockSize;j++){
86 memory[i][j].~T();
87 new(&memory[i][j]) T();
88 }
89 }
90 index=0;
91 remains=blockSize;
92 }
93 }
94 /** This method rolls back the allocator to the previous memory state and makes all of the memory previously
95 * allocated available for re-allocation. Note that it does it not call the constructor
96 * again, so after this method has been called, assumptions about the state of the values
97 * in memory are no longer valid. */
98 void rollBack(const AllocatorState& state){
99 if(state.index<index || (state.index==index && state.remains<remains)){
100 if(state.index<index){
101 for(int j=state.remains;j<blockSize;j++){
102 memory[state.index][j].~T();
103 new(&memory[state.index][j]) T();
104 }
105 for(int i=state.index+1;i<index-1;i++){
106 for(int j=0;j<blockSize;j++){
107 memory[i][j].~T();
108 new(&memory[i][j]) T();
109 }
110 }
111 for(int j=0;j<remains;j++){
112 memory[index][j].~T();
113 new(&memory[index][j]) T();
114 }
115 index=state.index;
116 remains=state.remains;
117 }
118 else{
119 for(int j=0;j<state.remains;j<remains){
120 memory[index][j].~T();
121 new(&memory[index][j]) T();
122 }
123 remains=state.remains;
124 }
125 }
126 }
127
128 /** This method initiallizes the constructor and the blockSize variable specifies the
129 * the number of objects that should be pre-allocated at a time. */
130 void set( int blockSize){
131 reset();
132 this->blockSize=blockSize;
133 index=-1;
134 remains=0;
135 }
136
137 /** This method returns a pointer to an array of elements objects. If there is left over pre-allocated
138 * memory, this method simply returns a pointer to the next free piece of memory, otherwise it pre-allocates
139 * more memory. Note that if the number of objects requested is larger than the value blockSize with which
140 * the allocator was initialized, the request for memory will fail.
141 */
142 T* newElements( int elements=1){
143 T* mem;
144 if(!elements){return NULL;}
145 if(elements>blockSize){
146 fprintf(stderr,"Allocator Error, elements bigger than block-size: %d>%d\n",elements,blockSize);
147 return NULL;
148 }
149 if(remains<elements){
150 if(index==memory.size()-1){
151 mem=new T[blockSize];
152 if(!mem){fprintf(stderr,"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
164
165 }
166}
167
168#endif // ALLOCATOR_INCLUDE
This templated class assists in memory allocation and is well suited for instances when it is known t...
Definition: allocator.h:50
AllocatorState getState(void) const
This method returns the memory state of the allocator.
Definition: allocator.h:70
T * newElements(int elements=1)
This method returns a pointer to an array of elements objects.
Definition: allocator.h:142
void set(int blockSize)
This method initiallizes the constructor and the blockSize variable specifies the the number of objec...
Definition: allocator.h:130
void reset(void)
This method is the allocators destructor.
Definition: allocator.h:64
void rollBack(void)
This method rolls back the allocator so that it makes all of the memory previously allocated availabl...
Definition: allocator.h:82
void rollBack(const AllocatorState &state)
This method rolls back the allocator to the previous memory state and makes all of the memory previou...
Definition: allocator.h:98