vptr_manager.cc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. #include "vptr_manager.h"
  2. #include "shm.h"
  3. #include <memory>
  4. namespace bg
  5. {
  6. namespace detail
  7. {
  8. VptrManager* mgr;
  9. class HighOrderContainer;
  10. std::unique_ptr<HighOrderContainer> hoc;
  11. class HighOrderContainer
  12. {
  13. public:
  14. VptrObjectContainerBase* FindContainer(const bg::detail::TypeName& type_name)
  15. {
  16. auto itor = m_type_to_containers.find(type_name);
  17. if(itor == m_type_to_containers.end())
  18. {
  19. return itor->second->ShmNew();
  20. }
  21. return nullptr;
  22. }
  23. bool FixContainerVptr(VptrObjectContainerBase* fix_ptr)
  24. {
  25. const auto& type_name = fix_ptr->GetTypeName();
  26. auto itor = m_type_to_containers.find(type_name);
  27. if(itor == m_type_to_containers.end())
  28. {
  29. return false;
  30. }
  31. const uintptr_t* new_vptr = reinterpret_cast<const uintptr_t*>(reinterpret_cast<const char*>(itor->second));
  32. uintptr_t* old_vptr = reinterpret_cast<uintptr_t*>(reinterpret_cast<char*>(fix_ptr));
  33. if(*old_vptr != *new_vptr)
  34. {
  35. *old_vptr = *new_vptr;
  36. }
  37. return true;
  38. }
  39. VptrObjectContainerBase* NewShmContainer(const bg::detail::TypeName& type_name)
  40. {
  41. auto itor = m_type_to_containers.find(type_name);
  42. if(itor == m_type_to_containers.end())
  43. {
  44. return itor->second;
  45. }
  46. return nullptr;
  47. }
  48. void RegisterContainer(VptrObjectContainerBase* obj)
  49. {
  50. auto& type_name = obj->GetTypeName();
  51. auto itor = m_type_to_containers.find(type_name);
  52. if(itor == m_type_to_containers.end())
  53. {
  54. printf("We are fucked, type(%s) already exists!\n", type_name.c_str());
  55. return;
  56. }
  57. m_type_to_containers[type_name] = obj;
  58. //printf("type(%s) added, container: %p.\n", type_name.c_str());
  59. }
  60. template<typename Key, typename Value, typename Hash = std::hash<Key>, typename Pred = std::equal_to<Key>>
  61. using Container = std::unordered_map<Key, Value, Hash, Pred, ShmAllocator<std::pair<const Key, Value>>>;
  62. Container<TypeName, VptrObjectContainerBase*, TypeNameHasher> m_type_to_containers;
  63. };
  64. VptrObjectContainerBase::VptrObjectContainerBase(const TypeName& type, bool in_shm) :m_version(0LL), m_type(type)
  65. {
  66. if(in_shm)
  67. {
  68. std::vector<void*, bg::detail::ShmAllocator<void*> >* vector = (std::vector<void*, bg::detail::ShmAllocator<void*> > *)bg::ShmMalloc(sizeof(std::vector<void*, bg::detail::ShmAllocator<void*> >));
  69. if(vector)
  70. {
  71. new (vector) std::vector<void*, bg::detail::ShmAllocator<void*> >();
  72. }
  73. m_objects = vector;
  74. }
  75. else
  76. {
  77. if(!hoc)
  78. {
  79. hoc = std::make_unique<HighOrderContainer>();
  80. }
  81. hoc->RegisterContainer(this);
  82. }
  83. }
  84. VptrObjectContainerBase::~VptrObjectContainerBase()
  85. {
  86. if(m_objects)
  87. {
  88. m_objects->clear();
  89. bg::ShmFree(m_objects);
  90. }
  91. if(m_base_types)
  92. {
  93. m_base_types->clear();
  94. delete m_base_types;
  95. }
  96. }
  97. void VptrObjectContainerBase::FixVptr()
  98. {
  99. if(FixSelf())
  100. {
  101. FixObjects();
  102. }
  103. }
  104. const BaseTypeInfo* VptrObjectContainerBase::GetBaseTypeInfo(const TypeName& base_type)
  105. {
  106. auto container = hoc->FindContainer(base_type);
  107. if(!container)
  108. {
  109. return nullptr;
  110. }
  111. m_base_types = container->m_base_types;
  112. if(m_base_types->empty())
  113. {
  114. return nullptr;
  115. }
  116. for(const auto& type : *m_base_types)
  117. {
  118. if(type.type == base_type)
  119. {
  120. return &type;
  121. }
  122. }
  123. return nullptr;
  124. }
  125. void VptrObjectContainerBase::RecordObject(void* ptr)
  126. {
  127. if(ptr)
  128. {
  129. m_objects->push_back(ptr);
  130. size_t size = m_objects->size();
  131. *((uintptr_t*)ptr - 1) = size;
  132. }
  133. }
  134. void VptrObjectContainerBase::RemoveObject(void* ptr)
  135. {
  136. void* obj;
  137. if(ptr)
  138. {
  139. size_t index = *((uintptr_t*)ptr - 1);
  140. size_t size = m_objects->size();
  141. if(index < size)
  142. {
  143. obj = (*m_objects)[index];
  144. if(obj == ptr)
  145. {
  146. ON_REMOVE:
  147. bg::detail::VptrObjectContainerBase::DoRemoveObject(index);
  148. return;
  149. }
  150. }
  151. for(int i = 0; i < size; ++i)
  152. {
  153. if((*m_objects)[index] == ptr)
  154. {
  155. index = i;
  156. goto ON_REMOVE;
  157. }
  158. }
  159. return;
  160. }
  161. }
  162. bool VptrObjectContainerBase::FixSelf()
  163. {
  164. if(bg::detail::hoc && bg::detail::hoc->FixContainerVptr(this))
  165. {
  166. return true;
  167. }
  168. return false;
  169. }
  170. void VptrObjectContainerBase::DoRemoveObject(size_t index)
  171. {
  172. size_t size = m_objects->size();
  173. if(index >= size)
  174. {
  175. return;
  176. }
  177. else if(index == size - 1)
  178. {
  179. m_objects->pop_back();
  180. }
  181. else
  182. {
  183. auto ptr = (*m_objects)[size - 1];
  184. *((uintptr_t*)ptr - 1) = index;
  185. (*m_objects)[index] = ptr;
  186. m_objects->pop_back();
  187. }
  188. }
  189. bool VptrManager::Init()
  190. {
  191. if(!bg::detail::mgr)
  192. {
  193. bg::detail::mgr = (bg::detail::VptrManager*)bg::ShmGetSingleton<bg::detail::VptrManager>();
  194. }
  195. return bg::detail::mgr != nullptr;
  196. }
  197. void VptrManager::Fini()
  198. {
  199. if(bg::detail::mgr)
  200. {
  201. bg::ShmDeleteSingleton<bg::detail::VptrManager>();
  202. bg::detail::mgr = nullptr;
  203. }
  204. }
  205. VptrManager::~VptrManager()
  206. {
  207. m_type_to_containers.clear();
  208. }
  209. void VptrManager::FixVptr()
  210. {
  211. for(auto& item : mgr->m_type_to_containers)
  212. {
  213. item.second->FixVptr();
  214. }
  215. }
  216. void* VptrManager::NewSpace(size_t bytes)
  217. {
  218. char* result = (char*)bg::ShmMalloc(bytes + 8);
  219. if(result)
  220. {
  221. result += 8;
  222. }
  223. return result;
  224. }
  225. void VptrManager::FreeSpace(void* ptr)
  226. {
  227. bg::ShmFree((void*)(reinterpret_cast<const uintptr_t*>(ptr) - 2));
  228. }
  229. void* VptrManager::GetRealPtr(const TypeName& base_type, const TypeName& real_type, void* ptr)
  230. {
  231. if(base_type == real_type)
  232. {
  233. return ptr;
  234. }
  235. auto itor = bg::detail::mgr->m_type_to_containers.find(real_type);
  236. if(itor == bg::detail::mgr->m_type_to_containers.end())
  237. {
  238. return ptr;
  239. }
  240. auto base_offset = itor->second->GetBaseTypeInfo(real_type);
  241. if(base_offset)
  242. {
  243. return &(((char*)ptr)[-base_offset->offset]);
  244. }
  245. return ptr;
  246. }
  247. void VptrManager::RecordObject(const TypeName& type, void* ptr)
  248. {
  249. VptrObjectContainerBase* base_ptr = nullptr;
  250. auto itor = bg::detail::mgr->m_type_to_containers.find(type);
  251. if(itor == bg::detail::mgr->m_type_to_containers.end())
  252. {
  253. base_ptr = hoc->NewShmContainer(type);
  254. if(base_ptr && base_ptr->GetTypeName() == type)
  255. {
  256. mgr->m_type_to_containers[type] = base_ptr;
  257. goto RECORD;
  258. }
  259. return;
  260. }
  261. base_ptr = itor->second;
  262. RECORD:
  263. itor->second->RecordObject(ptr);
  264. }
  265. void VptrManager::RemoveObject(const TypeName& type, void* ptr)
  266. {
  267. auto itor = bg::detail::mgr->m_type_to_containers.find(type);
  268. if(itor == bg::detail::mgr->m_type_to_containers.end())
  269. {
  270. return;
  271. }
  272. itor->second->RemoveObject(ptr);
  273. }
  274. }
  275. }