sp_shm.c 984 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include "precompile.h"
  2. #include "sp_shm.h"
  3. #include "sp_def.h"
  4. #include "shm_mem.h"
  5. #define MEM_KEY 95555
  6. #define MEM_SIZE (SHM_MEM_SIZE * 1024 * 1024)
  7. #define GET_ADDRESS(x) ((void*)((1+(x)) << 28))
  8. int sp_shm_get_range(int mask)
  9. {
  10. int i;
  11. int range = 0;
  12. /*if start to refactor, you can think about :
  13. * https://forums.pcsx2.net/Thread-blog-VirtualAlloc-on-Linux
  14. */
  15. for (i = 0; i < 7; ++i) {
  16. void *address = GET_ADDRESS(i);
  17. void *ret_address = VirtualAlloc(address, MEM_SIZE, MEM_RESERVE, PAGE_NOACCESS);
  18. if (address == ret_address) {
  19. range |= 1 << (i + 1);
  20. }
  21. if (ret_address)
  22. VirtualFree(ret_address, 0, MEM_RELEASE);
  23. }
  24. return range & mask;
  25. }
  26. void *sp_shm_init(int range, int newcreator)
  27. {
  28. int i;
  29. void *address;
  30. for (i = 0; i < 7; ++i) {
  31. if (range & (1 << (i+1))) {
  32. address = GET_ADDRESS(i);
  33. if (shm_mem_init2(MEM_KEY, address, newcreator) == 0)
  34. return address;
  35. }
  36. }
  37. return NULL;
  38. }
  39. void sp_shm_term()
  40. {
  41. //shm_mem_destroy();
  42. }