Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.65 KB | None | 0 0
  1. //MemoryAlignment.h
  2. #ifndef PlaneGC_MemoryAlignment_h
  3. #define PlaneGC_MemoryAlignment_h
  4.  
  5. namespace PlaneGC {
  6.     template<unsigned int X, unsigned int P=0> class RoundUpPowerOf2_CompileTime;
  7.     template<unsigned int X> class RoundUpPowerOf2_CompileTime<X, (sizeof(unsigned int) * 8) - 1> {
  8.         public: static const unsigned int V= 1u << ((sizeof(unsigned int) * 8) - 1);
  9.     };
  10.     template<unsigned int X, unsigned int P> class RoundUpPowerOf2_CompileTime {
  11.         public: static const unsigned int V= X > (1u << P) ? RoundUpPowerOf2_CompileTime<X, P+1>::V : (1u << P);
  12.     };
  13.  
  14.     template<unsigned int X, unsigned int P=31> class RoundDownPowerOf2_CompileTime;
  15.     template<unsigned int X> class RoundDownPowerOf2_CompileTime<X, 1> {
  16.         public: static const unsigned int V= 1;
  17.     };
  18.     template<unsigned int X, unsigned int P> class RoundDownPowerOf2_CompileTime {
  19.         public: static const unsigned int V= X < (1u << P) ? RoundDownPowerOf2_CompileTime<X, P-1>::V : (1u << P);
  20.     };
  21.  
  22.     #ifndef MEMORY_ALIGNMENT
  23.         //This union helps determine the "worst case" for memory alignment.
  24.         //Most systems will align memory to 4 or 8 bytes, this struct will probably be bigger than that
  25.         union GuessMemoryAlignment_u {
  26.             //long long l;
  27.             double d;
  28.             void* p;
  29.             void(*fptr)();
  30.         };
  31.  
  32.         const size_t MemoryAlignment= RoundUpPowerOf2_CompileTime<sizeof(GuessMemoryAlignment_u)>::V;
  33.     #else //#ifndef MEMORY_ALIGNMENT
  34.         const size_t MemoryAlignment= (size_t)MEMORY_ALIGNMENT;
  35.     #endif //#ifndef MEMORY_ALIGNMENT
  36.  
  37.  
  38.     template<unsigned int I, int M> struct IntegerCeiling_CompileTime {
  39.         static const unsigned int value= (((I - 1) / M) + 1) * M;
  40.     };
  41.  
  42.     template<size_t bytes> class ObjectPadding {
  43.         static const size_t ___padding_size= bytes;
  44.         char ___padding[bytes];
  45.     };
  46.     template<> class ObjectPadding<0> {
  47.         static const size_t ___padding_size= 0;
  48.     };
  49.  
  50.     template<size_t bytes, size_t ceiling= MemoryAlignment> struct GetPaddedSize {
  51.         static const size_t value= IntegerCeiling_CompileTime<bytes, ceiling>::value;
  52.     };
  53.  
  54.     template<size_t bytes, size_t ceiling= MemoryAlignment> struct GetPaddingNeeded {
  55.         static const size_t value= IntegerCeiling_CompileTime<bytes, ceiling>::value - bytes;
  56.     };
  57.  
  58.     inline size_t GetPaddedSize_f(size_t bytes, size_t ceiling= MemoryAlignment) {
  59.         return ((bytes - 1) | (ceiling - 1)) + 1;
  60.     };
  61.  
  62.     inline size_t GetPaddingNeeded_f(size_t bytes, size_t ceiling= MemoryAlignment) {
  63.         --bytes;
  64.         return (bytes | (ceiling - 1)) - bytes;
  65.     };
  66.  
  67.     template<typename T, size_t ceiling= MemoryAlignment> struct PaddedObject:
  68.       public T,
  69.         ObjectPadding<GetPaddingNeeded<sizeof(T), ceiling>::value>
  70.     {
  71.  
  72.     };
  73.  
  74. } //end namespace
  75.  
  76. #endif #ifndef PlaneGC_MemoryAlignment_h
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement