1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- #include "precompile.h"
- #include "sp_shm.h"
- #include "sp_def.h"
- #include "shm_mem.h"
- #include <winpr/file.h>
- #define MEM_KEY 95555
- #define MEM_SIZE (SHM_MEM_SIZE * 1024 * 1024)
- #define GET_ADDRESS(x) ((void*)((1+(x)) << 28))
- static int g_newcreator = -1;
- int sp_shm_get_range(int mask)
- {
- int i;
- int range = 0;
- /*if start to refactor, you can think about :
- * https://forums.pcsx2.net/Thread-blog-VirtualAlloc-on-Linux
- * VirtualAlloc has been implement at WinPR
- */
- for (i = 0; i < 7; ++i) {
- void *address = GET_ADDRESS(i);//0x10000000, 0x20000000, ..., 0x80000000
- void *ret_address = VirtualAlloc(address, MEM_SIZE, MEM_RESERVE, PAGE_NOACCESS);
- if (address == ret_address) {
- range |= 1 << (i + 1);
- }
- if (ret_address)
- #if _WIN32
- VirtualFree(ret_address, 0, MEM_RELEASE);
- #else
- VirtualFree(ret_address, MEM_SIZE, MEM_RELEASE);
- #endif
- }
- return range & mask;
- }
- void *sp_shm_init(int range, int newcreator)
- {
- int i;
- void *address;
- for (i = 0; i < 7; ++i) {
- if (range & (1 << (i+1))) {
- address = GET_ADDRESS(i);
- if (shm_mem_init2(MEM_KEY, address, newcreator) == 0) {
- if (address != NULL)
- g_newcreator = newcreator;
- return address;
- }
- }
- }
- return NULL;
- }
- void sp_shm_term()
- {
- /*we don't know why, this has been annotated at windows platform*/
- if(sp_shm_is_newalloc())
- shm_mem_destroy();
- }
- int sp_shm_is_newalloc()
- {
- if (g_newcreator == -1)
- return 0;
- return g_newcreator;
- }
|