test_dllshm.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include "shm.h"
  2. #include "catch.hpp"
  3. #include "shm_helper.h"
  4. #include "file_base.h"
  5. typedef void* (__cdecl* DllMallocPtr)(size_t bytes);
  6. typedef void*(__cdecl*DllReallocPtr)(void* old_ptr, size_t new_bytes);
  7. typedef void(__cdecl*DllFreePtr)(void*);
  8. TEST_CASE("Shmdll, ShmMalloc ShmFree")
  9. {
  10. char _dll_name[256];
  11. disk_file::dll_name("./testshmdll.dll", _dll_name, sizeof(_dll_name));
  12. DllMallocPtr mallocdll = nullptr;
  13. DllReallocPtr reallocdll = nullptr;
  14. DllFreePtr freedll = nullptr;
  15. dll_handle_t hModule = disk_file::dll_open(_dll_name);
  16. REQUIRE(hModule);
  17. mallocdll = (DllMallocPtr)disk_file::dll_symbol(hModule,"DllMalloc");
  18. reallocdll = (DllReallocPtr)disk_file::dll_symbol(hModule, "DllRealloc");
  19. freedll = (DllFreePtr)disk_file::dll_symbol(hModule,"DllFree");
  20. bg::ShmOptions opts(false, "Test", nullptr);
  21. opts.fix_vptr_on_init = false;
  22. REQUIRE(bg::ShmInit(opts) == true);
  23. for(size_t bytes = 8; bytes < (0x40000 << 4); bytes = bytes << 1)
  24. {
  25. void* memory = mallocdll(bytes);
  26. REQUIRE(memory);
  27. memset(memory, 0, bytes);
  28. *(int*)memory = 1;
  29. *(int*)((uintptr_t)memory + bytes - 4) = 2;
  30. void* memory1 = reallocdll(memory, bytes << 1);
  31. REQUIRE(memory1);
  32. REQUIRE(*(int*)memory1 == 1);
  33. REQUIRE(*(int*)((uintptr_t)memory1 + bytes - 4) == 2);
  34. freedll(memory1);
  35. }
  36. bg::ShmFini();
  37. }