]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TPCLib/tracking-ca/MemoryAssignmentHelpers.h
changes from Matthias
[u/mrichter/AliRoot.git] / HLT / TPCLib / tracking-ca / MemoryAssignmentHelpers.h
1 #ifndef MEMORYASSIGNMENTHELPERS_H
2 #define MEMORYASSIGNMENTHELPERS_H
3
4 #include <assert.h>
5
6 template<unsigned int X>
7 static inline void AlignTo( char *&mem )
8 {
9   STATIC_ASSERT( ( X & ( X - 1 ) ) == 0, X_needs_to_be_a_multiple_of_2 );
10   const int offset = reinterpret_cast<unsigned long>( mem ) & ( X - 1 );
11   if ( offset > 0 ) {
12     mem += ( X - offset );
13   }
14   //assert( ( reinterpret_cast<unsigned long>( mem ) & ( X - 1 ) ) == 0 );
15 }
16
17 template<unsigned int X>
18 static inline unsigned int NextMultipleOf( unsigned int value )
19 {
20   STATIC_ASSERT( ( X & ( X - 1 ) ) == 0, X_needs_to_be_a_multiple_of_2 );
21   const int offset = value & ( X - 1 );
22   if ( offset > 0 ) {
23     return value + X - offset;
24   }
25   return value;
26 }
27
28 template<typename T, unsigned int Alignment> static T *AssignMemory( char *&mem, unsigned int size )
29 {
30   STATIC_ASSERT( ( Alignment & ( Alignment - 1 ) ) == 0, Alignment_needs_to_be_a_multiple_of_2 );
31   AlignTo<Alignment> ( mem );
32   T *r = reinterpret_cast<T *>( mem );
33   mem += size * sizeof( T );
34   return r;
35 }
36
37 template<typename T, unsigned int Alignment> static inline T *AssignMemory( char *&mem, unsigned int stride, unsigned int count )
38 {
39   assert( 0 == ( stride & ( Alignment - 1 ) ) );
40   return AssignMemory<T, Alignment>( mem, stride * count );
41 }
42
43 template<typename T, unsigned int Alignment> static T *_assignMemory( char *&mem, unsigned int size )
44 {
45   STATIC_ASSERT( ( Alignment & ( Alignment - 1 ) ) == 0, Alignment_needs_to_be_a_multiple_of_2 );
46   AlignTo<Alignment>( mem );
47   T *r = reinterpret_cast<T *>( mem );
48   mem += size * sizeof( T );
49   return r;
50 }
51
52 template<typename T> static inline void AssignMemory( T *&dst, char *&mem, int count )
53 {
54   dst = _assignMemory < T, ( sizeof( T ) & ( sizeof( T ) - 1 ) ) == 0 && sizeof( T ) <= 16 ? sizeof( T ) : sizeof( void * ) > ( mem, count );
55 }
56
57 #endif // MEMORYASSIGNMENTHELPERS_H