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
,
52 wc
.style
= CS_DBLCLKS
| CS_SAVEBITS
| CS_BYTEALIGNWINDOW
;
53 wc
.lpfnWndProc
= DefDlgProc
;
55 wc
.cbWndExtra
= DLGWINDOWEXTRA
+ 8;
58 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
59 wc
.hbrBackground
= (HBRUSH
) (COLOR_BACKGROUND
+1);
60 wc
.lpszMenuName
= NULL
;
61 wc
.lpszClassName
= "PuTTYConfigBox";
64 hwnd
= CreateDialog(hinst
, tmpl
, hwndparent
, lpDialogFunc
);
66 SetWindowLong(hwnd
, BOXFLAGS
, 0); /* flags */
67 SetWindowLong(hwnd
, BOXRESULT
, 0); /* result from SaneEndDialog */
69 while ((gm
=GetMessage(&msg
, NULL
, 0, 0)) > 0) {
70 flags
=GetWindowLong(hwnd
, BOXFLAGS
);
71 if (!(flags
& DF_END
) && !IsDialogMessage(hwnd
, &msg
))
72 DispatchMessage(&msg
);
78 PostQuitMessage(msg
.wParam
); /* We got a WM_QUIT, pass it on */
80 ret
=GetWindowLong(hwnd
, BOXRESULT
);
85 void SaneEndDialog(HWND hwnd
, int ret
)
87 SetWindowLong(hwnd
, BOXRESULT
, ret
);
88 SetWindowLong(hwnd
, BOXFLAGS
, DF_END
);
92 static FILE *debug_fp
= NULL
;
93 static HANDLE debug_hdl
= INVALID_HANDLE_VALUE
;
94 static int debug_got_console
= 0;
100 if (!debug_got_console
) {
101 if (AllocConsole()) {
102 debug_got_console
= 1;
103 debug_hdl
= GetStdHandle(STD_OUTPUT_HANDLE
);
107 debug_fp
= fopen("debug.log", "w");
110 if (debug_hdl
!= INVALID_HANDLE_VALUE
) {
111 WriteFile(debug_hdl
, buf
, strlen(buf
), &dw
, NULL
);
113 fputs(buf
, debug_fp
);
120 * Minefield - a Windows equivalent for Electric Fence
123 #define PAGESIZE 4096
128 * We start by reserving as much virtual address space as Windows
129 * will sensibly (or not sensibly) let us have. We flag it all as
132 * Any allocation attempt is satisfied by committing one or more
133 * pages, with an uncommitted page on either side. The returned
134 * memory region is jammed up against the _end_ of the pages.
136 * Freeing anything causes instantaneous decommitment of the pages
137 * involved, so stale pointers are caught as soon as possible.
140 static int minefield_initialised
= 0;
141 static void *minefield_region
= NULL
;
142 static long minefield_size
= 0;
143 static long minefield_npages
= 0;
144 static long minefield_curpos
= 0;
145 static unsigned short *minefield_admin
= NULL
;
146 static void *minefield_pages
= NULL
;
148 static void minefield_admin_hide(int hide
)
150 int access
= hide ? PAGE_NOACCESS
: PAGE_READWRITE
;
151 VirtualProtect(minefield_admin
, minefield_npages
* 2, access
, NULL
);
154 static void minefield_init(void)
160 for (size
= 0x40000000; size
> 0; size
= ((size
>> 3) * 7) & ~0xFFF) {
161 minefield_region
= VirtualAlloc(NULL
, size
,
162 MEM_RESERVE
, PAGE_NOACCESS
);
163 if (minefield_region
)
166 minefield_size
= size
;
169 * Firstly, allocate a section of that to be the admin block.
170 * We'll need a two-byte field for each page.
172 minefield_admin
= minefield_region
;
173 minefield_npages
= minefield_size
/ PAGESIZE
;
174 admin_size
= (minefield_npages
* 2 + PAGESIZE
- 1) & ~(PAGESIZE
- 1);
175 minefield_npages
= (minefield_size
- admin_size
) / PAGESIZE
;
176 minefield_pages
= (char *) minefield_region
+ admin_size
;
179 * Commit the admin region.
181 VirtualAlloc(minefield_admin
, minefield_npages
* 2,
182 MEM_COMMIT
, PAGE_READWRITE
);
185 * Mark all pages as unused (0xFFFF).
187 for (i
= 0; i
< minefield_npages
; i
++)
188 minefield_admin
[i
] = 0xFFFF;
191 * Hide the admin region.
193 minefield_admin_hide(1);
195 minefield_initialised
= 1;
198 static void minefield_bomb(void)
200 div(1, *(int *) minefield_pages
);
203 static void *minefield_alloc(int size
)
206 int pos
, lim
, region_end
, region_start
;
210 npages
= (size
+ PAGESIZE
- 1) / PAGESIZE
;
212 minefield_admin_hide(0);
215 * Search from current position until we find a contiguous
216 * bunch of npages+2 unused pages.
218 pos
= minefield_curpos
;
219 lim
= minefield_npages
;
221 /* Skip over used pages. */
222 while (pos
< lim
&& minefield_admin
[pos
] != 0xFFFF)
224 /* Count unused pages. */
226 while (pos
< lim
&& pos
- start
< npages
+ 2 &&
227 minefield_admin
[pos
] == 0xFFFF)
229 if (pos
- start
== npages
+ 2)
231 /* If we've reached the limit, reset the limit or stop. */
233 if (lim
== minefield_npages
) {
234 /* go round and start again at zero */
235 lim
= minefield_curpos
;
238 minefield_admin_hide(1);
244 minefield_curpos
= pos
- 1;
247 * We have npages+2 unused pages starting at start. We leave
248 * the first and last of these alone and use the rest.
250 region_end
= (start
+ npages
+ 1) * PAGESIZE
;
251 region_start
= region_end
- size
;
252 /* FIXME: could align here if we wanted */
255 * Update the admin region.
257 for (i
= start
+ 2; i
< start
+ npages
+ 1; i
++)
258 minefield_admin
[i
] = 0xFFFE; /* used but no region starts here */
259 minefield_admin
[start
+ 1] = region_start
% PAGESIZE
;
261 minefield_admin_hide(1);
263 VirtualAlloc((char *) minefield_pages
+ region_start
, size
,
264 MEM_COMMIT
, PAGE_READWRITE
);
265 return (char *) minefield_pages
+ region_start
;
268 static void minefield_free(void *ptr
)
270 int region_start
, i
, j
;
272 minefield_admin_hide(0);
274 region_start
= (char *) ptr
- (char *) minefield_pages
;
275 i
= region_start
/ PAGESIZE
;
276 if (i
< 0 || i
>= minefield_npages
||
277 minefield_admin
[i
] != region_start
% PAGESIZE
)
279 for (j
= i
; j
< minefield_npages
&& minefield_admin
[j
] != 0xFFFF; j
++) {
280 minefield_admin
[j
] = 0xFFFF;
283 VirtualFree(ptr
, j
* PAGESIZE
- region_start
, MEM_DECOMMIT
);
285 minefield_admin_hide(1);
288 static int minefield_get_size(void *ptr
)
290 int region_start
, i
, j
;
292 minefield_admin_hide(0);
294 region_start
= (char *) ptr
- (char *) minefield_pages
;
295 i
= region_start
/ PAGESIZE
;
296 if (i
< 0 || i
>= minefield_npages
||
297 minefield_admin
[i
] != region_start
% PAGESIZE
)
299 for (j
= i
; j
< minefield_npages
&& minefield_admin
[j
] != 0xFFFF; j
++);
301 minefield_admin_hide(1);
303 return j
* PAGESIZE
- region_start
;
306 void *minefield_c_malloc(size_t size
)
308 if (!minefield_initialised
)
310 return minefield_alloc(size
);
313 void minefield_c_free(void *p
)
315 if (!minefield_initialised
)
321 * realloc _always_ moves the chunk, for rapid detection of code
322 * that assumes it won't.
324 void *minefield_c_realloc(void *p
, size_t size
)
328 if (!minefield_initialised
)
330 q
= minefield_alloc(size
);
331 oldsize
= minefield_get_size(p
);
332 memcpy(q
, p
, (oldsize
< size ? oldsize
: size
));
337 #endif /* MINEFIELD */