Make threadsafe
This commit is contained in:
@@ -5,7 +5,8 @@
|
|||||||
|
|
||||||
VirtualMemoryManager vmem_Manager;
|
VirtualMemoryManager vmem_Manager;
|
||||||
|
|
||||||
VirtualMemoryManager::VirtualMemoryManager()
|
VirtualMemoryManager::VirtualMemoryManager():
|
||||||
|
vmemMutex("VirtualMemory")
|
||||||
{
|
{
|
||||||
pages = 0;
|
pages = 0;
|
||||||
pageLRU = -1;
|
pageLRU = -1;
|
||||||
@@ -90,6 +91,8 @@ void* VirtualMemoryManager::Allocate(size_t size)
|
|||||||
if(!inited)
|
if(!inited)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
LockMut(vmemMutex);
|
||||||
|
|
||||||
unsigned long startPage = -1;
|
unsigned long startPage = -1;
|
||||||
unsigned long freeSegments = 0;
|
unsigned long freeSegments = 0;
|
||||||
unsigned long sizeInPages = (size / pageSize) + 1;
|
unsigned long sizeInPages = (size / pageSize) + 1;
|
||||||
@@ -164,6 +167,8 @@ bool VirtualMemoryManager::Free(void *ptr)
|
|||||||
if(!inited)
|
if(!inited)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
LockMut(vmemMutex);
|
||||||
|
|
||||||
// check that the address is within the virtual address bounds
|
// check that the address is within the virtual address bounds
|
||||||
if((DWORD)ptr < baseAddress || (DWORD)ptr >= baseAddress + (totalPages * pageSize))
|
if((DWORD)ptr < baseAddress || (DWORD)ptr >= baseAddress + (totalPages * pageSize))
|
||||||
{
|
{
|
||||||
@@ -214,6 +219,8 @@ bool VirtualMemoryManager::PageFault(void *ptr)
|
|||||||
if(!inited)
|
if(!inited)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
LockMut(vmemMutex);
|
||||||
|
|
||||||
// check that the address is within the virtual address bounds
|
// check that the address is within the virtual address bounds
|
||||||
if((DWORD)ptr < baseAddress || (DWORD)ptr >= baseAddress + (totalPages * pageSize))
|
if((DWORD)ptr < baseAddress || (DWORD)ptr >= baseAddress + (totalPages * pageSize))
|
||||||
{
|
{
|
||||||
@@ -279,6 +286,8 @@ bool VirtualMemoryManager::DecommitLRU()
|
|||||||
if(!inited)
|
if(!inited)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
LockMut(vmemMutex);
|
||||||
|
|
||||||
// choose random LRU
|
// choose random LRU
|
||||||
pageLRU = rand() % totalPages;
|
pageLRU = rand() % totalPages;
|
||||||
|
|
||||||
@@ -335,6 +344,8 @@ bool VirtualMemoryManager::EnsureFreeMemory(size_t size)
|
|||||||
if(!inited)
|
if(!inited)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
LockMut(vmemMutex);
|
||||||
|
|
||||||
MEMORYSTATUS ms;
|
MEMORYSTATUS ms;
|
||||||
GlobalMemoryStatus(&ms);
|
GlobalMemoryStatus(&ms);
|
||||||
|
|
||||||
@@ -359,6 +370,8 @@ void VirtualMemoryManager::Lock(void *ptr)
|
|||||||
if(!inited)
|
if(!inited)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
LockMut(vmemMutex);
|
||||||
|
|
||||||
// check that the address is within the virtual address bounds
|
// check that the address is within the virtual address bounds
|
||||||
if((DWORD)ptr < baseAddress || (DWORD)ptr >= baseAddress + (totalPages * pageSize))
|
if((DWORD)ptr < baseAddress || (DWORD)ptr >= baseAddress + (totalPages * pageSize))
|
||||||
{
|
{
|
||||||
@@ -394,6 +407,8 @@ void VirtualMemoryManager::Unlock(void *ptr)
|
|||||||
if(!inited)
|
if(!inited)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
LockMut(vmemMutex);
|
||||||
|
|
||||||
// check that the address is within the virtual address bounds
|
// check that the address is within the virtual address bounds
|
||||||
if((DWORD)ptr < baseAddress || (DWORD)ptr >= baseAddress + (totalPages * pageSize))
|
if((DWORD)ptr < baseAddress || (DWORD)ptr >= baseAddress + (totalPages * pageSize))
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -5,6 +5,8 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "RageThreads.h"
|
||||||
|
|
||||||
struct vm_page
|
struct vm_page
|
||||||
{
|
{
|
||||||
DWORD startAddress; // start address for this page
|
DWORD startAddress; // start address for this page
|
||||||
@@ -49,6 +51,9 @@ protected:
|
|||||||
|
|
||||||
bool inited;
|
bool inited;
|
||||||
bool logging;
|
bool logging;
|
||||||
|
|
||||||
|
// mutex to make sure pages aren't allocated/deallocated concurrently
|
||||||
|
RageMutex vmemMutex;
|
||||||
};
|
};
|
||||||
|
|
||||||
void *valloc(size_t size);
|
void *valloc(size_t size);
|
||||||
|
|||||||
Reference in New Issue
Block a user