sp_shm.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include "precompile.h"
  2. #include "sp_shm.h"
  3. #include "sp_def.h"
  4. #include "shm_mem.h"
  5. #include <winpr/file.h>
  6. #define MEM_KEY 95555
  7. #define MEM_SIZE (SHM_MEM_SIZE * 1024 * 1024)
  8. #define GET_ADDRESS(x) ((void*)((1+(x)) << 28))
  9. static int g_newcreator = -1;
  10. int sp_shm_get_range(int mask)
  11. {
  12. int i;
  13. int range = 0;
  14. /*if start to refactor, you can think about :
  15. * https://forums.pcsx2.net/Thread-blog-VirtualAlloc-on-Linux
  16. * VirtualAlloc has been implement at WinPR
  17. */
  18. for (i = 0; i < 7; ++i) {
  19. void *address = GET_ADDRESS(i);//0x10000000, 0x20000000, ..., 0x80000000
  20. void *ret_address = VirtualAlloc(address, MEM_SIZE, MEM_RESERVE, PAGE_NOACCESS);
  21. if (address == ret_address) {
  22. range |= 1 << (i + 1);
  23. }
  24. if (ret_address)
  25. #if _WIN32
  26. VirtualFree(ret_address, 0, MEM_RELEASE);
  27. #else
  28. VirtualFree(ret_address, MEM_SIZE, MEM_RELEASE);
  29. #endif
  30. }
  31. return range & mask;
  32. }
  33. void *sp_shm_init(int range, int newcreator)
  34. {
  35. int i;
  36. void *address;
  37. for (i = 0; i < 7; ++i) {
  38. if (range & (1 << (i+1))) {
  39. address = GET_ADDRESS(i);
  40. if (shm_mem_init2(MEM_KEY, address, newcreator) == 0) {
  41. if (address != NULL)
  42. g_newcreator = newcreator;
  43. return address;
  44. }
  45. }
  46. }
  47. return NULL;
  48. }
  49. void sp_shm_term()
  50. {
  51. /*we don't know why, this has been annotated at windows platform*/
  52. if(sp_shm_is_newalloc())
  53. shm_mem_destroy();
  54. }
  55. int sp_shm_is_newalloc()
  56. {
  57. if (g_newcreator == -1)
  58. return 0;
  59. return g_newcreator;
  60. }