forked from RPCS3/rpcs3
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Memory: Factorize virtual memory reservation/allocation code in function
- Loading branch information
Showing
5 changed files
with
75 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include "stdafx.h" | ||
#include "VirtualMemory.h" | ||
#ifdef _WIN32 | ||
#include <Windows.h> | ||
#else | ||
#include <sys/mman.h> | ||
#include <sys/stat.h> | ||
#include <fcntl.h> | ||
#include <unistd.h> | ||
#include <sys/types.h> | ||
#endif | ||
|
||
namespace memory_helper | ||
{ | ||
void* reserve_memory(size_t size) | ||
{ | ||
#ifdef _WIN32 | ||
return VirtualAlloc(NULL, size, MEM_RESERVE, PAGE_NOACCESS); | ||
#else | ||
return mmap(nullptr, size, PROT_NONE, MAP_ANON | MAP_PRIVATE, -1, 0); | ||
#endif | ||
} | ||
|
||
void commit_page_memory(void* pointer, size_t page_size) | ||
{ | ||
#ifdef _WIN32 | ||
VirtualAlloc((u8*)pointer, page_size, MEM_COMMIT, PAGE_READWRITE); | ||
#else | ||
mprotect((u8*)pointer, page_size, PROT_READ | PROT_WRITE); | ||
#endif | ||
} | ||
|
||
void free_reserved_memory(void* pointer, size_t size) | ||
{ | ||
#ifdef _WIN32 | ||
VirtualFree(pointer, 0, MEM_RELEASE); | ||
#else | ||
munmap(pointer, size); | ||
#endif | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#pragma once | ||
|
||
namespace memory_helper | ||
{ | ||
/** | ||
* Reserve size bytes of virtual memory and returns it. | ||
* The memory should be commited before usage. | ||
*/ | ||
void* reserve_memory(size_t size); | ||
|
||
/** | ||
* Commit page_size bytes of virtual memory starting at pointer. | ||
* That is, bake reserved memory with physical memory. | ||
* pointer should belong to a range of reserved memory. | ||
*/ | ||
void commit_page_memory(void* pointer, size_t page_size); | ||
|
||
/** | ||
* Free memory alloced via reserve_memory. | ||
*/ | ||
void free_reserved_memory(void* pointer, size_t size); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters