| 1 |
/* |
| 2 |
Copyright (c) 2011, Michael Kazhdan and Ming Chuang |
| 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 ARRAY_INCLUDED |
| 30 |
#define ARRAY_INCLUDED |
| 31 |
|
| 32 |
#include <vector> |
| 33 |
#include "MyExceptions.h" |
| 34 |
|
| 35 |
#ifdef ARRAY_DEBUG |
| 36 |
#else // !ARRAY_DEBUG |
| 37 |
namespace PoissonRecon |
| 38 |
{ |
| 39 |
#endif // ARRAY_DEBUG |
| 40 |
|
| 41 |
// Code from http://stackoverflow.com |
| 42 |
inline void* aligned_malloc( size_t size , size_t align ) |
| 43 |
{ |
| 44 |
// Align enough for the data, the alignment padding, and room to store a pointer to the actual start of the memory |
| 45 |
void* mem = malloc( size + align + sizeof( void* ) ); |
| 46 |
// The position at which we could potentially start addressing |
| 47 |
char* amem = ( (char*)mem ) + sizeof( void* ); |
| 48 |
// Add align-1 to the start of the address and then zero out at most of the first align-1 bits. |
| 49 |
amem = ( char* )( ( (size_t)( ( (char*)amem ) + (align-1) ) ) & ~( align-1 ) ); |
| 50 |
// Pre-write the actual address |
| 51 |
( ( void** ) amem )[-1] = mem; |
| 52 |
return amem; |
| 53 |
} |
| 54 |
inline void aligned_free( void* mem ) { free( ( ( void** )mem )[-1] ); } |
| 55 |
|
| 56 |
#ifdef ARRAY_DEBUG |
| 57 |
#ifdef SHOW_WARNINGS |
| 58 |
#pragma message ( "[WARNING] Array debugging is enabled" ) |
| 59 |
#endif // SHOW_WARNINGS |
| 60 |
|
| 61 |
#define Pointer( ... ) Array< __VA_ARGS__ > |
| 62 |
#define ConstPointer( ... ) ConstArray< __VA_ARGS__ > |
| 63 |
#define NullPointer( ... ) Array< __VA_ARGS__ >() |
| 64 |
|
| 65 |
#include "Array.inl" |
| 66 |
|
| 67 |
template< class C > void FreePointer( Array< C >& a ){ a.Free( ); } |
| 68 |
template< class C > void AlignedFreePointer( Array< C >& a ){ a.Free( ); } |
| 69 |
template< class C > void VFreePointer( Array< C >& a ){ a.Free( ); } |
| 70 |
template< class C > void DeletePointer( Array< C >& a ){ a.Delete( ); } |
| 71 |
|
| 72 |
template< class C > Array< C > NewPointer( size_t size , const char* name=NULL ){ return Array< C >::New ( size , name ); } |
| 73 |
template< class C > Array< C > AllocPointer( size_t size , const char* name=NULL ){ return Array< C >::Alloc ( size , false , name ); } |
| 74 |
template< class C > Array< C > AlignedAllocPointer( size_t size , size_t alignment , const char* name=NULL ){ return Array< C >::AlignedAlloc( size , alignment , false , name ); } |
| 75 |
template< class C > Array< C > ReAllocPointer( Array< C >& a , size_t size , const char* name=NULL ){ return Array< C >::ReAlloc ( a , size , false , name ); } |
| 76 |
|
| 77 |
template< class C > C* PointerAddress( Array< C > a ) { return a.pointer(); } |
| 78 |
template< class C > const C* PointerAddress( ConstArray< C > a ) { return a.pointer(); } |
| 79 |
template< class C > Array< C > GetPointer( C& c ) { return Array< C >::FromPointer( &c , 1 ); } |
| 80 |
template< class C > ConstArray< C > GetPointer( const C& c ) { return ConstArray< C >::FromPointer( &c , 1 ); } |
| 81 |
template< class C > Array< C > GetPointer( std::vector< C >& v ){ return Array< C >::FromPointer( &v[0] , v.size() ); } |
| 82 |
template< class C > ConstArray< C > GetPointer( const std::vector< C >& v ){ return ConstArray< C >::FromPointer( &v[0] , v.size() ); } |
| 83 |
|
| 84 |
template< class C > Array< C > GetPointer( C* c , size_t sz ) { return Array< C >::FromPointer( c , sz ); } |
| 85 |
template< class C > ConstArray< C > GetPointer( const C* c , size_t sz ) { return ConstArray< C >::FromPointer( c , sz ); } |
| 86 |
template< class C > Array< C > GetPointer( C* c , std::ptrdiff_t start , std::ptrdiff_t end ) { return Array< C >::FromPointer( c , start , end ); } |
| 87 |
template< class C > ConstArray< C > GetPointer( const C* c , std::ptrdiff_t start , std::ptrdiff_t end ) { return ConstArray< C >::FromPointer( c , start , end ); } |
| 88 |
|
| 89 |
#else // !ARRAY_DEBUG |
| 90 |
#define Pointer( ... ) __VA_ARGS__* |
| 91 |
#define ConstPointer( ... ) const __VA_ARGS__* |
| 92 |
#define NullPointer( ... ) NULL |
| 93 |
|
| 94 |
#define FreePointer( ... ) { if( __VA_ARGS__ ) free( __VA_ARGS__ ) , __VA_ARGS__ = NULL; } |
| 95 |
#define AlignedFreePointer( ... ) { if( __VA_ARGS__ ) aligned_free( __VA_ARGS__ ) , __VA_ARGS__ = NULL; } |
| 96 |
#define DeletePointer( ... ) { if( __VA_ARGS__ ) delete[] __VA_ARGS__ , __VA_ARGS__ = NULL; } |
| 97 |
|
| 98 |
template< class C > C* NewPointer( size_t size , const char* name=NULL ){ return new C[size]; } |
| 99 |
template< class C > C* AllocPointer( size_t size , const char* name=NULL ){ return (C*) malloc( sizeof(C) * size ); } |
| 100 |
template< class C > C* AlignedAllocPointer( size_t size , size_t alignment , const char* name=NULL ){ return (C*)aligned_malloc( sizeof(C) * size , alignment ); } |
| 101 |
template< class C > C* ReAllocPointer( C* c , size_t size , const char* name=NULL ){ return (C*) realloc( c , sizeof(C) * size ); } |
| 102 |
|
| 103 |
//template< class C > C* NullPointer( void ){ return NULL; } |
| 104 |
|
| 105 |
template< class C > C* PointerAddress( C* c ){ return c; } |
| 106 |
template< class C > const C* PointerAddress( const C* c ){ return c; } |
| 107 |
template< class C > C* GetPointer( C& c ){ return &c; } |
| 108 |
template< class C > const C* GetPointer( const C& c ){ return &c; } |
| 109 |
template< class C > C* GetPointer( std::vector< C >& v ){ return &v[0]; } |
| 110 |
template< class C > const C* GetPointer( const std::vector< C >& v ){ return &v[0]; } |
| 111 |
|
| 112 |
template< class C > C* GetPointer( C* c , size_t sz ) { return c; } |
| 113 |
template< class C > const C* GetPointer( const C* c , size_t sz ) { return c; } |
| 114 |
template< class C > C* GetPointer( C* c , std::ptrdiff_t start , std::ptrdiff_t end ) { return c; } |
| 115 |
template< class C > const C* GetPointer( const C* c , std::ptrdiff_t start , std::ptrdiff_t end ) { return c; } |
| 116 |
} |
| 117 |
#endif // ARRAY_DEBUG |
| 118 |
#endif // ARRAY_INCLUDED |