Debian Codegraph @

src/thirdparty/PoissonRecon/Allocator.h

raw
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 #include "Array.h"
33
34 namespace PoissonRecon
35 {
36 struct AllocatorState
37 {
38 size_t index , remains;
39 AllocatorState( void ) : index(0) , remains(0) {}
40 };
41
42 /** This templated class assists in memory allocation and is well suited for instances
43 * when it is known that the sequence of memory allocations is performed in a stack-based
44 * manner, so that memory allocated last is released first. It also preallocates memory
45 * in chunks so that multiple requests for small chunks of memory do not require separate
46 * system calls to the memory manager.
47 * The allocator is templated off of the class of objects that we would like it to allocate,
48 * ensuring that appropriate constructors and destructors are called as necessary.
49 */
50 template< class T >
51 class Allocator
52 {
53 size_t _blockSize;
54 AllocatorState _state;
55 std::vector< Pointer( T ) > _memory;
56 public:
57 Allocator( void ) : _blockSize(0) {}
58 ~Allocator( void ){ reset(); }
59
60 /** This method is the allocators destructor. It frees up any of the memory that
61 * it has allocated. */
62 void reset( void )
63 {
64 for( size_t i=0 ; i<_memory.size() ; i++ ) DeletePointer( _memory[i] );
65 _memory.clear();
66 _blockSize = 0;
67 _state = AllocatorState();
68 }
69 /** This method returns the memory state of the allocator. */
70 AllocatorState getState( void ) const { return _state; }
71
72 /** This method initiallizes the constructor and the blockSize variable specifies the
73 * the number of objects that should be pre-allocated at a time. */
74 void set( size_t blockSize )
75 {
76 reset();
77 _blockSize = blockSize;
78 _state.index = -1;
79 _state.remains = 0;
80 }
81
82 /** This method returns a pointer to an array of elements objects. If there is left over pre-allocated
83 * memory, this method simply returns a pointer to the next free piece of memory, otherwise it pre-allocates
84 * more memory. Note that if the number of objects requested is larger than the value blockSize with which
85 * the allocator was initialized, the request for memory will fail.
86 */
87 Pointer( T ) newElements( size_t elements=1 )
88 {
89 Pointer( T ) mem;
90 if( !elements ) return NullPointer( T );
91 if( elements>_blockSize ) MK_THROW( "elements bigger than block-size: " , elements , " > " , _blockSize );
92 if( _state.remains<elements )
93 {
94 if( _state.index==_memory.size()-1 )
95 {
96 mem = NewPointer< T >( _blockSize );
97 if( !mem ) MK_THROW( "Failed to allocate memory" );
98 _memory.push_back( mem );
99 }
100 _state.index++;
101 _state.remains = _blockSize;
102 }
103 mem = _memory[ _state.index ] + ( _blockSize-_state.remains );
104 _state.remains -= elements;
105 return mem;
106 }
107 };
108 }
109 #endif // ALLOCATOR_INCLUDE