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,
51 wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW;
52 wc.lpfnWndProc = DefDlgProc;
54 wc.cbWndExtra = DLGWINDOWEXTRA + 8;
57 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
58 wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
59 wc.lpszMenuName = NULL;
60 wc.lpszClassName = "PuTTYConfigBox";
63 hwnd = CreateDialog(hinst, tmpl, hwndparent, lpDialogFunc);
65 SetWindowLong(hwnd, BOXFLAGS, 0); /* flags */
66 SetWindowLong(hwnd, BOXRESULT, 0); /* result from SaneEndDialog */
68 while (GetMessage(&msg, NULL, 0, 0)) {
69 flags=GetWindowLong(hwnd, BOXFLAGS);
70 if (!(flags & DF_END) && !IsDialogMessage(hwnd, &msg))
71 DispatchMessage(&msg);
76 ret=GetWindowLong(hwnd, BOXRESULT);
81 void SaneEndDialog(HWND hwnd, int ret)
83 SetWindowLong(hwnd, BOXRESULT, ret);
84 SetWindowLong(hwnd, BOXFLAGS, DF_END);
88 static FILE *debug_fp = NULL;
89 static HANDLE debug_hdl = INVALID_HANDLE_VALUE;
90 static int debug_got_console = 0;
96 if (!debug_got_console) {
98 debug_got_console = 1;
99 debug_hdl = GetStdHandle(STD_OUTPUT_HANDLE);
103 debug_fp = fopen("debug.log", "w");
106 if (debug_hdl != INVALID_HANDLE_VALUE) {
107 WriteFile(debug_hdl, buf, strlen(buf), &dw, NULL);
109 fputs(buf, debug_fp);
116 * Minefield - a Windows equivalent for Electric Fence
119 #define PAGESIZE 4096
124 * We start by reserving as much virtual address space as Windows
125 * will sensibly (or not sensibly) let us have. We flag it all as
128 * Any allocation attempt is satisfied by committing one or more
129 * pages, with an uncommitted page on either side. The returned
130 * memory region is jammed up against the _end_ of the pages.
132 * Freeing anything causes instantaneous decommitment of the pages
133 * involved, so stale pointers are caught as soon as possible.
136 static int minefield_initialised = 0;
137 static void *minefield_region = NULL;
138 static long minefield_size = 0;
139 static long minefield_npages = 0;
140 static long minefield_curpos = 0;
141 static unsigned short *minefield_admin = NULL;
142 static void *minefield_pages = NULL;
144 static void minefield_admin_hide(int hide)
146 int access = hide ? PAGE_NOACCESS : PAGE_READWRITE;
147 VirtualProtect(minefield_admin, minefield_npages * 2, access, NULL);
150 static void minefield_init(void)
156 for (size = 0x40000000; size > 0; size = ((size >> 3) * 7) & ~0xFFF) {
157 minefield_region = VirtualAlloc(NULL, size,
158 MEM_RESERVE, PAGE_NOACCESS);
159 if (minefield_region)
162 minefield_size = size;
165 * Firstly, allocate a section of that to be the admin block.
166 * We'll need a two-byte field for each page.
168 minefield_admin = minefield_region;
169 minefield_npages = minefield_size / PAGESIZE;
170 admin_size = (minefield_npages * 2 + PAGESIZE - 1) & ~(PAGESIZE - 1);
171 minefield_npages = (minefield_size - admin_size) / PAGESIZE;
172 minefield_pages = (char *) minefield_region + admin_size;
175 * Commit the admin region.
177 VirtualAlloc(minefield_admin, minefield_npages * 2,
178 MEM_COMMIT, PAGE_READWRITE);
181 * Mark all pages as unused (0xFFFF).
183 for (i = 0; i < minefield_npages; i++)
184 minefield_admin[i] = 0xFFFF;
187 * Hide the admin region.
189 minefield_admin_hide(1);
191 minefield_initialised = 1;
194 static void minefield_bomb(void)
196 div(1, *(int *) minefield_pages);
199 static void *minefield_alloc(int size)
202 int pos, lim, region_end, region_start;
206 npages = (size + PAGESIZE - 1) / PAGESIZE;
208 minefield_admin_hide(0);
211 * Search from current position until we find a contiguous
212 * bunch of npages+2 unused pages.
214 pos = minefield_curpos;
215 lim = minefield_npages;
217 /* Skip over used pages. */
218 while (pos < lim && minefield_admin[pos] != 0xFFFF)
220 /* Count unused pages. */
222 while (pos < lim && pos - start < npages + 2 &&
223 minefield_admin[pos] == 0xFFFF)
225 if (pos - start == npages + 2)
227 /* If we've reached the limit, reset the limit or stop. */
229 if (lim == minefield_npages) {
230 /* go round and start again at zero */
231 lim = minefield_curpos;
234 minefield_admin_hide(1);
240 minefield_curpos = pos - 1;
243 * We have npages+2 unused pages starting at start. We leave
244 * the first and last of these alone and use the rest.
246 region_end = (start + npages + 1) * PAGESIZE;
247 region_start = region_end - size;
248 /* FIXME: could align here if we wanted */
251 * Update the admin region.
253 for (i = start + 2; i < start + npages + 1; i++)
254 minefield_admin[i] = 0xFFFE; /* used but no region starts here */
255 minefield_admin[start + 1] = region_start % PAGESIZE;
257 minefield_admin_hide(1);
259 VirtualAlloc((char *) minefield_pages + region_start, size,
260 MEM_COMMIT, PAGE_READWRITE);
261 return (char *) minefield_pages + region_start;
264 static void minefield_free(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++) {
276 minefield_admin[j] = 0xFFFF;
279 VirtualFree(ptr, j * PAGESIZE - region_start, MEM_DECOMMIT);
281 minefield_admin_hide(1);
284 static int minefield_get_size(void *ptr)
286 int region_start, i, j;
288 minefield_admin_hide(0);
290 region_start = (char *) ptr - (char *) minefield_pages;
291 i = region_start / PAGESIZE;
292 if (i < 0 || i >= minefield_npages ||
293 minefield_admin[i] != region_start % PAGESIZE)
295 for (j = i; j < minefield_npages && minefield_admin[j] != 0xFFFF; j++);
297 minefield_admin_hide(1);
299 return j * PAGESIZE - region_start;
302 void *minefield_c_malloc(size_t size)
304 if (!minefield_initialised)
306 return minefield_alloc(size);
309 void minefield_c_free(void *p)
311 if (!minefield_initialised)
317 * realloc _always_ moves the chunk, for rapid detection of code
318 * that assumes it won't.
320 void *minefield_c_realloc(void *p, size_t size)
324 if (!minefield_initialised)
326 q = minefield_alloc(size);
327 oldsize = minefield_get_size(p);
328 memcpy(q, p, (oldsize < size ? oldsize : size));
333 #endif /* MINEFIELD */