2 * winmisc.c: miscellaneous Windows-specific things
9 OSVERSIONINFO osVersion
;
11 char *platform_get_x_display(void) {
12 /* We may as well check for DISPLAY in case it's useful. */
13 return dupstr(getenv("DISPLAY"));
16 Filename
filename_from_str(const char *str
)
19 strncpy(ret
.path
, str
, sizeof(ret
.path
));
20 ret
.path
[sizeof(ret
.path
)-1] = '\0';
24 const char *filename_to_str(const Filename
*fn
)
29 int filename_equal(Filename f1
, Filename f2
)
31 return !strcmp(f1
.path
, f2
.path
);
34 int filename_is_null(Filename fn
)
39 char *get_username(void)
45 if (GetUserName(NULL
, &namelen
) == FALSE
) {
47 * Apparently this doesn't work at least on Windows XP SP2.
48 * Thus assume a maximum of 256. It will fail again if it
54 user
= snewn(namelen
, char);
55 GetUserName(user
, &namelen
);
60 BOOL
init_winver(void)
62 ZeroMemory(&osVersion
, sizeof(osVersion
));
63 osVersion
.dwOSVersionInfoSize
= sizeof (OSVERSIONINFO
);
64 return GetVersionEx ( (OSVERSIONINFO
*) &osVersion
);
68 static FILE *debug_fp
= NULL
;
69 static HANDLE debug_hdl
= INVALID_HANDLE_VALUE
;
70 static int debug_got_console
= 0;
76 if (!debug_got_console
) {
78 debug_got_console
= 1;
79 debug_hdl
= GetStdHandle(STD_OUTPUT_HANDLE
);
83 debug_fp
= fopen("debug.log", "w");
86 if (debug_hdl
!= INVALID_HANDLE_VALUE
) {
87 WriteFile(debug_hdl
, buf
, strlen(buf
), &dw
, NULL
);
96 * Minefield - a Windows equivalent for Electric Fence
104 * We start by reserving as much virtual address space as Windows
105 * will sensibly (or not sensibly) let us have. We flag it all as
108 * Any allocation attempt is satisfied by committing one or more
109 * pages, with an uncommitted page on either side. The returned
110 * memory region is jammed up against the _end_ of the pages.
112 * Freeing anything causes instantaneous decommitment of the pages
113 * involved, so stale pointers are caught as soon as possible.
116 static int minefield_initialised
= 0;
117 static void *minefield_region
= NULL
;
118 static long minefield_size
= 0;
119 static long minefield_npages
= 0;
120 static long minefield_curpos
= 0;
121 static unsigned short *minefield_admin
= NULL
;
122 static void *minefield_pages
= NULL
;
124 static void minefield_admin_hide(int hide
)
126 int access
= hide ? PAGE_NOACCESS
: PAGE_READWRITE
;
127 VirtualProtect(minefield_admin
, minefield_npages
* 2, access
, NULL
);
130 static void minefield_init(void)
136 for (size
= 0x40000000; size
> 0; size
= ((size
>> 3) * 7) & ~0xFFF) {
137 minefield_region
= VirtualAlloc(NULL
, size
,
138 MEM_RESERVE
, PAGE_NOACCESS
);
139 if (minefield_region
)
142 minefield_size
= size
;
145 * Firstly, allocate a section of that to be the admin block.
146 * We'll need a two-byte field for each page.
148 minefield_admin
= minefield_region
;
149 minefield_npages
= minefield_size
/ PAGESIZE
;
150 admin_size
= (minefield_npages
* 2 + PAGESIZE
- 1) & ~(PAGESIZE
- 1);
151 minefield_npages
= (minefield_size
- admin_size
) / PAGESIZE
;
152 minefield_pages
= (char *) minefield_region
+ admin_size
;
155 * Commit the admin region.
157 VirtualAlloc(minefield_admin
, minefield_npages
* 2,
158 MEM_COMMIT
, PAGE_READWRITE
);
161 * Mark all pages as unused (0xFFFF).
163 for (i
= 0; i
< minefield_npages
; i
++)
164 minefield_admin
[i
] = 0xFFFF;
167 * Hide the admin region.
169 minefield_admin_hide(1);
171 minefield_initialised
= 1;
174 static void minefield_bomb(void)
176 div(1, *(int *) minefield_pages
);
179 static void *minefield_alloc(int size
)
182 int pos
, lim
, region_end
, region_start
;
186 npages
= (size
+ PAGESIZE
- 1) / PAGESIZE
;
188 minefield_admin_hide(0);
191 * Search from current position until we find a contiguous
192 * bunch of npages+2 unused pages.
194 pos
= minefield_curpos
;
195 lim
= minefield_npages
;
197 /* Skip over used pages. */
198 while (pos
< lim
&& minefield_admin
[pos
] != 0xFFFF)
200 /* Count unused pages. */
202 while (pos
< lim
&& pos
- start
< npages
+ 2 &&
203 minefield_admin
[pos
] == 0xFFFF)
205 if (pos
- start
== npages
+ 2)
207 /* If we've reached the limit, reset the limit or stop. */
209 if (lim
== minefield_npages
) {
210 /* go round and start again at zero */
211 lim
= minefield_curpos
;
214 minefield_admin_hide(1);
220 minefield_curpos
= pos
- 1;
223 * We have npages+2 unused pages starting at start. We leave
224 * the first and last of these alone and use the rest.
226 region_end
= (start
+ npages
+ 1) * PAGESIZE
;
227 region_start
= region_end
- size
;
228 /* FIXME: could align here if we wanted */
231 * Update the admin region.
233 for (i
= start
+ 2; i
< start
+ npages
+ 1; i
++)
234 minefield_admin
[i
] = 0xFFFE; /* used but no region starts here */
235 minefield_admin
[start
+ 1] = region_start
% PAGESIZE
;
237 minefield_admin_hide(1);
239 VirtualAlloc((char *) minefield_pages
+ region_start
, size
,
240 MEM_COMMIT
, PAGE_READWRITE
);
241 return (char *) minefield_pages
+ region_start
;
244 static void minefield_free(void *ptr
)
246 int region_start
, i
, j
;
248 minefield_admin_hide(0);
250 region_start
= (char *) ptr
- (char *) minefield_pages
;
251 i
= region_start
/ PAGESIZE
;
252 if (i
< 0 || i
>= minefield_npages
||
253 minefield_admin
[i
] != region_start
% PAGESIZE
)
255 for (j
= i
; j
< minefield_npages
&& minefield_admin
[j
] != 0xFFFF; j
++) {
256 minefield_admin
[j
] = 0xFFFF;
259 VirtualFree(ptr
, j
* PAGESIZE
- region_start
, MEM_DECOMMIT
);
261 minefield_admin_hide(1);
264 static int minefield_get_size(void *ptr
)
266 int region_start
, i
, j
;
268 minefield_admin_hide(0);
270 region_start
= (char *) ptr
- (char *) minefield_pages
;
271 i
= region_start
/ PAGESIZE
;
272 if (i
< 0 || i
>= minefield_npages
||
273 minefield_admin
[i
] != region_start
% PAGESIZE
)
275 for (j
= i
; j
< minefield_npages
&& minefield_admin
[j
] != 0xFFFF; j
++);
277 minefield_admin_hide(1);
279 return j
* PAGESIZE
- region_start
;
282 void *minefield_c_malloc(size_t size
)
284 if (!minefield_initialised
)
286 return minefield_alloc(size
);
289 void minefield_c_free(void *p
)
291 if (!minefield_initialised
)
297 * realloc _always_ moves the chunk, for rapid detection of code
298 * that assumes it won't.
300 void *minefield_c_realloc(void *p
, size_t size
)
304 if (!minefield_initialised
)
306 q
= minefield_alloc(size
);
307 oldsize
= minefield_get_size(p
);
308 memcpy(q
, p
, (oldsize
< size ? oldsize
: size
));
313 #endif /* MINEFIELD */