Skip to content

Commit 3435866

Browse files
committed
Implement some basic Memory APIs
1 parent 7d523f2 commit 3435866

File tree

2 files changed

+162
-9
lines changed

2 files changed

+162
-9
lines changed

include/Zycore/API/Memory.h

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,41 +33,101 @@
3333
#define ZYCORE_MEMORY_H
3434

3535
#include <ZycoreExportConfig.h>
36+
#include <Zycore/Defines.h>
3637
#include <Zycore/Status.h>
3738
#include <Zycore/Types.h>
3839

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+
3948
/* ============================================================================================== */
4049
/* Enums and types */
4150
/* ============================================================================================== */
4251

4352
/**
44-
* @brief Defines the `ZyanMemoryManager` struct.
53+
* @brief Defines the `ZyanMemoryPageProtection` enum.
4554
*/
46-
typedef struct ZyanMemoryManager_
55+
typedef enum ZyanMemoryPageProtection_
4756
{
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;
5075

5176
/* ============================================================================================== */
5277
/* Exported functions */
5378
/* ============================================================================================== */
5479

5580
/* ---------------------------------------------------------------------------------------------- */
56-
/* Memory manager */
81+
/* General */
5782
/* ---------------------------------------------------------------------------------------------- */
5883

5984
/**
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();
6190

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.
63101
*/
64-
ZYCORE_EXPORT const ZyanMemoryManager* ZyanMemoryManagerDefault(void);
102+
ZyanU32 ZyanMemoryGetSystemAllocationGranularity();
65103

66104
/* ---------------------------------------------------------------------------------------------- */
67-
/* */
105+
/* Memory management */
68106
/* ---------------------------------------------------------------------------------------------- */
69107

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);
70129

130+
/* ---------------------------------------------------------------------------------------------- */
71131

72132
/* ============================================================================================== */
73133

src/API/Memory.c

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,103 @@
2626

2727
#include <Zycore/API/Memory.h>
2828

29+
#if defined(ZYAN_WINDOWS)
30+
31+
#elif defined(ZYAN_POSIX)
32+
# include <unistd.h>
33+
#else
34+
# error "Unsupported platform detected"
35+
#endif
36+
2937
/* ============================================================================================== */
3038
/* Exported functions */
3139
/* ============================================================================================== */
3240

41+
/* ---------------------------------------------------------------------------------------------- */
42+
/* General */
43+
/* ---------------------------------------------------------------------------------------------- */
44+
45+
ZyanU32 ZyanMemoryGetSystemPageSize()
46+
{
47+
#if defined(ZYAN_WINDOWS)
48+
49+
SYSTEM_INFO system_info;
50+
GetSystemInfo(&system_info);
51+
52+
return system_info.dwPageSize;
53+
54+
#elif defined(ZYAN_POSIX)
55+
56+
return sysconf(_SC_PAGE_SIZE);
57+
58+
#endif
59+
}
60+
61+
ZyanU32 ZyanMemoryGetSystemAllocationGranularity()
62+
{
63+
#if defined(ZYAN_WINDOWS)
64+
65+
SYSTEM_INFO system_info;
66+
GetSystemInfo(&system_info);
67+
68+
return system_info.dwAllocationGranularity;
69+
70+
#elif defined(ZYAN_POSIX)
71+
72+
return sysconf(_SC_PAGE_SIZE);
73+
74+
#endif
75+
}
76+
77+
/* ---------------------------------------------------------------------------------------------- */
78+
/* Memory management */
79+
/* ---------------------------------------------------------------------------------------------- */
80+
81+
ZyanStatus ZyanMemoryVirtualProtect(void* address, ZyanUSize size,
82+
ZyanMemoryPageProtection protection)
83+
{
84+
#if defined(ZYAN_WINDOWS)
85+
86+
DWORD old;
87+
if (!VirtualProtect(address, size, protection, &old))
88+
{
89+
return ZYAN_STATUS_BAD_SYSTEMCALL;
90+
}
91+
92+
#elif defined(ZYAN_POSIX)
93+
94+
if (mprotect(address, size, protection))
95+
{
96+
return ZYAN_STATUS_BAD_SYSTEMCALL;
97+
}
98+
99+
#endif
100+
101+
return ZYAN_STATUS_SUCCESS;
102+
}
103+
104+
ZyanStatus ZyanMemoryVirtualFree(void* address, ZyanUSize size)
105+
{
106+
#if defined(ZYAN_WINDOWS)
107+
108+
ZYAN_UNUSED(size);
109+
if (!VirtualFree(address, 0, MEM_RELEASE))
110+
{
111+
return ZYAN_STATUS_BAD_SYSTEMCALL;
112+
}
113+
114+
#elif defined(ZYAN_POSIX)
115+
116+
if (munmap(address, size))
117+
{
118+
return ZYAN_STATUS_BAD_SYSTEMCALL;
119+
}
120+
121+
#endif
122+
123+
return ZYAN_STATUS_SUCCESS;
124+
}
33125

126+
/* ---------------------------------------------------------------------------------------------- */
34127

35128
/* ============================================================================================== */

0 commit comments

Comments
 (0)