2 * winmisc.c: miscellaneous Windows-specific things.
11 void platform_get_x11_auth(char *display
, int *proto
,
12 unsigned char *data
, int *datalen
)
14 /* We don't support this at all under Windows. */
17 Filename
filename_from_str(const char *str
)
20 strncpy(ret
.path
, str
, sizeof(ret
.path
));
21 ret
.path
[sizeof(ret
.path
)-1] = '\0';
25 const char *filename_to_str(const Filename
*fn
)
30 int filename_equal(Filename f1
, Filename f2
)
32 return !strcmp(f1
.path
, f2
.path
);
35 int filename_is_null(Filename fn
)
40 int SaneDialogBox(HINSTANCE hinst
,
48 boxhwnd
= CreateDialog(hinst
, tmpl
, hwndparent
, lpDialogFunc
);
49 while (GetMessage(&msg
, NULL
, 0, 0)) {
50 if (!(boxinfo
.flags
& DF_END
) && !IsDialogMessage(boxhwnd
, &msg
))
51 DispatchMessage(&msg
);
52 if (boxinfo
.flags
& DF_END
) break;
55 return boxinfo
.result
;
58 void SaneEndDialog(HWND hwnd
, int ret
)
61 boxinfo
.flags
|= DF_END
;
66 static FILE *debug_fp
= NULL
;
67 static HANDLE debug_hdl
= INVALID_HANDLE_VALUE
;
68 static int debug_got_console
= 0;
74 if (!debug_got_console
) {
76 debug_got_console
= 1;
77 debug_hdl
= GetStdHandle(STD_OUTPUT_HANDLE
);
81 debug_fp
= fopen("debug.log", "w");
84 if (debug_hdl
!= INVALID_HANDLE_VALUE
) {
85 WriteFile(debug_hdl
, buf
, strlen(buf
), &dw
, NULL
);
94 * Minefield - a Windows equivalent for Electric Fence
102 * We start by reserving as much virtual address space as Windows
103 * will sensibly (or not sensibly) let us have. We flag it all as
106 * Any allocation attempt is satisfied by committing one or more
107 * pages, with an uncommitted page on either side. The returned
108 * memory region is jammed up against the _end_ of the pages.
110 * Freeing anything causes instantaneous decommitment of the pages
111 * involved, so stale pointers are caught as soon as possible.
114 static int minefield_initialised
= 0;
115 static void *minefield_region
= NULL
;
116 static long minefield_size
= 0;
117 static long minefield_npages
= 0;
118 static long minefield_curpos
= 0;
119 static unsigned short *minefield_admin
= NULL
;
120 static void *minefield_pages
= NULL
;
122 static void minefield_admin_hide(int hide
)
124 int access
= hide ? PAGE_NOACCESS
: PAGE_READWRITE
;
125 VirtualProtect(minefield_admin
, minefield_npages
* 2, access
, NULL
);
128 static void minefield_init(void)
134 for (size
= 0x40000000; size
> 0; size
= ((size
>> 3) * 7) & ~0xFFF) {
135 minefield_region
= VirtualAlloc(NULL
, size
,
136 MEM_RESERVE
, PAGE_NOACCESS
);
137 if (minefield_region
)
140 minefield_size
= size
;
143 * Firstly, allocate a section of that to be the admin block.
144 * We'll need a two-byte field for each page.
146 minefield_admin
= minefield_region
;
147 minefield_npages
= minefield_size
/ PAGESIZE
;
148 admin_size
= (minefield_npages
* 2 + PAGESIZE
- 1) & ~(PAGESIZE
- 1);
149 minefield_npages
= (minefield_size
- admin_size
) / PAGESIZE
;
150 minefield_pages
= (char *) minefield_region
+ admin_size
;
153 * Commit the admin region.
155 VirtualAlloc(minefield_admin
, minefield_npages
* 2,
156 MEM_COMMIT
, PAGE_READWRITE
);
159 * Mark all pages as unused (0xFFFF).
161 for (i
= 0; i
< minefield_npages
; i
++)
162 minefield_admin
[i
] = 0xFFFF;
165 * Hide the admin region.
167 minefield_admin_hide(1);
169 minefield_initialised
= 1;
172 static void minefield_bomb(void)
174 div(1, *(int *) minefield_pages
);
177 static void *minefield_alloc(int size
)
180 int pos
, lim
, region_end
, region_start
;
184 npages
= (size
+ PAGESIZE
- 1) / PAGESIZE
;
186 minefield_admin_hide(0);
189 * Search from current position until we find a contiguous
190 * bunch of npages+2 unused pages.
192 pos
= minefield_curpos
;
193 lim
= minefield_npages
;
195 /* Skip over used pages. */
196 while (pos
< lim
&& minefield_admin
[pos
] != 0xFFFF)
198 /* Count unused pages. */
200 while (pos
< lim
&& pos
- start
< npages
+ 2 &&
201 minefield_admin
[pos
] == 0xFFFF)
203 if (pos
- start
== npages
+ 2)
205 /* If we've reached the limit, reset the limit or stop. */
207 if (lim
== minefield_npages
) {
208 /* go round and start again at zero */
209 lim
= minefield_curpos
;
212 minefield_admin_hide(1);
218 minefield_curpos
= pos
- 1;
221 * We have npages+2 unused pages starting at start. We leave
222 * the first and last of these alone and use the rest.
224 region_end
= (start
+ npages
+ 1) * PAGESIZE
;
225 region_start
= region_end
- size
;
226 /* FIXME: could align here if we wanted */
229 * Update the admin region.
231 for (i
= start
+ 2; i
< start
+ npages
+ 1; i
++)
232 minefield_admin
[i
] = 0xFFFE; /* used but no region starts here */
233 minefield_admin
[start
+ 1] = region_start
% PAGESIZE
;
235 minefield_admin_hide(1);
237 VirtualAlloc((char *) minefield_pages
+ region_start
, size
,
238 MEM_COMMIT
, PAGE_READWRITE
);
239 return (char *) minefield_pages
+ region_start
;
242 static void minefield_free(void *ptr
)
244 int region_start
, i
, j
;
246 minefield_admin_hide(0);
248 region_start
= (char *) ptr
- (char *) minefield_pages
;
249 i
= region_start
/ PAGESIZE
;
250 if (i
< 0 || i
>= minefield_npages
||
251 minefield_admin
[i
] != region_start
% PAGESIZE
)
253 for (j
= i
; j
< minefield_npages
&& minefield_admin
[j
] != 0xFFFF; j
++) {
254 minefield_admin
[j
] = 0xFFFF;
257 VirtualFree(ptr
, j
* PAGESIZE
- region_start
, MEM_DECOMMIT
);
259 minefield_admin_hide(1);
262 static int minefield_get_size(void *ptr
)
264 int region_start
, i
, j
;
266 minefield_admin_hide(0);
268 region_start
= (char *) ptr
- (char *) minefield_pages
;
269 i
= region_start
/ PAGESIZE
;
270 if (i
< 0 || i
>= minefield_npages
||
271 minefield_admin
[i
] != region_start
% PAGESIZE
)
273 for (j
= i
; j
< minefield_npages
&& minefield_admin
[j
] != 0xFFFF; j
++);
275 minefield_admin_hide(1);
277 return j
* PAGESIZE
- region_start
;
280 void *minefield_c_malloc(size_t size
)
282 if (!minefield_initialised
)
284 return minefield_alloc(size
);
287 void minefield_c_free(void *p
)
289 if (!minefield_initialised
)
295 * realloc _always_ moves the chunk, for rapid detection of code
296 * that assumes it won't.
298 void *minefield_c_realloc(void *p
, size_t size
)
302 if (!minefield_initialised
)
304 q
= minefield_alloc(size
);
305 oldsize
= minefield_get_size(p
);
306 memcpy(q
, p
, (oldsize
< size ? oldsize
: size
));
311 #endif /* MINEFIELD */