memory.h 753 B

1234567891011121314151617181920212223242526272829303132333435
  1. // Memory-related utilities
  2. //
  3. // Platform: ISO C++ 98/11
  4. // $Id$
  5. //
  6. // (c) __vic 2019
  7. #ifndef __VIC_MEMORY_H
  8. #define __VIC_MEMORY_H
  9. #include<__vic/defs.h>
  10. namespace __vic {
  11. //----------------------------------------------------------------------------
  12. template<class T>
  13. inline T load_unaligned(const void *p)
  14. {
  15. #ifdef __VIC_STRICT_RAM_ALIGNMENT__
  16. #ifdef __GNUC__
  17. struct wrapper { T v; } __attribute__((packed));
  18. return static_cast<const wrapper *>(p)->v;
  19. #else
  20. # error Add unaligned access implementation for your compiler/platform
  21. #endif
  22. #else // unaligned access is OK
  23. return *static_cast<const T *>(p);
  24. #endif
  25. }
  26. //----------------------------------------------------------------------------
  27. } // namespace
  28. #endif // header guard