shm_manager.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #pragma once
  2. #include "shm.h"
  3. #include "page_heap.h"
  4. #include "size_map.h"
  5. namespace bg
  6. {
  7. namespace detail
  8. {
  9. struct ChunkCache
  10. {
  11. struct ClassCache
  12. {
  13. Span free_list;
  14. size_t span_count{};
  15. };
  16. ClassCache m_caches[87]{};
  17. };
  18. class ShmManager
  19. {
  20. public:
  21. struct ShmBlock
  22. {
  23. void* addr{};
  24. size_t used_size{};
  25. size_t real_size{};
  26. size_t mmap_size{};
  27. };
  28. ShmManager(const bg::ShmOptions&);
  29. ~ShmManager();
  30. void* AllocateRawMemory(size_t* bytes, size_t alignment);
  31. bool HasSingleton(const bg::detail::TypeName& type);
  32. void* GetSingleton(const bg::detail::TypeName& type, size_t bytes, bool* first_call);
  33. void FreeSingleton(const bg::detail::TypeName& type);
  34. bool OnResume(const char* identifier);
  35. bool OnCreate(void);
  36. bool DeleteBlock(size_t index);
  37. void* AllocateInBlock(size_t index, size_t bytes, size_t alignment);
  38. bool CreateBlock(size_t index, size_t min_size);
  39. bool AttachBlock(size_t index);
  40. bool ResizeBlock(size_t index, const char* new_size);
  41. using SingletonMap = std::map<TypeName, void*, std::equal_to<TypeName>, ShmAllocator<std::pair<const TypeName, void*>>>;
  42. ChunkCache m_chunk_cache;
  43. SizeMap m_size_map;
  44. char m_magic[64]{};
  45. PageHeap m_page_heap;
  46. uint64_t m_version = 0;
  47. size_t m_block_count = 0;
  48. SingletonMap* m_singletons{};
  49. ShmOptions m_options;
  50. ShmBlock m_blocks[256];
  51. };
  52. }
  53. }