12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- // Memory-related utilities
- //
- // Platform: ISO C++ 98/11
- // $Id$
- //
- // (c) __vic 2019
- #ifndef __VIC_MEMORY_H
- #define __VIC_MEMORY_H
- #include<__vic/defs.h>
- #if defined(__VIC_STRICT_RAM_ALIGNMENT__) && !defined(__GNUC__)
- #include<cstring>
- #endif
- namespace __vic {
- //----------------------------------------------------------------------------
- template<class T>
- inline T load_unaligned(const void *p)
- {
- #ifdef __VIC_STRICT_RAM_ALIGNMENT__
- #ifdef __GNUC__
- struct wrapper { T v; } __attribute__((packed));
- return static_cast<const wrapper *>(p)->v;
- #else
- T v;
- std::memcpy(&v, p, sizeof v);
- return v;
- #endif
- #else // unaligned access is OK
- return *static_cast<const T *>(p);
- #endif
- }
- //----------------------------------------------------------------------------
- template<class T>
- inline void store_unaligned(void *p, T v)
- {
- #ifdef __VIC_STRICT_RAM_ALIGNMENT__
- #ifdef __GNUC__
- struct wrapper { T v; } __attribute__((packed));
- static_cast<wrapper *>(p)->v = v;
- #else
- std::memcpy(p, &v, sizeof v);
- #endif
- #else // unaligned access is OK
- *static_cast<T *>(p) = v;
- #endif
- }
- //----------------------------------------------------------------------------
- } // namespace
- #endif // header guard
|