runtime_composer.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #pragma once
  8. template <typename Base>
  9. class RuntimeComposer;
  10. class RuntimeComposerBase;
  11. typedef void(*RuntimeComponentConstruct)(void *location, RuntimeComposerBase *composer);
  12. typedef void(*RuntimeComponentDestruct)(void *location);
  13. typedef void(*RuntimeComponentMove)(void *location, void *waslocation);
  14. struct RuntimeComponentWrapStruct {
  15. // Don't init any fields, because it is only created in
  16. // global scope, so it will be filled by zeros from the start.
  17. RuntimeComponentWrapStruct() = default;
  18. RuntimeComponentWrapStruct(std::size_t size, std::size_t align, RuntimeComponentConstruct construct, RuntimeComponentDestruct destruct, RuntimeComponentMove move)
  19. : Size(size)
  20. , Align(align)
  21. , Construct(construct)
  22. , Destruct(destruct)
  23. , Move(move) {
  24. }
  25. std::size_t Size;
  26. std::size_t Align;
  27. RuntimeComponentConstruct Construct;
  28. RuntimeComponentDestruct Destruct;
  29. RuntimeComponentMove Move;
  30. };
  31. template <int Value, int Denominator>
  32. struct CeilDivideMinimumOne {
  33. static constexpr int Result = ((Value / Denominator) + ((!Value || (Value % Denominator)) ? 1 : 0));
  34. };
  35. extern RuntimeComponentWrapStruct RuntimeComponentWraps[64];
  36. extern QAtomicInt RuntimeComponentIndexLast;
  37. template <typename Type, typename Base>
  38. struct RuntimeComponent {
  39. using RuntimeComponentBase = Base;
  40. RuntimeComponent() {
  41. // While there is no std::aligned_alloc().
  42. static_assert(alignof(Type) <= alignof(std::max_align_t), "Components should align to std::max_align_t!");
  43. }
  44. RuntimeComponent(const RuntimeComponent &other) = delete;
  45. RuntimeComponent &operator=(const RuntimeComponent &other) = delete;
  46. RuntimeComponent(RuntimeComponent &&other) = delete;
  47. RuntimeComponent &operator=(RuntimeComponent &&other) = default;
  48. static int Index() {
  49. static QAtomicInt MyIndex(0);
  50. if (auto index = MyIndex.loadAcquire()) {
  51. return index - 1;
  52. }
  53. while (true) {
  54. auto last = RuntimeComponentIndexLast.loadAcquire();
  55. if (RuntimeComponentIndexLast.testAndSetOrdered(last, last + 1)) {
  56. Assert(last < 64);
  57. if (MyIndex.testAndSetOrdered(0, last + 1)) {
  58. RuntimeComponentWraps[last] = RuntimeComponentWrapStruct(
  59. sizeof(Type),
  60. alignof(Type),
  61. Type::RuntimeComponentConstruct,
  62. Type::RuntimeComponentDestruct,
  63. Type::RuntimeComponentMove);
  64. }
  65. break;
  66. }
  67. }
  68. return MyIndex.loadAcquire() - 1;
  69. }
  70. static uint64 Bit() {
  71. return (1ULL << Index());
  72. }
  73. protected:
  74. static void RuntimeComponentConstruct(void *location, RuntimeComposerBase *composer) {
  75. new (location) Type();
  76. }
  77. static void RuntimeComponentDestruct(void *location) {
  78. ((Type*)location)->~Type();
  79. }
  80. static void RuntimeComponentMove(void *location, void *waslocation) {
  81. *(Type*)location = std::move(*(Type*)waslocation);
  82. }
  83. };
  84. class RuntimeComposerMetadata {
  85. public:
  86. RuntimeComposerMetadata(uint64 mask) : _mask(mask) {
  87. for (int i = 0; i != 64; ++i) {
  88. auto componentBit = (1ULL << i);
  89. if (_mask & componentBit) {
  90. auto componentSize = RuntimeComponentWraps[i].Size;
  91. if (componentSize) {
  92. auto componentAlign = RuntimeComponentWraps[i].Align;
  93. if (auto badAlign = (size % componentAlign)) {
  94. size += (componentAlign - badAlign);
  95. }
  96. offsets[i] = size;
  97. size += componentSize;
  98. accumulate_max(align, componentAlign);
  99. }
  100. } else if (_mask < componentBit) {
  101. last = i;
  102. break;
  103. }
  104. }
  105. }
  106. // Meta pointer in the start.
  107. std::size_t size = sizeof(const RuntimeComposerMetadata*);
  108. std::size_t align = alignof(const RuntimeComposerMetadata*);
  109. std::size_t offsets[64] = { 0 };
  110. int last = 64;
  111. bool equals(uint64 mask) const {
  112. return _mask == mask;
  113. }
  114. uint64 maskadd(uint64 mask) const {
  115. return _mask | mask;
  116. }
  117. uint64 maskremove(uint64 mask) const {
  118. return _mask & (~mask);
  119. }
  120. private:
  121. uint64 _mask;
  122. };
  123. const RuntimeComposerMetadata *GetRuntimeComposerMetadata(uint64 mask);
  124. class RuntimeComposerBase {
  125. public:
  126. RuntimeComposerBase(uint64 mask = 0) : _data(zerodata()) {
  127. if (mask) {
  128. auto meta = GetRuntimeComposerMetadata(mask);
  129. auto data = operator new(meta->size);
  130. Assert(data != nullptr);
  131. _data = data;
  132. _meta() = meta;
  133. for (int i = 0; i < meta->last; ++i) {
  134. auto offset = meta->offsets[i];
  135. if (offset >= sizeof(_meta())) {
  136. try {
  137. auto constructAt = _dataptrunsafe(offset);
  138. auto space = RuntimeComponentWraps[i].Size;
  139. auto alignedAt = constructAt;
  140. std::align(RuntimeComponentWraps[i].Align, space, alignedAt, space);
  141. Assert(alignedAt == constructAt);
  142. RuntimeComponentWraps[i].Construct(constructAt, this);
  143. } catch (...) {
  144. while (i > 0) {
  145. --i;
  146. offset = meta->offsets[--i];
  147. if (offset >= sizeof(_meta())) {
  148. RuntimeComponentWraps[i].Destruct(_dataptrunsafe(offset));
  149. }
  150. }
  151. throw;
  152. }
  153. }
  154. }
  155. }
  156. }
  157. RuntimeComposerBase(const RuntimeComposerBase &other) = delete;
  158. RuntimeComposerBase &operator=(const RuntimeComposerBase &other) = delete;
  159. ~RuntimeComposerBase() {
  160. if (_data != zerodata()) {
  161. auto meta = _meta();
  162. for (int i = 0; i < meta->last; ++i) {
  163. auto offset = meta->offsets[i];
  164. if (offset >= sizeof(_meta())) {
  165. RuntimeComponentWraps[i].Destruct(_dataptrunsafe(offset));
  166. }
  167. }
  168. operator delete(_data);
  169. }
  170. }
  171. protected:
  172. bool UpdateComponents(uint64 mask = 0) {
  173. if (_meta()->equals(mask)) {
  174. return false;
  175. }
  176. RuntimeComposerBase result(mask);
  177. result.swap(*this);
  178. if (_data != zerodata() && result._data != zerodata()) {
  179. const auto meta = _meta();
  180. const auto wasmeta = result._meta();
  181. for (auto i = 0; i != meta->last; ++i) {
  182. const auto offset = meta->offsets[i];
  183. const auto wasoffset = wasmeta->offsets[i];
  184. if (offset >= sizeof(_meta())
  185. && wasoffset >= sizeof(_meta())) {
  186. RuntimeComponentWraps[i].Move(
  187. _dataptrunsafe(offset),
  188. result._dataptrunsafe(wasoffset));
  189. }
  190. }
  191. }
  192. return true;
  193. }
  194. bool AddComponents(uint64 mask = 0) {
  195. return UpdateComponents(_meta()->maskadd(mask));
  196. }
  197. bool RemoveComponents(uint64 mask = 0) {
  198. return UpdateComponents(_meta()->maskremove(mask));
  199. }
  200. private:
  201. template <typename Base>
  202. friend class RuntimeComposer;
  203. static const RuntimeComposerMetadata *ZeroRuntimeComposerMetadata;
  204. static void *zerodata() {
  205. return &ZeroRuntimeComposerMetadata;
  206. }
  207. void *_dataptrunsafe(int skip) const {
  208. return (char*)_data + skip;
  209. }
  210. void *_dataptr(int skip) const {
  211. return (skip >= sizeof(_meta())) ? _dataptrunsafe(skip) : nullptr;
  212. }
  213. const RuntimeComposerMetadata *&_meta() const {
  214. return *static_cast<const RuntimeComposerMetadata**>(_data);
  215. }
  216. void *_data = nullptr;
  217. void swap(RuntimeComposerBase &other) {
  218. std::swap(_data, other._data);
  219. }
  220. };
  221. template <typename Base>
  222. class RuntimeComposer : public RuntimeComposerBase {
  223. public:
  224. using RuntimeComposerBase::RuntimeComposerBase;
  225. template <
  226. typename Type,
  227. typename = std::enable_if_t<std::is_same_v<
  228. typename Type::RuntimeComponentBase,
  229. Base>>>
  230. bool Has() const {
  231. return (_meta()->offsets[Type::Index()] >= sizeof(_meta()));
  232. }
  233. template <
  234. typename Type,
  235. typename = std::enable_if_t<std::is_same_v<
  236. typename Type::RuntimeComponentBase,
  237. Base>>>
  238. Type *Get() {
  239. return static_cast<Type*>(_dataptr(_meta()->offsets[Type::Index()]));
  240. }
  241. template <
  242. typename Type,
  243. typename = std::enable_if_t<std::is_same_v<
  244. typename Type::RuntimeComponentBase,
  245. Base>>>
  246. const Type *Get() const {
  247. return static_cast<const Type*>(_dataptr(_meta()->offsets[Type::Index()]));
  248. }
  249. };