10c9fe360d9f53c5b479857363cec2daffb6fbcf
2 * winmisc.c: miscellaneous Windows-specific things.
11 OSVERSIONINFO osVersion
;
13 void platform_get_x11_auth(char *display
, int *proto
,
14 unsigned char *data
, int *datalen
)
16 /* We don't support this at all under Windows. */
19 Filename
filename_from_str(const char *str
)
22 strncpy(ret
.path
, str
, sizeof(ret
.path
));
23 ret
.path
[sizeof(ret
.path
)-1] = '\0';
27 const char *filename_to_str(const Filename
*fn
)
32 int filename_equal(Filename f1
, Filename f2
)
34 return !strcmp(f1
.path
, f2
.path
);
37 int filename_is_null(Filename fn
)
42 int SaneDialogBox(HINSTANCE hinst
,
54 wc
.style
= CS_DBLCLKS
| CS_SAVEBITS
| CS_BYTEALIGNWINDOW
;
55 wc
.lpfnWndProc
= DefDlgProc
;
57 wc
.cbWndExtra
= DLGWINDOWEXTRA
+ 8;
60 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
61 wc
.hbrBackground
= (HBRUSH
) (COLOR_BACKGROUND
+1);
62 wc
.lpszMenuName
= NULL
;
63 wc
.lpszClassName
= "PuTTYConfigBox";
66 hwnd
= CreateDialog(hinst
, tmpl
, hwndparent
, lpDialogFunc
);
68 SetWindowLong(hwnd
, BOXFLAGS
, 0); /* flags */
69 SetWindowLong(hwnd
, BOXRESULT
, 0); /* result from SaneEndDialog */
71 while ((gm
=GetMessage(&msg
, NULL
, 0, 0)) > 0) {
72 flags
=GetWindowLong(hwnd
, BOXFLAGS
);
73 if (!(flags
& DF_END
) && !IsDialogMessage(hwnd
, &msg
))
74 DispatchMessage(&msg
);
80 PostQuitMessage(msg
.wParam
); /* We got a WM_QUIT, pass it on */
82 ret
=GetWindowLong(hwnd
, BOXRESULT
);
87 void SaneEndDialog(HWND hwnd
, int ret
)
89 SetWindowLong(hwnd
, BOXRESULT
, ret
);
90 SetWindowLong(hwnd
, BOXFLAGS
, DF_END
);
93 BOOL
init_winver(void)
95 ZeroMemory(&osVersion
, sizeof(osVersion
));
96 osVersion
.dwOSVersionInfoSize
= sizeof (OSVERSIONINFO
);
97 return GetVersionEx ( (OSVERSIONINFO
*) &osVersion
);
101 static FILE *debug_fp
= NULL
;
102 static HANDLE debug_hdl
= INVALID_HANDLE_VALUE
;
103 static int debug_got_console
= 0;
105 void dputs(char *buf
)
109 if (!debug_got_console
) {
110 if (AllocConsole()) {
111 debug_got_console
= 1;
112 debug_hdl
= GetStdHandle(STD_OUTPUT_HANDLE
);
116 debug_fp
= fopen("debug.log", "w");
119 if (debug_hdl
!= INVALID_HANDLE_VALUE
) {
120 WriteFile(debug_hdl
, buf
, strlen(buf
), &dw
, NULL
);
122 fputs(buf
, debug_fp
);
129 * Minefield - a Windows equivalent for Electric Fence
132 #define PAGESIZE 4096
137 * We start by reserving as much virtual address space as Windows
138 * will sensibly (or not sensibly) let us have. We flag it all as
141 * Any allocation attempt is satisfied by committing one or more
142 * pages, with an uncommitted page on either side. The returned
143 * memory region is jammed up against the _end_ of the pages.
145 * Freeing anything causes instantaneous decommitment of the pages
146 * involved, so stale pointers are caught as soon as possible.
149 static int minefield_initialised
= 0;
150 static void *minefield_region
= NULL
;
151 static long minefield_size
= 0;
152 static long minefield_npages
= 0;
153 static long minefield_curpos
= 0;
154 static unsigned short *minefield_admin
= NULL
;
155 static void *minefield_pages
= NULL
;
157 static void minefield_admin_hide(int hide
)
159 int access
= hide ? PAGE_NOACCESS
: PAGE_READWRITE
;
160 VirtualProtect(minefield_admin
, minefield_npages
* 2, access
, NULL
);
163 static void minefield_init(void)
169 for (size
= 0x40000000; size
> 0; size
= ((size
>> 3) * 7) & ~0xFFF) {
170 minefield_region
= VirtualAlloc(NULL
, size
,
171 MEM_RESERVE
, PAGE_NOACCESS
);
172 if (minefield_region
)
175 minefield_size
= size
;
178 * Firstly, allocate a section of that to be the admin block.
179 * We'll need a two-byte field for each page.
181 minefield_admin
= minefield_region
;
182 minefield_npages
= minefield_size
/ PAGESIZE
;
183 admin_size
= (minefield_npages
* 2 + PAGESIZE
- 1) & ~(PAGESIZE
- 1);
184 minefield_npages
= (minefield_size
- admin_size
) / PAGESIZE
;
185 minefield_pages
= (char *) minefield_region
+ admin_size
;
188 * Commit the admin region.
190 VirtualAlloc(minefield_admin
, minefield_npages
* 2,
191 MEM_COMMIT
, PAGE_READWRITE
);
194 * Mark all pages as unused (0xFFFF).
196 for (i
= 0; i
< minefield_npages
; i
++)
197 minefield_admin
[i
] = 0xFFFF;
200 * Hide the admin region.
202 minefield_admin_hide(1);
204 minefield_initialised
= 1;
207 static void minefield_bomb(void)
209 div(1, *(int *) minefield_pages
);
212 static void *minefield_alloc(int size
)
215 int pos
, lim
, region_end
, region_start
;
219 npages
= (size
+ PAGESIZE
- 1) / PAGESIZE
;
221 minefield_admin_hide(0);
224 * Search from current position until we find a contiguous
225 * bunch of npages+2 unused pages.
227 pos
= minefield_curpos
;
228 lim
= minefield_npages
;
230 /* Skip over used pages. */
231 while (pos
< lim
&& minefield_admin
[pos
] != 0xFFFF)
233 /* Count unused pages. */
235 while (pos
< lim
&& pos
- start
< npages
+ 2 &&
236 minefield_admin
[pos
] == 0xFFFF)
238 if (pos
- start
== npages
+ 2)
240 /* If we've reached the limit, reset the limit or stop. */
242 if (lim
== minefield_npages
) {
243 /* go round and start again at zero */
244 lim
= minefield_curpos
;
247 minefield_admin_hide(1);
253 minefield_curpos
= pos
- 1;
256 * We have npages+2 unused pages starting at start. We leave
257 * the first and last of these alone and use the rest.
259 region_end
= (start
+ npages
+ 1) * PAGESIZE
;
260 region_start
= region_end
- size
;
261 /* FIXME: could align here if we wanted */
264 * Update the admin region.
266 for (i
= start
+ 2; i
< start
+ npages
+ 1; i
++)
267 minefield_admin
[i
] = 0xFFFE; /* used but no region starts here */
268 minefield_admin
[start
+ 1] = region_start
% PAGESIZE
;
270 minefield_admin_hide(1);
272 VirtualAlloc((char *) minefield_pages
+ region_start
, size
,
273 MEM_COMMIT
, PAGE_READWRITE
);
274 return (char *) minefield_pages
+ region_start
;
277 static void minefield_free(void *ptr
)
279 int region_start
, i
, j
;
281 minefield_admin_hide(0);
283 region_start
= (char *) ptr
- (char *) minefield_pages
;
284 i
= region_start
/ PAGESIZE
;
285 if (i
< 0 || i
>= minefield_npages
||
286 minefield_admin
[i
] != region_start
% PAGESIZE
)
288 for (j
= i
; j
< minefield_npages
&& minefield_admin
[j
] != 0xFFFF; j
++) {
289 minefield_admin
[j
] = 0xFFFF;
292 VirtualFree(ptr
, j
* PAGESIZE
- region_start
, MEM_DECOMMIT
);
294 minefield_admin_hide(1);
297 static int minefield_get_size(void *ptr
)
299 int region_start
, i
, j
;
301 minefield_admin_hide(0);
303 region_start
= (char *) ptr
- (char *) minefield_pages
;
304 i
= region_start
/ PAGESIZE
;
305 if (i
< 0 || i
>= minefield_npages
||
306 minefield_admin
[i
] != region_start
% PAGESIZE
)
308 for (j
= i
; j
< minefield_npages
&& minefield_admin
[j
] != 0xFFFF; j
++);
310 minefield_admin_hide(1);
312 return j
* PAGESIZE
- region_start
;
315 void *minefield_c_malloc(size_t size
)
317 if (!minefield_initialised
)
319 return minefield_alloc(size
);
322 void minefield_c_free(void *p
)
324 if (!minefield_initialised
)
330 * realloc _always_ moves the chunk, for rapid detection of code
331 * that assumes it won't.
333 void *minefield_c_realloc(void *p
, size_t size
)
337 if (!minefield_initialised
)
339 q
= minefield_alloc(size
);
340 oldsize
= minefield_get_size(p
);
341 memcpy(q
, p
, (oldsize
< size ? oldsize
: size
));
346 #endif /* MINEFIELD */