Debian Codegraph @

js/src/zydis/Zycore/API/Memory.c

raw
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 #include "zydis/Zycore/API/Memory.h"
28
29 #ifndef ZYAN_NO_LIBC
30
31 #if defined(ZYAN_WINDOWS)
32
33 #elif defined(ZYAN_POSIX)
34 # include <unistd.h>
35 #else
36 # error "Unsupported platform detected"
37 #endif
38
39 /* ============================================================================================== */
40 /* Exported functions */
41 /* ============================================================================================== */
42
43 /* ---------------------------------------------------------------------------------------------- */
44 /* General */
45 /* ---------------------------------------------------------------------------------------------- */
46
47 ZyanU32 ZyanMemoryGetSystemPageSize(void)
48 {
49 #if defined(ZYAN_WINDOWS)
50
51 SYSTEM_INFO system_info;
52 GetSystemInfo(&system_info);
53
54 return system_info.dwPageSize;
55
56 #elif defined(ZYAN_POSIX)
57
58 return sysconf(_SC_PAGE_SIZE);
59
60 #endif
61 }
62
63 ZyanU32 ZyanMemoryGetSystemAllocationGranularity(void)
64 {
65 #if defined(ZYAN_WINDOWS)
66
67 SYSTEM_INFO system_info;
68 GetSystemInfo(&system_info);
69
70 return system_info.dwAllocationGranularity;
71
72 #elif defined(ZYAN_POSIX)
73
74 return sysconf(_SC_PAGE_SIZE);
75
76 #endif
77 }
78
79 /* ---------------------------------------------------------------------------------------------- */
80 /* Memory management */
81 /* ---------------------------------------------------------------------------------------------- */
82
83 ZyanStatus ZyanMemoryVirtualProtect(void* address, ZyanUSize size,
84 ZyanMemoryPageProtection protection)
85 {
86 #if defined(ZYAN_WINDOWS)
87
88 DWORD old;
89 if (!VirtualProtect(address, size, protection, &old))
90 {
91 return ZYAN_STATUS_BAD_SYSTEMCALL;
92 }
93
94 #elif defined(ZYAN_POSIX)
95
96 if (mprotect(address, size, protection))
97 {
98 return ZYAN_STATUS_BAD_SYSTEMCALL;
99 }
100
101 #endif
102
103 return ZYAN_STATUS_SUCCESS;
104 }
105
106 ZyanStatus ZyanMemoryVirtualFree(void* address, ZyanUSize size)
107 {
108 #if defined(ZYAN_WINDOWS)
109
110 ZYAN_UNUSED(size);
111 if (!VirtualFree(address, 0, MEM_RELEASE))
112 {
113 return ZYAN_STATUS_BAD_SYSTEMCALL;
114 }
115
116 #elif defined(ZYAN_POSIX)
117
118 if (munmap(address, size))
119 {
120 return ZYAN_STATUS_BAD_SYSTEMCALL;
121 }
122
123 #endif
124
125 return ZYAN_STATUS_SUCCESS;
126 }
127
128 /* ---------------------------------------------------------------------------------------------- */
129
130 /* ============================================================================================== */
131
132 #endif /* ZYAN_NO_LIBC */