test_manage.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #include "catch.hpp"
  2. #include "shm_manager.h"
  3. #include "shm.h"
  4. #include "shm_object.h"
  5. #if defined(__GNUC__)
  6. #include <sys/mman.h>
  7. #endif
  8. extern bg::detail::ShmContext g_shm_ctx;
  9. TEST_CASE("ShmManager, AllocateRawMemory")
  10. {
  11. bg::ShmOptions opts(false, "Test", nullptr);
  12. opts.fix_vptr_on_init = false;
  13. REQUIRE(bg::ShmInit(opts) == true);
  14. REQUIRE(g_shm_ctx.mgr);
  15. for(size_t i = 8; i < 0x100000000LL; i = i << 8)
  16. {
  17. size_t real_bytes = i - 1;
  18. void* memory = g_shm_ctx.mgr->AllocateRawMemory(&real_bytes, i);
  19. REQUIRE(memory);
  20. memset(memory, 0, real_bytes);
  21. REQUIRE(real_bytes == i);
  22. }
  23. bg::ShmFini();
  24. }
  25. struct TestSigleton
  26. {
  27. TestSigleton(int a, char b, void* c)
  28. {
  29. this->a = a;
  30. this->b = b;
  31. this->c = c;
  32. }
  33. int a{};
  34. char b{};
  35. void* c{};
  36. };
  37. TEST_CASE("ShmManager, Singleton")
  38. {
  39. bg::ShmOptions opts(false, "Test", nullptr);
  40. opts.fix_vptr_on_init = false;
  41. REQUIRE(bg::ShmInit(opts) == true);
  42. REQUIRE(g_shm_ctx.mgr);
  43. bg::detail::TypeName type(typeid(TestSigleton).name(), sizeof(TestSigleton));
  44. REQUIRE(g_shm_ctx.mgr->HasSingleton(type) == false);
  45. int a = 3;
  46. REQUIRE(bg::ShmGetSingleton<TestSigleton>(1, 2, &a));
  47. REQUIRE(g_shm_ctx.mgr->HasSingleton(type) == true);
  48. TestSigleton* temp = bg::ShmGetSingleton<TestSigleton>(1, 1, &a);
  49. REQUIRE(temp->a == 1);
  50. REQUIRE(temp->b == 2);
  51. REQUIRE(temp->c == &a);
  52. bg::ShmDeleteSingleton<TestSigleton>();
  53. REQUIRE(g_shm_ctx.mgr->HasSingleton(type) == false);
  54. bg::ShmFini();
  55. }
  56. class TestBase
  57. {
  58. public:
  59. virtual int Do1()
  60. {
  61. return a;
  62. }
  63. virtual ~TestBase() = default;
  64. int a = 2;
  65. };
  66. class TestBase2
  67. {
  68. public:
  69. virtual int Do1()
  70. {
  71. return b;
  72. }
  73. virtual ~TestBase2() = default;
  74. int b = 3;
  75. };
  76. class TestDrvied :public TestBase, public TestBase2
  77. {
  78. public:
  79. virtual ~TestDrvied() = default;
  80. };
  81. TEST_CASE("ShmManager, creat/resume")
  82. {
  83. bg::ShmOptions opts(false, "Test", nullptr);
  84. opts.fix_vptr_on_init = true;
  85. REQUIRE(bg::ShmInit(opts) == true);
  86. int a = 3;
  87. REQUIRE(g_shm_ctx.mgr);
  88. bg::detail::TypeName type(typeid(TestSigleton).name(), sizeof(TestSigleton));
  89. REQUIRE(g_shm_ctx.mgr->HasSingleton(type) == false);
  90. REQUIRE(bg::ShmGetSingleton<TestSigleton>(1, 2, &a));
  91. REQUIRE(g_shm_ctx.mgr->HasSingleton(type) == true);
  92. TestDrvied* drv = bg::ShmNew<TestDrvied>();
  93. uintptr_t drv_ptr = *reinterpret_cast<uintptr_t*>(drv);
  94. TestBase* base = static_cast<TestBase*>(drv);
  95. base->a = 2;
  96. TestBase2* base2 = static_cast<TestBase2*>(drv);
  97. base2->b = 3;
  98. opts.resume = true;
  99. size_t real_size = sizeof(bg::detail::ShmManager);
  100. #if defined(__GNUC__)
  101. munmap(g_shm_ctx.mgr, real_size);
  102. g_shm_ctx.mgr = nullptr;
  103. REQUIRE(bg::ShmInit(opts) == true);
  104. #elif defined(_MSC_VER)
  105. // 占住共享内存 windows 会自动释放
  106. char path[256];
  107. snprintf(path, 256, "%s-mgr.mmap", opts.identifier);
  108. void* memory1 = bg::detail::ShmObjectAttach(path, 0, &real_size, &real_size);
  109. REQUIRE(memory1);
  110. bg::detail::ShmObjectDelete(g_shm_ctx.mgr, real_size, path);
  111. real_size = 0x100000000;
  112. void* memory2 = bg::detail::ShmObjectAttach("Test-000.mmap", 0, &real_size, &real_size);
  113. REQUIRE(memory2);
  114. bg::detail::ShmObjectDelete((void*)0x00006f0000000000, real_size, "Test-000.mmap");
  115. g_shm_ctx.mgr = nullptr;
  116. REQUIRE(bg::ShmInit(opts) == true);
  117. bg::detail::ShmObjectDelete(memory1, real_size, path);
  118. bg::detail::ShmObjectDelete(memory2, real_size, "Test-000.mmap");
  119. #endif
  120. REQUIRE(bg::detail::ShmCurrentVersion() == 2);
  121. TestSigleton* temp = bg::ShmGetSingleton<TestSigleton>(1, 1, &a);
  122. REQUIRE(temp->a == 1);
  123. REQUIRE(temp->b == 2);
  124. REQUIRE(temp->c == &a);
  125. REQUIRE(*reinterpret_cast<uintptr_t*>(base) == drv_ptr);
  126. REQUIRE(*reinterpret_cast<uintptr_t*>(base2) != 0);
  127. REQUIRE(base->Do1() == 2);
  128. REQUIRE(base2->Do1() == 3);
  129. bg::ShmDelete(base);
  130. bg::ShmDeleteSingleton<TestSigleton>();
  131. REQUIRE(g_shm_ctx.mgr->HasSingleton(type) == false);
  132. bg::ShmFini();
  133. }
  134. TEST_CASE("ShmManager, malloc realloc Free Calloc")
  135. {
  136. bg::ShmOptions opts(false, "Test", nullptr);
  137. opts.fix_vptr_on_init = false;
  138. REQUIRE(bg::ShmInit(opts) == true);
  139. REQUIRE(g_shm_ctx.mgr);
  140. for(size_t bytes = 8; bytes < (CLASS_MAX_BYTES << 4); bytes = bytes << 1)
  141. {
  142. void* memory = g_shm_ctx.mgr->Malloc(bytes);
  143. REQUIRE(memory);
  144. memset(memory, 0, bytes);
  145. *(int*)memory = 1;
  146. *(int*)((uintptr_t)memory + bytes - 4) = 2;
  147. void* memory1 = g_shm_ctx.mgr->Realloc(memory, bytes << 1);
  148. REQUIRE(memory1);
  149. REQUIRE(*(int*)memory1 == 1);
  150. REQUIRE(*(int*)((uintptr_t)memory1 + bytes - 4) == 2);
  151. g_shm_ctx.mgr->Free(memory1);
  152. void* memory2 = g_shm_ctx.mgr->Calloc(bytes/ 4, 4);
  153. REQUIRE(memory2);
  154. for(size_t i = 0; i < bytes / 4; ++ i)
  155. {
  156. REQUIRE(*(int*)memory2 == 0);
  157. }
  158. g_shm_ctx.mgr->Free(memory2);
  159. }
  160. bg::ShmFini();
  161. }