Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Array.h
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 
34 #define ARRAY_DEBUG 0
35 #ifdef _WIN64
36 #define ASSERT( x ) { if( !( x ) ) __debugbreak(); }
37 #else // !_WIN64
38 #ifdef _WIN32
39 #define ASSERT( x ) { if( !( x ) ) _asm{ int 0x03 } }
40 #else // !_WIN32
41 #define ASSERT( x ) { if( !( x ) ) exit(0); }
42 #endif // _WIN32
43 #endif // _WIN64
44 
45 // Code from http://stackoverflow.com
46 void* aligned_malloc( size_t size , size_t align )
47 {
48  // Align enough for the data, the alignment padding, and room to store a pointer to the actual start of the memory
49  void* mem = malloc( size + align + sizeof( void* ) );
50  // The position at which we could potentially start addressing
51  char* amem = ( (char*)mem ) + sizeof( void* );
52  // Add align-1 to the start of the address and then zero out at most of the first align-1 bits.
53  amem = ( char* )( ( (size_t)( ( (char*)amem ) + (align-1) ) ) & ~( align-1 ) );
54  // Pre-write the actual address
55  ( ( void** ) amem )[-1] = mem;
56  return amem;
57 }
58 void aligned_free( void* mem ) { free( ( ( void** )mem )[-1] ); }
59 
60 #if ARRAY_DEBUG
61 #pragma message ( "[WARNING] Array debugging is enabled" )
62 #include "Array.inl"
63 #define Pointer( ... ) Array< __VA_ARGS__ >
64 #define ConstPointer( ... ) ConstArray< __VA_ARGS__ >
65 template< class C > void FreePointer( Array< C >& a ){ a.Free( ); }
66 template< class C > void AlignedFreePointer( Array< C >& a ){ a.Free( ); }
67 template< class C > void VFreePointer( Array< C >& a ){ a.Free( ); }
68 template< class C > void DeletePointer( Array< C >& a ){ a.Delete( ); }
69 
70 template< class C > Array< C > NewPointer( size_t size , const char* name=NULL ){ return Array< C >::New ( size , name ); }
71 template< class C > Array< C > AllocPointer( size_t size , const char* name=NULL ){ return Array< C >::Alloc ( size , false , name ); }
72 template< class C > Array< C > AlignedAllocPointer( size_t size , size_t alignment , const char* name=NULL ){ return Array< C >::AlignedAlloc( size , alignment , false , name ); }
73 template< class C > Array< C > ReAllocPointer( Array< C >& a , size_t size , const char* name=NULL ){ return Array< C >::ReAlloc ( a , size , false , name ); }
74 
75 template< class C > Array< C > NullPointer( void ){ return Array< C >( ); }
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 #else // !ARRAY_DEBUG
85 #define Pointer( ... ) __VA_ARGS__*
86 #define ConstPointer( ... ) const __VA_ARGS__*
87 
88 #define FreePointer( ... ) { if( __VA_ARGS__ ) free( __VA_ARGS__ ) , __VA_ARGS__ = NULL; }
89 #define AlignedFreePointer( ... ) { if( __VA_ARGS__ ) aligned_free( __VA_ARGS__ ) , __VA_ARGS__ = NULL; }
90 #define DeletePointer( ... ) { if( __VA_ARGS__ ) delete[] __VA_ARGS__ , __VA_ARGS__ = NULL; }
91 
92 template< class C > C* NewPointer( size_t size , const char* name=NULL ){ return new C[size]; }
93 template< class C > C* AllocPointer( size_t size , const char* name=NULL ){ return static_cast<C*> (malloc( sizeof(C) * size )); }
94 template< class C > C* AlignedAllocPointer( size_t size , size_t alignment , const char* name=NULL ){ return static_cast<C*>(aligned_malloc( sizeof(C) * size , alignment )); }
95 template< class C > C* ReAllocPointer( C* c , size_t size , const char* name=NULL ){ return static_cast<C*> (realloc( c , sizeof(C) * size )); }
96 
97 template< class C > C* NullPointer( void ){ return NULL; }
98 
99 template< class C > C* PointerAddress( C* c ){ return c; }
100 template< class C > const C* PointerAddress( const C* c ){ return c; }
101 template< class C > C* GetPointer( C& c ){ return &c; }
102 template< class C > const C* GetPointer( const C& c ){ return &c; }
103 template< class C > C* GetPointer( std::vector< C >& v ){ return &v[0]; }
104 template< class C > const C* GetPointer( const std::vector< C >& v ){ return &v[0]; }
105 #endif // ARRAY_DEBUG
106 #endif // ARRAY_INCLUDED
Definition: AlignT.cc:45
Definition: Array.inl:67