| 1 |
/*************************************************************************************************** |
| 2 |
|
| 3 |
Zyan Core Library (Zycore-C) |
| 4 |
|
| 5 |
Original Author : Florian Bernd |
| 6 |
|
| 7 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 |
* of this software and associated documentation files (the "Software"), to deal |
| 9 |
* in the Software without restriction, including without limitation the rights |
| 10 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 |
* copies of the Software, and to permit persons to whom the Software is |
| 12 |
* furnished to do so, subject to the following conditions: |
| 13 |
* |
| 14 |
* The above copyright notice and this permission notice shall be included in all |
| 15 |
* copies or substantial portions of the Software. |
| 16 |
* |
| 17 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 |
* SOFTWARE. |
| 24 |
|
| 25 |
***************************************************************************************************/ |
| 26 |
|
| 27 |
/** |
| 28 |
* @file |
| 29 |
* @brief |
| 30 |
*/ |
| 31 |
|
| 32 |
#ifndef ZYCORE_API_MEMORY_H |
| 33 |
#define ZYCORE_API_MEMORY_H |
| 34 |
|
| 35 |
#include "zydis/Zycore/Defines.h" |
| 36 |
#include "zydis/Zycore/Status.h" |
| 37 |
#include "zydis/Zycore/Types.h" |
| 38 |
|
| 39 |
#ifndef ZYAN_NO_LIBC |
| 40 |
|
| 41 |
#if defined(ZYAN_WINDOWS) |
| 42 |
# include <windows.h> |
| 43 |
#elif defined(ZYAN_POSIX) |
| 44 |
# include <sys/mman.h> |
| 45 |
#else |
| 46 |
# error "Unsupported platform detected" |
| 47 |
#endif |
| 48 |
|
| 49 |
/* ============================================================================================== */ |
| 50 |
/* Enums and types */ |
| 51 |
/* ============================================================================================== */ |
| 52 |
|
| 53 |
/** |
| 54 |
* Defines the `ZyanMemoryPageProtection` enum. |
| 55 |
*/ |
| 56 |
typedef enum ZyanMemoryPageProtection_ |
| 57 |
{ |
| 58 |
#if defined(ZYAN_WINDOWS) |
| 59 |
|
| 60 |
ZYAN_PAGE_READONLY = PAGE_READONLY, |
| 61 |
ZYAN_PAGE_READWRITE = PAGE_READWRITE, |
| 62 |
ZYAN_PAGE_EXECUTE = PAGE_EXECUTE, |
| 63 |
ZYAN_PAGE_EXECUTE_READ = PAGE_EXECUTE_READ, |
| 64 |
ZYAN_PAGE_EXECUTE_READWRITE = PAGE_EXECUTE_READWRITE |
| 65 |
|
| 66 |
#elif defined(ZYAN_POSIX) |
| 67 |
|
| 68 |
ZYAN_PAGE_READONLY = PROT_READ, |
| 69 |
ZYAN_PAGE_READWRITE = PROT_READ | PROT_WRITE, |
| 70 |
ZYAN_PAGE_EXECUTE = PROT_EXEC, |
| 71 |
ZYAN_PAGE_EXECUTE_READ = PROT_EXEC | PROT_READ, |
| 72 |
ZYAN_PAGE_EXECUTE_READWRITE = PROT_EXEC | PROT_READ | PROT_WRITE |
| 73 |
|
| 74 |
#endif |
| 75 |
} ZyanMemoryPageProtection; |
| 76 |
|
| 77 |
/* ============================================================================================== */ |
| 78 |
/* Exported functions */ |
| 79 |
/* ============================================================================================== */ |
| 80 |
|
| 81 |
/* ---------------------------------------------------------------------------------------------- */ |
| 82 |
/* General */ |
| 83 |
/* ---------------------------------------------------------------------------------------------- */ |
| 84 |
|
| 85 |
/** |
| 86 |
* Returns the system page size. |
| 87 |
* |
| 88 |
* @return The system page size. |
| 89 |
*/ |
| 90 |
ZYCORE_EXPORT ZyanU32 ZyanMemoryGetSystemPageSize(void); |
| 91 |
|
| 92 |
/** |
| 93 |
* Returns the system allocation granularity. |
| 94 |
* |
| 95 |
* The system allocation granularity specifies the minimum amount of bytes which can be allocated |
| 96 |
* at a specific address by a single call of `ZyanMemoryVirtualAlloc`. |
| 97 |
* |
| 98 |
* This value is typically 64KiB on Windows systems and equal to the page size on most POSIX |
| 99 |
* platforms. |
| 100 |
* |
| 101 |
* @return The system allocation granularity. |
| 102 |
*/ |
| 103 |
ZYCORE_EXPORT ZyanU32 ZyanMemoryGetSystemAllocationGranularity(void); |
| 104 |
|
| 105 |
/* ---------------------------------------------------------------------------------------------- */ |
| 106 |
/* Memory management */ |
| 107 |
/* ---------------------------------------------------------------------------------------------- */ |
| 108 |
|
| 109 |
/** |
| 110 |
* Changes the memory protection value of one or more pages. |
| 111 |
* |
| 112 |
* @param address The start address aligned to a page boundary. |
| 113 |
* @param size The size. |
| 114 |
* @param protection The new page protection value. |
| 115 |
* |
| 116 |
* @return A zyan status code. |
| 117 |
*/ |
| 118 |
ZYCORE_EXPORT ZyanStatus ZyanMemoryVirtualProtect(void* address, ZyanUSize size, |
| 119 |
ZyanMemoryPageProtection protection); |
| 120 |
|
| 121 |
/** |
| 122 |
* Releases one or more memory pages starting at the given address. |
| 123 |
* |
| 124 |
* @param address The start address aligned to a page boundary. |
| 125 |
* @param size The size. |
| 126 |
* |
| 127 |
* @return A zyan status code. |
| 128 |
*/ |
| 129 |
ZYCORE_EXPORT ZyanStatus ZyanMemoryVirtualFree(void* address, ZyanUSize size); |
| 130 |
|
| 131 |
/* ---------------------------------------------------------------------------------------------- */ |
| 132 |
|
| 133 |
/* ============================================================================================== */ |
| 134 |
|
| 135 |
#endif /* ZYAN_NO_LIBC */ |
| 136 |
|
| 137 |
#endif /* ZYCORE_API_MEMORY_H */ |