2 * Win32 kernel functions
4 * Copyright 1995 Martin von Loewis and Cameron Heide
20 #ifndef PROT_NONE /* FreeBSD doesn't define PROT_NONE */
32 virtual_mem_t
*mem
= 0;
36 /***********************************************************************
37 * VirtualAlloc (KERNEL32.548)
39 int TranslateProtectionFlags(DWORD
);
40 LPVOID
VirtualAlloc(LPVOID lpvAddress
, DWORD cbSize
,
41 DWORD fdwAllocationType
, DWORD fdwProtect
)
45 virtual_mem_t
*tmp_mem
;
48 dprintf_win32(stddeb
, "VirtualAlloc: size = %ld, address=%p\n", cbSize
, lpvAddress
);
49 if (fdwAllocationType
& MEM_RESERVE
|| !lpvAddress
) {
50 ptr
= mmap((void *)((((unsigned long)lpvAddress
-1) & 0xFFFF0000L
)
52 cbSize
, PROT_NONE
, MAP_ANON
|MAP_PRIVATE
,-1,0);
53 if (ptr
== (caddr_t
) -1) {
54 dprintf_win32(stddeb
, "VirtualAlloc: returning NULL");
57 if (lpvAddress
&& ((unsigned long)ptr
& 0xFFFF0000L
)) {
60 ptr
= mmap(lpvAddress
, cbSize
,
61 PROT_NONE
, MAP_ANON
|MAP_PRIVATE
,-1,0);
62 if (ptr
== (caddr_t
) -1) {
63 dprintf_win32(stddeb
, "VirtualAlloc: returning NULL");
66 ptr
= (void *)((((unsigned long)ptr
-1) & 0xFFFF0000L
)+0x00010000L
);
68 /* remember the size for VirtualFree since it's going to be handed
71 if (mem_count
== mem_used
) {
72 tmp_mem
= realloc(mem
,(mem_count
+10)*sizeof(virtual_mem_t
));
73 if (!tmp_mem
) return 0;
75 memset(mem
+mem_count
, 0, 10*sizeof(virtual_mem_t
));
78 for (i
=0; i
<mem_count
; i
++) {
81 (mem
+i
)->size
= cbSize
;
90 if (fdwAllocationType
& MEM_COMMIT
) {
91 prot
= TranslateProtectionFlags(fdwProtect
&
92 ~(PAGE_GUARD
| PAGE_NOCACHE
));
93 mprotect(ptr
, cbSize
, prot
);
96 /* kludge for gnu-win32 */
97 if (fdwAllocationType
& MEM_RESERVE
) return sbrk(0);
98 ptr
= malloc(cbSize
+ 65536);
101 /* Round it up to the next 64K boundary and zero it.
103 ptr
= (void *)(((unsigned long)ptr
& 0xFFFF0000L
) + 0x00010000L
);
104 memset(ptr
, 0, cbSize
);
107 dprintf_win32(stddeb
, "VirtualAlloc: got pointer %p\n", ptr
);
111 /***********************************************************************
112 * VirtualFree (KERNEL32.550)
114 BOOL
VirtualFree(LPVOID lpvAddress
, DWORD cbSize
, DWORD fdwFreeType
)
118 if (fdwFreeType
& MEM_RELEASE
) {
119 for (i
=0; i
<mem_count
; i
++) {
120 if ((mem
+i
)->ptr
== lpvAddress
) {
121 munmap(lpvAddress
, (mem
+i
)->size
);
128 mprotect(lpvAddress
, cbSize
, PROT_NONE
);
137 int TranslateProtectionFlags(DWORD protection_flags
)
141 switch(protection_flags
) {
146 prot
=PROT_READ
|PROT_WRITE
;
154 case PAGE_EXECUTE_READ
:
155 prot
=PROT_EXEC
|PROT_READ
;
157 case PAGE_EXECUTE_READWRITE
:
158 prot
=PROT_EXEC
|PROT_READ
|PROT_WRITE
;
160 case PAGE_EXECUTE_WRITECOPY
:
161 prot
=PROT_EXEC
|PROT_WRITE
;