TriviallyCopyable.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2024-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_TRIVIALLYCOPYABLE_HPP
  14. #define ZT_TRIVIALLYCOPYABLE_HPP
  15. #include "Constants.hpp"
  16. #include "Utils.hpp"
  17. #include <cstring>
  18. #include <cstdlib>
  19. namespace ZeroTier {
  20. /**
  21. * Classes inheriting from this base class are safe to abuse in C-like ways: memcpy, memset, etc.
  22. *
  23. * It also includes some static methods to do this conveniently.
  24. */
  25. ZT_PACKED_STRUCT(struct TriviallyCopyable
  26. {
  27. /**
  28. * Be absolutely sure a TriviallyCopyable object is zeroed using Utils::burn()
  29. *
  30. * @tparam T Automatically inferred type of object
  31. * @param obj Any TriviallyCopyable object
  32. */
  33. template<typename T>
  34. static ZT_INLINE void memoryBurn(T *obj) noexcept
  35. {
  36. TriviallyCopyable *const tmp = obj;
  37. Utils::burn(tmp,sizeof(T));
  38. }
  39. /**
  40. * Be absolutely sure a TriviallyCopyable object is zeroed using Utils::burn()
  41. *
  42. * @tparam T Automatically inferred type of object
  43. * @param obj Any TriviallyCopyable object
  44. */
  45. template<typename T>
  46. static ZT_INLINE void memoryBurn(T &obj) noexcept
  47. {
  48. TriviallyCopyable *const tmp = &obj;
  49. Utils::burn(tmp,sizeof(T));
  50. }
  51. /**
  52. * Zero a TriviallyCopyable object
  53. *
  54. * @tparam T Automatically inferred type of object
  55. * @param obj Any TriviallyCopyable object
  56. */
  57. template<typename T>
  58. static ZT_INLINE void memoryZero(T *obj) noexcept
  59. {
  60. TriviallyCopyable *const tmp = obj;
  61. memset(tmp,0,sizeof(T));
  62. }
  63. /**
  64. * Zero a TriviallyCopyable object
  65. *
  66. * @tparam T Automatically inferred type of object
  67. * @param obj Any TriviallyCopyable object
  68. */
  69. template<typename T>
  70. static ZT_INLINE void memoryZero(T &obj) noexcept
  71. {
  72. TriviallyCopyable *const tmp = &obj;
  73. memset(tmp,0,sizeof(T));
  74. }
  75. /**
  76. * Copy any memory over a TriviallyCopyable object
  77. *
  78. * @tparam T Automatically inferred type of destination
  79. * @param dest Any TriviallyCopyable object
  80. * @param src Source memory of same size or less than sizeof(dest)
  81. */
  82. template<typename T>
  83. static ZT_INLINE void memoryCopyUnsafe(T *dest,const void *src) noexcept
  84. {
  85. TriviallyCopyable *const tmp = dest;
  86. memcpy(tmp,src,sizeof(T));
  87. }
  88. /**
  89. * Copy any memory over a TriviallyCopyable object
  90. *
  91. * @tparam T Automatically inferred type of destination
  92. * @param dest Any TriviallyCopyable object
  93. * @param src Source memory of same size or less than sizeof(dest)
  94. */
  95. template<typename T>
  96. static ZT_INLINE void memoryCopyUnsafe(T &dest,const void *src) noexcept
  97. {
  98. TriviallyCopyable *const tmp = &dest;
  99. memcpy(tmp,src,sizeof(T));
  100. }
  101. /**
  102. * Copy a TriviallyCopyable object
  103. *
  104. * @tparam T Automatically inferred type of destination
  105. * @param dest Destination TriviallyCopyable object
  106. * @param src Source TriviallyCopyable object
  107. */
  108. template<typename T>
  109. static ZT_INLINE void memoryCopy(T *dest,const T *src) noexcept
  110. {
  111. TriviallyCopyable *const tmp = dest;
  112. memcpy(tmp,src,sizeof(T));
  113. }
  114. /**
  115. * Copy a TriviallyCopyable object
  116. *
  117. * @tparam T Automatically inferred type of destination
  118. * @param dest Destination TriviallyCopyable object
  119. * @param src Source TriviallyCopyable object
  120. */
  121. template<typename T>
  122. static ZT_INLINE void memoryCopy(T *dest,const T &src) noexcept
  123. {
  124. TriviallyCopyable *const tmp = dest;
  125. memcpy(tmp,&src,sizeof(T));
  126. }
  127. /**
  128. * Copy a TriviallyCopyable object
  129. *
  130. * @tparam T Automatically inferred type of destination
  131. * @param dest Destination TriviallyCopyable object
  132. * @param src Source TriviallyCopyable object
  133. */
  134. template<typename T>
  135. static ZT_INLINE void memoryCopy(T &dest,const T *src) noexcept
  136. {
  137. TriviallyCopyable *const tmp = &dest;
  138. memcpy(tmp,src,sizeof(T));
  139. }
  140. /**
  141. * Copy a TriviallyCopyable object
  142. *
  143. * @tparam T Automatically inferred type of destination
  144. * @param dest Destination TriviallyCopyable object
  145. * @param src Source TriviallyCopyable object
  146. */
  147. template<typename T>
  148. static ZT_INLINE void memoryCopy(T &dest,const T &src) noexcept
  149. {
  150. TriviallyCopyable *const tmp = &dest;
  151. memcpy(tmp,&src,sizeof(T));
  152. }
  153. });
  154. } // namespace ZeroTier
  155. #endif