get the cache sizes in bytes
[AROS.git] / arch / all-mingw32 / bootstrap / memory.c
blobb081cec2504415be25da98cc7ab5081846058094
1 #include <io.h>
2 #include <stdio.h>
3 #include <windows.h>
5 #include "sharedmem.h"
7 /* This is not included in my Mingw32 headers */
8 #ifndef FILE_MAP_EXECUTE
9 #define FILE_MAP_EXECUTE 0x0020
10 #endif
12 #define D(x)
13 #define ID_LEN 64
15 #ifdef UNDER_CE
16 /* In Windows CE there's no MapViewOfFileEx(). This means that we're not going to have warm reboot, sorry. */
17 #define MapViewOfFileEx(obj, access, offh, offl, size, addr) MapViewOfFile(obj, access, offh, offl, size)
18 #endif
20 HANDLE RAM_Handle = NULL;
21 void *RAM_Address = NULL;
23 void *AllocateRO(size_t len)
25 return VirtualAlloc(NULL, len, MEM_COMMIT|MEM_RESERVE, PAGE_EXECUTE_READWRITE);
29 * Commit executable and read-only state for kickstart's .code
31 int SetRO(void *addr, size_t len)
33 DWORD old;
35 return !VirtualProtect(addr, len, PAGE_EXECUTE_READ, &old);
38 void *AllocateRW(size_t len)
40 return VirtualAlloc(NULL, len, MEM_COMMIT|MEM_RESERVE, PAGE_READWRITE);
43 void *AllocateRAM(size_t len)
45 SECURITY_ATTRIBUTES sa;
46 #ifndef UNDER_CE
47 void *addr = NULL;
48 const char *var = getenv(SHARED_RAM_VAR);
50 if (var)
52 D(fprintf(stderr, "[AllocateRAM] Found RAM specification: %s\n", var));
53 if (sscanf(var, "%p:%p", &RAM_Handle, &addr) != 2) {
54 D(fprintf(stderr, "[AllocateRAM] Error parsing specification\n"));
55 RAM_Handle = NULL;
56 addr = NULL;
59 D(ffprintf(stderr, stderr, "[AllocateRAM] Inherited memory handle 0x%p address 0x%p\n", RAM_Handle, addr));
60 #endif
62 if (!RAM_Handle)
64 sa.nLength = sizeof(sa);
65 sa.lpSecurityDescriptor = NULL;
66 sa.bInheritHandle = TRUE;
67 RAM_Handle = CreateFileMapping(INVALID_HANDLE_VALUE, &sa, PAGE_EXECUTE_READWRITE, 0, len, NULL);
69 if (!RAM_Handle)
71 D(fprintf(stderr, "[AllocateRAM] PAGE_EXECUTE_READWRITE failed, retrying with PAGE_READWRITE\n"));
72 RAM_Handle = CreateFileMapping(INVALID_HANDLE_VALUE, &sa, PAGE_READWRITE, 0, len, NULL);
74 D(fprintf(stderr, "[AllocateRAM] Shared memory handle 0x%p\n", RAM_Handle));
75 if (!RAM_Handle)
76 return NULL;
78 RAM_Address = MapViewOfFileEx(RAM_Handle, FILE_MAP_ALL_ACCESS|FILE_MAP_EXECUTE, 0, 0, 0, addr);
79 if (!RAM_Address)
81 D(fprintf(stderr, "[AllocateRAM] FILE_MAP_EXECUTE failed, retrying without it\n"));
82 RAM_Address = MapViewOfFileEx(RAM_Handle, FILE_MAP_ALL_ACCESS, 0, 0, 0, addr);
84 D(fprintf(stderr, "[AllocateRAM] Mapped at 0x%p\n", RAM_Address));
86 if (!RAM_Address)
88 CloseHandle(RAM_Handle);
89 RAM_Handle = NULL;
92 return RAM_Address;