|
33 | 33 | #define ZYCORE_MEMORY_H |
34 | 34 |
|
35 | 35 | #include <ZycoreExportConfig.h> |
| 36 | +#include <Zycore/Defines.h> |
36 | 37 | #include <Zycore/Status.h> |
37 | 38 | #include <Zycore/Types.h> |
38 | 39 |
|
| 40 | +#if defined(ZYAN_WINDOWS) |
| 41 | +# include <windows.h> |
| 42 | +#elif defined(ZYAN_POSIX) |
| 43 | +# include <sys/mman.h> |
| 44 | +#else |
| 45 | +# error "Unsupported platform detected" |
| 46 | +#endif |
| 47 | + |
39 | 48 | /* ============================================================================================== */ |
40 | 49 | /* Enums and types */ |
41 | 50 | /* ============================================================================================== */ |
42 | 51 |
|
43 | 52 | /** |
44 | | - * @brief Defines the `ZyanMemoryManager` struct. |
| 53 | + * @brief Defines the `ZyanMemoryPageProtection` enum. |
45 | 54 | */ |
46 | | -typedef struct ZyanMemoryManager_ |
| 55 | +typedef enum ZyanMemoryPageProtection_ |
47 | 56 | { |
48 | | - int a; |
49 | | -} ZyanMemoryManager; |
| 57 | +#if defined(ZYAN_WINDOWS) |
| 58 | + |
| 59 | + ZYAN_PAGE_READONLY = PAGE_READONLY, |
| 60 | + ZYAN_PAGE_READWRITE = PAGE_READWRITE, |
| 61 | + ZYAN_PAGE_EXECUTE = PAGE_EXECUTE, |
| 62 | + ZYAN_PAGE_EXECUTE_READ = PAGE_EXECUTE_READ, |
| 63 | + ZYAN_PAGE_EXECUTE_READWRITE = PAGE_EXECUTE_READWRITE |
| 64 | + |
| 65 | +#elif defined(ZYAN_POSIX) |
| 66 | + |
| 67 | + ZYAN_PAGE_READONLY = PROT_READ, |
| 68 | + ZYAN_PAGE_READWRITE = PROT_READ | PROT_WRITE, |
| 69 | + ZYAN_PAGE_EXECUTE = PROT_EXEC, |
| 70 | + ZYAN_PAGE_EXECUTE_READ = PROT_EXEC | PROT_READ, |
| 71 | + ZYAN_PAGE_EXECUTE_READWRITE = PROT_EXEC | PROT_READ | PROT_WRITE |
| 72 | + |
| 73 | +#endif |
| 74 | +} ZyanMemoryPageProtection; |
50 | 75 |
|
51 | 76 | /* ============================================================================================== */ |
52 | 77 | /* Exported functions */ |
53 | 78 | /* ============================================================================================== */ |
54 | 79 |
|
55 | 80 | /* ---------------------------------------------------------------------------------------------- */ |
56 | | -/* Memory manager */ |
| 81 | +/* General */ |
57 | 82 | /* ---------------------------------------------------------------------------------------------- */ |
58 | 83 |
|
59 | 84 | /** |
60 | | - * @brief Returns the default memory manager. |
| 85 | + * @brief Returns the system page size. |
| 86 | + * |
| 87 | + * @return The system page size. |
| 88 | + */ |
| 89 | +ZyanU32 ZyanMemoryGetSystemPageSize(); |
61 | 90 |
|
62 | | - * @return The default memory manager. |
| 91 | +/** |
| 92 | + * @brief Returns the system allocation granularity. |
| 93 | + * |
| 94 | + * The system allocation granularity specifies the minimum amount of bytes which can be allocated |
| 95 | + * at a specific address by a single call of `ZyanMemoryVirtualAlloc`. |
| 96 | + * |
| 97 | + * This value is typically 64KiB on Windows systems and equal to the page size on most POSIX |
| 98 | + * platforms. |
| 99 | + * |
| 100 | + * @return The system allocation granularity. |
63 | 101 | */ |
64 | | -ZYCORE_EXPORT const ZyanMemoryManager* ZyanMemoryManagerDefault(void); |
| 102 | +ZyanU32 ZyanMemoryGetSystemAllocationGranularity(); |
65 | 103 |
|
66 | 104 | /* ---------------------------------------------------------------------------------------------- */ |
67 | | -/* */ |
| 105 | +/* Memory management */ |
68 | 106 | /* ---------------------------------------------------------------------------------------------- */ |
69 | 107 |
|
| 108 | +/** |
| 109 | + * @brief Changes the memory protection value of one or more pages. |
| 110 | + * |
| 111 | + * @param address The start address aligned to a page boundary. |
| 112 | + * @param size The size. |
| 113 | + * @param protection The new page protection value. |
| 114 | + * |
| 115 | + * @return A zyan status code. |
| 116 | + */ |
| 117 | +ZyanStatus ZyanMemoryVirtualProtect(void* address, ZyanUSize size, |
| 118 | + ZyanMemoryPageProtection protection); |
| 119 | + |
| 120 | +/** |
| 121 | + * @brief Releases one or more memory pages starting at the given address. |
| 122 | + * |
| 123 | + * @param address The start address aligned to a page boundary. |
| 124 | + * @param size The size. |
| 125 | + * |
| 126 | + * @return A zyan status code. |
| 127 | + */ |
| 128 | +ZyanStatus ZyanMemoryVirtualFree(void* address, ZyanUSize size); |
70 | 129 |
|
| 130 | +/* ---------------------------------------------------------------------------------------------- */ |
71 | 131 |
|
72 | 132 | /* ============================================================================================== */ |
73 | 133 |
|
|
0 commit comments